blob: bfc4ab4b8350386917f27532f14ad45b81603280 [file] [log] [blame]
reed18ea7772014-10-11 11:28:07 -07001
2function make_paint(size, color)
3 local paint = Sk.newPaint();
4 paint:setAntiAlias(true)
reed09a1d672014-10-11 13:13:11 -07005 paint:setSubpixelText(true)
reed18ea7772014-10-11 11:28:07 -07006 paint:setTextSize(size)
7 paint:setColor(color)
8 return paint
9end
10
11function find_paint(paints, style)
12 if not style then
13 style = "child"
14 end
15 local paint = paints[style]
16 return paint
17end
18
19function draw_node(canvas, node, x, y, paints)
20 if node.text then
21 local paint = find_paint(paints, node.style)
22 canvas:drawText(node.text, x, y, paint)
23 end
24 if node.draw then
25 node.draw(canvas)
26 end
27end
28
29function drawSlide(canvas, slide, template, paints)
30 draw_node(canvas, slide, template.title.x, template.title.y, paints)
31
32 if slide.children then
33 local x = template.child.x
34 local y = template.child.y
35 local dy = template.child.dy
36 for i = 1, #slide.children do
37 draw_node(canvas, slide.children[i], x, y, paints)
38 y = y + dy
39 end
40 end
41end
42
reedf355df52014-10-12 12:18:40 -070043function slide_transition(prev, next, is_forward)
44 local rec = {
45 proc = function(self, canvas, drawSlideProc)
46 if self:isDone() then
47 drawSlideProc(canvas)
48 return nil
49 end
50 canvas:drawImage(self.prevImage, self.curr_x, 0)
51 canvas:drawImage(self.nextImage, self.curr_x + 640, 0)
52 self.curr_x = self.curr_x + self.step_x
53 return self
54 end
55 }
56 if is_forward then
57 rec.prevImage = prev
58 rec.nextImage = next
59 rec.curr_x = 0
60 rec.step_x = -15
61 rec.isDone = function (self) return self.curr_x <= -640 end
62 else
63 rec.prevImage = next
64 rec.nextImage = prev
65 rec.curr_x = -640
66 rec.step_x = 15
67 rec.isDone = function (self) return self.curr_x >= 0 end
68 end
69 return rec
70end
71
reed18ea7772014-10-11 11:28:07 -070072--------------------------------------------------------------------------------------
73
74gTemplate = {
75 title = { x = 10, y = 64, textSize = 64 },
76 child = { x = 40, y = 120, dy = 50, textSize = 40 },
77}
78
79gPaints = {}
80gPaints.title = make_paint(gTemplate.title.textSize, { a=1, r=0, g=0, b=0 } )
81gPaints.child = make_paint(gTemplate.child.textSize, { a=.75, r=0, g=0, b=0 } )
82
83gRedPaint = Sk.newPaint()
84gRedPaint:setAntiAlias(true)
85gRedPaint:setColor{a=1, r=1, g=0, b=0 }
86
87gSlides = {
88 { text = "Title1", style="title", color = { a=1, r=1, g=0, b=0 },
89 children = {
90 { text = "bullet 1", style = "child" },
91 { text = "bullet 2", style = "child" },
92 { text = "bullet 3", style = "child" },
93 { draw = function (canvas)
94 canvas:drawOval({left=300, top=300, right=400, bottom=400}, gRedPaint)
95 end },
96 },
reedf355df52014-10-12 12:18:40 -070097 transition = slide_transition
reed18ea7772014-10-11 11:28:07 -070098 },
99 { text = "Title2", style="title", color = { a=1, r=0, g=1, b=0 },
100 children = {
101 { text = "bullet uno", style = "child" },
102 { text = "bullet 2", style = "child" },
103 { text = "bullet tres", style = "child" },
reedf355df52014-10-12 12:18:40 -0700104 },
105 transition = fade_transition
reed18ea7772014-10-11 11:28:07 -0700106 },
107 { text = "Title3", style="title",
108 children = {
109 { text = "bullet 1", style = "child", },
110 { text = "bullet 2", style = "child", color = { r=0, g=0, b=1 } },
111 { text = "bullet 3", style = "child" },
112 }
113 }
114}
115
reed18ea7772014-10-11 11:28:07 -0700116--------------------------------------------------------------------------------------
reedf355df52014-10-12 12:18:40 -0700117function tostr(t)
118 local str = ""
119 for k, v in next, t do
120 if #str > 0 then
121 str = str .. ", "
122 end
123 if type(k) == "number" then
124 str = str .. "[" .. k .. "] = "
125 else
126 str = str .. tostring(k) .. " = "
127 end
128 if type(v) == "table" then
129 str = str .. "{ " .. tostr(v) .. " }"
130 else
131 str = str .. tostring(v)
132 end
reed18ea7772014-10-11 11:28:07 -0700133 end
reedf355df52014-10-12 12:18:40 -0700134 return str
reed18ea7772014-10-11 11:28:07 -0700135end
136
reed09a1d672014-10-11 13:13:11 -0700137-- animation.proc is passed the canvas before drawing.
138-- The animation.proc returns itself or another animation (which means keep animating)
139-- or it returns nil, which stops the animation.
140--
141local gCurrAnimation
142
reedf355df52014-10-12 12:18:40 -0700143gSlideIndex = 1
144
145function next_slide()
146 local prev = gSlides[gSlideIndex]
147
148 gSlideIndex = gSlideIndex + 1
149 if gSlideIndex > #gSlides then
150 gSlideIndex = 1
151 end
152
153 spawn_transition(prev, gSlides[gSlideIndex], true)
154end
155
156function prev_slide()
157 local prev = gSlides[gSlideIndex]
158
159 gSlideIndex = gSlideIndex - 1
160 if gSlideIndex < 1 then
161 gSlideIndex = #gSlides
162 end
163
164 spawn_transition(prev, gSlides[gSlideIndex], false)
165end
166
167gSurfaceFactory = function (w, h) return Sk.newRasterSurface(w, h) end
168
169function spawn_transition(prevSlide, nextSlide, is_forward)
170 local transition
171 if is_forward then
172 transition = prevSlide.transition
173 else
174 transition = nextSlide.transition
175 end
176
177 if not transition then
178 return
179 end
180
181 local surf = gSurfaceFactory(640, 480)
182 local canvas = surf:getCanvas()
183
184 canvas:clear()
185 drawSlide(canvas, prevSlide, gTemplate, gPaints)
186 local prevImage = surf:newImageSnapshot()
187
188 canvas:clear()
189 drawSlide(canvas, nextSlide, gTemplate, gPaints)
190 local nextImage = surf:newImageSnapshot()
191
192 gCurrAnimation = transition(prevImage, nextImage, is_forward)
193end
194
195--------------------------------------------------------------------------------------
196
reed09a1d672014-10-11 13:13:11 -0700197function spawn_rotate_animation()
198 gCurrAnimation = {
199 angle = 0,
200 angle_delta = 5,
201 pivot_x = 320,
202 pivot_y = 240,
reedf355df52014-10-12 12:18:40 -0700203 proc = function (self, canvas, drawSlideProc)
204 if self.angle >= 360 then
205 drawSlideProc(canvas)
reed09a1d672014-10-11 13:13:11 -0700206 return nil
207 end
reedf355df52014-10-12 12:18:40 -0700208 canvas:translate(self.pivot_x, self.pivot_y)
209 canvas:rotate(self.angle)
210 canvas:translate(-self.pivot_x, -self.pivot_y)
211 drawSlideProc(canvas)
reed09a1d672014-10-11 13:13:11 -0700212
reedf355df52014-10-12 12:18:40 -0700213 self.angle = self.angle + self.angle_delta
214 return self
reed09a1d672014-10-11 13:13:11 -0700215 end
216 }
217end
218
219function spawn_scale_animation()
220 gCurrAnimation = {
221 scale = 1,
222 scale_delta = .95,
223 scale_limit = 0.2,
224 pivot_x = 320,
225 pivot_y = 240,
reedf355df52014-10-12 12:18:40 -0700226 proc = function (self, canvas, drawSlideProc)
227 if self.scale < self.scale_limit then
228 self.scale = self.scale_limit
229 self.scale_delta = 1 / self.scale_delta
reed09a1d672014-10-11 13:13:11 -0700230 end
reedf355df52014-10-12 12:18:40 -0700231 if self.scale > 1 then
232 drawSlideProc(canvas)
reed09a1d672014-10-11 13:13:11 -0700233 return nil
234 end
reedf355df52014-10-12 12:18:40 -0700235 canvas:translate(self.pivot_x, self.pivot_y)
236 canvas:scale(self.scale, self.scale)
237 canvas:translate(-self.pivot_x, -self.pivot_y)
238 drawSlideProc(canvas)
reed09a1d672014-10-11 13:13:11 -0700239
reedf355df52014-10-12 12:18:40 -0700240 self.scale = self.scale * self.scale_delta
241 return self
reed09a1d672014-10-11 13:13:11 -0700242 end
243 }
244end
245
246function onDrawContent(canvas)
reedf355df52014-10-12 12:18:40 -0700247 local drawSlideProc = function(canvas)
248 drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints)
reed09a1d672014-10-11 13:13:11 -0700249 end
250
reed09a1d672014-10-11 13:13:11 -0700251 if gCurrAnimation then
reedf355df52014-10-12 12:18:40 -0700252 gCurrAnimation = gCurrAnimation:proc(canvas, drawSlideProc)
reed09a1d672014-10-11 13:13:11 -0700253 return true
254 else
reedf355df52014-10-12 12:18:40 -0700255 drawSlideProc(canvas)
reed09a1d672014-10-11 13:13:11 -0700256 return false
257 end
258end
259
260function onClickHandler(x, y)
261 return false
262end
263
264local keyProcs = {
265 n = next_slide,
266 p = prev_slide,
267 r = spawn_rotate_animation,
268 s = spawn_scale_animation,
269}
270
271function onCharHandler(uni)
272 local proc = keyProcs[uni]
273 if proc then
274 proc()
275 return true
276 end
277 return false
278end