aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/SampleLua.cpp
diff options
context:
space:
mode:
authorGravatar reed <reed@chromium.org>2014-10-11 11:28:07 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-10-11 11:28:07 -0700
commit18ea777638f1494b068ba4ca1a5d6725a0e80cf1 (patch)
tree5008c512d24cd5ea270799603371fe3b50b8057b /samplecode/SampleLua.cpp
parent09445a4f7fe6c8cd8cfe172ce160af7007738490 (diff)
allow for lua click handlers
BUG=skia: TBR= Review URL: https://codereview.chromium.org/649013002
Diffstat (limited to 'samplecode/SampleLua.cpp')
-rw-r--r--samplecode/SampleLua.cpp28
1 files changed, 23 insertions, 5 deletions
diff --git a/samplecode/SampleLua.cpp b/samplecode/SampleLua.cpp
index 8e0eaf703d..917930a678 100644
--- a/samplecode/SampleLua.cpp
+++ b/samplecode/SampleLua.cpp
@@ -18,7 +18,11 @@ extern "C" {
#include "lauxlib.h"
}
+#define LUA_FILENAME "test.lua"
+//#define LUA_FILENAME "slides.lua"
+
static const char gDrawName[] = "onDrawContent";
+static const char gClickName[] = "onClickHandler";
static const char gMissingCode[] = ""
"local paint = Sk.newPaint()"
@@ -54,7 +58,7 @@ public:
if (NULL == fLua) {
fLua = SkNEW(SkLua);
- SkString str = GetResourcePath("test.lua");
+ SkString str = GetResourcePath(LUA_FILENAME);
SkData* data = SkData::NewFromFileName(str.c_str());
if (data) {
fLua->runCode(data->data(), data->size());
@@ -91,17 +95,31 @@ protected:
// does it make sense to try to "cache" the lua version of this
// canvas between draws?
fLua->pushCanvas(canvas);
- if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
+ 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)) {
+ this->inval(NULL);
+ }
}
}
- // need a way for the lua-sample to tell us if they want animations...
- // hard-code it ON for now.
- this->inval(NULL);
}
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
unsigned modi) SK_OVERRIDE {
+ lua_State* L = this->ensureLua();
+ lua_getglobal(L, gClickName);
+ if (lua_isfunction(L, -1)) {
+ fLua->pushScalar(x);
+ fLua->pushScalar(y);
+ if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
+ SkDebugf("lua err: %s\n", lua_tostring(L, -1));
+ } else {
+ if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
+ this->inval(NULL);
+ }
+ }
+ }
return this->INHERITED::onFindClickHandler(x, y, modi);
}