blob: 104a427d7aed8c33b2f50ec449580bb10ae2ee9d [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"
12
13extern "C" {
14#include "lua.h"
15#include "lualib.h"
16#include "lauxlib.h"
17}
18
19static const char gDrawName[] = "onDrawContent";
20
21static const char gCode[] = ""
reed@google.comfd345872013-05-22 20:53:42 +000022 "require \"math\" "
23 ""
reed@google.com3597b732013-05-22 20:12:50 +000024 "local r = { left = 10, top = 10, right = 100, bottom = 80 } "
25 "local x = 0;"
26 ""
27 "local paint = Sk.newPaint();"
28 "paint:setAntiAlias(true);"
29 ""
30 "local color = {a = 1, r = 1, g = 0, b = 0};"
31 ""
reed@google.comfd345872013-05-22 20:53:42 +000032 "function rnd(range) "
33 " return math.random() * range;"
34 "end "
35 ""
36 "rndX = function () return rnd(640) end "
37 "rndY = function () return rnd(480) end "
38 ""
39 "function draw_rand_path(canvas);"
40 " if not path_paint then "
41 " path_paint = Sk.newPaint();"
42 " path_paint:setAntiAlias(true);"
43 " end "
44 " path_paint:setColor({a = 1, r = math.random(), g = math.random(), b = math.random() });"
45 ""
46 " local path = Sk.newPath();"
47 " path:moveTo(rndX(), rndY());"
48 " for i = 0, 50 do "
49 " path:quadTo(rndX(), rndY(), rndX(), rndY());"
50 " end "
51 " canvas:drawPath(path, path_paint);"
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000052 ""
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000053 " paint:setColor{a=1,r=0,g=0,b=1};"
54 " local align = { 'left', 'center', 'right' };"
55 " paint:setTextSize(30);"
56 " for k, v in next, align do "
57 " paint:setTextAlign(v);"
58 " canvas:drawText('Hamburgefons', 320, 200 + 30*k, paint);"
59 " end "
60 "end "
61 ""
62 "function onStartup() "
63 " local paint = Sk.newPaint();"
64 " paint:setColor{a=1, r=1, g=0, b=0};"
65 " local doc = Sk.newDocumentPDF('/skia/trunk/test.pdf');"
66 " local canvas = doc:beginPage(72*8.5, 72*11);"
67 " canvas:drawText('Hello Lua', 300, 300, paint);"
68 " doc:close();"
69 " doc = nil;"
reed@google.comfd345872013-05-22 20:53:42 +000070 "end "
71 ""
reed@google.com3597b732013-05-22 20:12:50 +000072 "function onDrawContent(canvas) "
reed@google.comfd345872013-05-22 20:53:42 +000073 " draw_rand_path(canvas);"
reed@google.com3597b732013-05-22 20:12:50 +000074 " color.g = x / 100;"
75 " paint:setColor(color) "
76 " canvas:translate(x, 0);"
77 " canvas:drawOval(r, paint) "
78 " x = x + 1;"
79 " if x > 100 then x = 0 end;"
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000080 "end "
81 ""
82 "onStartup();";
reed@google.com3597b732013-05-22 20:12:50 +000083
84class LuaView : public SampleView {
85public:
86 LuaView() : fLua(NULL) {}
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +000087
reed@google.com3597b732013-05-22 20:12:50 +000088 virtual ~LuaView() {
89 SkDELETE(fLua);
90 }
91
92 lua_State* ensureLua() {
93 if (NULL == fLua) {
94 fLua = SkNEW(SkLua);
95 fLua->runCode(gCode);
96 }
97 return fLua->get();
98 }
99
100protected:
101 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
102 if (SampleCode::TitleQ(*evt)) {
103 SampleCode::TitleR(evt, "Lua");
104 return true;
105 }
106 SkUnichar uni;
107 if (SampleCode::CharQ(*evt, &uni)) {
reed@google.com3597b732013-05-22 20:12:50 +0000108 }
109 return this->INHERITED::onQuery(evt);
110 }
111
112 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
113 lua_State* L = this->ensureLua();
114
115 lua_getglobal(L, gDrawName);
116 if (!lua_isfunction(L, -1)) {
117 int t = lua_type(L, -1);
118 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t);
119 lua_pop(L, 1);
120 } else {
121 // does it make sense to try to "cache" the lua version of this
122 // canvas between draws?
123 fLua->pushCanvas(canvas);
124 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
125 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
126 }
127 }
128 // need a way for the lua-sample to tell us if they want animations...
129 // hard-code it ON for now.
130 this->inval(NULL);
131 }
132
133 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
134 unsigned modi) SK_OVERRIDE {
135 return this->INHERITED::onFindClickHandler(x, y, modi);
136 }
137
138 virtual bool onClick(Click* click) SK_OVERRIDE {
139 return this->INHERITED::onClick(click);
140 }
141
142private:
143 SkLua* fLua;
144
145 typedef SampleView INHERITED;
146};
147
148//////////////////////////////////////////////////////////////////////////////
149
150static SkView* MyFactory() { return new LuaView; }
151static SkViewRegister reg(MyFactory);