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 "require \"math\" " 23 "" 24 "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 image = Sk.loadImage('/skia/trunk/sailboat.jpg');" 31 "" 32 "local color = {a = 1, r = 1, g = 0, b = 0};" 33 "" 34 "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);" 54 "" 55 " 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;" 72 "end " 73 "" 74 "function onDrawContent(canvas) " 75 " draw_rand_path(canvas);" 76 " color.g = x / 100;" 77 " paint:setColor(color) " 78 " canvas:translate(x, 0);" 79 " canvas:drawOval(r, paint) " 80 " x = x + 1;" 81 " canvas:drawImage(image, x, r.bottom + 50, 0.5);" 82 " if x > 100 then x = 0 end;" 83 "end " 84 "" 85 "onStartup();"; 86 87 class LuaView : public SampleView { 88 public: 89 LuaView() : fLua(NULL) {} 90 91 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 103 protected: 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)) { 111 } 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 145 private: 146 SkLua* fLua; 147 148 typedef SampleView INHERITED; 149 }; 150 151 ////////////////////////////////////////////////////////////////////////////// 152 153 static SkView* MyFactory() { return new LuaView; } 154 static SkViewRegister reg(MyFactory); 155