blob: 2c94e81f762b2a934e3527e4262c85e9ffebeb20 [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
Hal Canaryc465d132017-12-08 10:21:31 -050021//#define LUA_FILENAME "lua/test.lua"
22#define LUA_FILENAME "lua/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
reed09445a42014-10-10 20:31:24 -070028static const char gMissingCode[] = ""
29 "local paint = Sk.newPaint()"
30 "paint:setAntiAlias(true)"
31 "paint:setTextSize(30)"
32 ""
33 "function onDrawContent(canvas)"
34 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)"
35 "end"
36 ;
reed@google.com3597b732013-05-22 20:12:50 +000037
38class LuaView : public SampleView {
39public:
halcanary96fcdcc2015-08-27 07:41:13 -070040 LuaView() : fLua(nullptr) {}
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +000041
Ben Wagner5a965802017-10-09 10:34:09 -040042 ~LuaView() override { delete fLua; }
reed@google.com3597b732013-05-22 20:12:50 +000043
reed09445a42014-10-10 20:31:24 -070044 void setImageFilename(lua_State* L) {
Hal Canaryc465d132017-12-08 10:21:31 -050045 SkString str = GetResourcePath("images/mandrill_256.png");
reed09445a42014-10-10 20:31:24 -070046
47 lua_getglobal(L, "setImageFilename");
48 if (lua_isfunction(L, -1)) {
49 fLua->pushString(str.c_str());
50 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
51 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
52 }
53 }
54 }
55
reed@google.com3597b732013-05-22 20:12:50 +000056 lua_State* ensureLua() {
halcanary96fcdcc2015-08-27 07:41:13 -070057 if (nullptr == fLua) {
halcanary385fe4d2015-08-26 13:07:48 -070058 fLua = new SkLua;
reed09445a42014-10-10 20:31:24 -070059
reed18ea7772014-10-11 11:28:07 -070060 SkString str = GetResourcePath(LUA_FILENAME);
bungeman38d909e2016-08-02 14:40:46 -070061 sk_sp<SkData> data(SkData::MakeFromFileName(str.c_str()));
reed09445a42014-10-10 20:31:24 -070062 if (data) {
63 fLua->runCode(data->data(), data->size());
reed09445a42014-10-10 20:31:24 -070064 this->setImageFilename(fLua->get());
65 } else {
66 fLua->runCode(gMissingCode);
67 }
reed@google.com3597b732013-05-22 20:12:50 +000068 }
69 return fLua->get();
70 }
71
72protected:
mtklein36352bf2015-03-25 18:17:31 -070073 bool onQuery(SkEvent* evt) override {
reed@google.com3597b732013-05-22 20:12:50 +000074 if (SampleCode::TitleQ(*evt)) {
75 SampleCode::TitleR(evt, "Lua");
76 return true;
77 }
78 SkUnichar uni;
79 if (SampleCode::CharQ(*evt, &uni)) {
reed09a1d672014-10-11 13:13:11 -070080 lua_State* L = this->ensureLua();
81 lua_getglobal(L, gUnicharName);
82 if (lua_isfunction(L, -1)) {
83 SkString str;
84 str.appendUnichar(uni);
85 fLua->pushString(str.c_str());
86 if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
87 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
88 } else {
89 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
reed09a1d672014-10-11 13:13:11 -070090 return true;
91 }
92 }
93 }
reed@google.com3597b732013-05-22 20:12:50 +000094 }
95 return this->INHERITED::onQuery(evt);
96 }
97
mtklein36352bf2015-03-25 18:17:31 -070098 void onDrawContent(SkCanvas* canvas) override {
reed@google.com3597b732013-05-22 20:12:50 +000099 lua_State* L = this->ensureLua();
100
101 lua_getglobal(L, gDrawName);
102 if (!lua_isfunction(L, -1)) {
103 int t = lua_type(L, -1);
104 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t);
105 lua_pop(L, 1);
106 } else {
107 // does it make sense to try to "cache" the lua version of this
108 // canvas between draws?
109 fLua->pushCanvas(canvas);
reedbdc49ae2014-10-14 09:34:52 -0700110 fLua->pushScalar(this->width());
111 fLua->pushScalar(this->height());
112 if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
reed@google.com3597b732013-05-22 20:12:50 +0000113 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
114 }
115 }
reed@google.com3597b732013-05-22 20:12:50 +0000116 }
117
118 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700119 unsigned modi) override {
reed18ea7772014-10-11 11:28:07 -0700120 lua_State* L = this->ensureLua();
121 lua_getglobal(L, gClickName);
122 if (lua_isfunction(L, -1)) {
123 fLua->pushScalar(x);
124 fLua->pushScalar(y);
reed7a72c672014-11-07 10:23:55 -0800125 fLua->pushString("down");
126 if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
reed18ea7772014-10-11 11:28:07 -0700127 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
128 } else {
129 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
Florin Malitaba19b6f2017-12-11 11:48:29 -0500130 return new Click(this);
reed18ea7772014-10-11 11:28:07 -0700131 }
132 }
133 }
reed@google.com3597b732013-05-22 20:12:50 +0000134 return this->INHERITED::onFindClickHandler(x, y, modi);
135 }
136
mtklein36352bf2015-03-25 18:17:31 -0700137 bool onClick(Click* click) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700138 const char* state = nullptr;
reed7a72c672014-11-07 10:23:55 -0800139 switch (click->fState) {
140 case Click::kMoved_State:
141 state = "moved";
142 break;
143 case Click::kUp_State:
144 state = "up";
145 break;
146 default:
147 break;
148 }
149 if (state) {
reed7a72c672014-11-07 10:23:55 -0800150 lua_State* L = fLua->get();
151 lua_getglobal(L, gClickName);
152 fLua->pushScalar(click->fCurr.x());
153 fLua->pushScalar(click->fCurr.y());
154 fLua->pushString(state);
155 lua_pcall(L, 3, 1, 0);
156 return lua_isboolean(L, -1) && lua_toboolean(L, -1);
157 }
158 return true;
reed@google.com3597b732013-05-22 20:12:50 +0000159 }
160
161private:
162 SkLua* fLua;
163
164 typedef SampleView INHERITED;
165};
166
167//////////////////////////////////////////////////////////////////////////////
168
169static SkView* MyFactory() { return new LuaView; }
170static SkViewRegister reg(MyFactory);