Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / tool_record.luc

## One operation as a tool (docs/research/POLY-MODELING.md §3): the node of
## its kind, the Edit step of its verb, and every menu, marking menu,
## right-click entry, prompt, icon and docs line that reaches it, all from
## this one record. tool_registry.luc lists the tools; mesh tools are built
## from their luce-geocore verbs (verb_tools.luc), CAD tools each declared
## in a tool_<name>.luc of their own (cad_tools.luc lists them).
from geometry_data import GeometryData
from parameter_rows import Parameter
from cad_blends import BlendMemo
import pick_kinds
from luce_cad.model import CadTolerances, MeshQuality

## One selection a tool works on: its first pick is the selection it runs
## on (a node's Group, a step's group); a CAD tool's later picks are what
## it asks for after (Combine's tool bodies, Align's master), each kept by
## name where a node keeps it and where an Edit step keeps it, and asked
## for in order by one prompt chain (tool_picks.luc).
pub class Pick:
    pub let label: str
    ## The selection levels it is picked at (edit_types' object_level,
    ## point_level, …); [] takes every level.
    pub let levels: list[int]
    ## What the editor says while it waits for this pick ("" for the
    ## generated "Select edges first").
    pub let prompt: str
    ## What a click gives (pick_kinds.luc): components (a mesh group), or
    ## for CAD faces, bodies, edges, a point, curves, curve words, ….
    pub let kind: int
    ## Where a node keeps it: its text `slot` (a point: its three values
    ## from `slot`); pick_kinds.nowhere: a node has no such pick.
    pub let slot: int
    ## Where an Edit step keeps it: its group (pick_kinds.group), its text
    ## `step` (a point: its numbers from `step`), or pick_kinds.nowhere.
    pub let step: int
    ## The parameter panel's condition hiding its rows, and their tooltip.
    pub let hide_when: str
    pub let doc: str
    ## Whether it takes what the pick before it left of the selection a
    ## tool started on (Combine's tool bodies after its target body).
    pub let rest: bool
    ## How many it takes before it finishes by itself (0: until Enter; a
    ## single face, body, edge or point always one).
    pub let most: int
    ## Whether Enter on the pick before goes on to it though the tool can
    ## do without it (Fill's guides, a Profile Blend's profiles), and
    ## whether a step's prompt starts on it though it has what it needs
    ## (Fill's boundaries take more until Enter).
    pub let asked: bool
    pub let stays: bool
    ## Whether a step started on enough of a selection (`most`, else any)
    ## keeps it as its group, else the words of what was picked in its
    ## text (Blend Curve's two curves; Project's curves).
    pub let grouped: bool
    ## A point's: the slot of the point its step holds it at while it is
    ## picked (Move's From and To: the move stands still meanwhile; -1
    ## none).
    pub let still: int
    ## Whether, empty, the tool waits for it as `values` and `texts` stand
    ## (`wired`: a second input is wired); none: always.
    pub let needed: func(list[float], list[str], bool) -> bool!?
    ## What taking it sets among the values (Pick object: Extent To
    ## object; Align's second master: the side beside it), `values` changed
    ## in place, given what was picked.
    pub let taken: func(list[float], str) -> unit!?
    ## Whether it takes whole bodies as the values stand (Move's Move
    ## object); none: as its kind says.
    pub let bodies_when: func(list[float]) -> bool!?
    ## Its part of the text it lives in (a Fillet's set k among its sets):
    ## read from that text, and that text with `picked` written in.
    pub let part_of: func(str) -> str!?
    pub let with_part: func(str, str) -> str!?
    ## What the parameter panel says it holds; none: by kind ("2 faces").
    pub let summary: func(str) -> str!?
    ## The panel's buttons: pick it ("": "Pick " and the label), and
    ## forget it ("": none; "All edges").
    pub let button: str
    pub let clear: str

    pub func init(self, label: str, levels: list[int], prompt: str = "", kind: int = 0, slot: int = -1, step: int = -1, hide_when: str = "", doc: str = "",
                  rest: bool = false, most: int = 0, asked: bool = false, stays: bool = false, grouped: bool = false, still: int = -1, needed: func(list[float], list[str], bool) -> bool!? = none, taken: func(list[float], str) -> unit!? = none,
                  bodies_when: func(list[float]) -> bool!? = none, part_of: func(str) -> str!? = none, with_part: func(str, str) -> str!? = none, summary: func(str) -> str!? = none, button: str = "", clear: str = ""):
        self.label = label
        self.levels = levels
        self.prompt = prompt
        self.kind = kind
        self.slot = slot
        self.step = step
        self.hide_when = hide_when
        self.doc = doc
        self.rest = rest
        self.most = most
        self.asked = asked
        self.stays = stays
        self.grouped = grouped
        self.still = still
        self.needed = needed
        self.taken = taken
        self.bodies_when = bodies_when
        self.part_of = part_of
        self.with_part = with_part
        self.summary = summary
        self.button = button
        self.clear = clear

    pub func takes(self, level: int) -> bool:
        return self.levels.length == 0 or level in self.levels

    ## The prompt's label while it is waited for ("Pick target faces").
    pub func asking(self) -> str:
        return "Pick " + self.label.lower()

    ## Whether one click is all it takes.
    pub func single(self) -> bool:
        return not pick_kinds.many(self.kind) or self.most == 1

    ## Whether the tool waits for it, empty, as `values` and `texts` stand.
    pub func wanted(self, values: list[float], texts: list[str], wired: bool) -> bool!:
        let check = self.needed else return true
        return check(values, texts, wired)

    ## Whether it takes whole bodies as `values` stand.
    pub func bodies(self, values: list[float]) -> bool!:
        if let check = self.bodies_when:
            return check(values)
        return pick_kinds.whole_bodies(self.kind)

    ## What it holds of text `text` (its own part, for a Fillet's set).
    pub func held(self, text: str) -> str!:
        let part = self.part_of else return text
        return part(text)

    ## Text `text` with `picked` as what it holds.
    pub func holding(self, text: str, picked: str) -> str!:
        let put = self.with_part else return picked
        return put(text, picked)

