reed | d2e7dfb | 2014-10-13 19:43:17 -0700 | [diff] [blame^] | 1 | function tostr(t) |
| 2 | local str = "" |
| 3 | for k, v in next, t do |
| 4 | if #str > 0 then |
| 5 | str = str .. ", " |
| 6 | end |
| 7 | if type(k) == "number" then |
| 8 | str = str .. "[" .. k .. "] = " |
| 9 | else |
| 10 | str = str .. tostring(k) .. " = " |
| 11 | end |
| 12 | if type(v) == "table" then |
| 13 | str = str .. "{ " .. tostr(v) .. " }" |
| 14 | elseif type(v) == "string" then |
| 15 | str = str .. '"' .. v .. '"' |
| 16 | else |
| 17 | str = str .. tostring(v) |
| 18 | end |
| 19 | end |
| 20 | return str |
| 21 | end |
| 22 | |
| 23 | |
| 24 | function trim_ws(s) |
| 25 | return s:match("^%s*(.*)") |
| 26 | end |
| 27 | |
| 28 | function count_hypens(s) |
| 29 | local leftover = s:match("^-*(.*)") |
| 30 | return string.len(s) - string.len(leftover) |
| 31 | end |
| 32 | |
| 33 | function parse_file(file) |
| 34 | local slides = {} |
| 35 | local block = {} |
| 36 | |
| 37 | for line in file:lines() do |
| 38 | local s = trim_ws(line) |
| 39 | if #s == 0 then -- done with a block |
| 40 | if #block > 0 then |
| 41 | slides[#slides + 1] = block |
| 42 | block = {} |
| 43 | end |
| 44 | else |
| 45 | local n = count_hypens(s) |
| 46 | block[#block + 1] = { |
| 47 | indent = n, |
| 48 | text = trim_ws(s:sub(n + 1, -1)) |
| 49 | } |
| 50 | end |
| 51 | end |
| 52 | return slides |
| 53 | end |
| 54 | |
| 55 | function pretty_print_slide(slide) |
| 56 | io.write("{\n") |
| 57 | for i = 1, #slide do |
| 58 | local node = slide[i] |
| 59 | for j = 0, node.indent do |
| 60 | io.write(" ") |
| 61 | end |
| 62 | io.write("{ ") |
| 63 | io.write(tostr(node)) |
| 64 | io.write(" },\n") |
| 65 | end |
| 66 | io.write("},\n") |
| 67 | end |
| 68 | |
| 69 | function pretty_print_slides(slides) |
| 70 | io.write("gSlides = {\n") |
| 71 | for i = 1, #slides do |
| 72 | pretty_print_slide(slides[i]) |
| 73 | end |
| 74 | io.write("}\n") |
| 75 | end |
| 76 | |
| 77 | gSlides = parse_file(io.open("/skia/trunk/resources/slides_content.lua", "r")) |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 78 | |
| 79 | function make_paint(size, color) |
| 80 | local paint = Sk.newPaint(); |
| 81 | paint:setAntiAlias(true) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 82 | paint:setSubpixelText(true) |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 83 | paint:setTextSize(size) |
| 84 | paint:setColor(color) |
| 85 | return paint |
| 86 | end |
| 87 | |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 88 | function drawSlide(canvas, slide, template, paints) |
reed | d2e7dfb | 2014-10-13 19:43:17 -0700 | [diff] [blame^] | 89 | local scale = 1.15 |
| 90 | local y = 0 |
| 91 | for i = 1, #slide do |
| 92 | local node = slide[i] |
| 93 | local temp = template[node.indent + 1] |
| 94 | local paint = paints[node.indent + 1] |
| 95 | local fm = paint:getFontMetrics() |
| 96 | y = y - fm.ascent * scale |
| 97 | canvas:drawText(node.text, temp.x, y, paint) |
| 98 | y = y + fm.descent * scale |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 99 | end |
| 100 | end |
| 101 | |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 102 | function slide_transition(prev, next, is_forward) |
| 103 | local rec = { |
| 104 | proc = function(self, canvas, drawSlideProc) |
| 105 | if self:isDone() then |
| 106 | drawSlideProc(canvas) |
| 107 | return nil |
| 108 | end |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 109 | self.prevDrawable:draw(canvas, self.curr_x, 0) |
| 110 | self.nextDrawable:draw(canvas, self.curr_x + 640, 0) |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 111 | self.curr_x = self.curr_x + self.step_x |
| 112 | return self |
| 113 | end |
| 114 | } |
| 115 | if is_forward then |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 116 | rec.prevDrawable = prev |
| 117 | rec.nextDrawable = next |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 118 | rec.curr_x = 0 |
| 119 | rec.step_x = -15 |
| 120 | rec.isDone = function (self) return self.curr_x <= -640 end |
| 121 | else |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 122 | rec.prevDrawable = next |
| 123 | rec.nextDrawable = prev |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 124 | rec.curr_x = -640 |
| 125 | rec.step_x = 15 |
| 126 | rec.isDone = function (self) return self.curr_x >= 0 end |
| 127 | end |
| 128 | return rec |
| 129 | end |
| 130 | |
reed | 0d76b95 | 2014-10-12 19:05:52 -0700 | [diff] [blame] | 131 | function fade_slide_transition(prev, next, is_forward) |
| 132 | local rec = { |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 133 | prevDrawable = prev, |
| 134 | nextDrawable = next, |
reed | 0d76b95 | 2014-10-12 19:05:52 -0700 | [diff] [blame] | 135 | proc = function(self, canvas, drawSlideProc) |
| 136 | if self:isDone() then |
| 137 | drawSlideProc(canvas) |
| 138 | return nil |
| 139 | end |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 140 | self.prevDrawable:draw(canvas, self.prev_x, 0, self.prev_a) |
| 141 | self.nextDrawable:draw(canvas, self.next_x, 0, self.next_a) |
reed | 0d76b95 | 2014-10-12 19:05:52 -0700 | [diff] [blame] | 142 | self:step() |
| 143 | return self |
| 144 | end |
| 145 | } |
| 146 | if is_forward then |
| 147 | rec.prev_x = 0 |
| 148 | rec.prev_a = 1 |
| 149 | rec.next_x = 640 |
| 150 | rec.next_a = 0 |
| 151 | rec.isDone = function (self) return self.next_x <= 0 end |
| 152 | rec.step = function (self) |
| 153 | self.next_x = self.next_x - 20 |
| 154 | self.next_a = (640 - self.next_x) / 640 |
| 155 | self.prev_a = 1 - self.next_a |
| 156 | end |
| 157 | else |
| 158 | rec.prev_x = 0 |
| 159 | rec.prev_a = 1 |
| 160 | rec.next_x = 0 |
| 161 | rec.next_a = 0 |
| 162 | rec.isDone = function (self) return self.prev_x >= 640 end |
| 163 | rec.step = function (self) |
| 164 | self.prev_x = self.prev_x + 20 |
| 165 | self.prev_a = (640 - self.prev_x) / 640 |
| 166 | self.next_a = 1 - self.prev_a |
| 167 | end |
| 168 | end |
| 169 | return rec |
| 170 | end |
| 171 | |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 172 | -------------------------------------------------------------------------------------- |
| 173 | |
| 174 | gTemplate = { |
reed | d2e7dfb | 2014-10-13 19:43:17 -0700 | [diff] [blame^] | 175 | { x = 10, textSize = 40, bullet = "" }, |
| 176 | { x = 40, textSize = 30, bullet = "\xE2\x80\xA2" }, |
| 177 | { x = 70, textSize = 20, bullet = "\xE2\x97\xA6" }, |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 178 | } |
| 179 | |
reed | d2e7dfb | 2014-10-13 19:43:17 -0700 | [diff] [blame^] | 180 | gPaints = { |
| 181 | make_paint(gTemplate[1].textSize, { a=1, r=0, g=0, b=0 } ), |
| 182 | make_paint(gTemplate[2].textSize, { a=1, r=1, g=0, b=0 } ), |
| 183 | make_paint(gTemplate[3].textSize, { a=1, r=0, g=1, b=0 } ), |
| 184 | } |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 185 | |
| 186 | gRedPaint = Sk.newPaint() |
| 187 | gRedPaint:setAntiAlias(true) |
| 188 | gRedPaint:setColor{a=1, r=1, g=0, b=0 } |
| 189 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 190 | -- animation.proc is passed the canvas before drawing. |
| 191 | -- The animation.proc returns itself or another animation (which means keep animating) |
| 192 | -- or it returns nil, which stops the animation. |
| 193 | -- |
| 194 | local gCurrAnimation |
| 195 | |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 196 | gSlideIndex = 1 |
| 197 | |
| 198 | function next_slide() |
| 199 | local prev = gSlides[gSlideIndex] |
| 200 | |
| 201 | gSlideIndex = gSlideIndex + 1 |
| 202 | if gSlideIndex > #gSlides then |
| 203 | gSlideIndex = 1 |
| 204 | end |
| 205 | |
| 206 | spawn_transition(prev, gSlides[gSlideIndex], true) |
| 207 | end |
| 208 | |
| 209 | function prev_slide() |
| 210 | local prev = gSlides[gSlideIndex] |
| 211 | |
| 212 | gSlideIndex = gSlideIndex - 1 |
| 213 | if gSlideIndex < 1 then |
| 214 | gSlideIndex = #gSlides |
| 215 | end |
| 216 | |
| 217 | spawn_transition(prev, gSlides[gSlideIndex], false) |
| 218 | end |
| 219 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 220 | function new_drawable_picture(pic) |
| 221 | return { |
| 222 | picture = pic, |
| 223 | width = pic:width(), |
| 224 | height = pic:height(), |
| 225 | draw = function (self, canvas, x, y, paint) |
| 226 | canvas:drawPicture(self.picture, x, y, paint) |
| 227 | end |
| 228 | } |
| 229 | end |
| 230 | |
| 231 | function new_drawable_image(img) |
| 232 | return { |
| 233 | image = img, |
| 234 | width = img:width(), |
| 235 | height = img:height(), |
| 236 | draw = function (self, canvas, x, y, paint) |
| 237 | canvas:drawImage(self.image, x, y, paint) |
| 238 | end |
| 239 | } |
| 240 | end |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 241 | |
| 242 | function spawn_transition(prevSlide, nextSlide, is_forward) |
| 243 | local transition |
| 244 | if is_forward then |
| 245 | transition = prevSlide.transition |
| 246 | else |
| 247 | transition = nextSlide.transition |
| 248 | end |
| 249 | |
| 250 | if not transition then |
reed | d2e7dfb | 2014-10-13 19:43:17 -0700 | [diff] [blame^] | 251 | transition = fade_slide_transition |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 252 | end |
| 253 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 254 | local rec = Sk.newPictureRecorder() |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 255 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 256 | drawSlide(rec:beginRecording(640, 480), prevSlide, gTemplate, gPaints) |
| 257 | local prevDrawable = new_drawable_picture(rec:endRecording()) |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 258 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 259 | drawSlide(rec:beginRecording(640, 480), nextSlide, gTemplate, gPaints) |
| 260 | local nextDrawable = new_drawable_picture(rec:endRecording()) |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 261 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 262 | gCurrAnimation = transition(prevDrawable, nextDrawable, is_forward) |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 263 | end |
| 264 | |
| 265 | -------------------------------------------------------------------------------------- |
| 266 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 267 | function spawn_rotate_animation() |
| 268 | gCurrAnimation = { |
| 269 | angle = 0, |
| 270 | angle_delta = 5, |
| 271 | pivot_x = 320, |
| 272 | pivot_y = 240, |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 273 | proc = function (self, canvas, drawSlideProc) |
| 274 | if self.angle >= 360 then |
| 275 | drawSlideProc(canvas) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 276 | return nil |
| 277 | end |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 278 | canvas:translate(self.pivot_x, self.pivot_y) |
| 279 | canvas:rotate(self.angle) |
| 280 | canvas:translate(-self.pivot_x, -self.pivot_y) |
| 281 | drawSlideProc(canvas) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 282 | |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 283 | self.angle = self.angle + self.angle_delta |
| 284 | return self |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 285 | end |
| 286 | } |
| 287 | end |
| 288 | |
| 289 | function spawn_scale_animation() |
| 290 | gCurrAnimation = { |
| 291 | scale = 1, |
| 292 | scale_delta = .95, |
| 293 | scale_limit = 0.2, |
| 294 | pivot_x = 320, |
| 295 | pivot_y = 240, |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 296 | proc = function (self, canvas, drawSlideProc) |
| 297 | if self.scale < self.scale_limit then |
| 298 | self.scale = self.scale_limit |
| 299 | self.scale_delta = 1 / self.scale_delta |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 300 | end |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 301 | if self.scale > 1 then |
| 302 | drawSlideProc(canvas) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 303 | return nil |
| 304 | end |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 305 | canvas:translate(self.pivot_x, self.pivot_y) |
| 306 | canvas:scale(self.scale, self.scale) |
| 307 | canvas:translate(-self.pivot_x, -self.pivot_y) |
| 308 | drawSlideProc(canvas) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 309 | |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 310 | self.scale = self.scale * self.scale_delta |
| 311 | return self |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 312 | end |
| 313 | } |
| 314 | end |
| 315 | |
| 316 | function onDrawContent(canvas) |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 317 | local drawSlideProc = function(canvas) |
| 318 | drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 319 | end |
| 320 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 321 | if gCurrAnimation then |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 322 | gCurrAnimation = gCurrAnimation:proc(canvas, drawSlideProc) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 323 | return true |
| 324 | else |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 325 | drawSlideProc(canvas) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 326 | return false |
| 327 | end |
| 328 | end |
| 329 | |
| 330 | function onClickHandler(x, y) |
| 331 | return false |
| 332 | end |
| 333 | |
| 334 | local keyProcs = { |
| 335 | n = next_slide, |
| 336 | p = prev_slide, |
| 337 | r = spawn_rotate_animation, |
| 338 | s = spawn_scale_animation, |
| 339 | } |
| 340 | |
| 341 | function onCharHandler(uni) |
| 342 | local proc = keyProcs[uni] |
| 343 | if proc then |
| 344 | proc() |
| 345 | return true |
| 346 | end |
| 347 | return false |
| 348 | end |