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