## What a node and a step both hand a tool's cook: the verb, its numbers
## and texts, one group per pick and how they were picked.
pub class Invocation:
    pub let verb: str
    pub let numbers: list[float]
    pub let texts: list[str]
    ## One group expression per pick slot.
    pub let picks: list[str]
    pub let group_type: int
    ## The connectivity hash of the mesh the groups were picked on (0: none).
    pub let topology: int
    pub let symmetry: int
    pub let keep: bool
    ## Whether a node calls (its picks kept by name in its texts, its
    ## values all 48) rather than an Edit step (its group picked on the pick
    ## geometry); a node's second input, its worker-side memo (a Fillet's
    ## plan) and the nearest sketch upstream (a Revolve's axis on a sketch
    ## line).
    pub let node: bool
    pub let second: GeometryData?
    pub let memo: BlendMemo?
    pub let sketch: str
    ## The project's CAD tolerances: what a tool that makes a body from
    ## nothing (a primitive, an extrude of a sketch, a surface from curves)
    ## builds it under.
    pub let tolerances: CadTolerances
    ## The project's display quality: what a tool finds its picks on (the
    ## bodies' pick mesh at it, cad_cook.pick_mesh), shared with the viewport.
    pub let quality: MeshQuality

    pub func init(self, verb: str, numbers: list[float], picks: list[str], group_type: int, topology: int = 0, symmetry: int = 0, keep: bool = false, texts: list[str] = [],
                  node: bool = false, second: GeometryData? = none, memo: BlendMemo? = none, sketch: str = "", tolerances: CadTolerances? = none, quality: MeshQuality? = none):
        self.verb = verb
        self.numbers = numbers
        self.picks = picks
        self.group_type = group_type
        self.topology = topology
        self.symmetry = symmetry
        self.keep = keep
        self.texts = texts
        self.node = node
        self.second = second
        self.memo = memo
        self.sketch = sketch
        self.tolerances = tolerances else CadTolerances()
        self.quality = quality else MeshQuality.medium()

    ## Text `at`, "" past the last.
    pub func text(self, at: int) -> str:
        return self.texts[at] if at >= 0 and at < self.texts.length else ""

    ## Number `at`, `otherwise` past the last.
    pub func number(self, at: int, otherwise: float = 0.0) -> float:
        return self.numbers[at] if at >= 0 and at < self.numbers.length else otherwise

    ## The group of the first pick ("" for everything).
    pub func group(self) -> str:
        return self.picks[0] if self.picks.length > 0 else ""

