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