Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.1

0.11.1 / src / tools.luc

## Viewport tools as verb runs: what a tool button does to the selection,
## whichever way it lands (a step of the active Edit node, or a new node after
## the displayed one). The Edit node's tool amount sets the one number the
## simple tools take; everything else is the verb's default.
from luce_geocore.geocore import Verbs
from luce_std import math

## A tool's verb and its parameter values (every slot, defaults filled).
pub class ToolRun:
    pub let verb: str
    pub let numbers: list[float]

    pub func init(self, verb: str, numbers: list[float]):
        self.verb = verb
        self.numbers = numbers

## The run of viewport tool `tool` with the tool amount `amount`, or none.
pub func tool_run(tool: str, amount: float) -> ToolRun?:
    if tool == "Rotate":
        return made("Transform Components", [(4, amount)])
    if tool == "Scale":
        return made("Transform Components", [(6, amount), (7, amount), (8, amount)])
    if tool == "Noise":
        return made("Mountain", [(0, amount)])
    if tool in ["Extrude", "PolyExtrude"]:
        return made("PolyExtrude", [(0, amount)])
    if tool in ["Inset", "Duplicate", "Smooth", "Peak", "Snap", "Fuse"]:
        return made(tool, [(0, amount)])
    if tool == "Merge Points":
        return made("Merge Points", [])
    if tool == "Bridge":
        return made("Bridge", [])
    if tool == "Loop Cut":
        return made("Loop Cut", [])
    # A knife cut across the selection: Clip keeping both sides, through
    # its centroid, across X.
    if tool == "Knife":
        return made("Clip", [(0, 0.0), (2, 1.0), (3, 2.0)])
    # The tool amount places Connect's new points along their edges and
    # sets how far Edge Slide slides.
    if tool == "Connect":
        return made("Connect", [(0, math.max(0.01, math.min(0.99, amount)))])
    if tool == "Edge Slide":
        return made("Edge Slide", [(0, math.max(-1.0, math.min(1.0, amount)))])
    # PolyMirror and Spin with their defaults (across X through the origin;
    # a full turn about Y in 12 steps); the tool amount is Crease's
    # sharpness; PolyDraw makes a face through the selected points.
    if tool in ["PolyMirror", "Spin", "PolyDraw"]:
        return made(tool, [])
    if tool == "Crease":
        return made("Crease", [(0, math.max(0.0, math.min(10.0, amount)))])
    if tool in ["Bevel", "PolyBevel"]:
        return made("PolyBevel", [(0, amount)])
    if tool in ["Reverse", "Triangulate", "Split", "Fill", "Dissolve", "Flatten", "Delete", "Subdivide", "Clean"]:
        return made(tool, [])
    return none

func made(verb: str, overrides: list[(int, float)]) -> ToolRun:
    let numbers = verb_defaults(verb)
    for (slot, value) in overrides:
        if slot < numbers.length: numbers[slot] = value
    return ToolRun(verb, numbers)

## A verb's initial parameter values, in slot order (vectors per component).
pub func verb_defaults(verb: str) -> list[float]:
    let index = Verbs.index_of(verb)
    let result: list[float] = []
    if index < 0: return result
    for parm in 0..<Verbs.parm_count(index):
        for component in 0..<Verbs.parm_components(index, parm):
            result.append(Verbs.parm_initial(index, parm))
    return result