dymokomi/luce-image
Source of release 0.45.1
0.45.1 / src / canvas / refining.lucb
#==============================================================================================
#
# canvas/refining - Select › Transform Selection: the outline reshaped, the pixels left
#
# DESCRIPTION:
# The selection stays as it began while the box is dragged. A move of the box only
# records the warp (`show_selection_warp`); each frame (`draw_selection`) takes the
# selection it began from, at the level the view draws (selection_preview.lucb),
# through that warp scaled to the level, over the cells the view shows, and draws that:
# the work of a frame follows the screen, and a drag's moves between two frames cost
# one. Keeping it warps the selection at full size once (Selection.warp, which samples
# only the cells the moved edge crosses) and records the one it began from as one
# undo step.
#
#==============================================================================================
extend Canvas:
## Select › Transform Selection: begin reshaping the selection's outline
## (the pixels stay); `preview_selection_transform` or
## `preview_selection_distort` show it, `end_selection_transform` keeps it
## as one step or puts it back.
pub func begin_selection_transform() -> !:
try self.require_open()
if self.selection_warping != none:
error(invalid, "the selection is already being transformed")
if not self.has_selection():
error(invalid, "nothing is selected to transform")
let origin = try self.live().copy()
self.selection_warping = try new SelectionWarping(origin = origin, warp = none, levels = LevelOrigin(selection = none, level = 0),
shown = Shown(selection = none, level = 0), shown_box = CellBox(), shown_current = false) in memory.heap
pub func transforming_selection() -> bool:
return self.selection_warping != none
## The selection as it began, its box's corners moved to `c0`..`c7` (x, y
## pairs clockwise from the top left): Distort and Perspective.
pub func preview_selection_distort(left: f64, top: f64, width: f64, height: f64, c0: f64, c1: f64, c2: f64, c3: f64, c4: f64, c5: f64, c6: f64, c7: f64) -> !:
let corners: f64[8] = [c0, c1, c2, c3, c4, c5, c6, c7]
try show_selection_warp(&self, try quad_warp(left, top, width, height, corners))
## Keep the reshaped selection as one step, or put back the one it began from.
pub func end_selection_transform(keep: bool) -> !:
let session = self.selection_warping else return
if keep:
if let warp = session.warp:
var moved = try session.origin.copy()
errdefer moved.destroy()
try moved.warp(warp)
# History records the state before the change: the live one, still the origin.
try self.remember_state("Transform Selection")
var live = self.live()
live.destroy()
*live = moved
drop_selection_transform(&self)
## Transform Selection while the box is up.
struct SelectionWarping:
## The selection it began from, as the live one stays meanwhile.
var origin: Selection
## The warp last asked for (none: the box as found).
var warp: Warp?
## The origin at the preview's level.
var levels: LevelOrigin
## The last preview drawn, the box of its level it covers, and whether
## it shows the warp last asked for.
var shown: Shown
var shown_box: CellBox
var shown_current: bool
## Show the selection it began from through `warp`, from the next frame.
func show_selection_warp(canvas: Canvas*, warp: Warp) -> !:
let session = canvas.selection_warping else error(invalid, "no selection transform is under way")
session.warp = warp
session.shown_current = false
## The preview for a view of `scope`: the origin at the scope's level taken
## through the warp there, over the scope's box, made when anything changed.
func prepare_selection_warp(session: SelectionWarping*, scope: Scope) -> !:
let warp = session.warp else return
if session.shown_current and session.shown.selection != none and session.shown.level == scope.level and session.shown_box.covers(scope.box):
return
let origin = try session.levels.at(&session.origin, scope.level)
var moved = try origin.copy()
errdefer moved.destroy()
try moved.warp(level_warp(warp, scope.level), scope.box)
try session.shown.show(moved, scope.level)
session.shown_box = scope.box
session.shown_current = true
## `warp` (a backwards map in document pixels) in the pixels of `level`:
## scaled up to the document, mapped, scaled back down.
func level_warp(warp: Warp, level: u32) -> Warp:
if level == 0:
return warp
let scale = (f64)(1u64 << level)
let up = Warp(m = [scale, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, 1.0])
let down = Warp(m = [1.0 / scale, 0.0, 0.0, 0.0, 1.0 / scale, 0.0, 0.0, 0.0, 1.0])
return multiply(down, multiply(warp, up))
## Let a selection transform go with all it holds (ended, or the canvas closing).
func drop_selection_transform(canvas: Canvas*):
let session = canvas.selection_warping else return
canvas.selection_warping = none
session.shown.destroy()
session.levels.destroy()
session.origin.destroy()
free(session) in memory.heap
## Transform Selection's outline over the document as `draw` drew it: the
## preview's ants (or, before the box moves, the selection's own). False when
## no selection transform is under way.
func draw_selection_warp(canvas: Canvas*, target: interop.View[gpu.RenderTarget], rectangle: gpu.Rect, zoom: f64, offset_x: f64, offset_y: f64, phase: f64) -> bool!:
let session = canvas.selection_warping else return false
if session.warp == none:
try draw_canvas_selection(canvas, target, rectangle, zoom, offset_x, offset_y, phase)
return true
try prepare_selection_warp(session, view_scope(canvas, rectangle, zoom, offset_x, offset_y))
if let shown = session.shown.selection:
try draw_shown(canvas, shown, session.shown.level, refine_ants, target, rectangle, zoom, offset_x, offset_y, phase)
return true