Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2877)

Unified Diff: resources/slides2.lua

Issue 712613002: add patch and clicktracking to lua (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | samplecode/SampleLua.cpp » ('j') | src/utils/SkLua.cpp » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: resources/slides2.lua
diff --git a/resources/slides.lua b/resources/slides2.lua
similarity index 73%
copy from resources/slides.lua
copy to resources/slides2.lua
index 43c9b3b32fa307d8f00d5146ed34335965c4a35f..a74a8d5bc6c6972fc786605937d64ef31ba0cdbd 100644
--- a/resources/slides.lua
+++ b/resources/slides2.lua
@@ -290,42 +290,161 @@ function spawn_scale_animation()
}
end
-local bgPaint = nil
-
-function draw_bg(canvas)
- if not bgPaint then
- bgPaint = Sk.newPaint()
- local grad = Sk.newLinearGradient( 0, 0, { a=1, r=0, g=0, b=.3 },
- 640, 480, { a=1, r=0, g=0, b=.8 })
- bgPaint:setShader(grad)
- bgPaint:setDither(true)
- end
+local bgPaint = Sk.newPaint()
+function draw_bg(canvas, w, h)
+ bgPaint:setShader(Sk.newLinearGradient(0, 0, { a=1, r=0, g=0, b=.3 },
+ w, h, { a=1, r=0, g=0, b=.8 }))
canvas:drawPaint(bgPaint)
end
-function onDrawContent(canvas, width, height)
- local matrix = Sk.newMatrix()
- matrix:setRectToRect(make_rect(0, 0, 640, 480), make_rect(0, 0, width, height), "center")
- canvas:concat(matrix)
+function draw_drag(canvas)
+ local paint = Sk.newPaint()
+ if gDragPts then
+ local r = make_rect(gDragPts[1], gDragPts[2], gDragPts[3], gDragPts[4])
+ canvas:drawRect(r, paint)
+ end
+end
+
+function setpt(array, index, x, y)
+ local i = index * 2 - 1
+ array[i + 0] = x
+ array[i + 1] = y
+end
+
+function make_patch_cubics(minX, minY, maxX, maxY)
+ local dx = (maxX - minX) / 3
+ local dy = (maxY - minY) / 3
+
+ cubics = {}
robertphillips 2014/11/07 18:02:54 // bottom
+ for i = 0, 2 do
+ setpt(cubics, 1 + i, minX + dx * i, minY)
+ end
robertphillips 2014/11/07 18:02:54 // right
+ for i = 0, 2 do
+ setpt(cubics, 4 + i, maxX, minY + dy * i)
+ end
robertphillips 2014/11/07 18:02:54 // top
+ for i = 0, 2 do
+ setpt(cubics, 7 + i, maxX - dx * i, maxY)
+ end
robertphillips 2014/11/07 18:02:54 // left
+ for i = 0, 2 do
+ setpt(cubics, 10 + i, minX, maxY - dy * i)
+ end
+
+ return cubics
+end
+
+gCubics = make_patch_cubics(10, 10, 630, 470)
+
robertphillips 2014/11/07 18:02:54 There doesn't seem to be any way to change this gu
+gPatchDelta = { x = 0, y = 0 }
+
robertphillips 2014/11/07 18:02:54 distSq instead ?
+function dist(x, y, x1, y1)
+ local dx = x - x1
+ local dy = y - y1
+ return math.sqrt(dx * dx + dy * dy)
+end
+
+function find_cubics_index(x, y)
+ x = x - gPatchDelta.x
+ y = y - gPatchDelta.y
+ for i = 1, 12 do
+ local index = i * 2 - 1
+ local cx = gCubics[index + 0]
+ local cy = gCubics[index + 1]
+ if dist(x, y, cx, cy) <= 5 then
+ return index
+ end
+ end
+ return nil
+end
+
+function set_cubic_pt(index, x, y)
+ gCubics[index + 0] = x - gPatchDelta.x
+ gCubics[index + 1] = y - gPatchDelta.y
+end
+
+function draw_ctrl(canvas, x, y, paint)
+ local n = 4
+ canvas:drawRect(make_rect(x - n, y - n, x + n, y + n), paint)
+end
+
+gBlackPaint = Sk.newPaint()
- draw_bg(canvas)
+function show_patch(canvas, paint, width, height)
+ local colors = nil
+ local texs = nil
+ if width and height then
+ texs = { 0, 0, width, 0, width, height, 0, height }
+ end
+
+ canvas:save()
+ canvas:translate(gPatchDelta.x, gPatchDelta.y)
+ canvas:drawPatch(gCubics, colors, texs, paint)
+
+ for i = 1, 12 do
+ local index = i * 2 - 1
+ local x = gCubics[index + 0]
+ local y = gCubics[index + 1]
+ draw_ctrl(canvas, x, y, gBlackPaint)
+ end
+
+ canvas:restore()
+end
+
+gCTM = Sk.newMatrix()
+
+function drawSlideIntoCanvas(canvas)
local drawSlideProc = function(canvas)
drawSlide(canvas, gSlides[gSlideIndex], gTemplate)
end
if gCurrAnimation then
gCurrAnimation = gCurrAnimation:proc(canvas, drawSlideProc)
- return true
else
drawSlideProc(canvas)
- return false
end
end
-function onClickHandler(x, y)
- return false
+function onDrawContent(canvas, width, height)
robertphillips 2014/11/07 18:02:54 add some comments here ? // We render the slide c
+ gCTM:setRectToRect(make_rect(0, 0, 640, 480), make_rect(0, 0, width, height), "center")
+
+ draw_bg(canvas, width, height)
+
+ local surf = canvas:newSurface(640, 480)
+ drawSlideIntoCanvas(surf:getCanvas())
+
+ canvas:concat(gCTM)
+
+ local image = surf:newImageSnapshot()
+ local paint = Sk.newPaint()
+ paint:setShader(image:newShader())
+ paint:setFilterLevel(1)
robertphillips 2014/11/07 18:02:54 I'm a bit surprised we don't have an option to not
+ show_patch(canvas, paint, image:width(), image:height())
+
+ return not (not gCurrAnimation)
+end
+
robertphillips 2014/11/07 18:02:54 This and draw_drag don't appear to be used.
+gDragPts = nil
+
+gDragCubicIndex = nil
+
+function onClickHandler(x, y, state)
+ local inv = Sk.newMatrix()
+ gCTM:invert(inv)
+ x, y = inv:mapXY(x, y)
+
+ if state == "down" then
+ gDragCubicIndex = find_cubics_index(x, y)
+ if gDragCubicIndex == nil then
+ return false
+ end
+ end
+
+ if gDragCubicIndex and (state == "moved") then
+ set_cubic_pt(gDragCubicIndex, x, y)
+ end
+
+ return true
end
local keyProcs = {
« no previous file with comments | « no previous file | samplecode/SampleLua.cpp » ('j') | src/utils/SkLua.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698