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 | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 21 | //#define LUA_FILENAME "test.lua" |
| 22 | #define LUA_FILENAME "slides.lua" |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 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 | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 28 | static const char gLuaClickHandlerName[] = "lua-click-handler"; |
| 29 | |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 30 | static const char gMissingCode[] = "" |
| 31 | "local paint = Sk.newPaint()" |
| 32 | "paint:setAntiAlias(true)" |
| 33 | "paint:setTextSize(30)" |
| 34 | "" |
| 35 | "function onDrawContent(canvas)" |
| 36 | " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" |
| 37 | "end" |
| 38 | ; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 39 | |
| 40 | class LuaView : public SampleView { |
| 41 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | LuaView() : fLua(nullptr) {} |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 43 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 44 | virtual ~LuaView() { delete fLua; } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 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() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | if (nullptr == fLua) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 60 | fLua = new 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); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 63 | sk_sp<SkData> data(SkData::MakeFromFileName(str.c_str())); |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 64 | if (data) { |
| 65 | fLua->runCode(data->data(), data->size()); |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 66 | this->setImageFilename(fLua->get()); |
| 67 | } else { |
| 68 | fLua->runCode(gMissingCode); |
| 69 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 70 | } |
| 71 | return fLua->get(); |
| 72 | } |
| 73 | |
| 74 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | bool onQuery(SkEvent* evt) override { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 76 | if (SampleCode::TitleQ(*evt)) { |
| 77 | SampleCode::TitleR(evt, "Lua"); |
| 78 | return true; |
| 79 | } |
| 80 | SkUnichar uni; |
| 81 | if (SampleCode::CharQ(*evt, &uni)) { |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 82 | lua_State* L = this->ensureLua(); |
| 83 | lua_getglobal(L, gUnicharName); |
| 84 | if (lua_isfunction(L, -1)) { |
| 85 | SkString str; |
| 86 | str.appendUnichar(uni); |
| 87 | fLua->pushString(str.c_str()); |
| 88 | if (lua_pcall(L, 1, 1, 0) != LUA_OK) { |
| 89 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 90 | } else { |
| 91 | if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 92 | this->inval(nullptr); |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | } |
| 96 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 97 | } |
| 98 | return this->INHERITED::onQuery(evt); |
| 99 | } |
| 100 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 101 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 102 | lua_State* L = this->ensureLua(); |
| 103 | |
| 104 | lua_getglobal(L, gDrawName); |
| 105 | if (!lua_isfunction(L, -1)) { |
| 106 | int t = lua_type(L, -1); |
| 107 | SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t); |
| 108 | lua_pop(L, 1); |
| 109 | } else { |
| 110 | // does it make sense to try to "cache" the lua version of this |
| 111 | // canvas between draws? |
| 112 | fLua->pushCanvas(canvas); |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 113 | fLua->pushScalar(this->width()); |
| 114 | fLua->pushScalar(this->height()); |
| 115 | if (lua_pcall(L, 3, 1, 0) != LUA_OK) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 116 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 117 | } else { |
| 118 | if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 119 | this->inval(nullptr); |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 120 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 126 | unsigned modi) override { |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 127 | lua_State* L = this->ensureLua(); |
| 128 | lua_getglobal(L, gClickName); |
| 129 | if (lua_isfunction(L, -1)) { |
| 130 | fLua->pushScalar(x); |
| 131 | fLua->pushScalar(y); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 132 | fLua->pushString("down"); |
| 133 | if (lua_pcall(L, 3, 1, 0) != LUA_OK) { |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 134 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 135 | } else { |
| 136 | if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | this->inval(nullptr); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 138 | Click* c = new Click(this); |
| 139 | c->setType(gLuaClickHandlerName); |
| 140 | return c; |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 144 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 145 | } |
| 146 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 147 | bool onClick(Click* click) override { |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 148 | if (click->getType() != gLuaClickHandlerName) { |
| 149 | return this->INHERITED::onClick(click); |
| 150 | } |
| 151 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 152 | const char* state = nullptr; |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 153 | switch (click->fState) { |
| 154 | case Click::kMoved_State: |
| 155 | state = "moved"; |
| 156 | break; |
| 157 | case Click::kUp_State: |
| 158 | state = "up"; |
| 159 | break; |
| 160 | default: |
| 161 | break; |
| 162 | } |
| 163 | if (state) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 164 | this->inval(nullptr); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 165 | lua_State* L = fLua->get(); |
| 166 | lua_getglobal(L, gClickName); |
| 167 | fLua->pushScalar(click->fCurr.x()); |
| 168 | fLua->pushScalar(click->fCurr.y()); |
| 169 | fLua->pushString(state); |
| 170 | lua_pcall(L, 3, 1, 0); |
| 171 | return lua_isboolean(L, -1) && lua_toboolean(L, -1); |
| 172 | } |
| 173 | return true; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private: |
| 177 | SkLua* fLua; |
| 178 | |
| 179 | typedef SampleView INHERITED; |
| 180 | }; |
| 181 | |
| 182 | ////////////////////////////////////////////////////////////////////////////// |
| 183 | |
| 184 | static SkView* MyFactory() { return new LuaView; } |
| 185 | static SkViewRegister reg(MyFactory); |