blob: 1a094bb1972ed8524ef61bf9fe82193eaa30f517 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/utils/SkLua.h"
11#include "samplecode/Sample.h"
12#include "tools/Resources.h"
reed@google.com3597b732013-05-22 20:12:50 +000013
14extern "C" {
15#include "lua.h"
16#include "lualib.h"
17#include "lauxlib.h"
18}
19
Hal Canaryc465d132017-12-08 10:21:31 -050020//#define LUA_FILENAME "lua/test.lua"
21#define LUA_FILENAME "lua/slides.lua"
reed18ea7772014-10-11 11:28:07 -070022
reed@google.com3597b732013-05-22 20:12:50 +000023static const char gDrawName[] = "onDrawContent";
reed18ea7772014-10-11 11:28:07 -070024static const char gClickName[] = "onClickHandler";
reed09a1d672014-10-11 13:13:11 -070025static const char gUnicharName[] = "onCharHandler";
reed@google.com3597b732013-05-22 20:12:50 +000026
reed09445a42014-10-10 20:31:24 -070027static const char gMissingCode[] = ""
28 "local paint = Sk.newPaint()"
29 "paint:setAntiAlias(true)"
30 "paint:setTextSize(30)"
31 ""
32 "function onDrawContent(canvas)"
33 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)"
34 "end"
35 ;
reed@google.com3597b732013-05-22 20:12:50 +000036
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040037class LuaView : public Sample {
reed@google.com3597b732013-05-22 20:12:50 +000038public:
halcanary96fcdcc2015-08-27 07:41:13 -070039 LuaView() : fLua(nullptr) {}
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +000040
Ben Wagner5a965802017-10-09 10:34:09 -040041 ~LuaView() override { delete fLua; }
reed@google.com3597b732013-05-22 20:12:50 +000042
reed09445a42014-10-10 20:31:24 -070043 void setImageFilename(lua_State* L) {
Hal Canaryc465d132017-12-08 10:21:31 -050044 SkString str = GetResourcePath("images/mandrill_256.png");
reed09445a42014-10-10 20:31:24 -070045
46 lua_getglobal(L, "setImageFilename");
47 if (lua_isfunction(L, -1)) {
48 fLua->pushString(str.c_str());
49 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
50 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
51 }
52 }
53 }
54
reed@google.com3597b732013-05-22 20:12:50 +000055 lua_State* ensureLua() {
halcanary96fcdcc2015-08-27 07:41:13 -070056 if (nullptr == fLua) {
halcanary385fe4d2015-08-26 13:07:48 -070057 fLua = new SkLua;
reed09445a42014-10-10 20:31:24 -070058
Ben Wagnerd6de89b2018-05-22 11:24:21 -040059 sk_sp<SkData> data = GetResourceAsData(LUA_FILENAME);
reed09445a42014-10-10 20:31:24 -070060 if (data) {
61 fLua->runCode(data->data(), data->size());
reed09445a42014-10-10 20:31:24 -070062 this->setImageFilename(fLua->get());
63 } else {
64 fLua->runCode(gMissingCode);
65 }
reed@google.com3597b732013-05-22 20:12:50 +000066 }
67 return fLua->get();
68 }
69
70protected:
Hal Canary8a027312019-07-03 10:55:44 -040071 SkString name() override { return SkString("Lua"); }
72
Hal Canary6cc65e12019-07-03 15:53:04 -040073 bool onChar(SkUnichar uni) override {
reed09a1d672014-10-11 13:13:11 -070074 lua_State* L = this->ensureLua();
75 lua_getglobal(L, gUnicharName);
76 if (lua_isfunction(L, -1)) {
77 SkString str;
78 str.appendUnichar(uni);
79 fLua->pushString(str.c_str());
80 if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
81 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
82 } else {
83 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
reed09a1d672014-10-11 13:13:11 -070084 return true;
85 }
86 }
87 }
Hal Canary6cc65e12019-07-03 15:53:04 -040088 return false;
reed@google.com3597b732013-05-22 20:12:50 +000089 }
90
mtklein36352bf2015-03-25 18:17:31 -070091 void onDrawContent(SkCanvas* canvas) override {
reed@google.com3597b732013-05-22 20:12:50 +000092 lua_State* L = this->ensureLua();
93
94 lua_getglobal(L, gDrawName);
95 if (!lua_isfunction(L, -1)) {
96 int t = lua_type(L, -1);
97 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t);
98 lua_pop(L, 1);
99 } else {
100 // does it make sense to try to "cache" the lua version of this
101 // canvas between draws?
102 fLua->pushCanvas(canvas);
reedbdc49ae2014-10-14 09:34:52 -0700103 fLua->pushScalar(this->width());
104 fLua->pushScalar(this->height());
105 if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
reed@google.com3597b732013-05-22 20:12:50 +0000106 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
107 }
108 }
reed@google.com3597b732013-05-22 20:12:50 +0000109 }
110
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400111 virtual Sample::Click* onFindClickHandler(SkScalar x, SkScalar y,
Hal Canaryb1f411a2019-08-29 10:39:22 -0400112 skui::ModifierKey modi) override {
reed18ea7772014-10-11 11:28:07 -0700113 lua_State* L = this->ensureLua();
114 lua_getglobal(L, gClickName);
115 if (lua_isfunction(L, -1)) {
116 fLua->pushScalar(x);
117 fLua->pushScalar(y);
reed7a72c672014-11-07 10:23:55 -0800118 fLua->pushString("down");
119 if (lua_pcall(L, 3, 1, 0) != LUA_OK) {
reed18ea7772014-10-11 11:28:07 -0700120 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
121 } else {
122 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400123 return new Click();
reed18ea7772014-10-11 11:28:07 -0700124 }
125 }
126 }
reed@google.com3597b732013-05-22 20:12:50 +0000127 return this->INHERITED::onFindClickHandler(x, y, modi);
128 }
129
mtklein36352bf2015-03-25 18:17:31 -0700130 bool onClick(Click* click) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700131 const char* state = nullptr;
reed7a72c672014-11-07 10:23:55 -0800132 switch (click->fState) {
Hal Canaryb1f411a2019-08-29 10:39:22 -0400133 case skui::InputState::kMove:
reed7a72c672014-11-07 10:23:55 -0800134 state = "moved";
135 break;
Hal Canaryb1f411a2019-08-29 10:39:22 -0400136 case skui::InputState::kUp:
reed7a72c672014-11-07 10:23:55 -0800137 state = "up";
138 break;
139 default:
140 break;
141 }
142 if (state) {
reed7a72c672014-11-07 10:23:55 -0800143 lua_State* L = fLua->get();
144 lua_getglobal(L, gClickName);
145 fLua->pushScalar(click->fCurr.x());
146 fLua->pushScalar(click->fCurr.y());
147 fLua->pushString(state);
148 lua_pcall(L, 3, 1, 0);
149 return lua_isboolean(L, -1) && lua_toboolean(L, -1);
150 }
151 return true;
reed@google.com3597b732013-05-22 20:12:50 +0000152 }
153
154private:
155 SkLua* fLua;
156
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400157 typedef Sample INHERITED;
reed@google.com3597b732013-05-22 20:12:50 +0000158};
159
160//////////////////////////////////////////////////////////////////////////////
161
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400162DEF_SAMPLE( return new LuaView(); )