| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
| 9 #include "SkView.h" | 9 #include "SkView.h" |
| 10 #include "SkLua.h" | 10 #include "SkLua.h" |
| 11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
| 12 #include "Resources.h" | 12 #include "Resources.h" |
| 13 #include "SkData.h" | 13 #include "SkData.h" |
| 14 | 14 |
| 15 extern "C" { | 15 extern "C" { |
| 16 #include "lua.h" | 16 #include "lua.h" |
| 17 #include "lualib.h" | 17 #include "lualib.h" |
| 18 #include "lauxlib.h" | 18 #include "lauxlib.h" |
| 19 } | 19 } |
| 20 | 20 |
| 21 #define LUA_FILENAME "test.lua" |
| 22 //#define LUA_FILENAME "slides.lua" |
| 23 |
| 21 static const char gDrawName[] = "onDrawContent"; | 24 static const char gDrawName[] = "onDrawContent"; |
| 25 static const char gClickName[] = "onClickHandler"; |
| 22 | 26 |
| 23 static const char gMissingCode[] = "" | 27 static const char gMissingCode[] = "" |
| 24 "local paint = Sk.newPaint()" | 28 "local paint = Sk.newPaint()" |
| 25 "paint:setAntiAlias(true)" | 29 "paint:setAntiAlias(true)" |
| 26 "paint:setTextSize(30)" | 30 "paint:setTextSize(30)" |
| 27 "" | 31 "" |
| 28 "function onDrawContent(canvas)" | 32 "function onDrawContent(canvas)" |
| 29 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" | 33 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" |
| 30 "end" | 34 "end" |
| 31 ; | 35 ; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 47 if (lua_pcall(L, 1, 0, 0) != LUA_OK) { | 51 if (lua_pcall(L, 1, 0, 0) != LUA_OK) { |
| 48 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); | 52 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 49 } | 53 } |
| 50 } | 54 } |
| 51 } | 55 } |
| 52 | 56 |
| 53 lua_State* ensureLua() { | 57 lua_State* ensureLua() { |
| 54 if (NULL == fLua) { | 58 if (NULL == fLua) { |
| 55 fLua = SkNEW(SkLua); | 59 fLua = SkNEW(SkLua); |
| 56 | 60 |
| 57 SkString str = GetResourcePath("test.lua"); | 61 SkString str = GetResourcePath(LUA_FILENAME); |
| 58 SkData* data = SkData::NewFromFileName(str.c_str()); | 62 SkData* data = SkData::NewFromFileName(str.c_str()); |
| 59 if (data) { | 63 if (data) { |
| 60 fLua->runCode(data->data(), data->size()); | 64 fLua->runCode(data->data(), data->size()); |
| 61 data->unref(); | 65 data->unref(); |
| 62 this->setImageFilename(fLua->get()); | 66 this->setImageFilename(fLua->get()); |
| 63 } else { | 67 } else { |
| 64 fLua->runCode(gMissingCode); | 68 fLua->runCode(gMissingCode); |
| 65 } | 69 } |
| 66 } | 70 } |
| 67 return fLua->get(); | 71 return fLua->get(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 84 | 88 |
| 85 lua_getglobal(L, gDrawName); | 89 lua_getglobal(L, gDrawName); |
| 86 if (!lua_isfunction(L, -1)) { | 90 if (!lua_isfunction(L, -1)) { |
| 87 int t = lua_type(L, -1); | 91 int t = lua_type(L, -1); |
| 88 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t); | 92 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t); |
| 89 lua_pop(L, 1); | 93 lua_pop(L, 1); |
| 90 } else { | 94 } else { |
| 91 // does it make sense to try to "cache" the lua version of this | 95 // does it make sense to try to "cache" the lua version of this |
| 92 // canvas between draws? | 96 // canvas between draws? |
| 93 fLua->pushCanvas(canvas); | 97 fLua->pushCanvas(canvas); |
| 94 if (lua_pcall(L, 1, 0, 0) != LUA_OK) { | 98 if (lua_pcall(L, 1, 1, 0) != LUA_OK) { |
| 95 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); | 99 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 100 } else { |
| 101 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { |
| 102 this->inval(NULL); |
| 103 } |
| 96 } | 104 } |
| 97 } | 105 } |
| 98 // need a way for the lua-sample to tell us if they want animations... | |
| 99 // hard-code it ON for now. | |
| 100 this->inval(NULL); | |
| 101 } | 106 } |
| 102 | 107 |
| 103 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, | 108 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
| 104 unsigned modi) SK_OVERRIDE { | 109 unsigned modi) SK_OVERRIDE { |
| 110 lua_State* L = this->ensureLua(); |
| 111 lua_getglobal(L, gClickName); |
| 112 if (lua_isfunction(L, -1)) { |
| 113 fLua->pushScalar(x); |
| 114 fLua->pushScalar(y); |
| 115 if (lua_pcall(L, 2, 1, 0) != LUA_OK) { |
| 116 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 117 } else { |
| 118 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { |
| 119 this->inval(NULL); |
| 120 } |
| 121 } |
| 122 } |
| 105 return this->INHERITED::onFindClickHandler(x, y, modi); | 123 return this->INHERITED::onFindClickHandler(x, y, modi); |
| 106 } | 124 } |
| 107 | 125 |
| 108 virtual bool onClick(Click* click) SK_OVERRIDE { | 126 virtual bool onClick(Click* click) SK_OVERRIDE { |
| 109 return this->INHERITED::onClick(click); | 127 return this->INHERITED::onClick(click); |
| 110 } | 128 } |
| 111 | 129 |
| 112 private: | 130 private: |
| 113 SkLua* fLua; | 131 SkLua* fLua; |
| 114 | 132 |
| 115 typedef SampleView INHERITED; | 133 typedef SampleView INHERITED; |
| 116 }; | 134 }; |
| 117 | 135 |
| 118 ////////////////////////////////////////////////////////////////////////////// | 136 ////////////////////////////////////////////////////////////////////////////// |
| 119 | 137 |
| 120 static SkView* MyFactory() { return new LuaView; } | 138 static SkView* MyFactory() { return new LuaView; } |
| 121 static SkViewRegister reg(MyFactory); | 139 static SkViewRegister reg(MyFactory); |
| OLD | NEW |