## A key that changes a tool while its drag lasts (Bevel's, after
## Blender's modal keys): "drag" makes the drag set parameter `parm`
## instead, moved by `mode` (the drag modes' words); "cycle" steps a menu
## to its next choice; "toggle" flips a toggle.
pub class ToolKey:
    pub let key: str
    pub let parm: int
    pub let action: str
    pub let mode: str

    pub func init(self, key: str, parm: int, action: str, mode: str = ""):
        self.key = key
        self.parm = parm
        self.action = action
        self.mode = mode

## How the `create` gesture (create_gesture.luc) sets a primitive's value
## slots (luce-geocore's PlaceSpec): a drag on the ground or a surface draws
## its base, "rect" (two corners: width and depth) or "radius" (out from
## the center); then, with a rise slot, the pointer raises it: "height"
## along Up, or "radius" off the base's circle (a torus's tube). Where the
## base lands and the way that surface faces set Position and Up.
pub class ToolPlace:
    pub let base: str
    ## The slots the base sets: the radius's, or the width's and depth's.
    pub let size: int
    pub let depth: int
    pub let rise: int
    pub let rise_mode: str
    pub let position: int
    pub let up: int
    ## Name piece's slot: on, the faces take a path of their own (Edit
    ## Mesh's steps turn it on).
    pub let piece: int

    pub func init(self, base: str, size: int, depth: int, rise: int, rise_mode: str, position: int, up: int, piece: int):
        self.base = base
        self.size = size
        self.depth = depth
        self.rise = rise
        self.rise_mode = rise_mode
        self.position = position
        self.up = up
        self.piece = piece

