Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.1

0.11.1 / src / edit_types.luc

## What an Edit node kind plugs into the Edit framework (edit_kinds.luc):
## its selection levels, its tools and primitives, the geometry each level
## picks on, and the step executor that gives its recipe's verbs meaning.
## The framework owns everything else: the recipe (undo is a shorter
## recipe), checkpoints, move coalescing, the tool amount, symmetry, soft
## selection and Keep selection, the selection and its hand-off, Tool →
## Select, the inspector rows, the level switcher and the tool strip.
from luce_geocore.geocore import Mesh, Domain, GroupType, Vector3
from geometry_data import GeometryData
from edit_operations import EditStep
from overlay_lines import OverlayLines

## A selection level: what one pick selects, on which domain of the level's
## pick geometry, and the group type its steps receive.
pub class EditLevel:
    ## The switcher's label and icon ("Polygons", "face-select").
    pub let name: str
    pub let icon: str
    pub let domain: Domain
    pub let group_type: GroupType
    ## With a piece function, a pick selects the whole piece around the
    ## picked element: the group expression naming it on `domain` (Object
    ## levels); without one, the element alone.
    pub let piece: func(Mesh, int) -> str!?

    pub func init(self, name: str, icon: str, domain: Domain, group_type: GroupType, piece: func(Mesh, int) -> str!? = none):
        self.name = name
        self.icon = icon
        self.domain = domain
        self.group_type = group_type
        self.piece = piece

    ## The group expression of what picking `element` of `mesh` selects: its
    ## piece, or "" for the element alone.
    pub func whole(self, mesh: Mesh, element: int) -> str!:
        let expand = self.piece else return ""
        return expand(mesh, element)

    ## The domain and group type as geocore's i64 parameters take them.
    pub func domain_number(self) -> int:
        return domain_number(self.domain)

    pub func group_number(self) -> int:
        return group_number(self.group_type)

## A tool button: its label, the verb its step runs, the levels it is
## offered on ([] every level) and its numbers for a tool amount.
pub class EditTool:
    pub let name: str
    pub let verb: str
    pub let levels: list[int]
    pub let numbers: func(float) -> list[float]

    pub func init(self, name: str, verb: str, levels: list[int], numbers: func(float) -> list[float]):
        self.name = name
        self.verb = verb
        self.levels = levels
        self.numbers = numbers

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

## A node parameter an Edit kind adds after the shared ones (tool amount,
## soft radius, falloff, symmetry, Keep selection), in value slots 5
## onwards. Its value at the time rides on every step the node records,
## after the verb's own numbers (EditKind.step_numbers), so replays need
## no node: Edit Sketch's snapping while dragging.
pub class EditParameter:
    pub let label: str
    ## "number" or "toggle".
    pub let kind: str
    pub let initial: float
    pub let minimum: float
    pub let maximum: float
    pub let doc: str

    pub func init(self, label: str, kind: str, initial: float, minimum: float, maximum: float, doc: str):
        self.label = label
        self.kind = kind
        self.initial = initial
        self.minimum = minimum
        self.maximum = maximum
        self.doc = doc

## The first value slot of a kind's own parameters, and how many fit.
pub let first_kind_slot: int = 5
pub let most_kind_parameters: int = 10

## An Edit node kind.
pub class EditKind:
    ## The node kind ("Edit Mesh").
    pub let name: str
    pub let levels: list[EditLevel]
    pub let tools: list[EditTool]
    ## Create entries ("Box", "Sphere", …).
    pub let primitives: list[str]
    ## The geometry the recipe starts from, made from the node's input (an
    ## empty GeometryData when unconnected); fails on input it cannot edit.
    pub let start: func(GeometryData) -> GeometryData!
    ## The geometry level `level` of `data` picks on (the worker publishes
    ## it for the viewport); none when there is nothing to pick.
    pub let pick: func(GeometryData, int) -> Mesh?!
    ## One recipe step on `data`.
    pub let run: func(EditStep, GeometryData) -> GeometryData!
    ## The step creating primitive `name`, with its default numbers.
    pub let create: func(str) -> EditStep?!
    ## Lines drawn while the node is displayed (Edit Sketch's plane grid),
    ## added to the viewport's hull lines; none draws nothing. They are drawn
    ## from the set's detail attributes: the worker keys them by those.
    pub let guides: func(GeometryData, OverlayLines) -> unit!?
    ## Whether the selection also shows through surfaces: for pick geometry
    ## the viewport does not draw (Edit SDF's proxies inside the surface).
    pub let see_through: bool
    ## Create entries placed by clicking a face in the viewport (Edit
    ## Sketch's Plane from Face): their step's first six numbers become the
    ## face's point and normal.
    pub let face_primitives: list[str]
    ## Tools that, with nothing selected, draw instead: clicks place corners
    ## (on points, surfaces or the ground), Enter or the first corner again
    ## finishes, and `drawn` makes the one step (Edit Mesh's PolyDraw).
    pub let draw_tools: list[str]
    pub let drawn: func(str, list[Vector3]) -> EditStep?!?
    ## The kind's own node parameters (see EditParameter).
    pub let parameters: list[EditParameter]

    pub func init(self, name: str, levels: list[EditLevel], tools: list[EditTool], primitives: list[str], start: func(GeometryData) -> GeometryData!,
                  pick: func(GeometryData, int) -> Mesh?!, run: func(EditStep, GeometryData) -> GeometryData!, create: func(str) -> EditStep?!,
                  guides: func(GeometryData, OverlayLines) -> unit!? = none, see_through: bool = false, face_primitives: list[str] = [],
                  draw_tools: list[str] = [], drawn: func(str, list[Vector3]) -> EditStep?!? = none, parameters: list[EditParameter] = []):
        self.name = name
        self.levels = levels
        self.tools = tools
        self.primitives = primitives
        self.start = start
        self.pick = pick
        self.run = run
        self.create = create
        self.guides = guides
        self.see_through = see_through
        self.face_primitives = face_primitives
        self.draw_tools = draw_tools
        self.drawn = drawn
        self.parameters = parameters

    ## A step's numbers with the kind's parameter values (from the node's
    ## `values`) after them.
    pub func step_numbers(self, numbers: list[float], values: list[float]) -> list[float]:
        let result = numbers.copy()
        for at in 0..<self.parameters.length:
            result.append(values[first_kind_slot + at])
        return result

    ## Tool `name`, when this kind has it.
    pub func tool(self, name: str) -> EditTool?:
        for tool in self.tools:
            if tool.name == name:
                return tool
        return none

pub func domain_number(domain: Domain) -> int:
    if domain == Domain.corner: return 1
    if domain == Domain.face: return 2
    if domain == Domain.edge: return 4
    return 0

pub func group_number(group_type: GroupType) -> int:
    if group_type == GroupType.points: return 1
    if group_type == GroupType.edges: return 2
    if group_type == GroupType.faces: return 3
    if group_type == GroupType.corners: return 4
    return 0

# -- Helpers kinds share.

## A tool's numbers whatever the tool amount (Delete, Close, a boolean mode).
pub func fixed_numbers(values: list[float]) -> func(float) -> list[float]:
    return func (amount: float) -> list[float]:
        _ = amount
        return values

## The input in a wrapper of the Edit node's own (its warning kept), so the
## pick geometry a kind caches on it is that kind's: a `start` for kinds
## that edit a component and pass the others through.
pub func own_data(data: GeometryData) -> GeometryData!:
    let result = GeometryData(geometry = data.geometry)
    result.warning = data.warning
    return result