reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 1 | |
| 2 | function make_paint(size, color) |
| 3 | local paint = Sk.newPaint(); |
| 4 | paint:setAntiAlias(true) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 5 | paint:setSubpixelText(true) |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 6 | paint:setTextSize(size) |
| 7 | paint:setColor(color) |
| 8 | return paint |
| 9 | end |
| 10 | |
| 11 | function find_paint(paints, style) |
| 12 | if not style then |
| 13 | style = "child" |
| 14 | end |
| 15 | local paint = paints[style] |
| 16 | return paint |
| 17 | end |
| 18 | |
| 19 | function 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 |
| 27 | end |
| 28 | |
| 29 | function 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 |
| 41 | end |
| 42 | |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 43 | function slide_transition(prev, next, is_forward) |
| 44 | local rec = { |
| 45 | proc = function(self, canvas, drawSlideProc) |
| 46 | if self:isDone() then |
| 47 | drawSlideProc(canvas) |
| 48 | return nil |
| 49 | end |
| 50 | canvas:drawImage(self.prevImage, self.curr_x, 0) |
| 51 | canvas:drawImage(self.nextImage, self.curr_x + 640, 0) |
| 52 | self.curr_x = self.curr_x + self.step_x |
| 53 | return self |
| 54 | end |
| 55 | } |
| 56 | if is_forward then |
| 57 | rec.prevImage = prev |
| 58 | rec.nextImage = next |
| 59 | rec.curr_x = 0 |
| 60 | rec.step_x = -15 |
| 61 | rec.isDone = function (self) return self.curr_x <= -640 end |
| 62 | else |
| 63 | rec.prevImage = next |
| 64 | rec.nextImage = prev |
| 65 | rec.curr_x = -640 |
| 66 | rec.step_x = 15 |
| 67 | rec.isDone = function (self) return self.curr_x >= 0 end |
| 68 | end |
| 69 | return rec |
| 70 | end |
| 71 | |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 72 | -------------------------------------------------------------------------------------- |
| 73 | |
| 74 | gTemplate = { |
| 75 | title = { x = 10, y = 64, textSize = 64 }, |
| 76 | child = { x = 40, y = 120, dy = 50, textSize = 40 }, |
| 77 | } |
| 78 | |
| 79 | gPaints = {} |
| 80 | gPaints.title = make_paint(gTemplate.title.textSize, { a=1, r=0, g=0, b=0 } ) |
| 81 | gPaints.child = make_paint(gTemplate.child.textSize, { a=.75, r=0, g=0, b=0 } ) |
| 82 | |
| 83 | gRedPaint = Sk.newPaint() |
| 84 | gRedPaint:setAntiAlias(true) |
| 85 | gRedPaint:setColor{a=1, r=1, g=0, b=0 } |
| 86 | |
| 87 | gSlides = { |
| 88 | { text = "Title1", style="title", color = { a=1, r=1, g=0, b=0 }, |
| 89 | children = { |
| 90 | { text = "bullet 1", style = "child" }, |
| 91 | { text = "bullet 2", style = "child" }, |
| 92 | { text = "bullet 3", style = "child" }, |
| 93 | { draw = function (canvas) |
| 94 | canvas:drawOval({left=300, top=300, right=400, bottom=400}, gRedPaint) |
| 95 | end }, |
| 96 | }, |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 97 | transition = slide_transition |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 98 | }, |
| 99 | { text = "Title2", style="title", color = { a=1, r=0, g=1, b=0 }, |
| 100 | children = { |
| 101 | { text = "bullet uno", style = "child" }, |
| 102 | { text = "bullet 2", style = "child" }, |
| 103 | { text = "bullet tres", style = "child" }, |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 104 | }, |
| 105 | transition = fade_transition |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 106 | }, |
| 107 | { text = "Title3", style="title", |
| 108 | children = { |
| 109 | { text = "bullet 1", style = "child", }, |
| 110 | { text = "bullet 2", style = "child", color = { r=0, g=0, b=1 } }, |
| 111 | { text = "bullet 3", style = "child" }, |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 116 | -------------------------------------------------------------------------------------- |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 117 | function tostr(t) |
| 118 | local str = "" |
| 119 | for k, v in next, t do |
| 120 | if #str > 0 then |
| 121 | str = str .. ", " |
| 122 | end |
| 123 | if type(k) == "number" then |
| 124 | str = str .. "[" .. k .. "] = " |
| 125 | else |
| 126 | str = str .. tostring(k) .. " = " |
| 127 | end |
| 128 | if type(v) == "table" then |
| 129 | str = str .. "{ " .. tostr(v) .. " }" |
| 130 | else |
| 131 | str = str .. tostring(v) |
| 132 | end |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 133 | end |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 134 | return str |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 135 | end |
| 136 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 137 | -- animation.proc is passed the canvas before drawing. |
| 138 | -- The animation.proc returns itself or another animation (which means keep animating) |
| 139 | -- or it returns nil, which stops the animation. |
| 140 | -- |
| 141 | local gCurrAnimation |
| 142 | |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 143 | gSlideIndex = 1 |
| 144 | |
| 145 | function next_slide() |
| 146 | local prev = gSlides[gSlideIndex] |
| 147 | |
| 148 | gSlideIndex = gSlideIndex + 1 |
| 149 | if gSlideIndex > #gSlides then |
| 150 | gSlideIndex = 1 |
| 151 | end |
| 152 | |
| 153 | spawn_transition(prev, gSlides[gSlideIndex], true) |
| 154 | end |
| 155 | |
| 156 | function prev_slide() |
| 157 | local prev = gSlides[gSlideIndex] |
| 158 | |
| 159 | gSlideIndex = gSlideIndex - 1 |
| 160 | if gSlideIndex < 1 then |
| 161 | gSlideIndex = #gSlides |
| 162 | end |
| 163 | |
| 164 | spawn_transition(prev, gSlides[gSlideIndex], false) |
| 165 | end |
| 166 | |
| 167 | gSurfaceFactory = function (w, h) return Sk.newRasterSurface(w, h) end |
| 168 | |
| 169 | function spawn_transition(prevSlide, nextSlide, is_forward) |
| 170 | local transition |
| 171 | if is_forward then |
| 172 | transition = prevSlide.transition |
| 173 | else |
| 174 | transition = nextSlide.transition |
| 175 | end |
| 176 | |
| 177 | if not transition then |
| 178 | return |
| 179 | end |
| 180 | |
| 181 | local surf = gSurfaceFactory(640, 480) |
| 182 | local canvas = surf:getCanvas() |
| 183 | |
| 184 | canvas:clear() |
| 185 | drawSlide(canvas, prevSlide, gTemplate, gPaints) |
| 186 | local prevImage = surf:newImageSnapshot() |
| 187 | |
| 188 | canvas:clear() |
| 189 | drawSlide(canvas, nextSlide, gTemplate, gPaints) |
| 190 | local nextImage = surf:newImageSnapshot() |
| 191 | |
| 192 | gCurrAnimation = transition(prevImage, nextImage, is_forward) |
| 193 | end |
| 194 | |
| 195 | -------------------------------------------------------------------------------------- |
| 196 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 197 | function spawn_rotate_animation() |
| 198 | gCurrAnimation = { |
| 199 | angle = 0, |
| 200 | angle_delta = 5, |
| 201 | pivot_x = 320, |
| 202 | pivot_y = 240, |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 203 | proc = function (self, canvas, drawSlideProc) |
| 204 | if self.angle >= 360 then |
| 205 | drawSlideProc(canvas) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 206 | return nil |
| 207 | end |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 208 | canvas:translate(self.pivot_x, self.pivot_y) |
| 209 | canvas:rotate(self.angle) |
| 210 | canvas:translate(-self.pivot_x, -self.pivot_y) |
| 211 | drawSlideProc(canvas) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 212 | |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 213 | self.angle = self.angle + self.angle_delta |
| 214 | return self |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 215 | end |
| 216 | } |
| 217 | end |
| 218 | |
| 219 | function spawn_scale_animation() |
| 220 | gCurrAnimation = { |
| 221 | scale = 1, |
| 222 | scale_delta = .95, |
| 223 | scale_limit = 0.2, |
| 224 | pivot_x = 320, |
| 225 | pivot_y = 240, |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 226 | proc = function (self, canvas, drawSlideProc) |
| 227 | if self.scale < self.scale_limit then |
| 228 | self.scale = self.scale_limit |
| 229 | self.scale_delta = 1 / self.scale_delta |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 230 | end |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 231 | if self.scale > 1 then |
| 232 | drawSlideProc(canvas) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 233 | return nil |
| 234 | end |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 235 | canvas:translate(self.pivot_x, self.pivot_y) |
| 236 | canvas:scale(self.scale, self.scale) |
| 237 | canvas:translate(-self.pivot_x, -self.pivot_y) |
| 238 | drawSlideProc(canvas) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 239 | |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 240 | self.scale = self.scale * self.scale_delta |
| 241 | return self |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 242 | end |
| 243 | } |
| 244 | end |
| 245 | |
| 246 | function onDrawContent(canvas) |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 247 | local drawSlideProc = function(canvas) |
| 248 | drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 249 | end |
| 250 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 251 | if gCurrAnimation then |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 252 | gCurrAnimation = gCurrAnimation:proc(canvas, drawSlideProc) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 253 | return true |
| 254 | else |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame^] | 255 | drawSlideProc(canvas) |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 256 | return false |
| 257 | end |
| 258 | end |
| 259 | |
| 260 | function onClickHandler(x, y) |
| 261 | return false |
| 262 | end |
| 263 | |
| 264 | local keyProcs = { |
| 265 | n = next_slide, |
| 266 | p = prev_slide, |
| 267 | r = spawn_rotate_animation, |
| 268 | s = spawn_scale_animation, |
| 269 | } |
| 270 | |
| 271 | function onCharHandler(uni) |
| 272 | local proc = keyProcs[uni] |
| 273 | if proc then |
| 274 | proc() |
| 275 | return true |
| 276 | end |
| 277 | return false |
| 278 | end |