dymokomi/luced-2d
Source of release 0.5.1
0.5.1 / src / selection_drag.luc
## A marquee or lasso being dragged, in document pixels, and how it combines
## with the selection when let go. As in Photoshop, Shift or Alt held at the
## press picks the mode (add, subtract; both: intersect); pressed during the
## drag, or let go and pressed again, Shift makes the marquee square or
## circular and Alt draws it out from its center.
from luce_ui.ui import Painter, Point
from luce_gpu.gpu import Rect, Color
from workspace import Workspace
from kit import outline
from luce_std import math
pub class SelectionDrag:
pub var active: bool = false
pub var lasso: bool = false
## An elliptical marquee rather than a rectangular one.
pub var ellipse: bool = false
## 0 replace, 1 add, 2 subtract, 3 intersect.
pub var mode: int = 0
## Where the drag started and where the pointer is now.
var start_x: float = 0.0
var start_y: float = 0.0
var now_x: float = 0.0
var now_y: float = 0.0
## The marquee's corners as dragged.
pub var x0: float = 0.0
pub var y0: float = 0.0
pub var x1: float = 0.0
pub var y1: float = 0.0
## Whether Shift and Alt have been up since the press, so pressing them
## again constrains rather than repeating the mode.
var shift_free: bool = false
var alt_free: bool = false
## The modifiers at the last drag, for a drag again as the view scrolls.
var held_shift: bool = false
var held_alt: bool = false
## The lasso's points so far and the last one kept.
var points: int = 0
var last_x: float = 0.0
var last_y: float = 0.0
## Start at document point `x`, `y`; a replacing marquee drops the old selection now.
pub func begin(self, workspace: Workspace, x: float, y: float, lasso: bool, ellipse: bool, shift: bool, alt: bool) -> unit!:
self.active = true
self.lasso = lasso
self.ellipse = ellipse
self.mode = workspace.tools.selecting.mode_for(shift, alt)
# The marquee snaps to whole pixels, so a square stays square.
self.start_x = x if lasso else math.floor(x + 0.5)
self.start_y = y if lasso else math.floor(y + 0.5)
self.shift_free = not shift
self.alt_free = not alt
self.shape_to(self.start_x, self.start_y, shift, alt)
if lasso:
workspace.canvas.clear_points()
workspace.canvas.add_point(self.start_x, self.start_y)
self.points = 1
self.last_x = self.start_x
self.last_y = self.start_y
elif self.mode == 0:
workspace.selection.deselect()
## The pointer at document point `x`, `y` with the modifiers held.
pub func drag(self, workspace: Workspace, x: float, y: float, shift: bool, alt: bool) -> unit!:
self.held_shift = shift
self.held_alt = alt
self.shape_to(x, y, shift, alt)
if not self.lasso:
return
# The lasso keeps a point every two pixels of travel.
let dx = x - self.last_x
let dy = y - self.last_y
if dx * dx + dy * dy >= 4.0:
workspace.canvas.add_point(x, y)
self.points += 1
self.last_x = x
self.last_y = y
## The pointer, still, at a document point the view scrolled under it.
pub func drag_again(self, workspace: Workspace, x: float, y: float) -> unit!:
self.drag(workspace, x, y, self.held_shift, self.held_alt)
## Shift or Alt pressed or let go without moving: reshape the marquee at once.
pub func modifiers(self, shift: bool, alt: bool):
if self.active and not self.lasso:
self.shape_to(self.now_x, self.now_y, shift, alt)
## Let go at document point `x`, `y`: combine the shape with the selection.
pub func finish(self, workspace: Workspace, x: float, y: float, shift: bool, alt: bool) -> unit!:
self.active = false
if self.lasso:
if self.points >= 3:
workspace.tools.selecting.apply(workspace.canvas, false)
workspace.canvas.select_polygon(self.mode)
workspace.touch()
else:
workspace.canvas.clear_points()
return
self.shape_to(x, y, shift, alt)
workspace.selection.select_shape(self.x0, self.y0, self.x1, self.y1, self.ellipse, self.mode)
## The marquee's outline while dragged, plainly, at screen resolution: the
## rectangle, or the ellipse inside it as a polygon fine enough to look round.
pub func draw(self, painter: Painter, zoom: float, left: float, top: float, ink: Color) -> unit!:
if not self.active or self.lasso:
return
let sx0 = math.min(self.x0, self.x1) * zoom + left
let sy0 = math.min(self.y0, self.y1) * zoom + top
let sx1 = math.max(self.x0, self.x1) * zoom + left
let sy1 = math.max(self.y0, self.y1) * zoom + top
if not self.ellipse:
outline(painter, Rect(sx0, sy0, sx1 - sx0, sy1 - sy0), ink)
return
let cx = (sx0 + sx1) * 0.5
let cy = (sy0 + sy1) * 0.5
let rx = (sx1 - sx0) * 0.5
let ry = (sy1 - sy0) * 0.5
# About one segment per four points of circumference, 16 at least.
let segments = int(math.max(16.0, math.min(256.0, (rx + ry) * 1.6)))
var previous = Point(cx + rx, cy)
for index in 1..<segments + 1:
let angle = 2.0 * math.pi * float(index) / float(segments)
let next = Point(cx + rx * math.cos(angle), cy + ry * math.sin(angle))
painter.line(previous, next, 1.0, ink)
previous = next
func shape_to(self, x: float, y: float, shift: bool, alt: bool):
if not shift:
self.shift_free = true
if not alt:
self.alt_free = true
var dx = x - self.start_x
var dy = y - self.start_y
if not self.lasso:
dx = math.floor(dx + 0.5)
dy = math.floor(dy + 0.5)
if shift and self.shift_free:
let side = math.max(math.abs(dx), math.abs(dy))
dx = side if dx >= 0.0 else -side
dy = side if dy >= 0.0 else -side
let centered = alt and self.alt_free
self.x0 = self.start_x - dx if centered else self.start_x
self.y0 = self.start_y - dy if centered else self.start_y
self.x1 = self.start_x + dx
self.y1 = self.start_y + dy
self.now_x = x
self.now_y = y
## The selection mode the modifiers held at a press pick: Shift adds, Alt
## subtracts, both intersect.
pub func mode_of(shift: bool, alt: bool) -> int:
if shift and alt:
return 3
return 1 if shift else (2 if alt else 0)