From 09a1d6751c5c7c09d72ff8e195148509d2cb87ce Mon Sep 17 00:00:00 2001 From: reed Date: Sat, 11 Oct 2014 13:13:11 -0700 Subject: add key handlers to lua BUG=skia: TBR= Review URL: https://codereview.chromium.org/652473002 --- resources/slides.lua | 100 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 90 insertions(+), 10 deletions(-) (limited to 'resources') diff --git a/resources/slides.lua b/resources/slides.lua index 2f44e78e1b..2f774e0de2 100644 --- a/resources/slides.lua +++ b/resources/slides.lua @@ -2,6 +2,7 @@ function make_paint(size, color) local paint = Sk.newPaint(); paint:setAntiAlias(true) + paint:setSubpixelText(true) paint:setTextSize(size) paint:setColor(color) return paint @@ -81,28 +82,107 @@ gSlides = { } } +-------------------------------------------------------------------------------------- + gSlideIndex = 1 +function next_slide() + gSlideIndex = gSlideIndex + 1 + if gSlideIndex > #gSlides then + gSlideIndex = 1 + end +end + +function prev_slide() + gSlideIndex = gSlideIndex - 1 + if gSlideIndex < 1 then + gSlideIndex = #gSlides + end +end + -------------------------------------------------------------------------------------- +-- animation.proc is passed the canvas before drawing. +-- The animation.proc returns itself or another animation (which means keep animating) +-- or it returns nil, which stops the animation. +-- +local gCurrAnimation + +function spawn_rotate_animation() + gCurrAnimation = { + angle = 0, + angle_delta = 5, + pivot_x = 320, + pivot_y = 240, + proc = function (this, canvas) + if this.angle >= 360 then + return nil + end + canvas:translate(this.pivot_x, this.pivot_y) + canvas:rotate(this.angle) + canvas:translate(-this.pivot_x, -this.pivot_y) + + this.angle = this.angle + this.angle_delta + return this + end + } +end + +function spawn_scale_animation() + gCurrAnimation = { + scale = 1, + scale_delta = .95, + scale_limit = 0.2, + pivot_x = 320, + pivot_y = 240, + proc = function (this, canvas) + if this.scale < this.scale_limit then + this.scale = this.scale_limit + this.scale_delta = 1 / this.scale_delta + end + if this.scale > 1 then + return nil + end + canvas:translate(this.pivot_x, this.pivot_y) + canvas:scale(this.scale, this.scale) + canvas:translate(-this.pivot_x, -this.pivot_y) + + this.scale = this.scale * this.scale_delta + return this + end + } +end + function onDrawContent(canvas) + if gCurrAnimation then + gCurrAnimation = gCurrAnimation:proc(canvas) + end + drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints) - return false -- we're not animating + if gCurrAnimation then + return true + else + return false + end end function onClickHandler(x, y) - if x < 100 and y < 100 then - onNextSlide() - return true - end return false end -function onNextSlide() - gSlideIndex = gSlideIndex + 1 - if gSlideIndex > #gSlides then - gSlideIndex = 1 +local keyProcs = { + n = next_slide, + p = prev_slide, + r = spawn_rotate_animation, + s = spawn_scale_animation, +} + +function onCharHandler(uni) + local proc = keyProcs[uni] + if proc then + proc() + return true end + return false end - -- cgit v1.2.3