dymokomi/luced-2d
Source of release 0.5.0
## The tool in hand and every tool's options: the brush, the retouching and
## gradient tools' modes, the shape tools' fill and stroke, the magic wand's
## tolerance, and the selection and Text tools' option sets. Nothing here
## touches the document; the workspace's parts read these as they work.
from brush import Brush, preset
from selection_options import SelectionOptions
from type_options import TypeOptions
from live import Live
from luce_std import math
pub class ToolState:
let live: Live
## The active tool, a `tools()` id, and whether the brush erases (the eraser).
pub var tool: str = "brush"
pub var erasing: bool = false
## Spring-loaded tools, as in Photoshop: the tool before the last switch,
## and whether the current one has been used on the canvas since. Holding a
## tool's key while using it returns to the previous tool on release.
pub var tool_before: str = "brush"
pub var tool_used: bool = false
pub var brush: Brush
## The Move tool's Auto-Select: a press picks the top layer with pixels under it.
pub var auto_select: bool = false
## The retouching tools' options: the Clone Stamp keeps its first offset
## (aligned) and samples this layer or all; the Smudge tool smudges (0),
## blurs (1) or pushes as Liquify does (2).
pub var clone_aligned: bool = true
pub var clone_all: bool = false
pub var smear_mode: int = 0
## Smudge's dab spacing, a fraction of the diameter: its own, not the brush's
## (from about 0.2 the dabs show as beads along a soft stroke).
pub var smudge_spacing: float = 0.15
## The Gradient tool's options: radial rather than linear, the colors
## reversed, and from the foreground to transparent instead of to the
## background.
pub var gradient_radial: bool = false
pub var gradient_reverse: bool = false
pub var gradient_transparent: bool = false
## Magic wand settings: color tolerance 0..255 and whether the pixels must connect.
pub var tolerance: float = 32.0
pub var contiguous: bool = true
## Dodge and Burn: 0 shadows, 1 midtones, 2 highlights, and the exposure 0..1.
pub var tone_range: int = 1
pub var tone_exposure: float = 0.5
## The Line tool's weight in pixels and the Polygon tool's sides.
pub var shape_weight: float = 4.0
pub var shape_sides: int = 5
## Shapes are filled (foreground) and stroked (background, this wide);
## a rectangle's corners rounded by the radius.
pub var shape_fill: bool = true
pub var shape_stroke: bool = false
pub var shape_stroke_width: float = 2.0
pub var shape_radius: float = 0.0
## The selection tools' options (selection_options.luc), and the Text tool's.
pub let selecting: SelectionOptions
pub let typing: TypeOptions
## Told of every tool chosen, to let go of what the tool before had under
## way (a view's half-drawn lasso or path).
var listeners: list[func(str)]
pub func init(self, live: Live):
self.live = live
self.brush = Brush()
self.selecting = SelectionOptions()
self.typing = TypeOptions()
self.listeners = []
## Tell `listener` of every tool chosen from now on.
pub func on_choose(self, listener: func(str)):
self.listeners.append(listener)
## Make `tool` (a `tools()` id) active. The workspace's `choose_tool` also
## settles what goes with the tool (the Crop box, a live gradient).
pub func choose(self, tool: str):
if tool != self.tool:
self.tool_before = self.tool
self.tool_used = false
self.tool = tool
self.erasing = tool == "eraser"
for listener in self.listeners:
listener(tool)
self.live.touch()
## Start again from brush preset `name`, keeping the size and opacity.
pub func apply_preset(self, name: str) -> unit!:
self.brush = preset(name, self.brush.diameter, self.brush.opacity)
self.live.touch()
## [ and ]: the brush size by `factor`.
pub func scale_brush(self, factor: float):
self.brush.scale(factor)
self.live.touch()
## Shift ] and Shift [: the brush's hardness by `step`, within 0..1.
pub func harden_brush(self, step: float):
self.brush.hardness = math.max(0.0, math.min(1.0, self.brush.hardness + step))
self.live.touch()
## The canvas's retouching tool for the active tool: 0 plain paint, 1 clone
## stamp, 2 spot healing, 3 blur, 4 smudge, 5 push, 6 dodge, 7 burn.
pub func retouch_kind(self) -> int:
if self.tool == "clone":
return 1
if self.tool == "heal":
return 2
if self.tool == "smear":
return 4 if self.smear_mode == 0 else (3 if self.smear_mode == 1 else 5)
if self.tool == "dodge":
return 6
if self.tool == "burn":
return 7
return 0
## Tab: the active tool's next mode — a selection tool's new, add,
## subtract, intersect; the Smudge tool's smudge, blur, liquify; the
## gradient's linear or radial; the Clone Stamp's
## sample; the Move tool's auto-select; the brush and the eraser swap.
pub func cycle_tool_mode(self) -> unit!:
let tool = self.tool
if tool == "marquee" or tool == "ellipse" or tool == "lasso" or tool == "polygon" or tool == "magnetic" or tool == "wand":
self.selecting.mode = (self.selecting.mode + 1) % 4
elif tool == "smear":
self.smear_mode = (self.smear_mode + 1) % 3
elif tool == "dodge" or tool == "burn":
self.tone_range = (self.tone_range + 1) % 3
elif tool == "gradient":
self.gradient_radial = not self.gradient_radial
elif tool == "clone":
self.clone_all = not self.clone_all
elif tool == "move":
self.auto_select = not self.auto_select
elif tool == "brush" or tool == "eraser":
self.tool = "eraser" if tool == "brush" else "brush"
self.erasing = self.tool == "eraser"
self.live.touch()