blob: 96b15b91b8778f00d26f98fd879cf334cac9879c [file] [log] [blame]
reed@google.com3597b732013-05-22 20:12:50 +00001/*
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
8#include "SampleCode.h"
9#include "SkView.h"
10#include "SkLua.h"
11#include "SkCanvas.h"
reed09445a42014-10-10 20:31:24 -070012#include "Resources.h"
13#include "SkData.h"
reed@google.com3597b732013-05-22 20:12:50 +000014
15extern "C" {
16#include "lua.h"
17#include "lualib.h"
18#include "lauxlib.h"
19}
20
reed9fbc3f32014-10-21 07:12:58 -070021//#define LUA_FILENAME "test.lua"
22#define LUA_FILENAME "slides.lua"
reed18ea7772014-10-11 11:28:07 -070023
reed@google.com3597b732013-05-22 20:12:50 +000024static const char gDrawName[] = "onDrawContent";
reed18ea7772014-10-11 11:28:07 -070025static const char gClickName[] = "onClickHandler";
reed09a1d672014-10-11 13:13:11 -070026static const char gUnicharName[] = "onCharHandler";
reed@google.com3597b732013-05-22 20:12:50 +000027
reed7a72c672014-11-07 10:23:55 -080028static const char gLuaClickHandlerName[] = "lua-click-handler";
29
reed09445a42014-10-10 20:31:24 -070030static const char gMissingCode[] = ""
31 "local paint = Sk.newPaint()"
32 "paint:setAntiAlias(true)"
33 "paint:setTextSize(30)"
34 ""
35 "function onDrawContent(canvas)"
36 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)"
37 "end"
38 ;
reed@google.com3597b732013-05-22 20:12:50 +000039
40class LuaView : public SampleView {
41public:
halcanary96fcdcc2015-08-27 07:41:13 -070042 LuaView() : fLua(nullptr) {}
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +000043
halcanary385fe4d2015-08-26 13:07:48 -070044 virtual ~LuaView() { delete fLua; }
reed@google.com3597b732013-05-22 20:12:50 +000045
reed09445a42014-10-10 20:31:24 -070046 void setImageFilename(lua_State* L) {
47 SkString str = GetResourcePath("mandrill_256.png");
48
49 lua_getglobal(L, "setImageFilename");
50 if (lua_isfunction(L, -1)) {
51 fLua->pushString(str.c_str());
52 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
53 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
54 }
55 }
56 }
57
reed@google.com3597b732013-05-22 20:12:50 +000058 lua_State* ensureLua() {
halcanary96fcdcc2015-08-27 07:41:13 -070059 if (nullptr == fLua) {
halcanary385fe4d2015-08-26 13:07:48 -070060 fLua = new SkLua;
reed09445a42014-10-10 20:31:24 -070061
reed18ea7772014-10-11 11:28:07 -070062 SkString str = GetResourcePath(LUA_FILENAME);
reed09445a42014-10-10 20:31:24 -070063 SkData* data = SkData::NewFromFileName(str.c_str());
64 if (data) {
65 fLua->runCode(data->data(), data->size());
66 data->unref();
67 this->setImageFilename(fLua->get());
68 } else {
69 fLua->runCode(gMissingCode);
70 }
reed@google.com3597b732013-05-22 20:12:50 +000071 }
72 return fLua->get();
73 }
74
75protected:
mtklein36352bf2015-03-25 18:17:31 -070076 bool onQuery(SkEvent* evt) override {
reed@google.com3597b732013-05-22 20:12:50 +000077 if (SampleCode::TitleQ(*evt)) {
78 SampleCode::TitleR(evt, "Lua");
79 return true;
80 }
81 SkUnichar uni;
82 if (SampleCode::CharQ(*evt, &uni)) {
reed09a1d672014-10-11 13:13:11 -070083 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)) {
halcanary96fcdcc2015-08-27 07:41:13 -070093 this->inval(nullptr);
reed09a1d672014-10-11 13:13:11 -070094 return true;
95 }
96 }
97 }
reed@google.com3597b732013-05-22 20:12:50 +000098 }
99 return this->INHERITED::onQuery(evt);
100 }
101
mtklein36352bf2015-03-25 18:17:31 -0700102 void onDrawContent(SkCanvas* canvas) override {
reed@google.com3597b732013-05-22 20:12:50 +0000103 lua_State* L = this->ensureLua();
104
105 lua_getglobal(L, gDrawName);
106 if (!lua_isfunction(L, -1)) {
107 int t = lua_type(L, -1);
108 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t);
109 lua_pop(L, 1);
110 } else {
111 // does it make sense to try to "cache" the lua version of this
112 // canvas between draws?
113 fLua->pushCanvas(canvas);
reedbdc49ae2014-10-14 09:34:52 -0700114 fLua->pushScalar(this->width());
115 fLua->pushScalar(this->height());
116 if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
reed@google.com3597b732013-05-22 20:12:50 +0000117 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
reed18ea7772014-10-11 11:28:07 -0700118 } else {
119 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700120 this->inval(nullptr);
reed18ea7772014-10-11 11:28:07 -0700121 }
reed@google.com3597b732013-05-22 20:12:50 +0000122 }
123 }
reed@google.com3597b732013-05-22 20:12:50 +0000124 }
125
126 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700127 unsigned modi) override {
reed18ea7772014-10-11 11:28:07 -0700128 lua_State* L = this->ensureLua();
129 lua_getglobal(L, gClickName);
130 if (lua_isfunction(L, -1)) {
131 fLua->pushScalar(x);
132 fLua->pushScalar(y);
reed7a72c672014-11-07 10:23:55 -0800133 fLua->pushString("down");
134 if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
reed18ea7772014-10-11 11:28:07 -0700135 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
136 } else {
137 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700138 this->inval(nullptr);
reed7a72c672014-11-07 10:23:55 -0800139 Click* c = new Click(this);
140 c->setType(gLuaClickHandlerName);
141 return c;
reed18ea7772014-10-11 11:28:07 -0700142 }
143 }
144 }
reed@google.com3597b732013-05-22 20:12:50 +0000145 return this->INHERITED::onFindClickHandler(x, y, modi);
146 }
147
mtklein36352bf2015-03-25 18:17:31 -0700148 bool onClick(Click* click) override {
reed7a72c672014-11-07 10:23:55 -0800149 if (click->getType() != gLuaClickHandlerName) {
150 return this->INHERITED::onClick(click);
151 }
152
halcanary96fcdcc2015-08-27 07:41:13 -0700153 const char* state = nullptr;
reed7a72c672014-11-07 10:23:55 -0800154 switch (click->fState) {
155 case Click::kMoved_State:
156 state = "moved";
157 break;
158 case Click::kUp_State:
159 state = "up";
160 break;
161 default:
162 break;
163 }
164 if (state) {
halcanary96fcdcc2015-08-27 07:41:13 -0700165 this->inval(nullptr);
reed7a72c672014-11-07 10:23:55 -0800166 lua_State* L = fLua->get();
167 lua_getglobal(L, gClickName);
168 fLua->pushScalar(click->fCurr.x());
169 fLua->pushScalar(click->fCurr.y());
170 fLua->pushString(state);
171 lua_pcall(L, 3, 1, 0);
172 return lua_isboolean(L, -1) && lua_toboolean(L, -1);
173 }
174 return true;
reed@google.com3597b732013-05-22 20:12:50 +0000175 }
176
177private:
178 SkLua* fLua;
179
180 typedef SampleView INHERITED;
181};
182
183//////////////////////////////////////////////////////////////////////////////
184
185static SkView* MyFactory() { return new LuaView; }
186static SkViewRegister reg(MyFactory);