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" | 21 //#define LUA_FILENAME "test.lua" |
22 #define LUA_FILENAME "slides.lua" | 22 #define LUA_FILENAME "slides.lua" |
23 | 23 |
24 static const char gDrawName[] = "onDrawContent"; | 24 static const char gDrawName[] = "onDrawContent"; |
25 static const char gClickName[] = "onClickHandler"; | 25 static const char gClickName[] = "onClickHandler"; |
26 static const char gUnicharName[] = "onCharHandler"; | 26 static const char gUnicharName[] = "onCharHandler"; |
27 | 27 |
| 28 static const char gLuaClickHandlerName[] = "lua-click-handler"; |
| 29 |
28 static const char gMissingCode[] = "" | 30 static const char gMissingCode[] = "" |
29 "local paint = Sk.newPaint()" | 31 "local paint = Sk.newPaint()" |
30 "paint:setAntiAlias(true)" | 32 "paint:setAntiAlias(true)" |
31 "paint:setTextSize(30)" | 33 "paint:setTextSize(30)" |
32 "" | 34 "" |
33 "function onDrawContent(canvas)" | 35 "function onDrawContent(canvas)" |
34 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" | 36 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" |
35 "end" | 37 "end" |
36 ; | 38 ; |
37 | 39 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 } | 125 } |
124 } | 126 } |
125 | 127 |
126 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, | 128 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
127 unsigned modi) SK_OVERRIDE { | 129 unsigned modi) SK_OVERRIDE { |
128 lua_State* L = this->ensureLua(); | 130 lua_State* L = this->ensureLua(); |
129 lua_getglobal(L, gClickName); | 131 lua_getglobal(L, gClickName); |
130 if (lua_isfunction(L, -1)) { | 132 if (lua_isfunction(L, -1)) { |
131 fLua->pushScalar(x); | 133 fLua->pushScalar(x); |
132 fLua->pushScalar(y); | 134 fLua->pushScalar(y); |
133 if (lua_pcall(L, 2, 1, 0) != LUA_OK) { | 135 fLua->pushString("down"); |
| 136 if (lua_pcall(L, 3, 1, 0) != LUA_OK) { |
134 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); | 137 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
135 } else { | 138 } else { |
136 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { | 139 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { |
137 this->inval(NULL); | 140 this->inval(NULL); |
| 141 Click* c = new Click(this); |
| 142 c->setType(gLuaClickHandlerName); |
| 143 return c; |
138 } | 144 } |
139 } | 145 } |
140 } | 146 } |
141 return this->INHERITED::onFindClickHandler(x, y, modi); | 147 return this->INHERITED::onFindClickHandler(x, y, modi); |
142 } | 148 } |
143 | 149 |
144 virtual bool onClick(Click* click) SK_OVERRIDE { | 150 virtual bool onClick(Click* click) SK_OVERRIDE { |
145 return this->INHERITED::onClick(click); | 151 if (click->getType() != gLuaClickHandlerName) { |
| 152 return this->INHERITED::onClick(click); |
| 153 } |
| 154 |
| 155 const char* state = NULL; |
| 156 switch (click->fState) { |
| 157 case Click::kMoved_State: |
| 158 state = "moved"; |
| 159 break; |
| 160 case Click::kUp_State: |
| 161 state = "up"; |
| 162 break; |
| 163 default: |
| 164 break; |
| 165 } |
| 166 if (state) { |
| 167 this->inval(NULL); |
| 168 lua_State* L = fLua->get(); |
| 169 lua_getglobal(L, gClickName); |
| 170 fLua->pushScalar(click->fCurr.x()); |
| 171 fLua->pushScalar(click->fCurr.y()); |
| 172 fLua->pushString(state); |
| 173 lua_pcall(L, 3, 1, 0); |
| 174 return lua_isboolean(L, -1) && lua_toboolean(L, -1); |
| 175 } |
| 176 return true; |
146 } | 177 } |
147 | 178 |
148 private: | 179 private: |
149 SkLua* fLua; | 180 SkLua* fLua; |
150 | 181 |
151 typedef SampleView INHERITED; | 182 typedef SampleView INHERITED; |
152 }; | 183 }; |
153 | 184 |
154 ////////////////////////////////////////////////////////////////////////////// | 185 ////////////////////////////////////////////////////////////////////////////// |
155 | 186 |
156 static SkView* MyFactory() { return new LuaView; } | 187 static SkView* MyFactory() { return new LuaView; } |
157 static SkViewRegister reg(MyFactory); | 188 static SkViewRegister reg(MyFactory); |
OLD | NEW |