2014-11-04 03:55:41 +00:00
|
|
|
gShowBounds = false
|
2014-11-04 21:24:47 +00:00
|
|
|
gUseBlurInTransitions = false
|
2014-10-11 18:28:07 +00:00
|
|
|
|
2017-08-07 13:56:21 +00:00
|
|
|
gPath = "resources/"
|
2014-10-14 02:43:17 +00:00
|
|
|
|
2014-10-26 03:44:40 +00:00
|
|
|
function load_file(file)
|
2014-10-30 03:36:05 +00:00
|
|
|
local prev_path = package.path
|
2014-10-26 03:44:40 +00:00
|
|
|
package.path = package.path .. ";" .. gPath .. file .. ".lua"
|
|
|
|
require(file)
|
2014-10-30 03:36:05 +00:00
|
|
|
package.path = prev_path
|
2014-10-14 02:43:17 +00:00
|
|
|
end
|
|
|
|
|
2014-10-26 03:44:40 +00:00
|
|
|
load_file("slides_utils")
|
2014-10-14 02:43:17 +00:00
|
|
|
|
2017-08-07 13:56:21 +00:00
|
|
|
gSlides = parse_file(io.open("resources/slides_content2.lua", "r"))
|
2014-10-14 02:43:17 +00:00
|
|
|
|
2014-10-14 16:34:52 +00:00
|
|
|
function make_rect(l, t, r, b)
|
|
|
|
return { left = l, top = t, right = r, bottom = b }
|
|
|
|
end
|
|
|
|
|
2017-08-07 13:56:21 +00:00
|
|
|
function make_paint(typefacename, style, size, color)
|
2014-10-14 02:43:17 +00:00
|
|
|
local paint = Sk.newPaint();
|
|
|
|
paint:setAntiAlias(true)
|
|
|
|
paint:setSubpixelText(true)
|
2017-08-07 13:56:21 +00:00
|
|
|
paint:setTypeface(Sk.newTypeface(typefacename, style))
|
2014-10-14 02:43:17 +00:00
|
|
|
paint:setTextSize(size)
|
|
|
|
paint:setColor(color)
|
|
|
|
return paint
|
2014-10-11 18:28:07 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 20:18:24 +00:00
|
|
|
function draw_bullet(canvas, x, y, paint, indent)
|
|
|
|
if 0 == indent then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local ps = paint:getTextSize()
|
|
|
|
local cx = x - ps * .8
|
|
|
|
local cy = y - ps * .4
|
|
|
|
local radius = ps * .2
|
|
|
|
canvas:drawCircle(cx, cy, radius, paint)
|
|
|
|
end
|
|
|
|
|
2014-11-04 03:55:41 +00:00
|
|
|
function stroke_rect(canvas, rect, color)
|
|
|
|
local paint = Sk.newPaint()
|
|
|
|
paint:setStroke(true);
|
|
|
|
paint:setColor(color)
|
|
|
|
canvas:drawRect(rect, paint)
|
|
|
|
end
|
|
|
|
|
2014-11-03 03:19:34 +00:00
|
|
|
function drawSlide(canvas, slide, master_template)
|
2014-11-04 06:32:07 +00:00
|
|
|
|
|
|
|
if #slide == 1 then
|
|
|
|
template = master_template.title
|
|
|
|
canvas:drawText(slide[1].text, 320, 240, template[1])
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
template = master_template.slide
|
2014-10-14 16:34:52 +00:00
|
|
|
|
|
|
|
local x = template.margin_x
|
|
|
|
local y = template.margin_y
|
2014-11-03 20:18:24 +00:00
|
|
|
local scale = 1.25
|
2014-10-14 16:34:52 +00:00
|
|
|
|
2014-11-03 03:19:34 +00:00
|
|
|
if slide.blockstyle == "code" then
|
|
|
|
local paint = master_template.codePaint
|
|
|
|
local fm = paint:getFontMetrics()
|
|
|
|
local height = #slide * (fm.descent - fm.ascent)
|
|
|
|
y = (480 - height) / 2
|
|
|
|
for i = 1, #slide do
|
|
|
|
local node = slide[i]
|
2014-11-03 20:18:24 +00:00
|
|
|
y = y - fm.ascent * scale
|
2014-11-03 03:19:34 +00:00
|
|
|
canvas:drawText(node.text, x, y, paint)
|
2014-11-03 20:18:24 +00:00
|
|
|
y = y + fm.descent * scale
|
2014-11-03 03:19:34 +00:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-10-14 02:43:17 +00:00
|
|
|
for i = 1, #slide do
|
|
|
|
local node = slide[i]
|
2014-10-21 14:12:58 +00:00
|
|
|
local paint = template[node.indent + 1].paint
|
|
|
|
local extra_dy = template[node.indent + 1].extra_dy
|
2014-10-14 02:43:17 +00:00
|
|
|
local fm = paint:getFontMetrics()
|
2014-11-03 20:18:24 +00:00
|
|
|
local x_offset = -fm.ascent * node.indent * 1.25
|
2014-10-14 16:34:52 +00:00
|
|
|
|
2014-11-04 03:55:41 +00:00
|
|
|
local bounds = make_rect(x + x_offset, y, 620, 640)
|
|
|
|
local blob, newBottom = Sk.newTextBlob(node.text, bounds, paint)
|
|
|
|
draw_bullet(canvas, x + x_offset, y - fm.ascent, paint, node.indent)
|
|
|
|
canvas:drawTextBlob(blob, 0, 0, paint)
|
2014-11-04 18:58:42 +00:00
|
|
|
y = newBottom + paint:getTextSize() * .5 + extra_dy
|
2014-11-04 03:55:41 +00:00
|
|
|
|
|
|
|
if gShowBounds then
|
|
|
|
bounds.bottom = newBottom
|
|
|
|
stroke_rect(canvas, bounds, {a=1,r=0,g=1,b=0})
|
|
|
|
stroke_rect(canvas, blob:bounds(), {a=1,r=1,g=0,b=0})
|
|
|
|
end
|
|
|
|
|
2014-10-21 14:12:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-11 18:28:07 +00:00
|
|
|
--------------------------------------------------------------------------------------
|
2014-10-21 14:12:58 +00:00
|
|
|
function make_tmpl(paint, extra_dy)
|
|
|
|
return { paint = paint, extra_dy = extra_dy }
|
|
|
|
end
|
2014-10-11 18:28:07 +00:00
|
|
|
|
2014-10-14 16:34:52 +00:00
|
|
|
function SkiaPoint_make_template()
|
2017-08-07 13:56:21 +00:00
|
|
|
normal = Sk.newFontStyle()
|
|
|
|
bold = Sk.newFontStyle(700)
|
2014-10-14 16:34:52 +00:00
|
|
|
local title = {
|
|
|
|
margin_x = 30,
|
|
|
|
margin_y = 100,
|
|
|
|
}
|
2017-08-07 13:56:21 +00:00
|
|
|
title[1] = make_paint("Arial", bold, 45, { a=1, r=1, g=1, b=1 })
|
2014-10-14 16:34:52 +00:00
|
|
|
title[1]:setTextAlign("center")
|
2017-08-07 13:56:21 +00:00
|
|
|
title[2] = make_paint("Arial", bold, 25, { a=1, r=.75, g=.75, b=.75 })
|
2014-10-14 16:34:52 +00:00
|
|
|
title[2]:setTextAlign("center")
|
|
|
|
|
|
|
|
local slide = {
|
|
|
|
margin_x = 20,
|
2014-10-21 14:12:58 +00:00
|
|
|
margin_y = 25,
|
2014-10-14 16:34:52 +00:00
|
|
|
}
|
2017-08-07 13:56:21 +00:00
|
|
|
slide[1] = make_tmpl(make_paint("Arial", bold, 35, { a=1, r=1, g=1, b=1 }), 18)
|
|
|
|
slide[2] = make_tmpl(make_paint("Arial", normal, 25, { a=1, r=1, g=1, b=1 }), 10)
|
|
|
|
slide[3] = make_tmpl(make_paint("Arial", normal, 20, { a=1, r=.9, g=.9, b=.9 }), 5)
|
2014-10-11 18:28:07 +00:00
|
|
|
|
2014-10-14 16:34:52 +00:00
|
|
|
return {
|
|
|
|
title = title,
|
|
|
|
slide = slide,
|
2017-08-07 13:56:21 +00:00
|
|
|
codePaint = make_paint("Courier", normal, 20, { a=1, r=.9, g=.9, b=.9 }),
|
2014-10-14 16:34:52 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
gTemplate = SkiaPoint_make_template()
|
2014-10-11 18:28:07 +00:00
|
|
|
|
|
|
|
gRedPaint = Sk.newPaint()
|
|
|
|
gRedPaint:setAntiAlias(true)
|
|
|
|
gRedPaint:setColor{a=1, r=1, g=0, b=0 }
|
|
|
|
|
2014-10-12 19:18:40 +00:00
|
|
|
-- 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-11-04 21:24:47 +00:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-13 19:38:04 +00:00
|
|
|
function new_drawable_picture(pic)
|
|
|
|
return {
|
|
|
|
picture = pic,
|
|
|
|
width = pic:width(),
|
|
|
|
height = pic:height(),
|
|
|
|
draw = function (self, canvas, x, y, paint)
|
|
|
|
canvas:drawPicture(self.picture, x, y, paint)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function new_drawable_image(img)
|
|
|
|
return {
|
|
|
|
image = img,
|
|
|
|
width = img:width(),
|
|
|
|
height = img:height(),
|
|
|
|
draw = function (self, canvas, x, y, paint)
|
|
|
|
canvas:drawImage(self.image, x, y, paint)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2014-10-11 18:28:07 +00:00
|
|
|
|
2014-11-04 21:24:47 +00:00
|
|
|
function convert_to_picture_drawable(slide)
|
|
|
|
local rec = Sk.newPictureRecorder()
|
|
|
|
drawSlide(rec:beginRecording(640, 480), slide, gTemplate)
|
|
|
|
return new_drawable_picture(rec:endRecording())
|
|
|
|
end
|
|
|
|
|
|
|
|
function convert_to_image_drawable(slide)
|
|
|
|
local surf = Sk.newRasterSurface(640, 480)
|
|
|
|
drawSlide(surf:getCanvas(), slide, gTemplate)
|
|
|
|
return new_drawable_image(surf:newImageSnapshot())
|
|
|
|
end
|
|
|
|
|
2014-10-26 03:44:40 +00:00
|
|
|
function new_drawable_slide(slide)
|
|
|
|
return {
|
|
|
|
slide = slide,
|
|
|
|
draw = function (self, canvas, x, y, paint)
|
|
|
|
if (nil == paint or ("number" == type(paint) and (1 == paint))) then
|
|
|
|
canvas:save()
|
|
|
|
else
|
|
|
|
canvas:saveLayer(paint)
|
|
|
|
end
|
|
|
|
canvas:translate(x, y)
|
|
|
|
drawSlide(canvas, self.slide, gTemplate)
|
|
|
|
canvas:restore()
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-11-04 21:24:47 +00:00
|
|
|
gNewDrawableFactory = {
|
|
|
|
default = new_drawable_slide,
|
|
|
|
picture = convert_to_picture_drawable,
|
|
|
|
image = convert_to_image_drawable,
|
|
|
|
}
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-26 03:44:40 +00:00
|
|
|
function next_slide()
|
|
|
|
local prev = gSlides[gSlideIndex]
|
|
|
|
|
2014-11-04 06:32:07 +00:00
|
|
|
if gSlideIndex < #gSlides then
|
|
|
|
gSlideIndex = gSlideIndex + 1
|
|
|
|
spawn_transition(prev, gSlides[gSlideIndex], true)
|
2014-10-26 03:44:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function prev_slide()
|
|
|
|
local prev = gSlides[gSlideIndex]
|
|
|
|
|
2014-11-04 06:32:07 +00:00
|
|
|
if gSlideIndex > 1 then
|
|
|
|
gSlideIndex = gSlideIndex - 1
|
|
|
|
spawn_transition(prev, gSlides[gSlideIndex], false)
|
2014-10-26 03:44:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-04 21:24:47 +00:00
|
|
|
gDrawableType = "default"
|
2014-10-19 18:42:54 +00:00
|
|
|
|
2014-10-30 03:36:05 +00:00
|
|
|
load_file("slides_transitions")
|
|
|
|
|
2014-10-12 19:18:40 +00:00
|
|
|
function spawn_transition(prevSlide, nextSlide, is_forward)
|
|
|
|
local transition
|
|
|
|
if is_forward then
|
2014-10-30 03:36:05 +00:00
|
|
|
transition = gTransitionTable[nextSlide.transition]
|
2014-10-12 19:18:40 +00:00
|
|
|
else
|
2014-10-30 03:36:05 +00:00
|
|
|
transition = gTransitionTable[prevSlide.transition]
|
2014-10-12 19:18:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if not transition then
|
2014-10-14 02:43:17 +00:00
|
|
|
transition = fade_slide_transition
|
2014-10-12 19:18:40 +00:00
|
|
|
end
|
|
|
|
|
2014-11-04 21:24:47 +00:00
|
|
|
local prevDrawable = gNewDrawableFactory[gDrawableType](prevSlide)
|
|
|
|
local nextDrawable = gNewDrawableFactory[gDrawableType](nextSlide)
|
2014-10-13 19:38:04 +00:00
|
|
|
gCurrAnimation = transition(prevDrawable, nextDrawable, is_forward)
|
2014-10-12 19:18:40 +00:00
|
|
|
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-21 14:12:58 +00:00
|
|
|
local bgPaint = nil
|
|
|
|
|
|
|
|
function draw_bg(canvas)
|
|
|
|
if not bgPaint then
|
|
|
|
bgPaint = Sk.newPaint()
|
|
|
|
local grad = Sk.newLinearGradient( 0, 0, { a=1, r=0, g=0, b=.3 },
|
|
|
|
640, 480, { a=1, r=0, g=0, b=.8 })
|
|
|
|
bgPaint:setShader(grad)
|
2014-11-04 06:32:07 +00:00
|
|
|
bgPaint:setDither(true)
|
2014-10-21 14:12:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
canvas:drawPaint(bgPaint)
|
|
|
|
end
|
|
|
|
|
2014-10-14 16:34:52 +00:00
|
|
|
function onDrawContent(canvas, width, height)
|
|
|
|
local matrix = Sk.newMatrix()
|
|
|
|
matrix:setRectToRect(make_rect(0, 0, 640, 480), make_rect(0, 0, width, height), "center")
|
|
|
|
canvas:concat(matrix)
|
|
|
|
|
2014-10-21 14:12:58 +00:00
|
|
|
draw_bg(canvas)
|
|
|
|
|
2014-10-12 19:18:40 +00:00
|
|
|
local drawSlideProc = function(canvas)
|
2014-10-14 16:34:52 +00:00
|
|
|
drawSlide(canvas, gSlides[gSlideIndex], gTemplate)
|
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,
|
2014-10-21 17:46:01 +00:00
|
|
|
["="] = function () scale_text_delta(gTemplate, 1) end,
|
|
|
|
["-"] = function () scale_text_delta(gTemplate, -1) end,
|
2014-11-04 03:55:41 +00:00
|
|
|
|
|
|
|
b = function () gShowBounds = not gShowBounds end,
|
2014-11-04 21:24:47 +00:00
|
|
|
B = function () gUseBlurInTransitions = not gUseBlurInTransitions end,
|
|
|
|
|
|
|
|
["1"] = function () gDrawableType = "default" end,
|
|
|
|
["2"] = function () gDrawableType = "picture" end,
|
|
|
|
["3"] = function () gDrawableType = "image" end,
|
2014-10-11 20:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|