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