diff options
-rw-r--r-- | resources/slides.lua | 108 | ||||
-rw-r--r-- | resources/test.lua | 2 | ||||
-rw-r--r-- | samplecode/SampleLua.cpp | 28 |
3 files changed, 133 insertions, 5 deletions
diff --git a/resources/slides.lua b/resources/slides.lua new file mode 100644 index 0000000000..2f44e78e1b --- /dev/null +++ b/resources/slides.lua @@ -0,0 +1,108 @@ + +function make_paint(size, color) + local paint = Sk.newPaint(); + paint:setAntiAlias(true) + paint:setTextSize(size) + paint:setColor(color) + return paint +end + +function find_paint(paints, style) + if not style then + style = "child" + end + local paint = paints[style] + return paint +end + +function draw_node(canvas, node, x, y, paints) + if node.text then + local paint = find_paint(paints, node.style) + canvas:drawText(node.text, x, y, paint) + end + if node.draw then + node.draw(canvas) + end +end + +function drawSlide(canvas, slide, template, paints) + draw_node(canvas, slide, template.title.x, template.title.y, paints) + + if slide.children then + local x = template.child.x + local y = template.child.y + local dy = template.child.dy + for i = 1, #slide.children do + draw_node(canvas, slide.children[i], x, y, paints) + y = y + dy + end + end +end + +-------------------------------------------------------------------------------------- + +gTemplate = { + title = { x = 10, y = 64, textSize = 64 }, + child = { x = 40, y = 120, dy = 50, textSize = 40 }, +} + +gPaints = {} +gPaints.title = make_paint(gTemplate.title.textSize, { a=1, r=0, g=0, b=0 } ) +gPaints.child = make_paint(gTemplate.child.textSize, { a=.75, r=0, g=0, b=0 } ) + +gRedPaint = Sk.newPaint() +gRedPaint:setAntiAlias(true) +gRedPaint:setColor{a=1, r=1, g=0, b=0 } + +gSlides = { + { text = "Title1", style="title", color = { a=1, r=1, g=0, b=0 }, + children = { + { text = "bullet 1", style = "child" }, + { text = "bullet 2", style = "child" }, + { text = "bullet 3", style = "child" }, + { draw = function (canvas) + canvas:drawOval({left=300, top=300, right=400, bottom=400}, gRedPaint) + end }, + }, + }, + { text = "Title2", style="title", color = { a=1, r=0, g=1, b=0 }, + children = { + { text = "bullet uno", style = "child" }, + { text = "bullet 2", style = "child" }, + { text = "bullet tres", style = "child" }, + } + }, + { text = "Title3", style="title", + children = { + { text = "bullet 1", style = "child", }, + { text = "bullet 2", style = "child", color = { r=0, g=0, b=1 } }, + { text = "bullet 3", style = "child" }, + } + } +} + +gSlideIndex = 1 + +-------------------------------------------------------------------------------------- + +function onDrawContent(canvas) + drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints) + + return false -- we're not animating +end + +function onClickHandler(x, y) + if x < 100 and y < 100 then + onNextSlide() + return true + end + return false +end + +function onNextSlide() + gSlideIndex = gSlideIndex + 1 + if gSlideIndex > #gSlides then + gSlideIndex = 1 + end +end + diff --git a/resources/test.lua b/resources/test.lua index 84d3517ae6..1029db41ed 100644 --- a/resources/test.lua +++ b/resources/test.lua @@ -69,6 +69,8 @@ function onDrawContent(canvas) r2.bottom = r2.top + image:height() * 1; canvas:drawImageRect(image, nil, r2, 0.75); if x > 200 then x = 0 end; + + return true -- so we can animate end onStartup() diff --git a/samplecode/SampleLua.cpp b/samplecode/SampleLua.cpp index 8e0eaf703d..917930a678 100644 --- a/samplecode/SampleLua.cpp +++ b/samplecode/SampleLua.cpp @@ -18,7 +18,11 @@ extern "C" { #include "lauxlib.h" } +#define LUA_FILENAME "test.lua" +//#define LUA_FILENAME "slides.lua" + static const char gDrawName[] = "onDrawContent"; +static const char gClickName[] = "onClickHandler"; static const char gMissingCode[] = "" "local paint = Sk.newPaint()" @@ -54,7 +58,7 @@ public: if (NULL == fLua) { fLua = SkNEW(SkLua); - SkString str = GetResourcePath("test.lua"); + SkString str = GetResourcePath(LUA_FILENAME); SkData* data = SkData::NewFromFileName(str.c_str()); if (data) { fLua->runCode(data->data(), data->size()); @@ -91,17 +95,31 @@ protected: // does it make sense to try to "cache" the lua version of this // canvas between draws? fLua->pushCanvas(canvas); - if (lua_pcall(L, 1, 0, 0) != LUA_OK) { + if (lua_pcall(L, 1, 1, 0) != LUA_OK) { SkDebugf("lua err: %s\n", lua_tostring(L, -1)); + } else { + if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { + this->inval(NULL); + } } } - // need a way for the lua-sample to tell us if they want animations... - // hard-code it ON for now. - this->inval(NULL); } virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE { + lua_State* L = this->ensureLua(); + lua_getglobal(L, gClickName); + if (lua_isfunction(L, -1)) { + fLua->pushScalar(x); + fLua->pushScalar(y); + if (lua_pcall(L, 2, 1, 0) != LUA_OK) { + SkDebugf("lua err: %s\n", lua_tostring(L, -1)); + } else { + if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { + this->inval(NULL); + } + } + } return this->INHERITED::onFindClickHandler(x, y, modi); } |