Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: samplecode/SampleLua.cpp

Issue 15742009: expand SkLua to handle creation of its own State (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
13 extern "C" {
14 #include "lua.h"
15 #include "lualib.h"
16 #include "lauxlib.h"
17 }
18
19 static const char gDrawName[] = "onDrawContent";
20
21 static const char gCode[] = ""
22 "local r = { left = 10, top = 10, right = 100, bottom = 80 } "
23 "local x = 0;"
24 ""
25 "local paint = Sk.newPaint();"
26 "paint:setAntiAlias(true);"
27 ""
28 "local color = {a = 1, r = 1, g = 0, b = 0};"
29 ""
30 "function onDrawContent(canvas) "
31 " color.g = x / 100;"
32 " paint:setColor(color) "
33 " canvas:translate(x, 0);"
34 " canvas:drawOval(r, paint) "
35 " x = x + 1;"
36 " if x > 100 then x = 0 end;"
37 "end";
38
39 class LuaView : public SampleView {
40 public:
41 LuaView() : fLua(NULL) {}
42
43 virtual ~LuaView() {
44 SkDELETE(fLua);
45 }
46
47 lua_State* ensureLua() {
48 if (NULL == fLua) {
49 fLua = SkNEW(SkLua);
50 fLua->runCode(gCode);
51 }
52 return fLua->get();
53 }
54
55 protected:
56 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
57 if (SampleCode::TitleQ(*evt)) {
58 SampleCode::TitleR(evt, "Lua");
59 return true;
60 }
61 SkUnichar uni;
62 if (SampleCode::CharQ(*evt, &uni)) {
63 switch (uni) {
64 default:
65 break;
66 }
67 }
68 return this->INHERITED::onQuery(evt);
69 }
70
71 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
72 lua_State* L = this->ensureLua();
73
74 lua_getglobal(L, gDrawName);
75 if (!lua_isfunction(L, -1)) {
76 int t = lua_type(L, -1);
77 SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t);
78 lua_pop(L, 1);
79 } else {
80 // does it make sense to try to "cache" the lua version of this
81 // canvas between draws?
82 fLua->pushCanvas(canvas);
83 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
84 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
85 }
86 }
87 // need a way for the lua-sample to tell us if they want animations...
88 // hard-code it ON for now.
89 this->inval(NULL);
90 }
91
92 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
93 unsigned modi) SK_OVERRIDE {
94 return this->INHERITED::onFindClickHandler(x, y, modi);
95 }
96
97 virtual bool onClick(Click* click) SK_OVERRIDE {
98 return this->INHERITED::onClick(click);
99 }
100
101 private:
102 SkLua* fLua;
103
104 typedef SampleView INHERITED;
105 };
106
107 //////////////////////////////////////////////////////////////////////////////
108
109 static SkView* MyFactory() { return new LuaView; }
110 static SkViewRegister reg(MyFactory);
OLDNEW
« no previous file with comments | « include/utils/SkLua.h ('k') | src/utils/SkLua.cpp » ('j') | src/utils/SkLua.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698