reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 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[] = "" |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 22 | "require \"math\" " |
| 23 | "" |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 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 color = {a = 1, r = 1, g = 0, b = 0};" |
| 31 | "" |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 32 | "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.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 52 | "" |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame^] | 53 | " paint:setColor{a=1,r=0,g=0,b=1};" |
| 54 | " local align = { 'left', 'center', 'right' };" |
| 55 | " paint:setTextSize(30);" |
| 56 | " for k, v in next, align do " |
| 57 | " paint:setTextAlign(v);" |
| 58 | " canvas:drawText('Hamburgefons', 320, 200 + 30*k, paint);" |
| 59 | " end " |
| 60 | "end " |
| 61 | "" |
| 62 | "function onStartup() " |
| 63 | " local paint = Sk.newPaint();" |
| 64 | " paint:setColor{a=1, r=1, g=0, b=0};" |
| 65 | " local doc = Sk.newDocumentPDF('/skia/trunk/test.pdf');" |
| 66 | " local canvas = doc:beginPage(72*8.5, 72*11);" |
| 67 | " canvas:drawText('Hello Lua', 300, 300, paint);" |
| 68 | " doc:close();" |
| 69 | " doc = nil;" |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 70 | "end " |
| 71 | "" |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 72 | "function onDrawContent(canvas) " |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 73 | " draw_rand_path(canvas);" |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 74 | " color.g = x / 100;" |
| 75 | " paint:setColor(color) " |
| 76 | " canvas:translate(x, 0);" |
| 77 | " canvas:drawOval(r, paint) " |
| 78 | " x = x + 1;" |
| 79 | " if x > 100 then x = 0 end;" |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame^] | 80 | "end " |
| 81 | "" |
| 82 | "onStartup();"; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 83 | |
| 84 | class LuaView : public SampleView { |
| 85 | public: |
| 86 | LuaView() : fLua(NULL) {} |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 87 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 88 | virtual ~LuaView() { |
| 89 | SkDELETE(fLua); |
| 90 | } |
| 91 | |
| 92 | lua_State* ensureLua() { |
| 93 | if (NULL == fLua) { |
| 94 | fLua = SkNEW(SkLua); |
| 95 | fLua->runCode(gCode); |
| 96 | } |
| 97 | return fLua->get(); |
| 98 | } |
| 99 | |
| 100 | protected: |
| 101 | virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { |
| 102 | if (SampleCode::TitleQ(*evt)) { |
| 103 | SampleCode::TitleR(evt, "Lua"); |
| 104 | return true; |
| 105 | } |
| 106 | SkUnichar uni; |
| 107 | if (SampleCode::CharQ(*evt, &uni)) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 108 | } |
| 109 | return this->INHERITED::onQuery(evt); |
| 110 | } |
| 111 | |
| 112 | virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { |
| 113 | lua_State* L = this->ensureLua(); |
| 114 | |
| 115 | lua_getglobal(L, gDrawName); |
| 116 | if (!lua_isfunction(L, -1)) { |
| 117 | int t = lua_type(L, -1); |
| 118 | SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t); |
| 119 | lua_pop(L, 1); |
| 120 | } else { |
| 121 | // does it make sense to try to "cache" the lua version of this |
| 122 | // canvas between draws? |
| 123 | fLua->pushCanvas(canvas); |
| 124 | if (lua_pcall(L, 1, 0, 0) != LUA_OK) { |
| 125 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 126 | } |
| 127 | } |
| 128 | // need a way for the lua-sample to tell us if they want animations... |
| 129 | // hard-code it ON for now. |
| 130 | this->inval(NULL); |
| 131 | } |
| 132 | |
| 133 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
| 134 | unsigned modi) SK_OVERRIDE { |
| 135 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 136 | } |
| 137 | |
| 138 | virtual bool onClick(Click* click) SK_OVERRIDE { |
| 139 | return this->INHERITED::onClick(click); |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | SkLua* fLua; |
| 144 | |
| 145 | typedef SampleView INHERITED; |
| 146 | }; |
| 147 | |
| 148 | ////////////////////////////////////////////////////////////////////////////// |
| 149 | |
| 150 | static SkView* MyFactory() { return new LuaView; } |
| 151 | static SkViewRegister reg(MyFactory); |