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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | samplecode/SampleLua.cpp » ('j') | src/utils/SkLua.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 gShowBounds = false 1 gShowBounds = false
2 gUseBlurInTransitions = false 2 gUseBlurInTransitions = false
3 3
4 gPath = "/skia/trunk/resources/" 4 gPath = "/skia/trunk/resources/"
5 5
6 function load_file(file) 6 function load_file(file)
7 local prev_path = package.path 7 local prev_path = package.path
8 package.path = package.path .. ";" .. gPath .. file .. ".lua" 8 package.path = package.path .. ";" .. gPath .. file .. ".lua"
9 require(file) 9 require(file)
10 package.path = prev_path 10 package.path = prev_path
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 canvas:scale(self.scale, self.scale) 283 canvas:scale(self.scale, self.scale)
284 canvas:translate(-self.pivot_x, -self.pivot_y) 284 canvas:translate(-self.pivot_x, -self.pivot_y)
285 drawSlideProc(canvas) 285 drawSlideProc(canvas)
286 286
287 self.scale = self.scale * self.scale_delta 287 self.scale = self.scale * self.scale_delta
288 return self 288 return self
289 end 289 end
290 } 290 }
291 end 291 end
292 292
293 local bgPaint = nil 293 local bgPaint = Sk.newPaint()
294 294
295 function draw_bg(canvas) 295 function draw_bg(canvas, w, h)
296 if not bgPaint then 296 bgPaint:setShader(Sk.newLinearGradient(0, 0, { a=1, r=0, g=0, b=.3 },
297 bgPaint = Sk.newPaint() 297 w, h, { a=1, r=0, g=0, b=.8 }))
298 local grad = Sk.newLinearGradient( 0, 0, { a=1, r=0, g=0, b=.3 },
299 640, 480, { a=1, r=0, g=0, b=.8 })
300 bgPaint:setShader(grad)
301 bgPaint:setDither(true)
302 end
303
304 canvas:drawPaint(bgPaint) 298 canvas:drawPaint(bgPaint)
305 end 299 end
306 300
307 function onDrawContent(canvas, width, height) 301 function draw_drag(canvas)
308 local matrix = Sk.newMatrix() 302 local paint = Sk.newPaint()
309 matrix:setRectToRect(make_rect(0, 0, 640, 480), make_rect(0, 0, width, heigh t), "center") 303 if gDragPts then
310 canvas:concat(matrix) 304 local r = make_rect(gDragPts[1], gDragPts[2], gDragPts[3], gDragPts[4])
305 canvas:drawRect(r, paint)
306 end
307 end
311 308
312 draw_bg(canvas) 309 function setpt(array, index, x, y)
310 local i = index * 2 - 1
311 array[i + 0] = x
312 array[i + 1] = y
313 end
313 314
315 function make_patch_cubics(minX, minY, maxX, maxY)
316 local dx = (maxX - minX) / 3
317 local dy = (maxY - minY) / 3
318
319 cubics = {}
robertphillips 2014/11/07 18:02:54 // bottom
320 for i = 0, 2 do
321 setpt(cubics, 1 + i, minX + dx * i, minY)
322 end
robertphillips 2014/11/07 18:02:54 // right
323 for i = 0, 2 do
324 setpt(cubics, 4 + i, maxX, minY + dy * i)
325 end
robertphillips 2014/11/07 18:02:54 // top
326 for i = 0, 2 do
327 setpt(cubics, 7 + i, maxX - dx * i, maxY)
328 end
robertphillips 2014/11/07 18:02:54 // left
329 for i = 0, 2 do
330 setpt(cubics, 10 + i, minX, maxY - dy * i)
331 end
332
333 return cubics
334 end
335
336 gCubics = make_patch_cubics(10, 10, 630, 470)
337
robertphillips 2014/11/07 18:02:54 There doesn't seem to be any way to change this gu
338 gPatchDelta = { x = 0, y = 0 }
339
robertphillips 2014/11/07 18:02:54 distSq instead ?
340 function dist(x, y, x1, y1)
341 local dx = x - x1
342 local dy = y - y1
343 return math.sqrt(dx * dx + dy * dy)
344 end
345
346 function find_cubics_index(x, y)
347 x = x - gPatchDelta.x
348 y = y - gPatchDelta.y
349 for i = 1, 12 do
350 local index = i * 2 - 1
351 local cx = gCubics[index + 0]
352 local cy = gCubics[index + 1]
353 if dist(x, y, cx, cy) <= 5 then
354 return index
355 end
356 end
357 return nil
358 end
359
360 function set_cubic_pt(index, x, y)
361 gCubics[index + 0] = x - gPatchDelta.x
362 gCubics[index + 1] = y - gPatchDelta.y
363 end
364
365 function draw_ctrl(canvas, x, y, paint)
366 local n = 4
367 canvas:drawRect(make_rect(x - n, y - n, x + n, y + n), paint)
368 end
369
370 gBlackPaint = Sk.newPaint()
371
372 function show_patch(canvas, paint, width, height)
373 local colors = nil
374 local texs = nil
375
376 if width and height then
377 texs = { 0, 0, width, 0, width, height, 0, height }
378 end
379
380 canvas:save()
381 canvas:translate(gPatchDelta.x, gPatchDelta.y)
382 canvas:drawPatch(gCubics, colors, texs, paint)
383
384 for i = 1, 12 do
385 local index = i * 2 - 1
386 local x = gCubics[index + 0]
387 local y = gCubics[index + 1]
388 draw_ctrl(canvas, x, y, gBlackPaint)
389 end
390
391 canvas:restore()
392 end
393
394 gCTM = Sk.newMatrix()
395
396 function drawSlideIntoCanvas(canvas)
314 local drawSlideProc = function(canvas) 397 local drawSlideProc = function(canvas)
315 drawSlide(canvas, gSlides[gSlideIndex], gTemplate) 398 drawSlide(canvas, gSlides[gSlideIndex], gTemplate)
316 end 399 end
317 400
318 if gCurrAnimation then 401 if gCurrAnimation then
319 gCurrAnimation = gCurrAnimation:proc(canvas, drawSlideProc) 402 gCurrAnimation = gCurrAnimation:proc(canvas, drawSlideProc)
320 return true
321 else 403 else
322 drawSlideProc(canvas) 404 drawSlideProc(canvas)
323 return false
324 end 405 end
325 end 406 end
326 407
327 function onClickHandler(x, y) 408 function onDrawContent(canvas, width, height)
robertphillips 2014/11/07 18:02:54 add some comments here ? // We render the slide c
328 return false 409 gCTM:setRectToRect(make_rect(0, 0, 640, 480), make_rect(0, 0, width, height) , "center")
410
411 draw_bg(canvas, width, height)
412
413 local surf = canvas:newSurface(640, 480)
414 drawSlideIntoCanvas(surf:getCanvas())
415
416 canvas:concat(gCTM)
417
418 local image = surf:newImageSnapshot()
419 local paint = Sk.newPaint()
420 paint:setShader(image:newShader())
421 paint:setFilterLevel(1)
robertphillips 2014/11/07 18:02:54 I'm a bit surprised we don't have an option to not
422 show_patch(canvas, paint, image:width(), image:height())
423
424 return not (not gCurrAnimation)
425 end
426
robertphillips 2014/11/07 18:02:54 This and draw_drag don't appear to be used.
427 gDragPts = nil
428
429 gDragCubicIndex = nil
430
431 function onClickHandler(x, y, state)
432 local inv = Sk.newMatrix()
433 gCTM:invert(inv)
434 x, y = inv:mapXY(x, y)
435
436 if state == "down" then
437 gDragCubicIndex = find_cubics_index(x, y)
438 if gDragCubicIndex == nil then
439 return false
440 end
441 end
442
443 if gDragCubicIndex and (state == "moved") then
444 set_cubic_pt(gDragCubicIndex, x, y)
445 end
446
447 return true
329 end 448 end
330 449
331 local keyProcs = { 450 local keyProcs = {
332 n = next_slide, 451 n = next_slide,
333 p = prev_slide, 452 p = prev_slide,
334 r = spawn_rotate_animation, 453 r = spawn_rotate_animation,
335 s = spawn_scale_animation, 454 s = spawn_scale_animation,
336 ["="] = function () scale_text_delta(gTemplate, 1) end, 455 ["="] = function () scale_text_delta(gTemplate, 1) end,
337 ["-"] = function () scale_text_delta(gTemplate, -1) end, 456 ["-"] = function () scale_text_delta(gTemplate, -1) end,
338 457
339 b = function () gShowBounds = not gShowBounds end, 458 b = function () gShowBounds = not gShowBounds end,
340 B = function () gUseBlurInTransitions = not gUseBlurInTransitions end, 459 B = function () gUseBlurInTransitions = not gUseBlurInTransitions end,
341 460
342 ["1"] = function () gDrawableType = "default" end, 461 ["1"] = function () gDrawableType = "default" end,
343 ["2"] = function () gDrawableType = "picture" end, 462 ["2"] = function () gDrawableType = "picture" end,
344 ["3"] = function () gDrawableType = "image" end, 463 ["3"] = function () gDrawableType = "image" end,
345 } 464 }
346 465
347 function onCharHandler(uni) 466 function onCharHandler(uni)
348 local proc = keyProcs[uni] 467 local proc = keyProcs[uni]
349 if proc then 468 if proc then
350 proc() 469 proc()
351 return true 470 return true
352 end 471 end
353 return false 472 return false
354 end 473 end
OLDNEW
« 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