2014-10-11 18:28:07 +00:00
|
|
|
|
|
|
|
function make_paint(size, color)
|
|
|
|
local paint = Sk.newPaint();
|
|
|
|
paint:setAntiAlias(true)
|
2014-10-11 20:13:11 +00:00
|
|
|
paint:setSubpixelText(true)
|
2014-10-11 18:28:07 +00:00
|
|
|
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
|
|
|
|
|
2014-10-12 19:18:40 +00:00
|
|
|
function slide_transition(prev, next, is_forward)
|
|
|
|
local rec = {
|
|
|
|
proc = function(self, canvas, drawSlideProc)
|
|
|
|
if self:isDone() then
|
|
|
|
drawSlideProc(canvas)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
canvas:drawImage(self.prevImage, self.curr_x, 0)
|
|
|
|
canvas:drawImage(self.nextImage, self.curr_x + 640, 0)
|
|
|
|
self.curr_x = self.curr_x + self.step_x
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
}
|
|
|
|
if is_forward then
|
|
|
|
rec.prevImage = prev
|
|
|
|
rec.nextImage = next
|
|
|
|
rec.curr_x = 0
|
|
|
|
rec.step_x = -15
|
|
|
|
rec.isDone = function (self) return self.curr_x <= -640 end
|
|
|
|
else
|
|
|
|
rec.prevImage = next
|
|
|
|
rec.nextImage = prev
|
|
|
|
rec.curr_x = -640
|
|
|
|
rec.step_x = 15
|
|
|
|
rec.isDone = function (self) return self.curr_x >= 0 end
|
|
|
|
end
|
|
|
|
return rec
|
|
|
|
end
|
|
|
|
|
2014-10-11 18:28:07 +00:00
|
|
|
--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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 },
|
|
|
|
},
|
2014-10-12 19:18:40 +00:00
|
|
|
transition = slide_transition
|
2014-10-11 18:28:07 +00:00
|
|
|
},
|
|
|
|
{ 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" },
|
2014-10-12 19:18:40 +00:00
|
|
|
},
|
|
|
|
transition = fade_transition
|
2014-10-11 18:28:07 +00:00
|
|
|
},
|
|
|
|
{ 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" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 20:13:11 +00:00
|
|
|
--------------------------------------------------------------------------------------
|
2014-10-12 19:18:40 +00:00
|
|
|
function tostr(t)
|
|
|
|
local str = ""
|
|
|
|
for k, v in next, t do
|
|
|
|
if #str > 0 then
|
|
|
|
str = str .. ", "
|
|
|
|
end
|
|
|
|
if type(k) == "number" then
|
|
|
|
str = str .. "[" .. k .. "] = "
|
|
|
|
else
|
|
|
|
str = str .. tostring(k) .. " = "
|
|
|
|
end
|
|
|
|
if type(v) == "table" then
|
|
|
|
str = str .. "{ " .. tostr(v) .. " }"
|
|
|
|
else
|
|
|
|
str = str .. tostring(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
|
|
|
-- animation.proc is passed the canvas before drawing.
|
|
|
|
-- The animation.proc returns itself or another animation (which means keep animating)
|
|
|
|
-- or it returns nil, which stops the animation.
|
|
|
|
--
|
|
|
|
local gCurrAnimation
|
2014-10-11 20:13:11 +00:00
|
|
|
|
2014-10-11 18:28:07 +00:00
|
|
|
gSlideIndex = 1
|
|
|
|
|
2014-10-11 20:13:11 +00:00
|
|
|
function next_slide()
|
2014-10-12 19:18:40 +00:00
|
|
|
local prev = gSlides[gSlideIndex]
|
|
|
|
|
2014-10-11 20:13:11 +00:00
|
|
|
gSlideIndex = gSlideIndex + 1
|
|
|
|
if gSlideIndex > #gSlides then
|
|
|
|
gSlideIndex = 1
|
|
|
|
end
|
2014-10-12 19:18:40 +00:00
|
|
|
|
|
|
|
spawn_transition(prev, gSlides[gSlideIndex], true)
|
2014-10-11 20:13:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function prev_slide()
|
2014-10-12 19:18:40 +00:00
|
|
|
local prev = gSlides[gSlideIndex]
|
|
|
|
|
2014-10-11 20:13:11 +00:00
|
|
|
gSlideIndex = gSlideIndex - 1
|
|
|
|
if gSlideIndex < 1 then
|
|
|
|
gSlideIndex = #gSlides
|
|
|
|
end
|
2014-10-12 19:18:40 +00:00
|
|
|
|
|
|
|
spawn_transition(prev, gSlides[gSlideIndex], false)
|
2014-10-11 20:13:11 +00:00
|
|
|
end
|
|
|
|
|
2014-10-12 19:18:40 +00:00
|
|
|
gSurfaceFactory = function (w, h) return Sk.newRasterSurface(w, h) end
|
2014-10-11 18:28:07 +00:00
|
|
|
|
2014-10-12 19:18:40 +00:00
|
|
|
function spawn_transition(prevSlide, nextSlide, is_forward)
|
|
|
|
local transition
|
|
|
|
if is_forward then
|
|
|
|
transition = prevSlide.transition
|
|
|
|
else
|
|
|
|
transition = nextSlide.transition
|
|
|
|
end
|
|
|
|
|
|
|
|
if not transition then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local surf = gSurfaceFactory(640, 480)
|
|
|
|
local canvas = surf:getCanvas()
|
|
|
|
|
|
|
|
canvas:clear()
|
|
|
|
drawSlide(canvas, prevSlide, gTemplate, gPaints)
|
|
|
|
local prevImage = surf:newImageSnapshot()
|
|
|
|
|
|
|
|
canvas:clear()
|
|
|
|
drawSlide(canvas, nextSlide, gTemplate, gPaints)
|
|
|
|
local nextImage = surf:newImageSnapshot()
|
|
|
|
|
|
|
|
gCurrAnimation = transition(prevImage, nextImage, is_forward)
|
|
|
|
end
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------------
|
2014-10-11 20:13:11 +00:00
|
|
|
|
|
|
|
function spawn_rotate_animation()
|
|
|
|
gCurrAnimation = {
|
|
|
|
angle = 0,
|
|
|
|
angle_delta = 5,
|
|
|
|
pivot_x = 320,
|
|
|
|
pivot_y = 240,
|
2014-10-12 19:18:40 +00:00
|
|
|
proc = function (self, canvas, drawSlideProc)
|
|
|
|
if self.angle >= 360 then
|
|
|
|
drawSlideProc(canvas)
|
2014-10-11 20:13:11 +00:00
|
|
|
return nil
|
|
|
|
end
|
2014-10-12 19:18:40 +00:00
|
|
|
canvas:translate(self.pivot_x, self.pivot_y)
|
|
|
|
canvas:rotate(self.angle)
|
|
|
|
canvas:translate(-self.pivot_x, -self.pivot_y)
|
|
|
|
drawSlideProc(canvas)
|
2014-10-11 20:13:11 +00:00
|
|
|
|
2014-10-12 19:18:40 +00:00
|
|
|
self.angle = self.angle + self.angle_delta
|
|
|
|
return self
|
2014-10-11 20:13:11 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function spawn_scale_animation()
|
|
|
|
gCurrAnimation = {
|
|
|
|
scale = 1,
|
|
|
|
scale_delta = .95,
|
|
|
|
scale_limit = 0.2,
|
|
|
|
pivot_x = 320,
|
|
|
|
pivot_y = 240,
|
2014-10-12 19:18:40 +00:00
|
|
|
proc = function (self, canvas, drawSlideProc)
|
|
|
|
if self.scale < self.scale_limit then
|
|
|
|
self.scale = self.scale_limit
|
|
|
|
self.scale_delta = 1 / self.scale_delta
|
2014-10-11 20:13:11 +00:00
|
|
|
end
|
2014-10-12 19:18:40 +00:00
|
|
|
if self.scale > 1 then
|
|
|
|
drawSlideProc(canvas)
|
2014-10-11 20:13:11 +00:00
|
|
|
return nil
|
|
|
|
end
|
2014-10-12 19:18:40 +00:00
|
|
|
canvas:translate(self.pivot_x, self.pivot_y)
|
|
|
|
canvas:scale(self.scale, self.scale)
|
|
|
|
canvas:translate(-self.pivot_x, -self.pivot_y)
|
|
|
|
drawSlideProc(canvas)
|
2014-10-11 20:13:11 +00:00
|
|
|
|
2014-10-12 19:18:40 +00:00
|
|
|
self.scale = self.scale * self.scale_delta
|
|
|
|
return self
|
2014-10-11 20:13:11 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-10-11 18:28:07 +00:00
|
|
|
function onDrawContent(canvas)
|
2014-10-12 19:18:40 +00:00
|
|
|
local drawSlideProc = function(canvas)
|
|
|
|
drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints)
|
2014-10-11 20:13:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if gCurrAnimation then
|
2014-10-12 19:18:40 +00:00
|
|
|
gCurrAnimation = gCurrAnimation:proc(canvas, drawSlideProc)
|
2014-10-11 20:13:11 +00:00
|
|
|
return true
|
|
|
|
else
|
2014-10-12 19:18:40 +00:00
|
|
|
drawSlideProc(canvas)
|
2014-10-11 20:13:11 +00:00
|
|
|
return false
|
|
|
|
end
|
2014-10-11 18:28:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function onClickHandler(x, y)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-10-11 20:13:11 +00:00
|
|
|
local keyProcs = {
|
|
|
|
n = next_slide,
|
|
|
|
p = prev_slide,
|
|
|
|
r = spawn_rotate_animation,
|
|
|
|
s = spawn_scale_animation,
|
|
|
|
}
|
|
|
|
|
|
|
|
function onCharHandler(uni)
|
|
|
|
local proc = keyProcs[uni]
|
|
|
|
if proc then
|
|
|
|
proc()
|
|
|
|
return true
|
2014-10-11 18:28:07 +00:00
|
|
|
end
|
2014-10-11 20:13:11 +00:00
|
|
|
return false
|
2014-10-11 18:28:07 +00:00
|
|
|
end
|