| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
| 9 #include "SkView.h" | 9 #include "SkView.h" |
| 10 #include "SkLua.h" | 10 #include "SkLua.h" |
| 11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
| 12 #include "Resources.h" |
| 13 #include "SkData.h" |
| 12 | 14 |
| 13 extern "C" { | 15 extern "C" { |
| 14 #include "lua.h" | 16 #include "lua.h" |
| 15 #include "lualib.h" | 17 #include "lualib.h" |
| 16 #include "lauxlib.h" | 18 #include "lauxlib.h" |
| 17 } | 19 } |
| 18 | 20 |
| 19 static const char gDrawName[] = "onDrawContent"; | 21 static const char gDrawName[] = "onDrawContent"; |
| 20 | 22 |
| 21 static const char gCode[] = "" | 23 static const char gMissingCode[] = "" |
| 22 "local r = { left = 10, top = 10, right = 100, bottom = 80 } \n" | 24 "local paint = Sk.newPaint()" |
| 23 "local x = 0;\n" | 25 "paint:setAntiAlias(true)" |
| 24 "\n" | 26 "paint:setTextSize(30)" |
| 25 "local paint = Sk.newPaint();\n" | 27 "" |
| 26 "paint:setAntiAlias(true);\n" | 28 "function onDrawContent(canvas)" |
| 27 "\n" | 29 " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" |
| 28 "local image = Sk.loadImage('/skia/sailboat.jpg');\n" | 30 "end" |
| 29 "\n" | 31 ; |
| 30 "local color = {a = 1, r = 1, g = 0, b = 0};\n" | |
| 31 "\n" | |
| 32 "function rnd(range) \n" | |
| 33 " return math.random() * range;\n" | |
| 34 "end \n" | |
| 35 "\n" | |
| 36 "rndX = function () return rnd(640) end \n" | |
| 37 "rndY = function () return rnd(480) end \n" | |
| 38 "\n" | |
| 39 "function draw_rand_path(canvas);\n" | |
| 40 " if not path_paint then \n" | |
| 41 " path_paint = Sk.newPaint();\n" | |
| 42 " path_paint:setAntiAlias(true);\n" | |
| 43 " end \n" | |
| 44 " path_paint:setColor({a = 1, r = math.random(), g = math.random(), b = ma
th.random() });\n" | |
| 45 "\n" | |
| 46 " local path = Sk.newPath();\n" | |
| 47 " path:moveTo(rndX(), rndY());\n" | |
| 48 " for i = 0, 50 do \n" | |
| 49 " path:quadTo(rndX(), rndY(), rndX(), rndY());\n" | |
| 50 " end \n" | |
| 51 " canvas:drawPath(path, path_paint);\n" | |
| 52 "\n" | |
| 53 " paint:setColor{a=1,r=0,g=0,b=1};\n" | |
| 54 " local align = { 'left', 'center', 'right' };\n" | |
| 55 " paint:setTextSize(30);\n" | |
| 56 " for k, v in next, align do \n" | |
| 57 " paint:setTextAlign(v);\n" | |
| 58 " canvas:drawText('Hamburgefons', 320, 200 + 30*k, paint);\n" | |
| 59 " end \n" | |
| 60 "end \n" | |
| 61 "\n" | |
| 62 "function onStartup() \n" | |
| 63 " local paint = Sk.newPaint();\n" | |
| 64 " paint:setColor{a=1, r=1, g=0, b=0};\n" | |
| 65 " local doc = Sk.newDocumentPDF('/skia/trunk/test.pdf');\n" | |
| 66 " local canvas = doc:beginPage(72*8.5, 72*11);\n" | |
| 67 " canvas:drawText('Hello Lua', 300, 300, paint);\n" | |
| 68 " doc:close();\n" | |
| 69 " doc = nil;\n" | |
| 70 "end \n" | |
| 71 "\n" | |
| 72 "function onDrawContent(canvas) \n" | |
| 73 " draw_rand_path(canvas);\n" | |
| 74 " color.g = x / 100;\n" | |
| 75 " paint:setColor(color) \n" | |
| 76 " canvas:translate(x, 0);\n" | |
| 77 " canvas:drawOval(r, paint) \n" | |
| 78 " x = x + 1;\n" | |
| 79 " local r2 = {}\n" | |
| 80 " r2.left = x;\n" | |
| 81 " r2.top = r.bottom + 50;\n" | |
| 82 " r2.right = r2.left + image:width() * 0.1;\n" | |
| 83 " r2.bottom = r2.top + image:height() * 0.1;\n" | |
| 84 " canvas:drawImageRect(image, nil, r2, 0.75);\n" | |
| 85 " if x > 100 then x = 0 end;\n" | |
| 86 "end \n" | |
| 87 "\n" | |
| 88 "onStartup();\n"; | |
| 89 | 32 |
| 90 class LuaView : public SampleView { | 33 class LuaView : public SampleView { |
| 91 public: | 34 public: |
| 92 LuaView() : fLua(NULL) {} | 35 LuaView() : fLua(NULL) {} |
| 93 | 36 |
| 94 virtual ~LuaView() { | 37 virtual ~LuaView() { |
| 95 SkDELETE(fLua); | 38 SkDELETE(fLua); |
| 96 } | 39 } |
| 97 | 40 |
| 41 void setImageFilename(lua_State* L) { |
| 42 SkString str = GetResourcePath("mandrill_256.png"); |
| 43 |
| 44 lua_getglobal(L, "setImageFilename"); |
| 45 if (lua_isfunction(L, -1)) { |
| 46 fLua->pushString(str.c_str()); |
| 47 if (lua_pcall(L, 1, 0, 0) != LUA_OK) { |
| 48 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 49 } |
| 50 } |
| 51 } |
| 52 |
| 98 lua_State* ensureLua() { | 53 lua_State* ensureLua() { |
| 99 if (NULL == fLua) { | 54 if (NULL == fLua) { |
| 100 fLua = SkNEW(SkLua); | 55 fLua = SkNEW(SkLua); |
| 101 fLua->runCode(gCode); | 56 |
| 57 SkString str = GetResourcePath("test.lua"); |
| 58 SkData* data = SkData::NewFromFileName(str.c_str()); |
| 59 if (data) { |
| 60 fLua->runCode(data->data(), data->size()); |
| 61 data->unref(); |
| 62 this->setImageFilename(fLua->get()); |
| 63 } else { |
| 64 fLua->runCode(gMissingCode); |
| 65 } |
| 102 } | 66 } |
| 103 return fLua->get(); | 67 return fLua->get(); |
| 104 } | 68 } |
| 105 | 69 |
| 106 protected: | 70 protected: |
| 107 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { | 71 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { |
| 108 if (SampleCode::TitleQ(*evt)) { | 72 if (SampleCode::TitleQ(*evt)) { |
| 109 SampleCode::TitleR(evt, "Lua"); | 73 SampleCode::TitleR(evt, "Lua"); |
| 110 return true; | 74 return true; |
| 111 } | 75 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 private: | 112 private: |
| 149 SkLua* fLua; | 113 SkLua* fLua; |
| 150 | 114 |
| 151 typedef SampleView INHERITED; | 115 typedef SampleView INHERITED; |
| 152 }; | 116 }; |
| 153 | 117 |
| 154 ////////////////////////////////////////////////////////////////////////////// | 118 ////////////////////////////////////////////////////////////////////////////// |
| 155 | 119 |
| 156 static SkView* MyFactory() { return new LuaView; } | 120 static SkView* MyFactory() { return new LuaView; } |
| 157 static SkViewRegister reg(MyFactory); | 121 static SkViewRegister reg(MyFactory); |
| OLD | NEW |