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) |
| 5 | paint:setTextSize(size) |
| 6 | paint:setColor(color) |
| 7 | return paint |
| 8 | end |
| 9 | |
| 10 | function find_paint(paints, style) |
| 11 | if not style then |
| 12 | style = "child" |
| 13 | end |
| 14 | local paint = paints[style] |
| 15 | return paint |
| 16 | end |
| 17 | |
| 18 | function draw_node(canvas, node, x, y, paints) |
| 19 | if node.text then |
| 20 | local paint = find_paint(paints, node.style) |
| 21 | canvas:drawText(node.text, x, y, paint) |
| 22 | end |
| 23 | if node.draw then |
| 24 | node.draw(canvas) |
| 25 | end |
| 26 | end |
| 27 | |
| 28 | function drawSlide(canvas, slide, template, paints) |
| 29 | draw_node(canvas, slide, template.title.x, template.title.y, paints) |
| 30 | |
| 31 | if slide.children then |
| 32 | local x = template.child.x |
| 33 | local y = template.child.y |
| 34 | local dy = template.child.dy |
| 35 | for i = 1, #slide.children do |
| 36 | draw_node(canvas, slide.children[i], x, y, paints) |
| 37 | y = y + dy |
| 38 | end |
| 39 | end |
| 40 | end |
| 41 | |
| 42 | -------------------------------------------------------------------------------------- |
| 43 | |
| 44 | gTemplate = { |
| 45 | title = { x = 10, y = 64, textSize = 64 }, |
| 46 | child = { x = 40, y = 120, dy = 50, textSize = 40 }, |
| 47 | } |
| 48 | |
| 49 | gPaints = {} |
| 50 | gPaints.title = make_paint(gTemplate.title.textSize, { a=1, r=0, g=0, b=0 } ) |
| 51 | gPaints.child = make_paint(gTemplate.child.textSize, { a=.75, r=0, g=0, b=0 } ) |
| 52 | |
| 53 | gRedPaint = Sk.newPaint() |
| 54 | gRedPaint:setAntiAlias(true) |
| 55 | gRedPaint:setColor{a=1, r=1, g=0, b=0 } |
| 56 | |
| 57 | gSlides = { |
| 58 | { text = "Title1", style="title", color = { a=1, r=1, g=0, b=0 }, |
| 59 | children = { |
| 60 | { text = "bullet 1", style = "child" }, |
| 61 | { text = "bullet 2", style = "child" }, |
| 62 | { text = "bullet 3", style = "child" }, |
| 63 | { draw = function (canvas) |
| 64 | canvas:drawOval({left=300, top=300, right=400, bottom=400}, gRedPaint) |
| 65 | end }, |
| 66 | }, |
| 67 | }, |
| 68 | { text = "Title2", style="title", color = { a=1, r=0, g=1, b=0 }, |
| 69 | children = { |
| 70 | { text = "bullet uno", style = "child" }, |
| 71 | { text = "bullet 2", style = "child" }, |
| 72 | { text = "bullet tres", style = "child" }, |
| 73 | } |
| 74 | }, |
| 75 | { text = "Title3", style="title", |
| 76 | children = { |
| 77 | { text = "bullet 1", style = "child", }, |
| 78 | { text = "bullet 2", style = "child", color = { r=0, g=0, b=1 } }, |
| 79 | { text = "bullet 3", style = "child" }, |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | gSlideIndex = 1 |
| 85 | |
| 86 | -------------------------------------------------------------------------------------- |
| 87 | |
| 88 | function onDrawContent(canvas) |
| 89 | drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints) |
| 90 | |
| 91 | return false -- we're not animating |
| 92 | end |
| 93 | |
| 94 | function onClickHandler(x, y) |
| 95 | if x < 100 and y < 100 then |
| 96 | onNextSlide() |
| 97 | return true |
| 98 | end |
| 99 | return false |
| 100 | end |
| 101 | |
| 102 | function onNextSlide() |
| 103 | gSlideIndex = gSlideIndex + 1 |
| 104 | if gSlideIndex > #gSlides then |
| 105 | gSlideIndex = 1 |
| 106 | end |
| 107 | end |
| 108 | |