blob: 2f774e0de266312739b37bd971fd3b7c5e50bd77 [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
43--------------------------------------------------------------------------------------
44
45gTemplate = {
46 title = { x = 10, y = 64, textSize = 64 },
47 child = { x = 40, y = 120, dy = 50, textSize = 40 },
48}
49
50gPaints = {}
51gPaints.title = make_paint(gTemplate.title.textSize, { a=1, r=0, g=0, b=0 } )
52gPaints.child = make_paint(gTemplate.child.textSize, { a=.75, r=0, g=0, b=0 } )
53
54gRedPaint = Sk.newPaint()
55gRedPaint:setAntiAlias(true)
56gRedPaint:setColor{a=1, r=1, g=0, b=0 }
57
58gSlides = {
59 { text = "Title1", style="title", color = { a=1, r=1, g=0, b=0 },
60 children = {
61 { text = "bullet 1", style = "child" },
62 { text = "bullet 2", style = "child" },
63 { text = "bullet 3", style = "child" },
64 { draw = function (canvas)
65 canvas:drawOval({left=300, top=300, right=400, bottom=400}, gRedPaint)
66 end },
67 },
68 },
69 { text = "Title2", style="title", color = { a=1, r=0, g=1, b=0 },
70 children = {
71 { text = "bullet uno", style = "child" },
72 { text = "bullet 2", style = "child" },
73 { text = "bullet tres", style = "child" },
74 }
75 },
76 { text = "Title3", style="title",
77 children = {
78 { text = "bullet 1", style = "child", },
79 { text = "bullet 2", style = "child", color = { r=0, g=0, b=1 } },
80 { text = "bullet 3", style = "child" },
81 }
82 }
83}
84
reed18ea7772014-10-11 11:28:07 -070085--------------------------------------------------------------------------------------
86
reed09a1d672014-10-11 13:13:11 -070087gSlideIndex = 1
reed18ea7772014-10-11 11:28:07 -070088
reed09a1d672014-10-11 13:13:11 -070089function next_slide()
reed18ea7772014-10-11 11:28:07 -070090 gSlideIndex = gSlideIndex + 1
91 if gSlideIndex > #gSlides then
92 gSlideIndex = 1
93 end
94end
95
reed09a1d672014-10-11 13:13:11 -070096function prev_slide()
97 gSlideIndex = gSlideIndex - 1
98 if gSlideIndex < 1 then
99 gSlideIndex = #gSlides
100 end
101end
102
103--------------------------------------------------------------------------------------
104
105-- animation.proc is passed the canvas before drawing.
106-- The animation.proc returns itself or another animation (which means keep animating)
107-- or it returns nil, which stops the animation.
108--
109local gCurrAnimation
110
111function spawn_rotate_animation()
112 gCurrAnimation = {
113 angle = 0,
114 angle_delta = 5,
115 pivot_x = 320,
116 pivot_y = 240,
117 proc = function (this, canvas)
118 if this.angle >= 360 then
119 return nil
120 end
121 canvas:translate(this.pivot_x, this.pivot_y)
122 canvas:rotate(this.angle)
123 canvas:translate(-this.pivot_x, -this.pivot_y)
124
125 this.angle = this.angle + this.angle_delta
126 return this
127 end
128 }
129end
130
131function spawn_scale_animation()
132 gCurrAnimation = {
133 scale = 1,
134 scale_delta = .95,
135 scale_limit = 0.2,
136 pivot_x = 320,
137 pivot_y = 240,
138 proc = function (this, canvas)
139 if this.scale < this.scale_limit then
140 this.scale = this.scale_limit
141 this.scale_delta = 1 / this.scale_delta
142 end
143 if this.scale > 1 then
144 return nil
145 end
146 canvas:translate(this.pivot_x, this.pivot_y)
147 canvas:scale(this.scale, this.scale)
148 canvas:translate(-this.pivot_x, -this.pivot_y)
149
150 this.scale = this.scale * this.scale_delta
151 return this
152 end
153 }
154end
155
156function onDrawContent(canvas)
157 if gCurrAnimation then
158 gCurrAnimation = gCurrAnimation:proc(canvas)
159 end
160
161 drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints)
162
163 if gCurrAnimation then
164 return true
165 else
166 return false
167 end
168end
169
170function onClickHandler(x, y)
171 return false
172end
173
174local keyProcs = {
175 n = next_slide,
176 p = prev_slide,
177 r = spawn_rotate_animation,
178 s = spawn_scale_animation,
179}
180
181function onCharHandler(uni)
182 local proc = keyProcs[uni]
183 if proc then
184 proc()
185 return true
186 end
187 return false
188end