2 gUseBlurInTransitions = false
4 gPath = "/skia/trunk/resources/"
6 function load_file(file)
7 local prev_path = package.path
8 package.path = package.path .. ";" .. gPath .. file .. ".lua"
10 package.path = prev_path
13 load_file("slides_utils")
15 gSlides = parse_file(io.open("/skia/trunk/resources/slides_content2.lua", "r"))
17 function make_rect(l, t, r, b)
18 return { left = l, top = t, right = r, bottom = b }
21 function make_paint(typefacename, stylebits, size, color)
22 local paint = Sk.newPaint();
23 paint:setAntiAlias(true)
24 paint:setSubpixelText(true)
25 paint:setTypeface(Sk.newTypeface(typefacename, stylebits))
26 paint:setTextSize(size)
31 function draw_bullet(canvas, x, y, paint, indent)
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)
42 function stroke_rect(canvas, rect, color)
43 local paint = Sk.newPaint()
44 paint:setStroke(true);
46 canvas:drawRect(rect, paint)
49 function drawSlide(canvas, slide, master_template)
52 template = master_template.title
53 canvas:drawText(slide[1].text, 320, 240, template[1])
57 template = master_template.slide
59 local x = template.margin_x
60 local y = template.margin_y
63 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
70 y = y - fm.ascent * scale
71 canvas:drawText(node.text, x, y, paint)
72 y = y + fm.descent * scale
79 local paint = template[node.indent + 1].paint
80 local extra_dy = template[node.indent + 1].extra_dy
81 local fm = paint:getFontMetrics()
82 local x_offset = -fm.ascent * node.indent * 1.25
84 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)
88 y = newBottom + paint:getTextSize() * .5 + extra_dy
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})
99 --------------------------------------------------------------------------------------
100 function make_tmpl(paint, extra_dy)
101 return { paint = paint, extra_dy = extra_dy }
104 function SkiaPoint_make_template()
109 title[1] = make_paint("Arial", 1, 45, { a=1, r=1, g=1, b=1 })
110 title[1]:setTextAlign("center")
111 title[2] = make_paint("Arial", 1, 25, { a=1, r=.75, g=.75, b=.75 })
112 title[2]:setTextAlign("center")
118 slide[1] = make_tmpl(make_paint("Arial", 1, 35, { a=1, r=1, g=1, b=1 }), 18)
119 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)
125 codePaint = make_paint("Courier", 0, 20, { a=1, r=.9, g=.9, b=.9 }),
129 gTemplate = SkiaPoint_make_template()
131 gRedPaint = Sk.newPaint()
132 gRedPaint:setAntiAlias(true)
133 gRedPaint:setColor{a=1, r=1, g=0, b=0 }
135 -- 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.
143 -----------------------------------------------------------------------------
145 function new_drawable_picture(pic)
149 height = pic:height(),
150 draw = function (self, canvas, x, y, paint)
151 canvas:drawPicture(self.picture, x, y, paint)
156 function new_drawable_image(img)
160 height = img:height(),
161 draw = function (self, canvas, x, y, paint)
162 canvas:drawImage(self.image, x, y, paint)
167 function 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())
173 function 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())
179 function new_drawable_slide(slide)
182 draw = function (self, canvas, x, y, paint)
183 if (nil == paint or ("number" == type(paint) and (1 == paint))) then
186 canvas:saveLayer(paint)
188 canvas:translate(x, y)
189 drawSlide(canvas, self.slide, gTemplate)
195 gNewDrawableFactory = {
196 default = new_drawable_slide,
197 picture = convert_to_picture_drawable,
198 image = convert_to_image_drawable,
201 -----------------------------------------------------------------------------
203 function next_slide()
204 local prev = gSlides[gSlideIndex]
206 if gSlideIndex < #gSlides then
207 gSlideIndex = gSlideIndex + 1
208 spawn_transition(prev, gSlides[gSlideIndex], true)
212 function prev_slide()
213 local prev = gSlides[gSlideIndex]
215 if gSlideIndex > 1 then
216 gSlideIndex = gSlideIndex - 1
217 spawn_transition(prev, gSlides[gSlideIndex], false)
221 gDrawableType = "default"
223 load_file("slides_transitions")
225 function spawn_transition(prevSlide, nextSlide, is_forward)
228 transition = gTransitionTable[nextSlide.transition]
230 transition = gTransitionTable[prevSlide.transition]
233 if not transition then
234 transition = fade_slide_transition
237 local prevDrawable = gNewDrawableFactory[gDrawableType](prevSlide)
238 local nextDrawable = gNewDrawableFactory[gDrawableType](nextSlide)
239 gCurrAnimation = transition(prevDrawable, nextDrawable, is_forward)
242 --------------------------------------------------------------------------------------
244 function spawn_rotate_animation()
250 proc = function (self, canvas, drawSlideProc)
251 if self.angle >= 360 then
252 drawSlideProc(canvas)
255 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)
260 self.angle = self.angle + self.angle_delta
266 function spawn_scale_animation()
273 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
278 if self.scale > 1 then
279 drawSlideProc(canvas)
282 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)
287 self.scale = self.scale * self.scale_delta
295 function draw_bg(canvas)
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)
301 bgPaint:setDither(true)
304 canvas:drawPaint(bgPaint)
307 function 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)
314 local drawSlideProc = function(canvas)
315 drawSlide(canvas, gSlides[gSlideIndex], gTemplate)
318 if gCurrAnimation then
319 gCurrAnimation = gCurrAnimation:proc(canvas, drawSlideProc)
322 drawSlideProc(canvas)
327 function onClickHandler(x, y)
334 r = spawn_rotate_animation,
335 s = spawn_scale_animation,
336 ["="] = function () scale_text_delta(gTemplate, 1) end,
337 ["-"] = function () scale_text_delta(gTemplate, -1) end,
339 b = function () gShowBounds = not gShowBounds end,
340 B = function () gUseBlurInTransitions = not gUseBlurInTransitions end,
342 ["1"] = function () gDrawableType = "default" end,
343 ["2"] = function () gDrawableType = "picture" end,
344 ["3"] = function () gDrawableType = "image" end,
347 function onCharHandler(uni)
348 local proc = keyProcs[uni]