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 | 09efb17 | 2013-06-08 13:50:37 +0000 | [diff] [blame^] | 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.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 57 | "end " |
| 58 | "" |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 59 | "function onDrawContent(canvas) " |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 60 | " draw_rand_path(canvas);" |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 61 | " 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;" |
mike@reedtribe.org | 09efb17 | 2013-06-08 13:50:37 +0000 | [diff] [blame^] | 67 | "end"; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 68 | |
| 69 | class LuaView : public SampleView { |
| 70 | public: |
| 71 | LuaView() : fLua(NULL) {} |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 72 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 73 | 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 | |
| 85 | protected: |
| 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.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 93 | } |
| 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 | |
| 127 | private: |
| 128 | SkLua* fLua; |
| 129 | |
| 130 | typedef SampleView INHERITED; |
| 131 | }; |
| 132 | |
| 133 | ////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | static SkView* MyFactory() { return new LuaView; } |
| 136 | static SkViewRegister reg(MyFactory); |