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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkData.h" |
| 10 | #include "include/utils/SkLua.h" |
| 11 | #include "samplecode/Sample.h" |
| 12 | #include "tools/Resources.h" |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 13 | |
| 14 | extern "C" { |
| 15 | #include "lua.h" |
| 16 | #include "lualib.h" |
| 17 | #include "lauxlib.h" |
| 18 | } |
| 19 | |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 20 | //#define LUA_FILENAME "lua/test.lua" |
| 21 | #define LUA_FILENAME "lua/slides.lua" |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 22 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 23 | static const char gDrawName[] = "onDrawContent"; |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 24 | static const char gClickName[] = "onClickHandler"; |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 25 | static const char gUnicharName[] = "onCharHandler"; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 26 | |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 27 | static const char gMissingCode[] = "" |
| 28 | "local paint = Sk.newPaint()" |
| 29 | "paint:setAntiAlias(true)" |
| 30 | "paint:setTextSize(30)" |
| 31 | "" |
| 32 | "function onDrawContent(canvas)" |
| 33 | " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" |
| 34 | "end" |
| 35 | ; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 36 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 37 | class LuaView : public Sample { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 38 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | LuaView() : fLua(nullptr) {} |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 40 | |
Ben Wagner | 5a96580 | 2017-10-09 10:34:09 -0400 | [diff] [blame] | 41 | ~LuaView() override { delete fLua; } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 42 | |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 43 | void setImageFilename(lua_State* L) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 44 | SkString str = GetResourcePath("images/mandrill_256.png"); |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 45 | |
| 46 | lua_getglobal(L, "setImageFilename"); |
| 47 | if (lua_isfunction(L, -1)) { |
| 48 | fLua->pushString(str.c_str()); |
| 49 | if (lua_pcall(L, 1, 0, 0) != LUA_OK) { |
| 50 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 55 | lua_State* ensureLua() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | if (nullptr == fLua) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 57 | fLua = new SkLua; |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 58 | |
Ben Wagner | d6de89b | 2018-05-22 11:24:21 -0400 | [diff] [blame] | 59 | sk_sp<SkData> data = GetResourceAsData(LUA_FILENAME); |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 60 | if (data) { |
| 61 | fLua->runCode(data->data(), data->size()); |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame] | 62 | this->setImageFilename(fLua->get()); |
| 63 | } else { |
| 64 | fLua->runCode(gMissingCode); |
| 65 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 66 | } |
| 67 | return fLua->get(); |
| 68 | } |
| 69 | |
| 70 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 71 | SkString name() override { return SkString("Lua"); } |
| 72 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 73 | bool onQuery(Sample::Event* evt) override { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 74 | SkUnichar uni; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 75 | if (Sample::CharQ(*evt, &uni)) { |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 76 | lua_State* L = this->ensureLua(); |
| 77 | lua_getglobal(L, gUnicharName); |
| 78 | if (lua_isfunction(L, -1)) { |
| 79 | SkString str; |
| 80 | str.appendUnichar(uni); |
| 81 | fLua->pushString(str.c_str()); |
| 82 | if (lua_pcall(L, 1, 1, 0) != LUA_OK) { |
| 83 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 84 | } else { |
| 85 | if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 90 | } |
| 91 | return this->INHERITED::onQuery(evt); |
| 92 | } |
| 93 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 94 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 95 | lua_State* L = this->ensureLua(); |
| 96 | |
| 97 | lua_getglobal(L, gDrawName); |
| 98 | if (!lua_isfunction(L, -1)) { |
| 99 | int t = lua_type(L, -1); |
| 100 | SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t); |
| 101 | lua_pop(L, 1); |
| 102 | } else { |
| 103 | // does it make sense to try to "cache" the lua version of this |
| 104 | // canvas between draws? |
| 105 | fLua->pushCanvas(canvas); |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 106 | fLua->pushScalar(this->width()); |
| 107 | fLua->pushScalar(this->height()); |
| 108 | if (lua_pcall(L, 3, 1, 0) != LUA_OK) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 109 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 110 | } |
| 111 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 114 | virtual Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 115 | unsigned modi) override { |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 116 | lua_State* L = this->ensureLua(); |
| 117 | lua_getglobal(L, gClickName); |
| 118 | if (lua_isfunction(L, -1)) { |
| 119 | fLua->pushScalar(x); |
| 120 | fLua->pushScalar(y); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 121 | fLua->pushString("down"); |
| 122 | if (lua_pcall(L, 3, 1, 0) != LUA_OK) { |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 123 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 124 | } else { |
| 125 | if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { |
Florin Malita | ba19b6f | 2017-12-11 11:48:29 -0500 | [diff] [blame] | 126 | return new Click(this); |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 130 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 131 | } |
| 132 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 133 | bool onClick(Click* click) override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 134 | const char* state = nullptr; |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 135 | switch (click->fState) { |
| 136 | case Click::kMoved_State: |
| 137 | state = "moved"; |
| 138 | break; |
| 139 | case Click::kUp_State: |
| 140 | state = "up"; |
| 141 | break; |
| 142 | default: |
| 143 | break; |
| 144 | } |
| 145 | if (state) { |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 146 | lua_State* L = fLua->get(); |
| 147 | lua_getglobal(L, gClickName); |
| 148 | fLua->pushScalar(click->fCurr.x()); |
| 149 | fLua->pushScalar(click->fCurr.y()); |
| 150 | fLua->pushString(state); |
| 151 | lua_pcall(L, 3, 1, 0); |
| 152 | return lua_isboolean(L, -1) && lua_toboolean(L, -1); |
| 153 | } |
| 154 | return true; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private: |
| 158 | SkLua* fLua; |
| 159 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 160 | typedef Sample INHERITED; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | ////////////////////////////////////////////////////////////////////////////// |
| 164 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 165 | DEF_SAMPLE( return new LuaView(); ) |