aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--resources/test.lua74
-rw-r--r--samplecode/SampleLua.cpp102
2 files changed, 107 insertions, 69 deletions
diff --git a/resources/test.lua b/resources/test.lua
new file mode 100644
index 0000000000..84d3517ae6
--- /dev/null
+++ b/resources/test.lua
@@ -0,0 +1,74 @@
+
+local r = { left = 10, top = 10, right = 100, bottom = 80 }
+local x = 0;
+
+local paint = Sk.newPaint();
+paint:setAntiAlias(true);
+
+local image -- = Sk.loadImage('/skia/sailboat.jpg');
+function setImageFilename(filename)
+ image = Sk.loadImage(filename)
+end
+
+
+local color = {a = 1, r = 1, g = 0, b = 0};
+
+function rnd(range)
+ return math.random() * range;
+end
+
+rndX = function () return rnd(640) end
+rndY = function () return rnd(480) end
+
+function draw_rand_path(canvas);
+ if not path_paint then
+ path_paint = Sk.newPaint();
+ path_paint:setAntiAlias(true);
+ end
+ path_paint:setColor({a = 1, r = math.random(), g = math.random(), b = math.random() });
+
+ local path = Sk.newPath();
+ path:moveTo(rndX(), rndY());
+ for i = 0, 50 do
+ path:quadTo(rndX(), rndY(), rndX(), rndY());
+ end
+ canvas:drawPath(path, path_paint);
+
+ paint:setColor{a=1,r=0,g=0,b=1};
+ local align = { 'left', 'center', 'right' };
+ paint:setTextSize(30);
+ for k, v in next, align do
+ paint:setTextAlign(v);
+ canvas:drawText('Hamburgefons', 320, 200 + 30*k, paint);
+ end
+end
+
+function onStartup()
+ local paint = Sk.newPaint();
+ paint:setColor{a=1, r=1, g=0, b=0};
+ if false then
+ local doc = Sk.newDocumentPDF('/skia/trunk/test.pdf');
+ local canvas = doc:beginPage(72*8.5, 72*11);
+ canvas:drawText('Hello Lua', 300, 300, paint);
+ doc:close();
+ doc = nil;
+ end
+end
+
+function onDrawContent(canvas)
+ draw_rand_path(canvas);
+ color.g = x / 100;
+ paint:setColor(color)
+ canvas:translate(x, 0);
+ canvas:drawOval(r, paint)
+ x = x + 1;
+ local r2 = {}
+ r2.left = x;
+ r2.top = r.bottom + 50;
+ r2.right = r2.left + image:width() * 1;
+ r2.bottom = r2.top + image:height() * 1;
+ canvas:drawImageRect(image, nil, r2, 0.75);
+ if x > 200 then x = 0 end;
+end
+
+onStartup()
diff --git a/samplecode/SampleLua.cpp b/samplecode/SampleLua.cpp
index b4460c8550..8e0eaf703d 100644
--- a/samplecode/SampleLua.cpp
+++ b/samplecode/SampleLua.cpp
@@ -9,6 +9,8 @@
#include "SkView.h"
#include "SkLua.h"
#include "SkCanvas.h"
+#include "Resources.h"
+#include "SkData.h"
extern "C" {
#include "lua.h"
@@ -18,74 +20,15 @@ extern "C" {
static const char gDrawName[] = "onDrawContent";
-static const char gCode[] = ""
- "local r = { left = 10, top = 10, right = 100, bottom = 80 } \n"
- "local x = 0;\n"
- "\n"
- "local paint = Sk.newPaint();\n"
- "paint:setAntiAlias(true);\n"
- "\n"
- "local image = Sk.loadImage('/skia/sailboat.jpg');\n"
- "\n"
- "local color = {a = 1, r = 1, g = 0, b = 0};\n"
- "\n"
- "function rnd(range) \n"
- " return math.random() * range;\n"
- "end \n"
- "\n"
- "rndX = function () return rnd(640) end \n"
- "rndY = function () return rnd(480) end \n"
- "\n"
- "function draw_rand_path(canvas);\n"
- " if not path_paint then \n"
- " path_paint = Sk.newPaint();\n"
- " path_paint:setAntiAlias(true);\n"
- " end \n"
- " path_paint:setColor({a = 1, r = math.random(), g = math.random(), b = math.random() });\n"
- "\n"
- " local path = Sk.newPath();\n"
- " path:moveTo(rndX(), rndY());\n"
- " for i = 0, 50 do \n"
- " path:quadTo(rndX(), rndY(), rndX(), rndY());\n"
- " end \n"
- " canvas:drawPath(path, path_paint);\n"
- "\n"
- " paint:setColor{a=1,r=0,g=0,b=1};\n"
- " local align = { 'left', 'center', 'right' };\n"
- " paint:setTextSize(30);\n"
- " for k, v in next, align do \n"
- " paint:setTextAlign(v);\n"
- " canvas:drawText('Hamburgefons', 320, 200 + 30*k, paint);\n"
- " end \n"
- "end \n"
- "\n"
- "function onStartup() \n"
- " local paint = Sk.newPaint();\n"
- " paint:setColor{a=1, r=1, g=0, b=0};\n"
- " local doc = Sk.newDocumentPDF('/skia/trunk/test.pdf');\n"
- " local canvas = doc:beginPage(72*8.5, 72*11);\n"
- " canvas:drawText('Hello Lua', 300, 300, paint);\n"
- " doc:close();\n"
- " doc = nil;\n"
- "end \n"
- "\n"
- "function onDrawContent(canvas) \n"
- " draw_rand_path(canvas);\n"
- " color.g = x / 100;\n"
- " paint:setColor(color) \n"
- " canvas:translate(x, 0);\n"
- " canvas:drawOval(r, paint) \n"
- " x = x + 1;\n"
- " local r2 = {}\n"
- " r2.left = x;\n"
- " r2.top = r.bottom + 50;\n"
- " r2.right = r2.left + image:width() * 0.1;\n"
- " r2.bottom = r2.top + image:height() * 0.1;\n"
- " canvas:drawImageRect(image, nil, r2, 0.75);\n"
- " if x > 100 then x = 0 end;\n"
- "end \n"
- "\n"
- "onStartup();\n";
+static const char gMissingCode[] = ""
+ "local paint = Sk.newPaint()"
+ "paint:setAntiAlias(true)"
+ "paint:setTextSize(30)"
+ ""
+ "function onDrawContent(canvas)"
+ " canvas:drawText('missing \"test.lua\"', 20, 50, paint)"
+ "end"
+ ;
class LuaView : public SampleView {
public:
@@ -95,10 +38,31 @@ public:
SkDELETE(fLua);
}
+ void setImageFilename(lua_State* L) {
+ SkString str = GetResourcePath("mandrill_256.png");
+
+ lua_getglobal(L, "setImageFilename");
+ if (lua_isfunction(L, -1)) {
+ fLua->pushString(str.c_str());
+ if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
+ SkDebugf("lua err: %s\n", lua_tostring(L, -1));
+ }
+ }
+ }
+
lua_State* ensureLua() {
if (NULL == fLua) {
fLua = SkNEW(SkLua);
- fLua->runCode(gCode);
+
+ SkString str = GetResourcePath("test.lua");
+ SkData* data = SkData::NewFromFileName(str.c_str());
+ if (data) {
+ fLua->runCode(data->data(), data->size());
+ data->unref();
+ this->setImageFilename(fLua->get());
+ } else {
+ fLua->runCode(gMissingCode);
+ }
}
return fLua->get();
}