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 | |
| 43 | -------------------------------------------------------------------------------------- |
| 44 | |
| 45 | gTemplate = { |
| 46 | title = { x = 10, y = 64, textSize = 64 }, |
| 47 | child = { x = 40, y = 120, dy = 50, textSize = 40 }, |
| 48 | } |
| 49 | |
| 50 | gPaints = {} |
| 51 | 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 } ) |
| 53 | |
| 54 | gRedPaint = Sk.newPaint() |
| 55 | gRedPaint:setAntiAlias(true) |
| 56 | gRedPaint:setColor{a=1, r=1, g=0, b=0 } |
| 57 | |
| 58 | gSlides = { |
| 59 | { text = "Title1", style="title", color = { a=1, r=1, g=0, b=0 }, |
| 60 | children = { |
| 61 | { text = "bullet 1", style = "child" }, |
| 62 | { text = "bullet 2", style = "child" }, |
| 63 | { text = "bullet 3", style = "child" }, |
| 64 | { draw = function (canvas) |
| 65 | canvas:drawOval({left=300, top=300, right=400, bottom=400}, gRedPaint) |
| 66 | end }, |
| 67 | }, |
| 68 | }, |
| 69 | { text = "Title2", style="title", color = { a=1, r=0, g=1, b=0 }, |
| 70 | children = { |
| 71 | { text = "bullet uno", style = "child" }, |
| 72 | { text = "bullet 2", style = "child" }, |
| 73 | { text = "bullet tres", style = "child" }, |
| 74 | } |
| 75 | }, |
| 76 | { text = "Title3", style="title", |
| 77 | children = { |
| 78 | { text = "bullet 1", style = "child", }, |
| 79 | { text = "bullet 2", style = "child", color = { r=0, g=0, b=1 } }, |
| 80 | { text = "bullet 3", style = "child" }, |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 85 | -------------------------------------------------------------------------------------- |
| 86 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame^] | 87 | gSlideIndex = 1 |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 88 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame^] | 89 | function next_slide() |
reed | 18ea777 | 2014-10-11 11:28:07 -0700 | [diff] [blame] | 90 | gSlideIndex = gSlideIndex + 1 |
| 91 | if gSlideIndex > #gSlides then |
| 92 | gSlideIndex = 1 |
| 93 | end |
| 94 | end |
| 95 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame^] | 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. |
| 106 | -- The animation.proc returns itself or another animation (which means keep animating) |
| 107 | -- or it returns nil, which stops the animation. |
| 108 | -- |
| 109 | local gCurrAnimation |
| 110 | |
| 111 | function spawn_rotate_animation() |
| 112 | gCurrAnimation = { |
| 113 | angle = 0, |
| 114 | angle_delta = 5, |
| 115 | pivot_x = 320, |
| 116 | pivot_y = 240, |
| 117 | proc = function (this, canvas) |
| 118 | if this.angle >= 360 then |
| 119 | return nil |
| 120 | end |
| 121 | canvas:translate(this.pivot_x, this.pivot_y) |
| 122 | canvas:rotate(this.angle) |
| 123 | canvas:translate(-this.pivot_x, -this.pivot_y) |
| 124 | |
| 125 | this.angle = this.angle + this.angle_delta |
| 126 | return this |
| 127 | end |
| 128 | } |
| 129 | end |
| 130 | |
| 131 | function spawn_scale_animation() |
| 132 | gCurrAnimation = { |
| 133 | scale = 1, |
| 134 | scale_delta = .95, |
| 135 | scale_limit = 0.2, |
| 136 | pivot_x = 320, |
| 137 | pivot_y = 240, |
| 138 | proc = function (this, canvas) |
| 139 | if this.scale < this.scale_limit then |
| 140 | this.scale = this.scale_limit |
| 141 | this.scale_delta = 1 / this.scale_delta |
| 142 | end |
| 143 | if this.scale > 1 then |
| 144 | return nil |
| 145 | end |
| 146 | canvas:translate(this.pivot_x, this.pivot_y) |
| 147 | canvas:scale(this.scale, this.scale) |
| 148 | canvas:translate(-this.pivot_x, -this.pivot_y) |
| 149 | |
| 150 | this.scale = this.scale * this.scale_delta |
| 151 | return this |
| 152 | end |
| 153 | } |
| 154 | end |
| 155 | |
| 156 | function onDrawContent(canvas) |
| 157 | if gCurrAnimation then |
| 158 | gCurrAnimation = gCurrAnimation:proc(canvas) |
| 159 | end |
| 160 | |
| 161 | drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints) |
| 162 | |
| 163 | if gCurrAnimation then |
| 164 | return true |
| 165 | else |
| 166 | return false |
| 167 | end |
| 168 | end |
| 169 | |
| 170 | function onClickHandler(x, y) |
| 171 | return false |
| 172 | end |
| 173 | |
| 174 | local keyProcs = { |
| 175 | n = next_slide, |
| 176 | p = prev_slide, |
| 177 | r = spawn_rotate_animation, |
| 178 | s = spawn_scale_animation, |
| 179 | } |
| 180 | |
| 181 | function onCharHandler(uni) |
| 182 | local proc = keyProcs[uni] |
| 183 | if proc then |
| 184 | proc() |
| 185 | return true |
| 186 | end |
| 187 | return false |
| 188 | end |