blob: 1ad89f9d540171929bb7e9582649f3f505623765 [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);"
52 "end "
53 ""
reed@google.com3597b732013-05-22 20:12:50 +000054 "function onDrawContent(canvas) "
reed@google.comfd345872013-05-22 20:53:42 +000055 " draw_rand_path(canvas);"
reed@google.com3597b732013-05-22 20:12:50 +000056 " color.g = x / 100;"
57 " paint:setColor(color) "
58 " canvas:translate(x, 0);"
59 " canvas:drawOval(r, paint) "
60 " x = x + 1;"
61 " if x > 100 then x = 0 end;"
62 "end";
63
64class LuaView : public SampleView {
65public:
66 LuaView() : fLua(NULL) {}
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +000067
reed@google.com3597b732013-05-22 20:12:50 +000068 virtual ~LuaView() {
69 SkDELETE(fLua);
70 }
71
72 lua_State* ensureLua() {
73 if (NULL == fLua) {
74 fLua = SkNEW(SkLua);
75 fLua->runCode(gCode);
76 }
77 return fLua->get();
78 }
79
80protected:
81 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
82 if (SampleCode::TitleQ(*evt)) {
83 SampleCode::TitleR(evt, "Lua");
84 return true;
85 }
86 SkUnichar uni;
87 if (SampleCode::CharQ(*evt, &uni)) {
reed@google.com3597b732013-05-22 20:12:50 +000088 }
89 return this->INHERITED::onQuery(evt);
90 }
91
92 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
93 lua_State* L = this->ensureLua();
94
95 lua_getglobal(L, gDrawName);
96 if (!lua_isfunction(L, -1)) {
97 int t = lua_type(L, -1);
98 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t);
99 lua_pop(L, 1);
100 } else {
101 // does it make sense to try to "cache" the lua version of this
102 // canvas between draws?
103 fLua->pushCanvas(canvas);
104 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
105 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
106 }
107 }
108 // need a way for the lua-sample to tell us if they want animations...
109 // hard-code it ON for now.
110 this->inval(NULL);
111 }
112
113 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
114 unsigned modi) SK_OVERRIDE {
115 return this->INHERITED::onFindClickHandler(x, y, modi);
116 }
117
118 virtual bool onClick(Click* click) SK_OVERRIDE {
119 return this->INHERITED::onClick(click);
120 }
121
122private:
123 SkLua* fLua;
124
125 typedef SampleView INHERITED;
126};
127
128//////////////////////////////////////////////////////////////////////////////
129
130static SkView* MyFactory() { return new LuaView; }
131static SkViewRegister reg(MyFactory);