pub class Tool:
    ## The node kind and step verb ("PolyBevel"), and the label menus,
    ## commands and steps show ("Bevel").
    pub let name: str
    pub let label: str
    ## "Create", "Modify" or "Select", then the family menus group it under
    ## ("Extrude", "Cut", "Shape", …).
    pub let section: str
    pub let family: str
    pub let icon: str
    pub let doc: str
    ## The network contexts it is a node in ("geometry") and the Edit kinds
    ## it is a step of ("Edit Mesh").
    pub let contexts: list[str]
    pub let kinds: list[str]
    pub let picks: list[Pick]
    ## Its parameter rows (`in_steps`: on the tool bar after it runs).
    pub let rows: list[Parameter]
    ## The row a drag in the viewport sets after it runs (-1: none), and
    ## how the pointer sets it (tool_drag.luc): "length", "normal",
    ## "amount", "angle" or "slide".
    pub let drag: int
    pub let drag_mode: str
    ## Whether the value follows the pointer as soon as the tool runs, no
    ## button held (Blender's Extrude); a click keeps it.
    pub let follows: bool
    ## The count row the wheel steps during the drag, the vector row X, Y
    ## and Z set (an axis lock) and the menu row switched to its last choice
    ## with it (-1: none).
    pub let wheel: int
    pub let axis: int
    pub let axis_choice: int
    ## Viewport input besides the selection: "" none, "stroke" (Bisect's
    ## line), "place" (PolyDraw's clicked corners), "hover" (Loop Cut's ring
    ## under the pointer, cut by a click), "knife" (the Knife's points),
    ## "create" (a primitive drawn on the ground or a surface: `place`),
    ## "pen" (Poly Pen's faces built under the pointer).
    pub let gesture: str
    ## Whether it runs on everything with nothing selected.
    pub let whole: bool
    ## The run, the same for a node and a step.
    pub let cook: func(Invocation, GeometryData, str) -> GeometryData!
    ## A Select tool's group function (`loop`: `loop(group, …)` in a node's
    ## Group re-runs it on every cook), or "".
    pub let function: str
    ## Keys that change it while its drag lasts.
    pub let keys: list[ToolKey]
    ## A primitive's interactive creation (the "create" gesture), or none
    ## (set by its home after the rows it names are made).
    pub var place: ToolPlace? = none

    ## The verb of its Edit step when it is not its node's kind (the Move
    ## node's Transform Components), its node menu category in its context
    ## ("Surfaces"), its node's inputs, its step's starting numbers when
    ## they are not its rows' (a Trim's picks after its rows; [] its rows').
    pub let verb: str
    pub let category: str
    pub let inputs: int
    pub let numbers: list[float]
    ## Whether its cook's handles (Node.handles) drag its values, and
    ## whether its value box opens by its handle as it is made (Fillet's
    ## radius).
    pub let handles: bool
    pub let asks: bool
    ## What its node waits for as its values and texts stand ("Pick the
    ## faces to remove", "" none; `wired`: its second input is wired);
    ## none: its empty needed picks say it.
    pub let waits: func(list[float], list[str], bool) -> str!?

    pub func init(self, name: str, label: str, section: str, family: str, icon: str, doc: str, contexts: list[str], kinds: list[str], picks: list[Pick],
                  rows: list[Parameter], drag: int, gesture: str, whole: bool, cook: func(Invocation, GeometryData, str) -> GeometryData!, function: str = "",
                  drag_mode: str = "length", follows: bool = false, wheel: int = -1, axis: int = -1, axis_choice: int = -1, keys: list[ToolKey] = [],
                  verb: str = "", category: str = "", inputs: int = 1, numbers: list[float] = [], handles: bool = false, asks: bool = false,
                  waits: func(list[float], list[str], bool) -> str!? = none):
        self.name = name
        self.label = label
        self.section = section
        self.family = family
        self.icon = icon
        self.doc = doc
        self.contexts = contexts
        self.kinds = kinds
        self.picks = picks
        self.rows = rows
        self.drag = drag
        self.drag_mode = drag_mode
        self.follows = follows
        self.wheel = wheel
        self.axis = axis
        self.axis_choice = axis_choice
        self.gesture = gesture
        self.whole = whole
        self.cook = cook
        self.function = function
        self.keys = keys
        self.verb = verb if verb != "" else name
        self.category = category
        self.inputs = inputs
        self.numbers = numbers
        self.handles = handles
        self.asks = asks
        self.waits = waits

    ## The selection it runs on.
    pub func first_pick(self) -> Pick:
        return self.picks[0] if self.picks.length > 0 else Pick("", [])

    ## Whether it is offered at selection level `level`: its first pick's
    ## levels; a hover tool at every component level (it picks under the
    ## pointer, at Edges).
    pub func offered(self, level: int) -> bool:
        return self.offered_levels().length == 0 or level in self.offered_levels()

    ## The levels it is offered at ([] every level).
    pub func offered_levels(self) -> list[int]:
        if self.gesture == "hover":
            return [1, 2, 3]
        return self.first_pick().levels

    ## Parameter `parm`'s row (a verb's parameter index: its valued rows in
    ## slot order, past any folder heading), if it has one.
    pub func row_at(self, parm: int) -> Parameter?:
        if parm < 0:
            return none
        # Its place among the valued rows by slot (More options' rows come
        # after the others on the bar, but keep their slots).
        for row in self.rows:
            if not row.valued():
                continue
            var before = 0
            for other in self.rows:
                if other.valued() and other.slot < row.slot:
                    before += 1
            if before == parm:
                return row
        return none

    ## Parameter `parm`'s value slot (-1 for none).
    pub func slot_of(self, parm: int) -> int:
        let row = self.row_at(parm) else return -1
        return row.slot

    ## Its step's starting numbers: its own, else its node's.
    pub func starting(self) -> list[float]:
        if self.numbers.length > 0:
            return self.numbers.copy()
        return self.node_values()

    ## Its node's starting values: each row's initial value in its slot (a
    ## vector's in each component's, a value ramp's numbers in its slots).
    pub func node_values(self) -> list[float]:
        let result: list[float] = []
        for row in self.rows:
            if not row.valued() and not row.value_ramp():
                continue
            while result.length < row.slot + row.components:
                result.append(0.0)
            result[row.slot] = row.initial
            for component in 1..<row.components:
                result[row.slot + component] = row.rest[component - 1]
        return result

    ## Its first pick's levels as words ("edges or faces"), given each
    ## level's name; "components" at every level.
    pub func levels_text(self, level_names: list[str]) -> str:
        let named: list[str] = []
        for level in self.first_pick().levels:
            if level < level_names.length:
                named.append(level_names[level].lower())
        return named.join(" or ") if named.length > 0 else "components"

    ## What it says with nothing selected: its pick's own prompt, else
    ## "Select edges for Bevel".
    pub func waiting(self, level_names: list[str]) -> str:
        let pick = self.first_pick()
        if pick.prompt != "":
            return pick.prompt
        return f"Select {self.levels_text(level_names)} for {self.label}"

    ## The rows its step's tool bar shows (`in_steps`; texts are a node's).
    pub func bar_rows(self) -> list[Parameter]:
        let result: list[Parameter] = []
        for row in self.rows:
            if row.in_steps and row.valued():
                result.append(row)
        return result

    ## Its picks a node has, or an Edit step (`step`), with their places in
    ## `picks`.
    pub func picks_of(self, step: bool) -> list[int]:
        let result: list[int] = []
        for at in 0..<self.picks.length:
            if (self.picks[at].step if step else self.picks[at].slot) != pick_kinds.nowhere:
                result.append(at)
        return result

