dymokomi/luced-2d
Source of release 0.5.1
## The boxes that reshape layers on the canvas: Free Transform (and its Skew,
## Distort and Perspective modes), Transform Selection, Warp and Puppet Warp,
## the Move tool's transform controls, and Flip. Each shows live until kept
## (Enter) or dropped (Esc).
from live import Live
from layer_picking import LayerPicking
from layer_commands import LayerCommands
from tool_state import ToolState
from image_commands import ImageCommands
from free_transform import FreeTransform, begin_free_transform, box_around
from mesh_warp import MeshWarp
from transform_box import TransformBox, mode_free
pub class Transforms:
let live: Live
let layers: LayerPicking
let edits: LayerCommands
let tools: ToolState
let image: ImageCommands
## The free transform under way on the canvas, if any, and how the next resamples.
pub var transform: FreeTransform? = none
pub var transform_sampling: int = 1
## Edit › Transform › Warp or Edit › Puppet Warp under way, if any.
pub var mesh_warp: MeshWarp? = none
## The transform numbers keep width and height in proportion.
pub var lock_transform_ratio: bool = true
## The Move tool shows the transform box around the selected layers; a
## handle dragged starts a free transform (View › Show Transform Controls).
pub var show_transform_controls: bool = false
var controls_cache: TransformBox? = none
var controls_revision: int = -1
pub func init(self, live: Live, layers: LayerPicking, edits: LayerCommands, tools: ToolState, image: ImageCommands):
self.live = live
self.layers = layers
self.edits = edits
self.tools = tools
self.image = image
## Edit › Free Transform: a box around the selected layers' pixels (or the
## selection's, whose pixels then float), shown live until
## `finish_transform`. False when there is nothing to transform, or
## something else is previewing.
pub func begin_transform(self) -> bool!:
if self.transform != none or self.live.canvas.previewing():
return false
self.transform = begin_free_transform(self.live.canvas, self.layers.moving_ids(), self.transform_sampling)
self.live.touch()
return self.transform != none
## Show the layers as the transform box has them now.
pub func preview_transform(self) -> unit!:
if let session = self.transform:
session.preview()
self.live.touch()
## Apply the transform (Enter) or drop it (Esc). Applied, it is one undo
## step, and linked masks go with the pixels.
pub func finish_transform(self, keep: bool) -> unit!:
if let session = self.transform:
self.transform = none
session.finish(keep)
self.live.touch()
## Edit › Transform › Skew, Distort or Perspective (transform_box's
## `mode_*`): the free-transform box, up already or put up now, with plain
## handle drags doing that. False as `begin_transform`.
pub func begin_transform_mode(self, mode: int) -> bool!:
if self.transform == none and not self.begin_transform():
return false
if let session = self.transform:
# A vector element follows only scale, turn and move: its box stays free.
if not session.distorts() and mode != mode_free:
return false
session.box.mode = mode
self.live.touch()
return true
## Select › Transform Selection: the free-transform box around the
## selection, moving only its outline. False when nothing is selected or
## something else is under way.
pub func begin_selection_transform(self) -> bool!:
let canvas = self.live.canvas
if self.transform != none or canvas.previewing() or not canvas.has_selection():
return false
let box = TransformBox(float(canvas.selection_left()), float(canvas.selection_top()), float(canvas.selection_width()), float(canvas.selection_height()))
canvas.begin_selection_transform()
self.transform = FreeTransform(canvas, [], box, outline = true)
self.live.touch()
return true
## Alt-drag inside the box: carry on the same box on a copy of the layer,
## the original left as it was. Not for floating pixels or several layers.
pub func duplicate_in_transform(self) -> unit!:
let session = self.transform else return
if session.floats() or session.ids.length != 1:
return
self.transform = none
session.finish(false)
self.edits.duplicate()
self.transform = begin_free_transform(self.live.canvas, [self.layers.selected_id()], session.sampling, session.box)
self.live.touch()
## The transform's sampling (0 nearest, 1 smooth, 2 high quality), kept for the next.
pub func set_transform_sampling(self, sampling: int) -> unit!:
self.transform_sampling = sampling
if let session = self.transform:
session.set_sampling(sampling)
self.live.touch()
## The box the Move tool shows with Show Transform Controls, measured again
## only after a change; none when it is not shown or nothing is painted.
pub func controls_box(self) -> TransformBox?!:
if not self.show_transform_controls or self.tools.tool != "move" or self.transform != none or self.image.crop_box != none:
return none
if self.controls_revision != self.live.revision:
self.controls_cache = box_around(self.live.canvas, self.layers.moving_ids())
self.controls_revision = self.live.revision
return self.controls_cache
# -- Warps.
## Edit › Transform › Warp (or, `puppet`, Edit › Puppet Warp) of the selected
## layer's painted pixels (the puppet's mesh at `density`: 0 fewer, 1 normal,
## 2 more points), shown live until `finish_mesh_warp`. False when something
## else is under way.
pub func begin_mesh_warp(self, puppet: bool, density: int = 1) -> bool!:
let canvas = self.live.canvas
if self.mesh_warp != none or self.transform != none or canvas.previewing():
return false
if puppet:
canvas.begin_puppet(self.layers.selected_id(), density)
else:
canvas.begin_warp(self.layers.selected_id())
let session = MeshWarp(canvas, puppet) catch failure:
canvas.end_mesh_warp(false)
error(failure.code, failure.message)
self.mesh_warp = session
self.live.touch()
return true
## Keep the warp as one undo step (Enter), or drop it (Esc).
pub func finish_mesh_warp(self, keep: bool) -> unit!:
if let session = self.mesh_warp:
self.mesh_warp = none
session.finish(keep)
self.live.touch()
# -- Flip.
## Edit › Transform › Flip: the selected layer mirrored about its painted middle.
pub func flip_layer(self, horizontal: bool) -> unit!:
self.live.canvas.flip_layer(self.layers.selected_id(), horizontal)
self.live.touch()