| OLD | NEW |
| 1 function tostr(t) | 1 |
| 2 local str = "" | 2 gPath = "/skia/trunk/resources/" |
| 3 for k, v in next, t do | 3 |
| 4 if #str > 0 then | 4 function load_file(file) |
| 5 str = str .. ", " | 5 package.path = package.path .. ";" .. gPath .. file .. ".lua" |
| 6 end | 6 require(file) |
| 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 | 7 end |
| 22 | 8 |
| 23 | 9 load_file("slides_utils") |
| 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 | 10 |
| 77 gSlides = parse_file(io.open("/skia/trunk/resources/slides_content.lua", "r")) | 11 gSlides = parse_file(io.open("/skia/trunk/resources/slides_content.lua", "r")) |
| 78 | 12 |
| 79 function make_rect(l, t, r, b) | 13 function make_rect(l, t, r, b) |
| 80 return { left = l, top = t, right = r, bottom = b } | 14 return { left = l, top = t, right = r, bottom = b } |
| 81 end | 15 end |
| 82 | 16 |
| 83 function make_paint(typefacename, stylebits, size, color) | 17 function make_paint(typefacename, stylebits, size, color) |
| 84 local paint = Sk.newPaint(); | 18 local paint = Sk.newPaint(); |
| 85 paint:setAntiAlias(true) | 19 paint:setAntiAlias(true) |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 rec.step_x = 15 | 78 rec.step_x = 15 |
| 145 rec.isDone = function (self) return self.curr_x >= 0 end | 79 rec.isDone = function (self) return self.curr_x >= 0 end |
| 146 end | 80 end |
| 147 return rec | 81 return rec |
| 148 end | 82 end |
| 149 | 83 |
| 150 function sqr(value) return value * value end | 84 function sqr(value) return value * value end |
| 151 | 85 |
| 152 function set_blur(paint, alpha) | 86 function set_blur(paint, alpha) |
| 153 local sigma = sqr(1 - alpha) * 20 | 87 local sigma = sqr(1 - alpha) * 20 |
| 154 paint:setImageFilter(Sk.newBlurImageFilter(sigma, sigma)) | 88 -- paint:setImageFilter(Sk.newBlurImageFilter(sigma, sigma)) |
| 155 paint:setAlpha(alpha) | 89 paint:setAlpha(alpha) |
| 156 end | 90 end |
| 157 | 91 |
| 158 function fade_slide_transition(prev, next, is_forward) | 92 function fade_slide_transition(prev, next, is_forward) |
| 159 local rec = { | 93 local rec = { |
| 160 paint = Sk.newPaint(), | 94 paint = Sk.newPaint(), |
| 161 prevDrawable = prev, | 95 prevDrawable = prev, |
| 162 nextDrawable = next, | 96 nextDrawable = next, |
| 163 proc = function(self, canvas, drawSlideProc) | 97 proc = function(self, canvas, drawSlideProc) |
| 164 if self:isDone() then | 98 if self:isDone() then |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 gRedPaint:setColor{a=1, r=1, g=0, b=0 } | 171 gRedPaint:setColor{a=1, r=1, g=0, b=0 } |
| 238 | 172 |
| 239 -- animation.proc is passed the canvas before drawing. | 173 -- animation.proc is passed the canvas before drawing. |
| 240 -- The animation.proc returns itself or another animation (which means keep anim
ating) | 174 -- The animation.proc returns itself or another animation (which means keep anim
ating) |
| 241 -- or it returns nil, which stops the animation. | 175 -- or it returns nil, which stops the animation. |
| 242 -- | 176 -- |
| 243 local gCurrAnimation | 177 local gCurrAnimation |
| 244 | 178 |
| 245 gSlideIndex = 1 | 179 gSlideIndex = 1 |
| 246 | 180 |
| 247 function next_slide() | |
| 248 local prev = gSlides[gSlideIndex] | |
| 249 | |
| 250 gSlideIndex = gSlideIndex + 1 | |
| 251 if gSlideIndex > #gSlides then | |
| 252 gSlideIndex = 1 | |
| 253 end | |
| 254 | |
| 255 spawn_transition(prev, gSlides[gSlideIndex], true) | |
| 256 end | |
| 257 | |
| 258 function prev_slide() | |
| 259 local prev = gSlides[gSlideIndex] | |
| 260 | |
| 261 gSlideIndex = gSlideIndex - 1 | |
| 262 if gSlideIndex < 1 then | |
| 263 gSlideIndex = #gSlides | |
| 264 end | |
| 265 | |
| 266 spawn_transition(prev, gSlides[gSlideIndex], false) | |
| 267 end | |
| 268 | |
| 269 function new_drawable_picture(pic) | 181 function new_drawable_picture(pic) |
| 270 return { | 182 return { |
| 271 picture = pic, | 183 picture = pic, |
| 272 width = pic:width(), | 184 width = pic:width(), |
| 273 height = pic:height(), | 185 height = pic:height(), |
| 274 draw = function (self, canvas, x, y, paint) | 186 draw = function (self, canvas, x, y, paint) |
| 275 canvas:drawPicture(self.picture, x, y, paint) | 187 canvas:drawPicture(self.picture, x, y, paint) |
| 276 end | 188 end |
| 277 } | 189 } |
| 278 end | 190 end |
| 279 | 191 |
| 280 function new_drawable_image(img) | 192 function new_drawable_image(img) |
| 281 return { | 193 return { |
| 282 image = img, | 194 image = img, |
| 283 width = img:width(), | 195 width = img:width(), |
| 284 height = img:height(), | 196 height = img:height(), |
| 285 draw = function (self, canvas, x, y, paint) | 197 draw = function (self, canvas, x, y, paint) |
| 286 canvas:drawImage(self.image, x, y, paint) | 198 canvas:drawImage(self.image, x, y, paint) |
| 287 end | 199 end |
| 288 } | 200 } |
| 289 end | 201 end |
| 290 | 202 |
| 203 function new_drawable_slide(slide) |
| 204 return { |
| 205 slide = slide, |
| 206 draw = function (self, canvas, x, y, paint) |
| 207 if (nil == paint or ("number" == type(paint) and (1 == paint))) then |
| 208 canvas:save() |
| 209 else |
| 210 canvas:saveLayer(paint) |
| 211 end |
| 212 canvas:translate(x, y) |
| 213 drawSlide(canvas, self.slide, gTemplate) |
| 214 canvas:restore() |
| 215 end |
| 216 } |
| 217 end |
| 218 |
| 219 function next_slide() |
| 220 local prev = gSlides[gSlideIndex] |
| 221 |
| 222 gSlideIndex = gSlideIndex + 1 |
| 223 if gSlideIndex > #gSlides then |
| 224 gSlideIndex = 1 |
| 225 end |
| 226 |
| 227 spawn_transition(prev, gSlides[gSlideIndex], true) |
| 228 end |
| 229 |
| 230 function prev_slide() |
| 231 local prev = gSlides[gSlideIndex] |
| 232 |
| 233 gSlideIndex = gSlideIndex - 1 |
| 234 if gSlideIndex < 1 then |
| 235 gSlideIndex = #gSlides |
| 236 end |
| 237 |
| 238 spawn_transition(prev, gSlides[gSlideIndex], false) |
| 239 end |
| 240 |
| 291 function convert_to_picture_drawable(slide) | 241 function convert_to_picture_drawable(slide) |
| 292 local rec = Sk.newPictureRecorder() | 242 local rec = Sk.newPictureRecorder() |
| 293 drawSlide(rec:beginRecording(640, 480), slide, gTemplate) | 243 drawSlide(rec:beginRecording(640, 480), slide, gTemplate) |
| 294 return new_drawable_picture(rec:endRecording()) | 244 return new_drawable_picture(rec:endRecording()) |
| 295 end | 245 end |
| 296 | 246 |
| 297 function convert_to_image_drawable(slide) | 247 function convert_to_image_drawable(slide) |
| 298 local surf = Sk.newRasterSurface(640, 480) | 248 local surf = Sk.newRasterSurface(640, 480) |
| 299 drawSlide(surf:getCanvas(), slide, gTemplate) | 249 drawSlide(surf:getCanvas(), slide, gTemplate) |
| 300 return new_drawable_image(surf:newImageSnapshot()) | 250 return new_drawable_image(surf:newImageSnapshot()) |
| 301 end | 251 end |
| 302 | 252 |
| 303 gMakeDrawable = convert_to_picture_drawable | 253 -- gMakeDrawable = convert_to_picture_drawable |
| 254 gMakeDrawable = new_drawable_slide |
| 304 | 255 |
| 305 function spawn_transition(prevSlide, nextSlide, is_forward) | 256 function spawn_transition(prevSlide, nextSlide, is_forward) |
| 306 local transition | 257 local transition |
| 307 if is_forward then | 258 if is_forward then |
| 308 transition = prevSlide.transition | 259 transition = prevSlide.transition |
| 309 else | 260 else |
| 310 transition = nextSlide.transition | 261 transition = nextSlide.transition |
| 311 end | 262 end |
| 312 | 263 |
| 313 if not transition then | 264 if not transition then |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 } | 368 } |
| 418 | 369 |
| 419 function onCharHandler(uni) | 370 function onCharHandler(uni) |
| 420 local proc = keyProcs[uni] | 371 local proc = keyProcs[uni] |
| 421 if proc then | 372 if proc then |
| 422 proc() | 373 proc() |
| 423 return true | 374 return true |
| 424 end | 375 end |
| 425 return false | 376 return false |
| 426 end | 377 end |
| OLD | NEW |