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 | 27 |
27 static const char gMissingCode[] = "" | 28 static const char gMissingCode[] = "" |
28 "local paint = Sk.newPaint()" | 29 "local paint = Sk.newPaint()" |
29 "paint:setAntiAlias(true)" | 30 "paint:setAntiAlias(true)" |
30 "paint:setTextSize(30)" | 31 "paint:setTextSize(30)" |
31 "" | 32 "" |
32 "function onDrawContent(canvas)" | 33 "function onDrawContent(canvas)" |
33 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" | 34 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" |
34 "end" | 35 "end" |
35 ; | 36 ; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 } | 73 } |
73 | 74 |
74 protected: | 75 protected: |
75 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { | 76 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { |
76 if (SampleCode::TitleQ(*evt)) { | 77 if (SampleCode::TitleQ(*evt)) { |
77 SampleCode::TitleR(evt, "Lua"); | 78 SampleCode::TitleR(evt, "Lua"); |
78 return true; | 79 return true; |
79 } | 80 } |
80 SkUnichar uni; | 81 SkUnichar uni; |
81 if (SampleCode::CharQ(*evt, &uni)) { | 82 if (SampleCode::CharQ(*evt, &uni)) { |
| 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 } |
82 } | 98 } |
83 return this->INHERITED::onQuery(evt); | 99 return this->INHERITED::onQuery(evt); |
84 } | 100 } |
85 | 101 |
86 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { | 102 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { |
87 lua_State* L = this->ensureLua(); | 103 lua_State* L = this->ensureLua(); |
88 | 104 |
89 lua_getglobal(L, gDrawName); | 105 lua_getglobal(L, gDrawName); |
90 if (!lua_isfunction(L, -1)) { | 106 if (!lua_isfunction(L, -1)) { |
91 int t = lua_type(L, -1); | 107 int t = lua_type(L, -1); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 private: | 146 private: |
131 SkLua* fLua; | 147 SkLua* fLua; |
132 | 148 |
133 typedef SampleView INHERITED; | 149 typedef SampleView INHERITED; |
134 }; | 150 }; |
135 | 151 |
136 ////////////////////////////////////////////////////////////////////////////// | 152 ////////////////////////////////////////////////////////////////////////////// |
137 | 153 |
138 static SkView* MyFactory() { return new LuaView; } | 154 static SkView* MyFactory() { return new LuaView; } |
139 static SkViewRegister reg(MyFactory); | 155 static SkViewRegister reg(MyFactory); |
OLD | NEW |