Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-2d

Source of release 0.5.0

0.5.0 / src / tools.luc

## Every tool on the rail, in rail order, in one table: the id the workspace
## keeps, the menu name and the shorter title the header shows, its
## luciaos-assets icon, its key (with Shift for the second tool on a key, as
## Photoshop's Shift-M), and the hint the status line shows while it is active.
from luce_window.input import Key
from luce_ui import ui

pub class Tool:
    pub let id: str
    pub let name: str
    pub let title: str
    pub let icon: str
    pub let key: Key
    pub let shift: bool
    pub let hint: str

    pub func init(self, id: str, name: str, title: str, icon: str, key: Key, hint: str, shift: bool = false):
        self.id = id
        self.name = name
        self.title = title
        self.icon = icon
        self.key = key
        self.shift = shift
        self.hint = hint

pub func tools() -> list[Tool]!:
    # The primary modifier as the platform names it: cmd on macOS, ctrl elsewhere.
    let command = ui.primary_key_name().lower()
    return [Tool("move", "Move", "Move", "move-tool", Key.v, f"Drag moves the layer or selection · alt copies · shift locks axis · {command}-click picks"),
            Tool("marquee", "Rectangular Marquee", "Marquee", "selection-tool", Key.m,
                 "Drag to select · shift adds, alt subtracts, both intersect · drag inside to move the outline · shift while dragging squares, alt centers"),
            Tool("ellipse", "Elliptical Marquee", "Ellipse", "ellipse-select-tool", Key.m,
                 f"Drag to select · shift adds, alt subtracts · shift while dragging makes a circle, alt centers · {command}-D deselects", shift = true),
            Tool("lasso", "Lasso", "Lasso", "lasso-tool", Key.l, f"Drag around the area to select · shift adds · alt subtracts · {command}-D deselects"),
            Tool("polygon", "Polygonal Lasso", "Polygonal Lasso", "polygon-select-tool", Key.l,
                 "Click the corners · click the first again, double-click or Enter closes · Delete takes one back · Esc cancels", shift = true),
            Tool("wand", "Magic Wand", "Magic Wand", "magic-wand-tool", Key.w, "Click a color to select it · shift adds · alt subtracts"),
            Tool("crop", "Crop", "Crop", "crop-tool", Key.c, "Drag an edge or corner · Alt: about the middle · inside moves · outside draws anew · Enter crops · Esc resets"),
            Tool("text", "Text", "Text", "text-tool", Key.t, "Click where the text starts, then type it"),
            Tool("eyedropper", "Eyedropper", "Eyedropper", "eyedropper-tool", Key.i, "Click a color to make it the foreground color"),
            Tool("gradient", "Gradient", "Gradient", "gradient-tool", Key.g, "Drag from the foreground color to the background color · fills the selection"),
            Tool("brush", "Brush", "Brush", "brush-tool", Key.b, "Drag to paint · [ ] size · Space or the middle button pans · the wheel zooms, shift-wheel pans"),
            Tool("pencil", "Pencil", "Pencil", "pencil-tool", Key.b, "Drag to draw hard-edged, whole pixels · [ ] size · shift-click draws a line", shift = true),
            Tool("eraser", "Eraser", "Eraser", "eraser-tool", Key.e, "Drag to erase · [ ] size · Space to pan"),
            Tool("clone", "Clone Stamp", "Clone Stamp", "clone-stamp", Key.s, "Alt-click where to copy from · drag to stamp it · [ ] size · digits set opacity"),
            Tool("heal", "Spot Healing Brush", "Spot Healing", "healing-brush", Key.j, "Brush over a spot: it is filled from the picture beside it · [ ] size"),
            Tool("dodge", "Dodge", "Dodge", "dodge-tool", Key.o, "Brush to lighten · the range and exposure in the header · Tab steps the range · [ ] size"),
            Tool("burn", "Burn", "Burn", "burn-tool", Key.o, "Brush to darken · the range and exposure in the header · Tab steps the range · [ ] size", shift = true),
            Tool("smear", "Smudge", "Smudge", "smudge-tool", Key.r, "Drag to smudge, blur or push pixels (the mode in the header) · digits set strength"),
            Tool("rectangle", "Rectangle", "Rectangle", "rectangle-tool", Key.u, "Drag a rectangle onto a layer of its own · shift squares it · alt draws from the centre"),
            Tool("shape_ellipse", "Ellipse", "Ellipse", "ellipse-tool", Key.u, "Drag an ellipse onto a layer of its own · shift makes a circle · alt draws from the centre", shift = true),
            Tool("line", "Line", "Line", "line-tool", Key.unknown, "Drag a line of the header's weight onto a layer of its own · shift keeps it to 45°"),
            Tool("shape_polygon", "Polygon", "Polygon", "path-tool", Key.unknown, "Drag a polygon of the header's sides onto a layer of its own · shift keeps it even · alt from the centre"),
            Tool("pen", "Pen", "Pen", "pen-tool", Key.p,
                 "Click corners, drag curves · click the first anchor to close · Enter ends · on a path: drag anchors or handles (alt: one handle), click the curve to add, an anchor to remove"),
            Tool("hand", "Hand", "Hand", "pan-tool", Key.h, "Drag to pan"),
            Tool("idle", "Idle", "Idle", "node-tool", Key.a, "A tool that does nothing on the canvas: a safe place to rest · Space pans · Z zooms"),
            Tool("zoom", "Zoom", "Zoom", "zoom-in", Key.z, "Click to zoom in · alt-click to zoom out · drag right or left to zoom · hold Z with any tool"),
            # Last, so the others keep their places; the rail shows it in the lassos' group.
            Tool("magnetic", "Magnetic Lasso", "Magnetic Lasso", "lasso-tool", Key.unknown,
                 "Click, then move along an edge: the wire snaps to it · click adds an anchor · Backspace takes one back · double-click or Enter closes · Esc cancels")]

## The tool whose id is `id`; the brush for an unknown one.
pub func tool_by_id(id: str) -> Tool!:
    for tool in tools():
        if tool.id == id:
            return tool
    return tool_by_id("brush")

## The rail's groups, as Photoshop's: one button shows the group's current
## tool; a long press (or a right-click) on it offers the others. Tools in no
## group have a button of their own.
pub func tool_groups() -> list[list[str]]:
    return [["marquee", "ellipse", "lasso", "polygon", "magnetic"], ["brush", "pencil"], ["dodge", "burn"], ["rectangle", "shape_ellipse", "line", "shape_polygon"]]

## The group holding `tool`, as its index in `tool_groups()`, or -1.
pub func group_of(tool: str) -> int:
    var index = 0
    for group in tool_groups():
        if tool in group:
            return index
        index += 1
    return -1