blob: 8e0eaf703d600f9b953e85d9938eda092506b900 [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
21static const char gDrawName[] = "onDrawContent";
22
reed09445a42014-10-10 20:31:24 -070023static const char gMissingCode[] = ""
24 "local paint = Sk.newPaint()"
25 "paint:setAntiAlias(true)"
26 "paint:setTextSize(30)"
27 ""
28 "function onDrawContent(canvas)"
29 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)"
30 "end"
31 ;
reed@google.com3597b732013-05-22 20:12:50 +000032
33class LuaView : public SampleView {
34public:
35 LuaView() : fLua(NULL) {}
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +000036
reed@google.com3597b732013-05-22 20:12:50 +000037 virtual ~LuaView() {
38 SkDELETE(fLua);
39 }
40
reed09445a42014-10-10 20:31:24 -070041 void setImageFilename(lua_State* L) {
42 SkString str = GetResourcePath("mandrill_256.png");
43
44 lua_getglobal(L, "setImageFilename");
45 if (lua_isfunction(L, -1)) {
46 fLua->pushString(str.c_str());
47 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
48 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
49 }
50 }
51 }
52
reed@google.com3597b732013-05-22 20:12:50 +000053 lua_State* ensureLua() {
54 if (NULL == fLua) {
55 fLua = SkNEW(SkLua);
reed09445a42014-10-10 20:31:24 -070056
57 SkString str = GetResourcePath("test.lua");
58 SkData* data = SkData::NewFromFileName(str.c_str());
59 if (data) {
60 fLua->runCode(data->data(), data->size());
61 data->unref();
62 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:
71 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
72 if (SampleCode::TitleQ(*evt)) {
73 SampleCode::TitleR(evt, "Lua");
74 return true;
75 }
76 SkUnichar uni;
77 if (SampleCode::CharQ(*evt, &uni)) {
reed@google.com3597b732013-05-22 20:12:50 +000078 }
79 return this->INHERITED::onQuery(evt);
80 }
81
82 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
83 lua_State* L = this->ensureLua();
84
85 lua_getglobal(L, gDrawName);
86 if (!lua_isfunction(L, -1)) {
87 int t = lua_type(L, -1);
88 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t);
89 lua_pop(L, 1);
90 } else {
91 // does it make sense to try to "cache" the lua version of this
92 // canvas between draws?
93 fLua->pushCanvas(canvas);
94 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
95 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
96 }
97 }
98 // need a way for the lua-sample to tell us if they want animations...
99 // hard-code it ON for now.
100 this->inval(NULL);
101 }
102
103 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
104 unsigned modi) SK_OVERRIDE {
105 return this->INHERITED::onFindClickHandler(x, y, modi);
106 }
107
108 virtual bool onClick(Click* click) SK_OVERRIDE {
109 return this->INHERITED::onClick(click);
110 }
111
112private:
113 SkLua* fLua;
114
115 typedef SampleView INHERITED;
116};
117
118//////////////////////////////////////////////////////////////////////////////
119
120static SkView* MyFactory() { return new LuaView; }
121static SkViewRegister reg(MyFactory);