## A tool as its registry lists it: what menus, icons and the node catalog
## read without building it (its name, label, icon, node menu category,
## the contexts it is a node in, the Edit kinds it is a step of, its step's
## verb), and how to build it.
pub class ToolEntry:
    pub let name: str
    pub let label: str
    pub let icon: str
    pub let category: str
    pub let contexts: list[str]
    pub let kinds: list[str]
    pub let verb: str
    pub let make: func() -> Tool

    pub func init(self, name: str, label: str, icon: str, category: str, contexts: list[str], kinds: list[str], make: func() -> Tool, verb: str = ""):
        self.name = name
        self.label = label
        self.icon = icon
        self.category = category
        self.contexts = contexts
        self.kinds = kinds
        self.make = make
        self.verb = verb if verb != "" else name

    ## The tool: its entry's facts with the rest of its declaration.
    pub func tool(self, section: str, family: str, doc: str, picks: list[Pick], rows: list[Parameter], cook: func(Invocation, GeometryData, str) -> GeometryData!,
                  drag: int = -1, gesture: str = "", whole: bool = false, inputs: int = 1, numbers: list[float] = [], handles: bool = false, asks: bool = false,
                  waits: func(list[float], list[str], bool) -> str!? = none) -> Tool:
        return Tool(self.name, self.label, section, family, self.icon, doc, self.contexts, self.kinds, picks, rows, drag, gesture, whole, cook,
                    verb = self.verb, category = self.category, inputs = inputs, numbers = numbers, handles = handles, asks = asks, waits = waits)