dymokomi/luced-2d
Source of release 0.5.1
## The shape tools, as Photoshop's shape group: Rectangle, Ellipse, Line and
## Polygon, each dragged out on the canvas and drawn in the foreground color
## on a new shape layer of its own. Shift keeps a rectangle square, an ellipse round
## and a line at a multiple of 45°; Alt draws a rectangle, ellipse or polygon
## out from its centre. The line's weight and the polygon's sides are the
## header's, as are the fill, the stroke and a rectangle's corner radius.
## Shape layers are vector layers (luce-image's shape layers): they stay
## crisp through moves and free transforms until painted on.
from luce_ui.ui import Painter, Point
from luce_gpu.gpu import Color
from workspace import Workspace
from luce_image.canvas import Canvas
from vectors import vector_layer
from luce_std import math
## The shape tools' ids, in the group's order.
pub func shape_tools() -> list[str]:
return ["rectangle", "shape_ellipse", "line", "shape_polygon"]
pub func is_shape(tool: str) -> bool:
return tool in shape_tools()
## A shape being dragged out, in document pixels.
pub class ShapeDrag:
pub var active: bool = false
var tool: str = ""
var start_x: float = 0.0
var start_y: float = 0.0
var end_x: float = 0.0
var end_y: float = 0.0
var square: bool = false
var centered: bool = false
pub func init(self):
self.active = false
pub func begin(self, tool: str, x: float, y: float):
self.active = true
self.tool = tool
self.start_x = x
self.start_y = y
self.end_x = x
self.end_y = y
self.square = false
self.centered = false
pub func drag(self, x: float, y: float, shift: bool, alt: bool):
self.end_x = x
self.end_y = y
self.square = shift
self.centered = alt
pub func cancel(self):
self.active = false
## Let go: the shape drawn, unless it is too small to be one.
pub func finish(self, workspace: Workspace) -> unit!:
if not self.active:
return
self.active = false
let box = self.box()
if self.tool == "line":
let end = self.line_end()
if math.hypot(end.x - self.start_x, end.y - self.start_y) < 1.0:
return
draw_line(workspace, self.start_x, self.start_y, end.x, end.y)
return
if box[2] < 1.0 or box[3] < 1.0:
return
draw_shape(workspace, self.tool, box[0], box[1], box[2], box[3])
## The shape's box, left, top, width and height, with Shift and Alt applied.
func box(self) -> list[float]:
var dx = self.end_x - self.start_x
var dy = self.end_y - self.start_y
if self.square:
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
if self.centered:
return [self.start_x - math.abs(dx), self.start_y - math.abs(dy), math.abs(dx) * 2.0, math.abs(dy) * 2.0]
return [math.min(self.start_x, self.start_x + dx), math.min(self.start_y, self.start_y + dy), math.abs(dx), math.abs(dy)]
## The line's end, turned to the nearest 45° with Shift.
func line_end(self) -> Point:
if not self.square:
return Point(self.end_x, self.end_y)
let dx = self.end_x - self.start_x
let dy = self.end_y - self.start_y
let length = math.hypot(dx, dy)
let step = math.pi / 4.0
let angle = math.round(math.atan2(dy, dx) / step) * step
return Point(self.start_x + math.cos(angle) * length, self.start_y + math.sin(angle) * length)
## The outline while it is dragged, in view points: dark under light.
pub func draw(self, painter: Painter, workspace: Workspace, zoom: float, left: float, top: float, ink: Color, under: Color) -> unit!:
if not self.active:
return
let points = self.outline(workspace)
var index = 0
while index + 1 < points.length:
let a = Point(left + points[index].x * zoom, top + points[index].y * zoom)
let b = Point(left + points[index + 1].x * zoom, top + points[index + 1].y * zoom)
painter.line(a, b, 3.0, under)
painter.line(a, b, 1.0, ink)
index += 1
## The outline in document pixels, closed for the closed shapes.
func outline(self, workspace: Workspace) -> list[Point]:
if self.tool == "line":
return [Point(self.start_x, self.start_y), self.line_end()]
let box = self.box()
if self.tool == "rectangle":
return [Point(box[0], box[1]), Point(box[0] + box[2], box[1]), Point(box[0] + box[2], box[1] + box[3]), Point(box[0], box[1] + box[3]), Point(box[0], box[1])]
let sides = 48 if self.tool == "shape_ellipse" else workspace.tools.shape_sides
return ring(box, sides)
## `sides` corners around the ellipse in `box`, the first at the top, closed.
func ring(box: list[float], sides: int) -> list[Point]:
var points: list[Point] = []
var index = 0
while index <= sides:
let angle = -math.pi / 2.0 + float(index) * 2.0 * math.pi / float(sides)
points.append(Point(box[0] + box[2] * 0.5 + math.cos(angle) * box[2] * 0.5, box[1] + box[3] * 0.5 + math.sin(angle) * box[3] * 0.5))
index += 1
return points
## A rectangle, ellipse or polygon in (`x`, `y`, `width`, `height`) on a
## new shape layer named after it, as one undo step: a vector layer that
## stays crisp however it is moved or scaled, filled with the foreground
## color and stroked, when the header says so, with the background color.
## With a vector layer selected it is one more element of that layer instead.
pub func draw_shape(workspace: Workspace, tool: str, x: float, y: float, width: float, height: float) -> unit!:
let name = "Rectangle" if tool == "rectangle" else ("Ellipse" if tool == "shape_ellipse" else "Polygon")
let kind = 0 if tool == "rectangle" else (1 if tool == "shape_ellipse" else 3)
let canvas = workspace.canvas
if vector_layer(workspace):
canvas.add_shape(workspace.layers.selected_id(), kind, x, y, width, height, workspace.tools.shape_sides, workspace.tools.shape_radius,
workspace.tools.shape_fill, workspace.colors.red, workspace.colors.green, workspace.colors.blue,
workspace.tools.shape_stroke, workspace.colors.back_red, workspace.colors.back_green, workspace.colors.back_blue, workspace.tools.shape_stroke_width)
workspace.touch()
return
workspace.edits.as_one(name, func () -> unit!:
workspace.edits.add_layer()
workspace.edits.rename(name)
canvas.set_shape(workspace.layers.selected_id(), kind, x, y, width, height, workspace.tools.shape_sides, workspace.tools.shape_radius,
workspace.tools.shape_fill, workspace.colors.red, workspace.colors.green, workspace.colors.blue,
workspace.tools.shape_stroke, workspace.colors.back_red, workspace.colors.back_green, workspace.colors.back_blue, workspace.tools.shape_stroke_width))
workspace.touch()
## A straight line of the header's weight in the foreground color, on a new
## shape layer, or on the selected vector layer.
pub func draw_line(workspace: Workspace, x0: float, y0: float, x1: float, y1: float) -> unit!:
let canvas = workspace.canvas
if vector_layer(workspace):
canvas.add_shape(workspace.layers.selected_id(), 2, x0, y0, x1 - x0, y1 - y0, workspace.tools.shape_sides, 0.0,
false, 0.0, 0.0, 0.0, true, workspace.colors.red, workspace.colors.green, workspace.colors.blue, workspace.tools.shape_weight)
workspace.touch()
return
workspace.edits.as_one("Line", func () -> unit!:
workspace.edits.add_layer()
workspace.edits.rename("Line")
canvas.set_shape(workspace.layers.selected_id(), 2, x0, y0, x1 - x0, y1 - y0, workspace.tools.shape_sides, 0.0,
false, 0.0, 0.0, 0.0, true, workspace.colors.red, workspace.colors.green, workspace.colors.blue, workspace.tools.shape_weight))
workspace.touch()
## A selected shape layer's options, as the header shows them: the stroke
## width in document pixels, the corner radius scaled with the shape.
pub class ShapeOptions:
pub let line: bool
pub let fill: bool
pub let stroke: bool
pub let stroke_width: float
pub let radius: float
pub let sides: int
pub func init(self, line: bool, fill: bool, stroke: bool, stroke_width: float, radius: float, sides: int):
self.line = line
self.fill = fill
self.stroke = stroke
self.stroke_width = stroke_width
self.radius = radius
self.sides = sides
## The selected layer's shape options; none when it is not a shape layer.
pub func selected_shape_options(workspace: Workspace) -> ShapeOptions?:
let canvas = workspace.canvas
let index = workspace.layers.selected
if index < 0 or index >= workspace.canvas.layer_count():
return none
let options = read_shape_options(canvas, index) catch _:
return none
return options
func read_shape_options(canvas: Canvas, index: int) -> ShapeOptions?!:
if not canvas.layer_shaped(index):
return none
return ShapeOptions(canvas.layer_shape_value(index, 0) == 2.0, canvas.layer_shape_value(index, 7) != 0.0, canvas.layer_shape_value(index, 11) != 0.0,
round_tenth(canvas.layer_shape_value(index, 15)), round_tenth(canvas.layer_shape_value(index, 6) * shape_scale(canvas, index)),
int(canvas.layer_shape_value(index, 5)))
## The mean factor a shape layer's matrix scales by.
func shape_scale(canvas: Canvas, index: int) -> float!:
let a = canvas.layer_shape_value(index, 16)
let b = canvas.layer_shape_value(index, 17)
let c = canvas.layer_shape_value(index, 18)
let d = canvas.layer_shape_value(index, 19)
return math.sqrt(math.abs(a * d - b * c))
func round_tenth(value: float) -> float:
return math.round(value * 10.0) / 10.0
## The header's shape options changed: the selected shape layer takes them.
pub func restyle_selected(workspace: Workspace) -> unit!:
let canvas = workspace.canvas
let index = workspace.layers.selected
if index < 0 or index >= workspace.canvas.layer_count() or not canvas.layer_shaped(index):
return
let scale = shape_scale(canvas, index)
let radius = workspace.tools.shape_radius / scale if scale > 1e-9 else workspace.tools.shape_radius
let line = canvas.layer_shape_value(index, 0) == 2.0
if line:
canvas.restyle_shape(workspace.layers.selected_id(), false, 0.0, 0.0, 0.0, true, canvas.layer_shape_value(index, 12), canvas.layer_shape_value(index, 13),
canvas.layer_shape_value(index, 14), workspace.tools.shape_weight, 0.0, workspace.tools.shape_sides)
else:
canvas.restyle_shape(workspace.layers.selected_id(), workspace.tools.shape_fill, canvas.layer_shape_value(index, 8), canvas.layer_shape_value(index, 9),
canvas.layer_shape_value(index, 10), workspace.tools.shape_stroke, canvas.layer_shape_value(index, 12), canvas.layer_shape_value(index, 13),
canvas.layer_shape_value(index, 14), workspace.tools.shape_stroke_width, radius, workspace.tools.shape_sides)
workspace.touch()