dymokomi/luced-2d
Source of release 0.5.0
## The Crop tool's box, as in Photoshop: it starts around the selection, or
## else the whole canvas; its eight handles and edges pull in (or out, past
## the canvas, to grow it), a drag inside moves it, and a drag outside draws a
## new one. A ratio (the header's) holds its proportions, as Shift does a
## corner's; Alt pulls both sides about the middle. Enter crops to it; what is
## outside is shaded. Everything is in whole document pixels.
from luce_ui.ui import Painter, Point
from luce_gpu.gpu import Rect, Color
from luce_std import math
pub let crop_none: int = -1
## 0..7 are the handles, clockwise from the top-left corner; then these.
pub let crop_move: int = 8
pub let crop_new: int = 9
pub class CropBox:
pub var left: float
pub var top: float
pub var right: float
pub var bottom: float
## Width over height to keep, or 0 for any.
pub var ratio: float = 0.0
## The drag under way: what was grabbed, where, and the box as it was.
pub var grabbed: int = -1
var start_x: float = 0.0
var start_y: float = 0.0
var from_left: float = 0.0
var from_top: float = 0.0
var from_right: float = 0.0
var from_bottom: float = 0.0
## A box `width` by `height` at (`left`, `top`).
pub func init(self, width: float, height: float, left: float = 0.0, top: float = 0.0):
self.left = left
self.top = top
self.right = left + width
self.bottom = top + height
## Keep `ratio` (0: any) from now on: the box becomes the largest of that
## shape inside it, about its middle.
pub func set_ratio(self, ratio: float):
self.ratio = ratio
if ratio <= 0.0:
return
let wide = self.right - self.left
let tall = self.bottom - self.top
let middle_x = (self.left + self.right) * 0.5
let middle_y = (self.top + self.bottom) * 0.5
var new_wide = wide
var new_tall = math.round(wide / ratio)
if new_tall > tall:
new_tall = tall
new_wide = math.round(tall * ratio)
self.left = math.round(middle_x - new_wide * 0.5)
self.right = self.left + math.max(1.0, new_wide)
self.top = math.round(middle_y - new_tall * 0.5)
self.bottom = self.top + math.max(1.0, new_tall)
pub func width(self) -> int:
return int(self.right - self.left)
pub func height(self) -> int:
return int(self.bottom - self.top)
func handle_x(self, index: int) -> float:
if index == 0 or index == 6 or index == 7:
return self.left
if index == 1 or index == 5:
return (self.left + self.right) * 0.5
return self.right
func handle_y(self, index: int) -> float:
if index <= 2:
return self.top
if index == 3 or index == 7:
return (self.top + self.bottom) * 0.5
return self.bottom
## What a press at document point (`x`, `y`) grabs, with handles `reach`
## document pixels about.
pub func hit(self, x: float, y: float, reach: float) -> int:
var index = 0
while index < 8:
if math.abs(x - self.handle_x(index)) <= reach and math.abs(y - self.handle_y(index)) <= reach:
return index
index += 1
if x >= self.left and x <= self.right and y >= self.top and y <= self.bottom:
return crop_move
return crop_new
pub func begin(self, grab: int, x: float, y: float):
self.grabbed = grab
self.start_x = math.round(x)
self.start_y = math.round(y)
self.from_left = self.left
self.from_top = self.top
self.from_right = self.right
self.from_bottom = self.bottom
pub func end(self):
self.grabbed = crop_none
# A box pulled inside out, or to nothing, is put right.
let left = math.min(self.left, self.right)
let right = math.max(self.left, self.right)
let top = math.min(self.top, self.bottom)
let bottom = math.max(self.top, self.bottom)
self.left = left
self.right = math.max(right, left + 1.0)
self.top = top
self.bottom = math.max(bottom, top + 1.0)
## The pointer at document point (`x`, `y`); Shift keeps a corner's
## proportions, Alt pulls about the middle.
pub func drag(self, x: float, y: float, shift: bool, alt: bool = false):
let px = math.round(x)
let py = math.round(y)
let grab = self.grabbed
if grab == crop_move:
let dx = px - self.start_x
let dy = py - self.start_y
self.left = self.from_left + dx
self.right = self.from_right + dx
self.top = self.from_top + dy
self.bottom = self.from_bottom + dy
elif grab == crop_new:
var dx = px - self.start_x
var dy = py - self.start_y
if self.ratio > 0.0:
dy = math.round(math.abs(dx) / self.ratio) * (1.0 if dy >= 0.0 else -1.0)
elif shift:
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
# With Alt the press is the middle, not a corner.
self.left = self.start_x - dx if alt else self.start_x
self.top = self.start_y - dy if alt else self.start_y
self.right = self.start_x + dx
self.bottom = self.start_y + dy
elif grab >= 0 and grab < 8:
self.left = self.from_left
self.top = self.from_top
self.right = self.from_right
self.bottom = self.from_bottom
let corner = grab == 0 or grab == 2 or grab == 4 or grab == 6
let moves_left = grab == 0 or grab == 6 or grab == 7
let moves_right = grab == 2 or grab == 3 or grab == 4
let moves_top = grab <= 2
let moves_bottom = grab >= 4 and grab <= 6
if moves_left:
self.left = px
if moves_right:
self.right = px
if moves_top:
self.top = py
if moves_bottom:
self.bottom = py
# Alt: the opposite side moves as far the other way.
if alt:
if moves_left:
self.right = self.from_right + (self.from_left - self.left)
if moves_right:
self.left = self.from_left - (self.right - self.from_right)
if moves_top:
self.bottom = self.from_bottom + (self.from_top - self.top)
if moves_bottom:
self.top = self.from_top - (self.bottom - self.from_bottom)
let wide = self.from_right - self.from_left
let tall = self.from_bottom - self.from_top
let keep = self.ratio if self.ratio > 0.0 else (wide / tall if corner and shift and tall > 0.0 else 0.0)
if keep > 0.0:
if moves_top or moves_bottom and not (moves_left or moves_right):
self.fit_width(keep, moves_left, moves_right)
else:
self.fit_height(keep, moves_top, moves_bottom, alt)
## The height follows the width at `ratio`: from the edge not dragged, or
## about the middle when neither (an edge handle) or Alt.
func fit_height(self, ratio: float, moves_top: bool, moves_bottom: bool, alt: bool):
let new_tall = math.round(math.abs(self.right - self.left) / ratio)
if moves_top and not alt:
self.top = self.bottom - new_tall
elif moves_bottom and not alt:
self.bottom = self.top + new_tall
else:
let middle = math.round((self.from_top + self.from_bottom) * 0.5)
self.top = middle - math.round(new_tall * 0.5)
self.bottom = self.top + new_tall
## The width follows the height at `ratio`, about the middle (a top or
## bottom edge handle dragged).
func fit_width(self, ratio: float, moves_left: bool, moves_right: bool):
_ = moves_left
_ = moves_right
let new_wide = math.round(math.abs(self.bottom - self.top) * ratio)
let middle = math.round((self.from_left + self.from_right) * 0.5)
self.left = middle - math.round(new_wide * 0.5)
self.right = self.left + new_wide
## The shade outside the box, the box with its thirds, and the handles,
## for a view showing `size` points of the document at `zoom` with its
## corner at (`left`, `top`).
pub func draw(self, painter: Painter, width: float, height: float, zoom: float, left: float, top: float, ink: Color, backing: Color) -> unit!:
let x0 = math.min(self.left, self.right) * zoom + left
let y0 = math.min(self.top, self.bottom) * zoom + top
let x1 = math.max(self.left, self.right) * zoom + left
let y1 = math.max(self.top, self.bottom) * zoom + top
let shade = Color(0.0, 0.0, 0.0, 0.55)
painter.rectangle(Rect(0.0, 0.0, width, math.max(0.0, y0)), shade)
painter.rectangle(Rect(0.0, y1, width, math.max(0.0, height - y1)), shade)
painter.rectangle(Rect(0.0, y0, math.max(0.0, x0), y1 - y0), shade)
painter.rectangle(Rect(x1, y0, math.max(0.0, width - x1), y1 - y0), shade)
# The rule of thirds, faintly.
let faint = Color(ink.red, ink.green, ink.blue, 0.35)
var third = 1
while third < 3:
let tx = x0 + (x1 - x0) * float(third) / 3.0
let ty = y0 + (y1 - y0) * float(third) / 3.0
painter.rectangle(Rect(math.floor(tx), y0, 1.0, y1 - y0), faint)
painter.rectangle(Rect(x0, math.floor(ty), x1 - x0, 1.0), faint)
third += 1
let corners = [Point(x0, y0), Point(x1, y0), Point(x1, y1), Point(x0, y1)]
var index = 0
while index < 4:
let a = corners[index]
let b = corners[(index + 1) % 4]
painter.line(a, b, 3.0, backing)
painter.line(a, b, 1.0, ink)
index += 1
index = 0
while index < 8:
let hx = self.handle_x(index) * zoom + left
let hy = self.handle_y(index) * zoom + top
painter.rectangle(Rect(math.floor(hx) - 4.0, math.floor(hy) - 4.0, 9.0, 9.0), ink)
painter.rectangle(Rect(math.floor(hx) - 3.0, math.floor(hy) - 3.0, 7.0, 7.0), backing)
index += 1