2013-05-22 20:12:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkLua.h"
|
|
|
|
#include "SkCanvas.h"
|
2014-10-11 03:31:24 +00:00
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkData.h"
|
2013-05-22 20:12:50 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "lua.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:21:31 +00:00
|
|
|
//#define LUA_FILENAME "lua/test.lua"
|
|
|
|
#define LUA_FILENAME "lua/slides.lua"
|
2014-10-11 18:28:07 +00:00
|
|
|
|
2013-05-22 20:12:50 +00:00
|
|
|
static const char gDrawName[] = "onDrawContent";
|
2014-10-11 18:28:07 +00:00
|
|
|
static const char gClickName[] = "onClickHandler";
|
2014-10-11 20:13:11 +00:00
|
|
|
static const char gUnicharName[] = "onCharHandler";
|
2013-05-22 20:12:50 +00:00
|
|
|
|
2014-10-11 03:31:24 +00:00
|
|
|
static const char gMissingCode[] = ""
|
|
|
|
"local paint = Sk.newPaint()"
|
|
|
|
"paint:setAntiAlias(true)"
|
|
|
|
"paint:setTextSize(30)"
|
|
|
|
""
|
|
|
|
"function onDrawContent(canvas)"
|
|
|
|
" canvas:drawText('missing \"test.lua\"', 20, 50, paint)"
|
|
|
|
"end"
|
|
|
|
;
|
2013-05-22 20:12:50 +00:00
|
|
|
|
|
|
|
class LuaView : public SampleView {
|
|
|
|
public:
|
2015-08-27 14:41:13 +00:00
|
|
|
LuaView() : fLua(nullptr) {}
|
2013-05-23 07:01:22 +00:00
|
|
|
|
2017-10-09 14:34:09 +00:00
|
|
|
~LuaView() override { delete fLua; }
|
2013-05-22 20:12:50 +00:00
|
|
|
|
2014-10-11 03:31:24 +00:00
|
|
|
void setImageFilename(lua_State* L) {
|
2017-12-08 15:21:31 +00:00
|
|
|
SkString str = GetResourcePath("images/mandrill_256.png");
|
2014-10-11 03:31:24 +00:00
|
|
|
|
|
|
|
lua_getglobal(L, "setImageFilename");
|
|
|
|
if (lua_isfunction(L, -1)) {
|
|
|
|
fLua->pushString(str.c_str());
|
|
|
|
if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
|
|
|
|
SkDebugf("lua err: %s\n", lua_tostring(L, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-22 20:12:50 +00:00
|
|
|
lua_State* ensureLua() {
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fLua) {
|
2015-08-26 20:07:48 +00:00
|
|
|
fLua = new SkLua;
|
2014-10-11 03:31:24 +00:00
|
|
|
|
2018-05-22 15:24:21 +00:00
|
|
|
sk_sp<SkData> data = GetResourceAsData(LUA_FILENAME);
|
2014-10-11 03:31:24 +00:00
|
|
|
if (data) {
|
|
|
|
fLua->runCode(data->data(), data->size());
|
|
|
|
this->setImageFilename(fLua->get());
|
|
|
|
} else {
|
|
|
|
fLua->runCode(gMissingCode);
|
|
|
|
}
|
2013-05-22 20:12:50 +00:00
|
|
|
}
|
|
|
|
return fLua->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onQuery(SkEvent* evt) override {
|
2013-05-22 20:12:50 +00:00
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Lua");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkUnichar uni;
|
|
|
|
if (SampleCode::CharQ(*evt, &uni)) {
|
2014-10-11 20:13:11 +00:00
|
|
|
lua_State* L = this->ensureLua();
|
|
|
|
lua_getglobal(L, gUnicharName);
|
|
|
|
if (lua_isfunction(L, -1)) {
|
|
|
|
SkString str;
|
|
|
|
str.appendUnichar(uni);
|
|
|
|
fLua->pushString(str.c_str());
|
|
|
|
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
|
|
|
|
SkDebugf("lua err: %s\n", lua_tostring(L, -1));
|
|
|
|
} else {
|
|
|
|
if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 20:12:50 +00:00
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2013-05-22 20:12:50 +00:00
|
|
|
lua_State* L = this->ensureLua();
|
|
|
|
|
|
|
|
lua_getglobal(L, gDrawName);
|
|
|
|
if (!lua_isfunction(L, -1)) {
|
|
|
|
int t = lua_type(L, -1);
|
|
|
|
SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
} else {
|
|
|
|
// does it make sense to try to "cache" the lua version of this
|
|
|
|
// canvas between draws?
|
|
|
|
fLua->pushCanvas(canvas);
|
2014-10-14 16:34:52 +00:00
|
|
|
fLua->pushScalar(this->width());
|
|
|
|
fLua->pushScalar(this->height());
|
|
|
|
if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
|
2013-05-22 20:12:50 +00:00
|
|
|
SkDebugf("lua err: %s\n", lua_tostring(L, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
|
2015-03-26 01:17:31 +00:00
|
|
|
unsigned modi) override {
|
2014-10-11 18:28:07 +00:00
|
|
|
lua_State* L = this->ensureLua();
|
|
|
|
lua_getglobal(L, gClickName);
|
|
|
|
if (lua_isfunction(L, -1)) {
|
|
|
|
fLua->pushScalar(x);
|
|
|
|
fLua->pushScalar(y);
|
2014-11-07 18:23:55 +00:00
|
|
|
fLua->pushString("down");
|
|
|
|
if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
|
2014-10-11 18:28:07 +00:00
|
|
|
SkDebugf("lua err: %s\n", lua_tostring(L, -1));
|
|
|
|
} else {
|
|
|
|
if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
|
2017-12-11 16:48:29 +00:00
|
|
|
return new Click(this);
|
2014-10-11 18:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 20:12:50 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onClick(Click* click) override {
|
2015-08-27 14:41:13 +00:00
|
|
|
const char* state = nullptr;
|
2014-11-07 18:23:55 +00:00
|
|
|
switch (click->fState) {
|
|
|
|
case Click::kMoved_State:
|
|
|
|
state = "moved";
|
|
|
|
break;
|
|
|
|
case Click::kUp_State:
|
|
|
|
state = "up";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (state) {
|
|
|
|
lua_State* L = fLua->get();
|
|
|
|
lua_getglobal(L, gClickName);
|
|
|
|
fLua->pushScalar(click->fCurr.x());
|
|
|
|
fLua->pushScalar(click->fCurr.y());
|
|
|
|
fLua->pushString(state);
|
|
|
|
lua_pcall(L, 3, 1, 0);
|
|
|
|
return lua_isboolean(L, -1) && lua_toboolean(L, -1);
|
|
|
|
}
|
|
|
|
return true;
|
2013-05-22 20:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkLua* fLua;
|
|
|
|
|
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new LuaView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|