blob: e7af727ab5b7b63e30870eeb347878fee5de286b [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
reed18ea7772014-10-11 11:28:07 -070021#define LUA_FILENAME "test.lua"
22//#define LUA_FILENAME "slides.lua"
23
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:
40 LuaView() : fLua(NULL) {}
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +000041
reed@google.com3597b732013-05-22 20:12:50 +000042 virtual ~LuaView() {
43 SkDELETE(fLua);
44 }
45
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() {
59 if (NULL == fLua) {
60 fLua = SkNEW(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:
76 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
77 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)) {
93 this->inval(NULL);
94 return true;
95 }
96 }
97 }
reed@google.com3597b732013-05-22 20:12:50 +000098 }
99 return this->INHERITED::onQuery(evt);
100 }
101
102 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
103 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);
reed18ea7772014-10-11 11:28:07 -0700114 if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
reed@google.com3597b732013-05-22 20:12:50 +0000115 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
reed18ea7772014-10-11 11:28:07 -0700116 } else {
117 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
118 this->inval(NULL);
119 }
reed@google.com3597b732013-05-22 20:12:50 +0000120 }
121 }
reed@google.com3597b732013-05-22 20:12:50 +0000122 }
123
124 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
125 unsigned modi) SK_OVERRIDE {
reed18ea7772014-10-11 11:28:07 -0700126 lua_State* L = this->ensureLua();
127 lua_getglobal(L, gClickName);
128 if (lua_isfunction(L, -1)) {
129 fLua->pushScalar(x);
130 fLua->pushScalar(y);
131 if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
132 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
133 } else {
134 if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
135 this->inval(NULL);
136 }
137 }
138 }
reed@google.com3597b732013-05-22 20:12:50 +0000139 return this->INHERITED::onFindClickHandler(x, y, modi);
140 }
141
142 virtual bool onClick(Click* click) SK_OVERRIDE {
143 return this->INHERITED::onClick(click);
144 }
145
146private:
147 SkLua* fLua;
148
149 typedef SampleView INHERITED;
150};
151
152//////////////////////////////////////////////////////////////////////////////
153
154static SkView* MyFactory() { return new LuaView; }
155static SkViewRegister reg(MyFactory);