OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 #include "SkPath.h" | |
robertphillips
2013/05/22 19:25:06
Why include SkRegion.h?
reed1
2013/05/22 19:30:59
Done.
| |
13 #include "SkRegion.h" | |
14 #include "SkShader.h" | |
15 #include "SkUtils.h" | |
16 #include "SkImage.h" | |
17 #include "SkSurface.h" | |
18 | |
19 extern "C" { | |
20 #include "lua.h" | |
21 #include "lualib.h" | |
22 #include "lauxlib.h" | |
23 } | |
24 | |
25 static const char gDrawName[] = "onDrawContent"; | |
26 | |
27 static const char gCode[] = "" | |
28 "local r = { left = 10, top = 10, right = 100, bottom = 80 } " | |
29 "local x = 0;" | |
30 "" | |
31 "local paint = Sk.newPaint();" | |
32 "paint:setAntiAlias(true);" | |
33 "" | |
34 "local color = {a = 1, r = 1, g = 0, b = 0};" | |
35 "" | |
36 "function onDrawContent(canvas) " | |
37 " color.g = x / 100;" | |
38 " paint:setColor(color) " | |
39 " canvas:translate(x, 0);" | |
40 " canvas:drawOval(r, paint) " | |
41 " x = x + 1;" | |
42 " if x > 100 then x = 0 end;" | |
43 "end"; | |
44 | |
45 class LuaView : public SampleView { | |
46 public: | |
47 LuaView() { | |
robertphillips
2013/05/22 19:25:06
Defer this?
reed1
2013/05/22 19:30:59
Marked as a todo for now
| |
48 fLua.runCode(gCode); | |
49 } | |
50 | |
51 protected: | |
52 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { | |
53 if (SampleCode::TitleQ(*evt)) { | |
54 SampleCode::TitleR(evt, "Lua"); | |
55 return true; | |
56 } | |
57 SkUnichar uni; | |
58 if (SampleCode::CharQ(*evt, &uni)) { | |
59 switch (uni) { | |
60 case 'c': | |
61 this->inval(NULL); | |
62 return true; | |
63 default: | |
64 break; | |
65 } | |
66 } | |
67 return this->INHERITED::onQuery(evt); | |
68 } | |
69 | |
robertphillips
2013/05/22 19:25:06
override?
reed1
2013/05/22 19:30:59
Done.
| |
70 virtual void onDrawContent(SkCanvas* canvas) { | |
71 SkPaint paint; | |
72 paint.setAntiAlias(true); | |
73 | |
74 lua_State* L = fLua.get(); | |
75 | |
76 lua_getglobal(L, gDrawName); | |
77 if (!lua_isfunction(L, -1)) { | |
78 int t = lua_type(L, -1); | |
79 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t); | |
80 lua_pop(L, 1); | |
81 } else { | |
82 // does it make sense to try to "cache" the lua version of this | |
83 // canvas between draws? | |
84 fLua.pushCanvas(canvas); | |
85 // hack: when we can create new Paints in lua, we won't need to | |
86 // push one up. | |
robertphillips
2013/05/22 19:25:06
remove
reed1
2013/05/22 19:30:59
Done.
| |
87 fLua.pushPaint(paint); | |
88 if (lua_pcall(L, 2, 0, 0) != LUA_OK) { | |
89 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); | |
90 } | |
91 } | |
92 // need a way for the lua-sample to tell us if they want animations... | |
93 // hard-code it ON for now. | |
94 this->inval(NULL); | |
95 } | |
96 | |
97 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, | |
98 unsigned modi) SK_OVERRIDE { | |
99 return this->INHERITED::onFindClickHandler(x, y, modi); | |
100 } | |
101 | |
102 virtual bool onClick(Click* click) SK_OVERRIDE { | |
103 return this->INHERITED::onClick(click); | |
104 } | |
105 | |
106 private: | |
107 SkLua fLua; | |
108 | |
109 typedef SampleView INHERITED; | |
110 }; | |
111 | |
112 ////////////////////////////////////////////////////////////////////////////// | |
113 | |
114 static SkView* MyFactory() { return new LuaView; } | |
115 static SkViewRegister reg(MyFactory); | |
OLD | NEW |