aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar reed <reed@chromium.org>2014-10-12 12:18:40 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-10-12 12:18:40 -0700
commitf355df58517691b05be8bc3f0e99331ede6c4d44 (patch)
tree9e6067392bdef11dab96fca112bbda4729550b57
parent485557f95d7a4b04536c0c5f01fad5f9744531da (diff)
add canvas:clear to lua
BUG=skia: TBR= Review URL: https://codereview.chromium.org/648083002
-rw-r--r--resources/slides.lua146
-rw-r--r--src/utils/SkLua.cpp8
2 files changed, 125 insertions, 29 deletions
diff --git a/resources/slides.lua b/resources/slides.lua
index 2f774e0de2..bfc4ab4b83 100644
--- a/resources/slides.lua
+++ b/resources/slides.lua
@@ -40,6 +40,35 @@ function drawSlide(canvas, slide, template, paints)
end
end
+function slide_transition(prev, next, is_forward)
+ local rec = {
+ proc = function(self, canvas, drawSlideProc)
+ if self:isDone() then
+ drawSlideProc(canvas)
+ return nil
+ end
+ canvas:drawImage(self.prevImage, self.curr_x, 0)
+ canvas:drawImage(self.nextImage, self.curr_x + 640, 0)
+ self.curr_x = self.curr_x + self.step_x
+ return self
+ end
+ }
+ if is_forward then
+ rec.prevImage = prev
+ rec.nextImage = next
+ rec.curr_x = 0
+ rec.step_x = -15
+ rec.isDone = function (self) return self.curr_x <= -640 end
+ else
+ rec.prevImage = next
+ rec.nextImage = prev
+ rec.curr_x = -640
+ rec.step_x = 15
+ rec.isDone = function (self) return self.curr_x >= 0 end
+ end
+ return rec
+end
+
--------------------------------------------------------------------------------------
gTemplate = {
@@ -65,13 +94,15 @@ gSlides = {
canvas:drawOval({left=300, top=300, right=400, bottom=400}, gRedPaint)
end },
},
+ transition = slide_transition
},
{ text = "Title2", style="title", color = { a=1, r=0, g=1, b=0 },
children = {
{ text = "bullet uno", style = "child" },
{ text = "bullet 2", style = "child" },
{ text = "bullet tres", style = "child" },
- }
+ },
+ transition = fade_transition
},
{ text = "Title3", style="title",
children = {
@@ -83,30 +114,85 @@ gSlides = {
}
--------------------------------------------------------------------------------------
+function tostr(t)
+ local str = ""
+ for k, v in next, t do
+ if #str > 0 then
+ str = str .. ", "
+ end
+ if type(k) == "number" then
+ str = str .. "[" .. k .. "] = "
+ else
+ str = str .. tostring(k) .. " = "
+ end
+ if type(v) == "table" then
+ str = str .. "{ " .. tostr(v) .. " }"
+ else
+ str = str .. tostring(v)
+ end
+ end
+ return str
+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
gSlideIndex = 1
function next_slide()
+ local prev = gSlides[gSlideIndex]
+
gSlideIndex = gSlideIndex + 1
if gSlideIndex > #gSlides then
gSlideIndex = 1
end
+
+ spawn_transition(prev, gSlides[gSlideIndex], true)
end
function prev_slide()
+ local prev = gSlides[gSlideIndex]
+
gSlideIndex = gSlideIndex - 1
if gSlideIndex < 1 then
gSlideIndex = #gSlides
end
+
+ spawn_transition(prev, gSlides[gSlideIndex], false)
end
---------------------------------------------------------------------------------------
+gSurfaceFactory = function (w, h) return Sk.newRasterSurface(w, h) 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_transition(prevSlide, nextSlide, is_forward)
+ local transition
+ if is_forward then
+ transition = prevSlide.transition
+ else
+ transition = nextSlide.transition
+ end
+
+ if not transition then
+ return
+ end
+
+ local surf = gSurfaceFactory(640, 480)
+ local canvas = surf:getCanvas()
+
+ canvas:clear()
+ drawSlide(canvas, prevSlide, gTemplate, gPaints)
+ local prevImage = surf:newImageSnapshot()
+
+ canvas:clear()
+ drawSlide(canvas, nextSlide, gTemplate, gPaints)
+ local nextImage = surf:newImageSnapshot()
+
+ gCurrAnimation = transition(prevImage, nextImage, is_forward)
+end
+
+--------------------------------------------------------------------------------------
function spawn_rotate_animation()
gCurrAnimation = {
@@ -114,16 +200,18 @@ function spawn_rotate_animation()
angle_delta = 5,
pivot_x = 320,
pivot_y = 240,
- proc = function (this, canvas)
- if this.angle >= 360 then
+ proc = function (self, canvas, drawSlideProc)
+ if self.angle >= 360 then
+ drawSlideProc(canvas)
return nil
end
- canvas:translate(this.pivot_x, this.pivot_y)
- canvas:rotate(this.angle)
- canvas:translate(-this.pivot_x, -this.pivot_y)
+ canvas:translate(self.pivot_x, self.pivot_y)
+ canvas:rotate(self.angle)
+ canvas:translate(-self.pivot_x, -self.pivot_y)
+ drawSlideProc(canvas)
- this.angle = this.angle + this.angle_delta
- return this
+ self.angle = self.angle + self.angle_delta
+ return self
end
}
end
@@ -135,34 +223,36 @@ function spawn_scale_animation()
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
+ proc = function (self, canvas, drawSlideProc)
+ if self.scale < self.scale_limit then
+ self.scale = self.scale_limit
+ self.scale_delta = 1 / self.scale_delta
end
- if this.scale > 1 then
+ if self.scale > 1 then
+ drawSlideProc(canvas)
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)
+ canvas:translate(self.pivot_x, self.pivot_y)
+ canvas:scale(self.scale, self.scale)
+ canvas:translate(-self.pivot_x, -self.pivot_y)
+ drawSlideProc(canvas)
- this.scale = this.scale * this.scale_delta
- return this
+ self.scale = self.scale * self.scale_delta
+ return self
end
}
end
function onDrawContent(canvas)
- if gCurrAnimation then
- gCurrAnimation = gCurrAnimation:proc(canvas)
+ local drawSlideProc = function(canvas)
+ drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints)
end
- drawSlide(canvas, gSlides[gSlideIndex], gTemplate, gPaints)
-
if gCurrAnimation then
+ gCurrAnimation = gCurrAnimation:proc(canvas, drawSlideProc)
return true
else
+ drawSlideProc(canvas)
return false
end
end
diff --git a/src/utils/SkLua.cpp b/src/utils/SkLua.cpp
index 578166b2e3..3dbcf8e629 100644
--- a/src/utils/SkLua.cpp
+++ b/src/utils/SkLua.cpp
@@ -424,6 +424,11 @@ static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
return rect;
}
+static int lcanvas_clear(lua_State* L) {
+ get_ref<SkCanvas>(L, 1)->clear(0);
+ return 0;
+}
+
static int lcanvas_drawColor(lua_State* L) {
get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
return 0;
@@ -455,7 +460,7 @@ static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) {
if (lua_isnumber(L, index)) {
paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255));
return paint;
- } else {
+ } else if (lua_isuserdata(L, index)) {
const SkPaint* ptr = get_obj<SkPaint>(L, index);
if (ptr) {
*paint = *ptr;
@@ -623,6 +628,7 @@ static int lcanvas_gc(lua_State* L) {
}
const struct luaL_Reg gSkCanvas_Methods[] = {
+ { "clear", lcanvas_clear },
{ "drawColor", lcanvas_drawColor },
{ "drawRect", lcanvas_drawRect },
{ "drawOval", lcanvas_drawOval },