2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SampleCode.h"
12 #include "Resources.h"
21 //#define LUA_FILENAME "test.lua"
22 #define LUA_FILENAME "slides.lua"
24 static const char gDrawName[] = "onDrawContent";
25 static const char gClickName[] = "onClickHandler";
26 static const char gUnicharName[] = "onCharHandler";
28 static const char gLuaClickHandlerName[] = "lua-click-handler";
30 static const char gMissingCode[] = ""
31 "local paint = Sk.newPaint()"
32 "paint:setAntiAlias(true)"
33 "paint:setTextSize(30)"
35 "function onDrawContent(canvas)"
36 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)"
40 class LuaView : public SampleView {
42 LuaView() : fLua(NULL) {}
48 void setImageFilename(lua_State* L) {
49 SkString str = GetResourcePath("mandrill_256.png");
51 lua_getglobal(L, "setImageFilename");
52 if (lua_isfunction(L, -1)) {
53 fLua->pushString(str.c_str());
54 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
55 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
60 lua_State* ensureLua() {
64 SkString str = GetResourcePath(LUA_FILENAME);
65 SkData* data = SkData::NewFromFileName(str.c_str());
67 fLua->runCode(data->data(), data->size());
69 this->setImageFilename(fLua->get());
71 fLua->runCode(gMissingCode);
78 bool onQuery(SkEvent* evt) SK_OVERRIDE {
79 if (SampleCode::TitleQ(*evt)) {
80 SampleCode::TitleR(evt, "Lua");
84 if (SampleCode::CharQ(*evt, &uni)) {
85 lua_State* L = this->ensureLua();
86 lua_getglobal(L, gUnicharName);
87 if (lua_isfunction(L, -1)) {
89 str.appendUnichar(uni);
90 fLua->pushString(str.c_str());
91 if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
92 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
94 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
101 return this->INHERITED::onQuery(evt);
104 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
105 lua_State* L = this->ensureLua();
107 lua_getglobal(L, gDrawName);
108 if (!lua_isfunction(L, -1)) {
109 int t = lua_type(L, -1);
110 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t);
113 // does it make sense to try to "cache" the lua version of this
114 // canvas between draws?
115 fLua->pushCanvas(canvas);
116 fLua->pushScalar(this->width());
117 fLua->pushScalar(this->height());
118 if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
119 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
121 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
128 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
129 unsigned modi) SK_OVERRIDE {
130 lua_State* L = this->ensureLua();
131 lua_getglobal(L, gClickName);
132 if (lua_isfunction(L, -1)) {
135 fLua->pushString("down");
136 if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
137 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
139 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
141 Click* c = new Click(this);
142 c->setType(gLuaClickHandlerName);
147 return this->INHERITED::onFindClickHandler(x, y, modi);
150 bool onClick(Click* click) SK_OVERRIDE {
151 if (click->getType() != gLuaClickHandlerName) {
152 return this->INHERITED::onClick(click);
155 const char* state = NULL;
156 switch (click->fState) {
157 case Click::kMoved_State:
160 case Click::kUp_State:
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);
182 typedef SampleView INHERITED;
185 //////////////////////////////////////////////////////////////////////////////
187 static SkView* MyFactory() { return new LuaView; }
188 static SkViewRegister reg(MyFactory);