reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkLua.h" |
| 11 | #include "SkCanvas.h" |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 12 | #include "Resources.h" |
| 13 | #include "SkData.h" |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 14 | |
| 15 | extern "C" { |
| 16 | #include "lua.h" |
| 17 | #include "lualib.h" |
| 18 | #include "lauxlib.h" |
| 19 | } |
| 20 | |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 21 | #define LUA_FILENAME "test.lua" |
| 22 | //#define LUA_FILENAME "slides.lua" |
| 23 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 24 | static const char gDrawName[] = "onDrawContent"; |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 25 | static const char gClickName[] = "onClickHandler"; |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame^] | 26 | static const char gUnicharName[] = "onCharHandler"; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 27 | |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 28 | static const char gMissingCode[] = "" |
| 29 | "local paint = Sk.newPaint()" |
| 30 | "paint:setAntiAlias(true)" |
| 31 | "paint:setTextSize(30)" |
| 32 | "" |
| 33 | "function onDrawContent(canvas)" |
| 34 | " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" |
| 35 | "end" |
| 36 | ; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 37 | |
| 38 | class LuaView : public SampleView { |
| 39 | public: |
| 40 | LuaView() : fLua(NULL) {} |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 41 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 42 | virtual ~LuaView() { |
| 43 | SkDELETE(fLua); |
| 44 | } |
| 45 | |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 46 | void setImageFilename(lua_State* L) { |
| 47 | SkString str = GetResourcePath("mandrill_256.png"); |
| 48 | |
| 49 | lua_getglobal(L, "setImageFilename"); |
| 50 | if (lua_isfunction(L, -1)) { |
| 51 | fLua->pushString(str.c_str()); |
| 52 | if (lua_pcall(L, 1, 0, 0) != LUA_OK) { |
| 53 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 58 | lua_State* ensureLua() { |
| 59 | if (NULL == fLua) { |
| 60 | fLua = SkNEW(SkLua); |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 61 | |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 62 | SkString str = GetResourcePath(LUA_FILENAME); |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 63 | SkData* data = SkData::NewFromFileName(str.c_str()); |
| 64 | if (data) { |
| 65 | fLua->runCode(data->data(), data->size()); |
| 66 | data->unref(); |
| 67 | this->setImageFilename(fLua->get()); |
| 68 | } else { |
| 69 | fLua->runCode(gMissingCode); |
| 70 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 71 | } |
| 72 | return fLua->get(); |
| 73 | } |
| 74 | |
| 75 | protected: |
| 76 | virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { |
| 77 | if (SampleCode::TitleQ(*evt)) { |
| 78 | SampleCode::TitleR(evt, "Lua"); |
| 79 | return true; |
| 80 | } |
| 81 | SkUnichar uni; |
| 82 | if (SampleCode::CharQ(*evt, &uni)) { |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame^] | 83 | lua_State* L = this->ensureLua(); |
| 84 | lua_getglobal(L, gUnicharName); |
| 85 | if (lua_isfunction(L, -1)) { |
| 86 | SkString str; |
| 87 | str.appendUnichar(uni); |
| 88 | fLua->pushString(str.c_str()); |
| 89 | if (lua_pcall(L, 1, 1, 0) != LUA_OK) { |
| 90 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 91 | } else { |
| 92 | if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { |
| 93 | this->inval(NULL); |
| 94 | return true; |
| 95 | } |
| 96 | } |
| 97 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 98 | } |
| 99 | return this->INHERITED::onQuery(evt); |
| 100 | } |
| 101 | |
| 102 | virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { |
| 103 | lua_State* L = this->ensureLua(); |
| 104 | |
| 105 | lua_getglobal(L, gDrawName); |
| 106 | if (!lua_isfunction(L, -1)) { |
| 107 | int t = lua_type(L, -1); |
| 108 | SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t); |
| 109 | lua_pop(L, 1); |
| 110 | } else { |
| 111 | // does it make sense to try to "cache" the lua version of this |
| 112 | // canvas between draws? |
| 113 | fLua->pushCanvas(canvas); |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 114 | if (lua_pcall(L, 1, 1, 0) != LUA_OK) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 115 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 116 | } else { |
| 117 | if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { |
| 118 | this->inval(NULL); |
| 119 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
| 125 | unsigned modi) SK_OVERRIDE { |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 126 | lua_State* L = this->ensureLua(); |
| 127 | lua_getglobal(L, gClickName); |
| 128 | if (lua_isfunction(L, -1)) { |
| 129 | fLua->pushScalar(x); |
| 130 | fLua->pushScalar(y); |
| 131 | if (lua_pcall(L, 2, 1, 0) != LUA_OK) { |
| 132 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 133 | } else { |
| 134 | if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { |
| 135 | this->inval(NULL); |
| 136 | } |
| 137 | } |
| 138 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 139 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 140 | } |
| 141 | |
| 142 | virtual bool onClick(Click* click) SK_OVERRIDE { |
| 143 | return this->INHERITED::onClick(click); |
| 144 | } |
| 145 | |
| 146 | private: |
| 147 | SkLua* fLua; |
| 148 | |
| 149 | typedef SampleView INHERITED; |
| 150 | }; |
| 151 | |
| 152 | ////////////////////////////////////////////////////////////////////////////// |
| 153 | |
| 154 | static SkView* MyFactory() { return new LuaView; } |
| 155 | static SkViewRegister reg(MyFactory); |