diff options
author | reed <reed@chromium.org> | 2014-10-11 13:13:11 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-10-11 13:13:11 -0700 |
commit | 09a1d6751c5c7c09d72ff8e195148509d2cb87ce (patch) | |
tree | d7ad0962f3bc243a9f99058fb19cf6d4b8b2d7cf | |
parent | 18ea777638f1494b068ba4ca1a5d6725a0e80cf1 (diff) |
add key handlers to lua
BUG=skia:
TBR=
Review URL: https://codereview.chromium.org/652473002
-rw-r--r-- | resources/slides.lua | 100 | ||||
-rw-r--r-- | samplecode/SampleLua.cpp | 16 | ||||
-rw-r--r-- | src/utils/SkLua.cpp | 6 |
3 files changed, 112 insertions, 10 deletions
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 - diff --git a/samplecode/SampleLua.cpp b/samplecode/SampleLua.cpp index 917930a678..e7af727ab5 100644 --- a/samplecode/SampleLua.cpp +++ b/samplecode/SampleLua.cpp @@ -23,6 +23,7 @@ extern "C" { static const char gDrawName[] = "onDrawContent"; static const char gClickName[] = "onClickHandler"; +static const char gUnicharName[] = "onCharHandler"; static const char gMissingCode[] = "" "local paint = Sk.newPaint()" @@ -79,6 +80,21 @@ protected: } SkUnichar uni; if (SampleCode::CharQ(*evt, &uni)) { + lua_State* L = this->ensureLua(); + lua_getglobal(L, gUnicharName); + if (lua_isfunction(L, -1)) { + SkString str; + str.appendUnichar(uni); + fLua->pushString(str.c_str()); + if (lua_pcall(L, 1, 1, 0) != LUA_OK) { + SkDebugf("lua err: %s\n", lua_tostring(L, -1)); + } else { + if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { + this->inval(NULL); + return true; + } + } + } } return this->INHERITED::onQuery(evt); } diff --git a/src/utils/SkLua.cpp b/src/utils/SkLua.cpp index 1336404d8c..4a6546575f 100644 --- a/src/utils/SkLua.cpp +++ b/src/utils/SkLua.cpp @@ -693,6 +693,11 @@ static int lpaint_isSubpixelText(lua_State* L) { return 1; } +static int lpaint_setSubpixelText(lua_State* L) { + get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2)); + return 1; +} + static int lpaint_isDevKernText(lua_State* L) { lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText()); return 1; @@ -936,6 +941,7 @@ static const struct luaL_Reg gSkPaint_Methods[] = { { "isFakeBoldText", lpaint_isFakeBoldText }, { "isLinearText", lpaint_isLinearText }, { "isSubpixelText", lpaint_isSubpixelText }, + { "setSubpixelText", lpaint_setSubpixelText }, { "isDevKernText", lpaint_isDevKernText }, { "isLCDRenderText", lpaint_isLCDRenderText }, { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText }, |