blob: 7b04fd073bf62722268b82cde06d3f818b389434 [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
Ben Wagnerd6de89b2018-05-22 11:24:21 -040060 sk_sp<SkData> data = GetResourceAsData(LUA_FILENAME);
reed09445a42014-10-10 20:31:24 -070061 if (data) {
62 fLua->runCode(data->data(), data->size());
reed09445a42014-10-10 20:31:24 -070063 this->setImageFilename(fLua->get());
64 } else {
65 fLua->runCode(gMissingCode);
66 }
reed@google.com3597b732013-05-22 20:12:50 +000067 }
68 return fLua->get();
69 }
70
71protected:
mtklein36352bf2015-03-25 18:17:31 -070072 bool onQuery(SkEvent* evt) override {
reed@google.com3597b732013-05-22 20:12:50 +000073 if (SampleCode::TitleQ(*evt)) {
74 SampleCode::TitleR(evt, "Lua");
75 return true;
76 }
77 SkUnichar uni;
78 if (SampleCode::CharQ(*evt, &uni)) {
reed09a1d672014-10-11 13:13:11 -070079 lua_State* L = this->ensureLua();
80 lua_getglobal(L, gUnicharName);
81 if (lua_isfunction(L, -1)) {
82 SkString str;
83 str.appendUnichar(uni);
84 fLua->pushString(str.c_str());
85 if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
86 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
87 } else {
88 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
reed09a1d672014-10-11 13:13:11 -070089 return true;
90 }
91 }
92 }
reed@google.com3597b732013-05-22 20:12:50 +000093 }
94 return this->INHERITED::onQuery(evt);
95 }
96
mtklein36352bf2015-03-25 18:17:31 -070097 void onDrawContent(SkCanvas* canvas) override {
reed@google.com3597b732013-05-22 20:12:50 +000098 lua_State* L = this->ensureLua();
99
100 lua_getglobal(L, gDrawName);
101 if (!lua_isfunction(L, -1)) {
102 int t = lua_type(L, -1);
103 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t);
104 lua_pop(L, 1);
105 } else {
106 // does it make sense to try to "cache" the lua version of this
107 // canvas between draws?
108 fLua->pushCanvas(canvas);
reedbdc49ae2014-10-14 09:34:52 -0700109 fLua->pushScalar(this->width());
110 fLua->pushScalar(this->height());
111 if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
reed@google.com3597b732013-05-22 20:12:50 +0000112 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
113 }
114 }
reed@google.com3597b732013-05-22 20:12:50 +0000115 }
116
117 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700118 unsigned modi) override {
reed18ea7772014-10-11 11:28:07 -0700119 lua_State* L = this->ensureLua();
120 lua_getglobal(L, gClickName);
121 if (lua_isfunction(L, -1)) {
122 fLua->pushScalar(x);
123 fLua->pushScalar(y);
reed7a72c672014-11-07 10:23:55 -0800124 fLua->pushString("down");
125 if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
reed18ea7772014-10-11 11:28:07 -0700126 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
127 } else {
128 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
Florin Malitaba19b6f2017-12-11 11:48:29 -0500129 return new Click(this);
reed18ea7772014-10-11 11:28:07 -0700130 }
131 }
132 }
reed@google.com3597b732013-05-22 20:12:50 +0000133 return this->INHERITED::onFindClickHandler(x, y, modi);
134 }
135
mtklein36352bf2015-03-25 18:17:31 -0700136 bool onClick(Click* click) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700137 const char* state = nullptr;
reed7a72c672014-11-07 10:23:55 -0800138 switch (click->fState) {
139 case Click::kMoved_State:
140 state = "moved";
141 break;
142 case Click::kUp_State:
143 state = "up";
144 break;
145 default:
146 break;
147 }
148 if (state) {
reed7a72c672014-11-07 10:23:55 -0800149 lua_State* L = fLua->get();
150 lua_getglobal(L, gClickName);
151 fLua->pushScalar(click->fCurr.x());
152 fLua->pushScalar(click->fCurr.y());
153 fLua->pushString(state);
154 lua_pcall(L, 3, 1, 0);
155 return lua_isboolean(L, -1) && lua_toboolean(L, -1);
156 }
157 return true;
reed@google.com3597b732013-05-22 20:12:50 +0000158 }
159
160private:
161 SkLua* fLua;
162
163 typedef SampleView INHERITED;
164};
165
166//////////////////////////////////////////////////////////////////////////////
167
168static SkView* MyFactory() { return new LuaView; }
169static SkViewRegister reg(MyFactory);