Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / verb_tools.luc

## Mesh tools: one Tool for each luce-geocore verb that declares a section
## (its label, family, icon, picks, drag, gesture and rows are the verb's
## own), a node in Geo and a step of Edit Mesh. Their cook runs the verb, so
## a node and a step run the same kernel the same way. Adding a mesh tool is
## its verb in luce-geocore and nothing here.
from luce_geocore.geocore import Mesh, Verbs
from geometry_data import GeometryData
from tool_record import Tool, Pick, Invocation, ToolKey, ToolPlace
from edit_operations import verb_numbers, second_of
from edit_types import object_level, point_level, edge_level, face_level
import verb_rows
import pick_kinds
from luce_ui import ui

## The Edit kind mesh tools are steps of, and the network context they are
## nodes in.
pub let edit_kind_name = "Edit Mesh"
pub let context = "geometry"

## Every mesh verb with a section, as a tool, in the verb registry's order.
pub func tools() -> list[Tool]:
    let result: list[Tool] = []
    for index in 0..<Verbs.count():
        if Verbs.section(index) != "":
            result.append(tool_of(index))
    return result

## Verb `index` as a tool.
func tool_of(index: int) -> Tool:
    let tool = Tool(Verbs.name(index), Verbs.label(index), Verbs.section(index), Verbs.family(index), Verbs.icon(index), Verbs.description(index), [context], [edit_kind_name],
                    picks_of(index), verb_rows.rows(index), Verbs.drag(index), Verbs.gesture(index), Verbs.whole(index), cook, Verbs.function(index),
                    Verbs.drag_mode(index), Verbs.follows(index), Verbs.wheel(index), Verbs.axis(index), Verbs.axis_choice(index), keys_of(index), inputs = Verbs.inputs(index))
    if Verbs.place_base(index) != "":
        tool.place = place_of(index, tool)
    return tool

## A primitive's creation slots (Verbs' place_*), from its rows: a vector
## size's X and Z, else two numbers, width then depth.
func place_of(index: int, tool: Tool) -> ToolPlace:
    let size = Verbs.place_size(index)
    let row = tool.row_at(size) else trap("a primitive's size row")
    let depth = row.slot + 2 if row.components == 3 else tool.slot_of(size + 1)
    let rise = Verbs.place_rise(index)
    let rise_slot = tool.slot_of(rise) + Verbs.place_rise_component(index) if rise >= 0 else -1
    return ToolPlace(Verbs.place_base(index), row.slot, depth, rise_slot, Verbs.place_rise_mode(index), tool.slot_of(Verbs.place_position(index)), tool.slot_of(Verbs.place_up(index)), tool.slot_of(Verbs.place_piece(index)))

## The keys that change verb `index`'s tool while its drag lasts.
func keys_of(index: int) -> list[ToolKey]:
    let result: list[ToolKey] = []
    for key in 0..<Verbs.key_count(index):
        result.append(ToolKey(Verbs.key_name(index, key), Verbs.key_parm(index, key), Verbs.key_action(index, key), Verbs.key_mode(index, key)))
    return result

## The verb's picks (one at every level when it declares none): the first
## is a node's Group (text 0) and a step's group; a later one (Bridge's
## second loop, Target Weld's target) a group a node keeps in text k and a
## step in its text k - 1.
func picks_of(index: int) -> list[Pick]:
    let result: list[Pick] = []
    # A primitive works on nothing.
    if Verbs.inputs(index) == 0:
        return result
    for pick in 0..<Verbs.pick_count(index):
        let levels = levels_of(Verbs.pick_levels(index, pick))
        let prompt = Verbs.pick_prompt(index, pick)
        if pick == 0:
            result.append(Pick(Verbs.pick_label(index, pick), levels, prompt, pick_kinds.components, 0, pick_kinds.group))
        else:
            result.append(Pick(Verbs.pick_label(index, pick), levels, prompt, pick_kinds.components, pick, pick - 1))
    if result.length == 0:
        result.append(Pick("Components", [], "", pick_kinds.components, 0, pick_kinds.group))
    return result

## A verb's level words ("objects faces") as Edit levels.
pub func levels_of(words: str) -> list[int]:
    let result: list[int] = []
    for word in words.split(" "):
        if word == "objects": result.append(object_level)
        elif word == "points": result.append(point_level)
        elif word == "edges": result.append(edge_level)
        elif word == "faces": result.append(face_level)
    return result

## The verb index of the mesh tool labeled `name` (or of verb `name`), or
## -1: the cheap lookups below need no rows built.
func index_of(name: str) -> int:
    for index in 0..<Verbs.count():
        if Verbs.section(index) != "" and (Verbs.label(index) == name or Verbs.name(index) == name):
            return index
    return -1

## The luciaos-assets icon of mesh tool `name`, or "".
pub func icon_of(name: str) -> str:
    let index = index_of(name)
    return Verbs.icon(index) if index >= 0 else ""

## The gesture of mesh tool `name` ("" for none or no tool).
pub func gesture_of(name: str) -> str:
    let index = index_of(name)
    return Verbs.gesture(index) if index >= 0 else ""

## A mesh tool's run: its verb on the operand's polygons (a CAD model is
## tessellated first; other components pass through), guarded by the
## topology its group was picked on.
func cook(call: Invocation, data: GeometryData, tag: str) -> GeometryData!:
    return data.with_operand(run(call, data.operand() else Mesh.empty()))

## Run `call`'s verb on `mesh` (a move as well as a tool's verb); a verb of
## two picks reads its second from the step's first text, a verb of a
## reference surface (Shrinkwrap) the invocation's second input's mesh.
## `over`: a mesh the result may be made over where only positions change
## (a drag's last frame).
pub func run(call: Invocation, mesh: Mesh, over: Mesh? = none) -> Mesh!:
    if call.topology != 0 and call.topology != mesh.topology_hash():
        error(ui.invalid, "The components picked are gone (what they were picked on changed): pick them again")
    var reference: Mesh? = none
    if let second = call.second:
        reference = second.operand()
    return Verbs.run(call.verb, mesh, call.group(), call.group_type, verb_numbers(call.verb, call.numbers), call.symmetry, call.keep, second_of(call.verb, call.texts), reference, over).mesh()