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" |
(...skipping 23 matching lines...) Expand all Loading... |
34 "" | 34 "" |
35 "function onDrawContent(canvas)" | 35 "function onDrawContent(canvas)" |
36 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" | 36 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" |
37 "end" | 37 "end" |
38 ; | 38 ; |
39 | 39 |
40 class LuaView : public SampleView { | 40 class LuaView : public SampleView { |
41 public: | 41 public: |
42 LuaView() : fLua(NULL) {} | 42 LuaView() : fLua(NULL) {} |
43 | 43 |
44 virtual ~LuaView() { | 44 virtual ~LuaView() { delete fLua; } |
45 SkDELETE(fLua); | |
46 } | |
47 | 45 |
48 void setImageFilename(lua_State* L) { | 46 void setImageFilename(lua_State* L) { |
49 SkString str = GetResourcePath("mandrill_256.png"); | 47 SkString str = GetResourcePath("mandrill_256.png"); |
50 | 48 |
51 lua_getglobal(L, "setImageFilename"); | 49 lua_getglobal(L, "setImageFilename"); |
52 if (lua_isfunction(L, -1)) { | 50 if (lua_isfunction(L, -1)) { |
53 fLua->pushString(str.c_str()); | 51 fLua->pushString(str.c_str()); |
54 if (lua_pcall(L, 1, 0, 0) != LUA_OK) { | 52 if (lua_pcall(L, 1, 0, 0) != LUA_OK) { |
55 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); | 53 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
56 } | 54 } |
57 } | 55 } |
58 } | 56 } |
59 | 57 |
60 lua_State* ensureLua() { | 58 lua_State* ensureLua() { |
61 if (NULL == fLua) { | 59 if (NULL == fLua) { |
62 fLua = SkNEW(SkLua); | 60 fLua = new SkLua; |
63 | 61 |
64 SkString str = GetResourcePath(LUA_FILENAME); | 62 SkString str = GetResourcePath(LUA_FILENAME); |
65 SkData* data = SkData::NewFromFileName(str.c_str()); | 63 SkData* data = SkData::NewFromFileName(str.c_str()); |
66 if (data) { | 64 if (data) { |
67 fLua->runCode(data->data(), data->size()); | 65 fLua->runCode(data->data(), data->size()); |
68 data->unref(); | 66 data->unref(); |
69 this->setImageFilename(fLua->get()); | 67 this->setImageFilename(fLua->get()); |
70 } else { | 68 } else { |
71 fLua->runCode(gMissingCode); | 69 fLua->runCode(gMissingCode); |
72 } | 70 } |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 private: | 177 private: |
180 SkLua* fLua; | 178 SkLua* fLua; |
181 | 179 |
182 typedef SampleView INHERITED; | 180 typedef SampleView INHERITED; |
183 }; | 181 }; |
184 | 182 |
185 ////////////////////////////////////////////////////////////////////////////// | 183 ////////////////////////////////////////////////////////////////////////////// |
186 | 184 |
187 static SkView* MyFactory() { return new LuaView; } | 185 static SkView* MyFactory() { return new LuaView; } |
188 static SkViewRegister reg(MyFactory); | 186 static SkViewRegister reg(MyFactory); |
OLD | NEW |