add key handlers to lua

BUG=skia:
TBR=

Review URL: https://codereview.chromium.org/652473002
This commit is contained in:
reed 2014-10-11 13:13:11 -07:00 committed by Commit bot
parent 18ea777638
commit 09a1d6751c
3 changed files with 118 additions and 16 deletions

View File

@ -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 onDrawContent(canvas)
drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints)
gSlideIndex = 1
return false -- we're not animating
end
function onClickHandler(x, y)
if x < 100 and y < 100 then
onNextSlide()
return true
end
return false
end
function onNextSlide()
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)
if gCurrAnimation then
return true
else
return false
end
end
function onClickHandler(x, y)
return false
end
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

View File

@ -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);
}

View File

@ -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 },