blob: 43c9b3b32fa307d8f00d5146ed34335965c4a35f [file] [log] [blame]
reed1b6ab442014-11-03 19:55:41 -08001gShowBounds = false
reed7b864662014-11-04 13:24:47 -08002gUseBlurInTransitions = false
reed86217d82014-10-25 20:44:40 -07003
4gPath = "/skia/trunk/resources/"
5
6function load_file(file)
reed07dada72014-10-29 20:36:05 -07007 local prev_path = package.path
reed86217d82014-10-25 20:44:40 -07008 package.path = package.path .. ";" .. gPath .. file .. ".lua"
9 require(file)
reed07dada72014-10-29 20:36:05 -070010 package.path = prev_path
reedd2e7dfb2014-10-13 19:43:17 -070011end
12
reed86217d82014-10-25 20:44:40 -070013load_file("slides_utils")
reedd2e7dfb2014-10-13 19:43:17 -070014
reedde330ff2014-11-02 19:19:34 -080015gSlides = parse_file(io.open("/skia/trunk/resources/slides_content2.lua", "r"))
reed18ea7772014-10-11 11:28:07 -070016
reedbdc49ae2014-10-14 09:34:52 -070017function make_rect(l, t, r, b)
18 return { left = l, top = t, right = r, bottom = b }
19end
20
21function make_paint(typefacename, stylebits, size, color)
reed18ea7772014-10-11 11:28:07 -070022 local paint = Sk.newPaint();
23 paint:setAntiAlias(true)
reed09a1d672014-10-11 13:13:11 -070024 paint:setSubpixelText(true)
reedbdc49ae2014-10-14 09:34:52 -070025 paint:setTypeface(Sk.newTypeface(typefacename, stylebits))
reed18ea7772014-10-11 11:28:07 -070026 paint:setTextSize(size)
27 paint:setColor(color)
28 return paint
29end
30
reed9e233b22014-11-03 12:18:24 -080031function draw_bullet(canvas, x, y, paint, indent)
32 if 0 == indent then
33 return
34 end
35 local ps = paint:getTextSize()
36 local cx = x - ps * .8
37 local cy = y - ps * .4
38 local radius = ps * .2
39 canvas:drawCircle(cx, cy, radius, paint)
40end
41
reed1b6ab442014-11-03 19:55:41 -080042function stroke_rect(canvas, rect, color)
43 local paint = Sk.newPaint()
44 paint:setStroke(true);
45 paint:setColor(color)
46 canvas:drawRect(rect, paint)
47end
48
reedde330ff2014-11-02 19:19:34 -080049function drawSlide(canvas, slide, master_template)
reedbb8a0ab2014-11-03 22:32:07 -080050
51 if #slide == 1 then
52 template = master_template.title
53 canvas:drawText(slide[1].text, 320, 240, template[1])
54 return
55 end
56
57 template = master_template.slide
reedbdc49ae2014-10-14 09:34:52 -070058
59 local x = template.margin_x
60 local y = template.margin_y
reed9e233b22014-11-03 12:18:24 -080061 local scale = 1.25
reedbdc49ae2014-10-14 09:34:52 -070062
reedde330ff2014-11-02 19:19:34 -080063 if slide.blockstyle == "code" then
64 local paint = master_template.codePaint
65 local fm = paint:getFontMetrics()
66 local height = #slide * (fm.descent - fm.ascent)
67 y = (480 - height) / 2
68 for i = 1, #slide do
69 local node = slide[i]
reed9e233b22014-11-03 12:18:24 -080070 y = y - fm.ascent * scale
reedde330ff2014-11-02 19:19:34 -080071 canvas:drawText(node.text, x, y, paint)
reed9e233b22014-11-03 12:18:24 -080072 y = y + fm.descent * scale
reedde330ff2014-11-02 19:19:34 -080073 end
74 return
75 end
76
reedd2e7dfb2014-10-13 19:43:17 -070077 for i = 1, #slide do
78 local node = slide[i]
reed9fbc3f32014-10-21 07:12:58 -070079 local paint = template[node.indent + 1].paint
80 local extra_dy = template[node.indent + 1].extra_dy
reedd2e7dfb2014-10-13 19:43:17 -070081 local fm = paint:getFontMetrics()
reed9e233b22014-11-03 12:18:24 -080082 local x_offset = -fm.ascent * node.indent * 1.25
reedbdc49ae2014-10-14 09:34:52 -070083
reed1b6ab442014-11-03 19:55:41 -080084 local bounds = make_rect(x + x_offset, y, 620, 640)
85 local blob, newBottom = Sk.newTextBlob(node.text, bounds, paint)
86 draw_bullet(canvas, x + x_offset, y - fm.ascent, paint, node.indent)
87 canvas:drawTextBlob(blob, 0, 0, paint)
reed36c9c112014-11-04 10:58:42 -080088 y = newBottom + paint:getTextSize() * .5 + extra_dy
reed1b6ab442014-11-03 19:55:41 -080089
90 if gShowBounds then
91 bounds.bottom = newBottom
92 stroke_rect(canvas, bounds, {a=1,r=0,g=1,b=0})
93 stroke_rect(canvas, blob:bounds(), {a=1,r=1,g=0,b=0})
94 end
95
reed9fbc3f32014-10-21 07:12:58 -070096 end
97end
98
reed18ea7772014-10-11 11:28:07 -070099--------------------------------------------------------------------------------------
reed9fbc3f32014-10-21 07:12:58 -0700100function make_tmpl(paint, extra_dy)
101 return { paint = paint, extra_dy = extra_dy }
102end
reed18ea7772014-10-11 11:28:07 -0700103
reedbdc49ae2014-10-14 09:34:52 -0700104function SkiaPoint_make_template()
105 local title = {
106 margin_x = 30,
107 margin_y = 100,
108 }
reed9e233b22014-11-03 12:18:24 -0800109 title[1] = make_paint("Arial", 1, 45, { a=1, r=1, g=1, b=1 })
reedbdc49ae2014-10-14 09:34:52 -0700110 title[1]:setTextAlign("center")
reed9fbc3f32014-10-21 07:12:58 -0700111 title[2] = make_paint("Arial", 1, 25, { a=1, r=.75, g=.75, b=.75 })
reedbdc49ae2014-10-14 09:34:52 -0700112 title[2]:setTextAlign("center")
reed18ea7772014-10-11 11:28:07 -0700113
reedbdc49ae2014-10-14 09:34:52 -0700114 local slide = {
115 margin_x = 20,
reed9fbc3f32014-10-21 07:12:58 -0700116 margin_y = 25,
reedbdc49ae2014-10-14 09:34:52 -0700117 }
reed9e233b22014-11-03 12:18:24 -0800118 slide[1] = make_tmpl(make_paint("Arial", 1, 35, { a=1, r=1, g=1, b=1 }), 18)
reed36c9c112014-11-04 10:58:42 -0800119 slide[2] = make_tmpl(make_paint("Arial", 0, 25, { a=1, r=1, g=1, b=1 }), 10)
120 slide[3] = make_tmpl(make_paint("Arial", 0, 20, { a=1, r=.9, g=.9, b=.9 }), 5)
reedbdc49ae2014-10-14 09:34:52 -0700121
122 return {
123 title = title,
124 slide = slide,
reed9e233b22014-11-03 12:18:24 -0800125 codePaint = make_paint("Courier", 0, 20, { a=1, r=.9, g=.9, b=.9 }),
reedbdc49ae2014-10-14 09:34:52 -0700126 }
127end
128
129gTemplate = SkiaPoint_make_template()
reed18ea7772014-10-11 11:28:07 -0700130
131gRedPaint = Sk.newPaint()
132gRedPaint:setAntiAlias(true)
133gRedPaint:setColor{a=1, r=1, g=0, b=0 }
134
reed09a1d672014-10-11 13:13:11 -0700135-- animation.proc is passed the canvas before drawing.
136-- The animation.proc returns itself or another animation (which means keep animating)
137-- or it returns nil, which stops the animation.
138--
139local gCurrAnimation
140
reedf355df52014-10-12 12:18:40 -0700141gSlideIndex = 1
142
reed7b864662014-11-04 13:24:47 -0800143-----------------------------------------------------------------------------
144
reed96affcd2014-10-13 12:38:04 -0700145function new_drawable_picture(pic)
146 return {
147 picture = pic,
148 width = pic:width(),
149 height = pic:height(),
150 draw = function (self, canvas, x, y, paint)
151 canvas:drawPicture(self.picture, x, y, paint)
152 end
153 }
154end
155
156function new_drawable_image(img)
157 return {
158 image = img,
159 width = img:width(),
160 height = img:height(),
161 draw = function (self, canvas, x, y, paint)
162 canvas:drawImage(self.image, x, y, paint)
163 end
164 }
165end
reedf355df52014-10-12 12:18:40 -0700166
reed7b864662014-11-04 13:24:47 -0800167function convert_to_picture_drawable(slide)
168 local rec = Sk.newPictureRecorder()
169 drawSlide(rec:beginRecording(640, 480), slide, gTemplate)
170 return new_drawable_picture(rec:endRecording())
171end
172
173function convert_to_image_drawable(slide)
174 local surf = Sk.newRasterSurface(640, 480)
175 drawSlide(surf:getCanvas(), slide, gTemplate)
176 return new_drawable_image(surf:newImageSnapshot())
177end
178
reed86217d82014-10-25 20:44:40 -0700179function new_drawable_slide(slide)
180 return {
181 slide = slide,
182 draw = function (self, canvas, x, y, paint)
183 if (nil == paint or ("number" == type(paint) and (1 == paint))) then
184 canvas:save()
185 else
186 canvas:saveLayer(paint)
187 end
188 canvas:translate(x, y)
189 drawSlide(canvas, self.slide, gTemplate)
190 canvas:restore()
191 end
192 }
193end
194
reed7b864662014-11-04 13:24:47 -0800195gNewDrawableFactory = {
196 default = new_drawable_slide,
197 picture = convert_to_picture_drawable,
198 image = convert_to_image_drawable,
199}
200
201-----------------------------------------------------------------------------
202
reed86217d82014-10-25 20:44:40 -0700203function next_slide()
204 local prev = gSlides[gSlideIndex]
205
reedbb8a0ab2014-11-03 22:32:07 -0800206 if gSlideIndex < #gSlides then
207 gSlideIndex = gSlideIndex + 1
208 spawn_transition(prev, gSlides[gSlideIndex], true)
reed86217d82014-10-25 20:44:40 -0700209 end
reed86217d82014-10-25 20:44:40 -0700210end
211
212function prev_slide()
213 local prev = gSlides[gSlideIndex]
214
reedbb8a0ab2014-11-03 22:32:07 -0800215 if gSlideIndex > 1 then
216 gSlideIndex = gSlideIndex - 1
217 spawn_transition(prev, gSlides[gSlideIndex], false)
reed86217d82014-10-25 20:44:40 -0700218 end
reed86217d82014-10-25 20:44:40 -0700219end
220
reed7b864662014-11-04 13:24:47 -0800221gDrawableType = "default"
reed468b1812014-10-19 11:42:54 -0700222
reed07dada72014-10-29 20:36:05 -0700223load_file("slides_transitions")
224
reedf355df52014-10-12 12:18:40 -0700225function spawn_transition(prevSlide, nextSlide, is_forward)
226 local transition
227 if is_forward then
reed07dada72014-10-29 20:36:05 -0700228 transition = gTransitionTable[nextSlide.transition]
reedf355df52014-10-12 12:18:40 -0700229 else
reed07dada72014-10-29 20:36:05 -0700230 transition = gTransitionTable[prevSlide.transition]
reedf355df52014-10-12 12:18:40 -0700231 end
232
233 if not transition then
reedd2e7dfb2014-10-13 19:43:17 -0700234 transition = fade_slide_transition
reedf355df52014-10-12 12:18:40 -0700235 end
236
reed7b864662014-11-04 13:24:47 -0800237 local prevDrawable = gNewDrawableFactory[gDrawableType](prevSlide)
238 local nextDrawable = gNewDrawableFactory[gDrawableType](nextSlide)
reed96affcd2014-10-13 12:38:04 -0700239 gCurrAnimation = transition(prevDrawable, nextDrawable, is_forward)
reedf355df52014-10-12 12:18:40 -0700240end
241
242--------------------------------------------------------------------------------------
243
reed09a1d672014-10-11 13:13:11 -0700244function spawn_rotate_animation()
245 gCurrAnimation = {
246 angle = 0,
247 angle_delta = 5,
248 pivot_x = 320,
249 pivot_y = 240,
reedf355df52014-10-12 12:18:40 -0700250 proc = function (self, canvas, drawSlideProc)
251 if self.angle >= 360 then
252 drawSlideProc(canvas)
reed09a1d672014-10-11 13:13:11 -0700253 return nil
254 end
reedf355df52014-10-12 12:18:40 -0700255 canvas:translate(self.pivot_x, self.pivot_y)
256 canvas:rotate(self.angle)
257 canvas:translate(-self.pivot_x, -self.pivot_y)
258 drawSlideProc(canvas)
reed09a1d672014-10-11 13:13:11 -0700259
reedf355df52014-10-12 12:18:40 -0700260 self.angle = self.angle + self.angle_delta
261 return self
reed09a1d672014-10-11 13:13:11 -0700262 end
263 }
264end
265
266function spawn_scale_animation()
267 gCurrAnimation = {
268 scale = 1,
269 scale_delta = .95,
270 scale_limit = 0.2,
271 pivot_x = 320,
272 pivot_y = 240,
reedf355df52014-10-12 12:18:40 -0700273 proc = function (self, canvas, drawSlideProc)
274 if self.scale < self.scale_limit then
275 self.scale = self.scale_limit
276 self.scale_delta = 1 / self.scale_delta
reed09a1d672014-10-11 13:13:11 -0700277 end
reedf355df52014-10-12 12:18:40 -0700278 if self.scale > 1 then
279 drawSlideProc(canvas)
reed09a1d672014-10-11 13:13:11 -0700280 return nil
281 end
reedf355df52014-10-12 12:18:40 -0700282 canvas:translate(self.pivot_x, self.pivot_y)
283 canvas:scale(self.scale, self.scale)
284 canvas:translate(-self.pivot_x, -self.pivot_y)
285 drawSlideProc(canvas)
reed09a1d672014-10-11 13:13:11 -0700286
reedf355df52014-10-12 12:18:40 -0700287 self.scale = self.scale * self.scale_delta
288 return self
reed09a1d672014-10-11 13:13:11 -0700289 end
290 }
291end
292
reed9fbc3f32014-10-21 07:12:58 -0700293local bgPaint = nil
294
295function draw_bg(canvas)
296 if not bgPaint then
297 bgPaint = Sk.newPaint()
298 local grad = Sk.newLinearGradient( 0, 0, { a=1, r=0, g=0, b=.3 },
299 640, 480, { a=1, r=0, g=0, b=.8 })
300 bgPaint:setShader(grad)
reedbb8a0ab2014-11-03 22:32:07 -0800301 bgPaint:setDither(true)
reed9fbc3f32014-10-21 07:12:58 -0700302 end
303
304 canvas:drawPaint(bgPaint)
305end
306
reedbdc49ae2014-10-14 09:34:52 -0700307function onDrawContent(canvas, width, height)
308 local matrix = Sk.newMatrix()
309 matrix:setRectToRect(make_rect(0, 0, 640, 480), make_rect(0, 0, width, height), "center")
310 canvas:concat(matrix)
311
reed9fbc3f32014-10-21 07:12:58 -0700312 draw_bg(canvas)
313
reedf355df52014-10-12 12:18:40 -0700314 local drawSlideProc = function(canvas)
reedbdc49ae2014-10-14 09:34:52 -0700315 drawSlide(canvas, gSlides[gSlideIndex], gTemplate)
reed09a1d672014-10-11 13:13:11 -0700316 end
317
reed09a1d672014-10-11 13:13:11 -0700318 if gCurrAnimation then
reedf355df52014-10-12 12:18:40 -0700319 gCurrAnimation = gCurrAnimation:proc(canvas, drawSlideProc)
reed09a1d672014-10-11 13:13:11 -0700320 return true
321 else
reedf355df52014-10-12 12:18:40 -0700322 drawSlideProc(canvas)
reed09a1d672014-10-11 13:13:11 -0700323 return false
324 end
325end
326
327function onClickHandler(x, y)
328 return false
329end
330
331local keyProcs = {
332 n = next_slide,
333 p = prev_slide,
334 r = spawn_rotate_animation,
335 s = spawn_scale_animation,
reedc7663982014-10-21 10:46:01 -0700336 ["="] = function () scale_text_delta(gTemplate, 1) end,
337 ["-"] = function () scale_text_delta(gTemplate, -1) end,
reed1b6ab442014-11-03 19:55:41 -0800338
339 b = function () gShowBounds = not gShowBounds end,
reed7b864662014-11-04 13:24:47 -0800340 B = function () gUseBlurInTransitions = not gUseBlurInTransitions end,
341
342 ["1"] = function () gDrawableType = "default" end,
343 ["2"] = function () gDrawableType = "picture" end,
344 ["3"] = function () gDrawableType = "image" end,
reed09a1d672014-10-11 13:13:11 -0700345}
346
347function onCharHandler(uni)
348 local proc = keyProcs[uni]
349 if proc then
350 proc()
351 return true
352 end
353 return false
354end