Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / src / marking_items.luc

## What the marking menus offer (marking_menu.luc), by context:
##   a sketch being edited: its CREATE tools with Extrude and Revolve (its
##     picked profiles) and Finish Sketch (A), MODIFY (S) and CONSTRAINTS
##     (D);
##   an Edit node: its primitives and drawing tools (A) and its tools (S),
##     steps inside it; Edit Mesh's S is Repeat, then the registry's Modify
##     tools the selection level takes, by family (tool_menus.luc);
##     Edit CAD's A leads with Create Sketch (on the selected face, or an
##     origin plane) and Extrude face, then its curve drawing tools; its S
##     with Press Pull, then the tools the level takes (Blend Curve,
##     Attach… at Curves);
##   inside a CAD node: both lead with Repeat and the CAD tool used last
##     (Fusion's marking menu puts Repeat first); sketches, extrusions and
##     solids (A, Sketch on face and Extrude face first with a face
##     selected), features (S: Press Pull on what is selected, Offset Face
##     of selected faces, Fillet and Chamfer of selected edges);
##   elsewhere: the Create tools (primitives drawn in the viewport, each a
##   node), lights, containers and Edit nodes (A), and the
##   modeling tools on the displayed mesh's selection at its level, each a
##   new node (S, Repeat first);
##   D outside a sketch: Select, as columns by family in the level's order
##   (tool_menus.select_order: Loops first at Edges), then Selection (all,
##   none, invert, through the depth), the selection sets (save, and each
##   set: a click selects it) and Hide (select_items).
## Entries carry their family as their group, which the menu shows as a
## column under its name.
## Node entries add the node after the selected one (`add`).
from marking_menu import MarkItem
from workspace import Workspace
from actions import Actions
from sketch_editor import SketchEditor
from tool_icons import asset_name
from luce_ui.ui import Command
import sketch_tools
from sketch_tools import SketchTool
import edit_cad_commands
import tool_menus
import selection_sets

## Create (A).
pub func create_items(w: Workspace, actions: Actions, add: func(str) -> unit!, sketch: SketchEditor) -> list[MarkItem]!:
    if w.sketching():
        return sketch_items(sketch, 0) + command_items([actions.extrude_profiles, actions.revolve_profiles, actions.finish_sketch])
    if edit_cad_commands.active(w):
        # Edit CAD: sketches first (Sketch on face with a face selected),
        # then its primitives.
        let model = actions.model
        let first = command_items([actions.sketch_on_face]) if actions.sketch_on_face.is_enabled() else command_items([model.planes[1]])
        let sketching = MarkItem("Create Sketch", first[0].icon, true, first[0].run, variants = command_items([actions.sketch_on_face] + model.planes))
        let rest = command_items([actions.extrude_face]) if actions.extrude_face.is_enabled() else no_items()
        return repeat_items(w, actions) + [sketching] + rest + command_items(actions.draw_commands()) + command_items(actions.create_commands())
    if w.editing():
        # Edit Mesh: the registry's Create tools by family (primitives,
        # drawing tools); other kinds their own.
        if tool_menus.from_registry(w):
            return family_items(tool_menus.families(w, actions, "Create"))
        return command_items(actions.create_commands() + actions.draw_commands())
    let context = w.network.context()
    if context == "cad":
        # A CAD network: Sketch on face and Extrude face with a face
        # selected, then the Create tools by family (columns).
        let lead = command_items([actions.sketch_on_face, actions.extrude_face]) if actions.sketch_on_face.is_enabled() else no_items()
        return repeat_items(w, actions) + lead + family_items(tool_menus.families(w, actions, "Create"))
    if context == "shading":
        return no_items()
    # Geo: the Create tools (a primitive drawn in the viewport is a node),
    # then lights, containers and Edit nodes.
    return family_items(tool_menus.families(w, actions, "Create")) + node_items(["Light", "Camera", "Group", "Null", "Code", "CAD", "Edit Mesh", "Edit SDF", "File"], add, "Nodes")

## Modify (S).
pub func modify_items(w: Workspace, actions: Actions, add: func(str) -> unit!, sketch: SketchEditor) -> list[MarkItem]!:
    if w.sketching():
        return sketch_items(sketch, 1)
    let pull = command_items([actions.press_pull]) if actions.press_pull.is_enabled() else no_items()
    if edit_cad_commands.active(w):
        # Edit CAD: Press Pull on what is selected, then the model's tools
        # the level takes, by family (its drawing tools are A's).
        return repeat_items(w, actions) + pull + family_items(tool_menus.families(w, actions, "Modify", w.level_index()))
    if w.editing():
        return tools_at_level(w, actions)
    let context = w.network.context()
    if context == "cad":
        # A CAD network: Press Pull on what is selected, then the tools the
        # selection's level takes (with nothing selected all of them), by
        # family.
        let level = w.level_index() if w.selected_count() > 0 else -1
        return repeat_items(w, actions) + pull + family_items(tool_menus.families(w, actions, "Modify", level))
    if context == "shading":
        return no_items()
    return tools_at_level(w, actions) + node_items(["Transform", "Mirror", "CopyTransform"], add, "Nodes")

## Select (D, outside a sketch): the Select tools at the level by family,
## Selection, the sets and Hide; where the kind has no registry tools (CAD),
## Selection alone.
pub func select_items(w: Workspace, actions: Actions) -> list[MarkItem]!:
    let selection = actions.selection
    if not tool_menus.selects(w):
        return command_items([actions.select_all, selection.none_selected, selection.invert, actions.through_depth])
    let result = no_items()
    for family in tool_menus.select_families(w, actions):
        result.extend(command_items(family.commands, family.name))
        if family.name != "Selection":
            continue
        # The sets: Save as new set, then each set (a click selects it).
        result.extend(command_items([selection.save_set], "Sets"))
        for name in selection_sets.names(w):
            let chosen = name
            let recall = func () -> unit!: selection_sets.recall(w, chosen, 0)
            result.append(MarkItem(name, "group", true, recall, group = "Sets"))
    return result

## The viewport kind's tools for S: Repeat and the registry's Modify tools
## the level takes by family (Edit Mesh, Geo), else every tool of the kind.
func tools_at_level(w: Workspace, actions: Actions) -> list[MarkItem]!:
    if not tool_menus.from_registry(w):
        return command_items(actions.modify_commands())
    return repeat_items(w, actions) + family_items(tool_menus.families(w, actions, "Modify", w.level_index()))

## Repeat (the registry tool run last; none yet: no item), led by the
## repeat icon (its name shows on hover).
func repeat_items(w: Workspace, actions: Actions) -> list[MarkItem]!:
    let result = no_items()
    for command in tool_menus.repeat(w, actions):
        let chosen = command
        result.append(MarkItem(command.text(), "repeat", command.is_enabled(), func () -> unit!: chosen.trigger()))
    return result

## Families as entries, each in its column.
func family_items(families: list[tool_menus.Family]) -> list[MarkItem]:
    let result = no_items()
    for family in families:
        result.extend(command_items(family.commands, family.name))
    return result

## Constraints (D, in a sketch).
pub func constraint_items(sketch: SketchEditor) -> list[MarkItem]:
    return sketch_items(sketch, 2)

## A sketch menu's tools: a tool with variants is one family item.
func sketch_items(sketch: SketchEditor, menu: int) -> list[MarkItem]:
    let result = no_items()
    for tool in sketch_tools.tools(menu):
        if tool.variants.length == 0:
            result.append(sketch_item(sketch, tool))
            continue
        let variants = no_items()
        for variant in tool.variants:
            variants.append(sketch_item(sketch, variant))
        let first = variants[0]
        result.append(MarkItem(tool.name, first.icon, tool.ready, first.run, variants = variants))
    return result

func sketch_item(sketch: SketchEditor, tool: SketchTool) -> MarkItem:
    let name = tool.name
    return MarkItem(name, tool.icon, tool.ready, func () -> unit!: sketch.choose(name))

## Commands as entries, in column `group` ("" none).
func command_items(commands: list[Command], group: str = "") -> list[MarkItem]:
    let result = no_items()
    for command in commands:
        let chosen = command
        let icon = command.icon() if command.icon() != "" else asset_name(command.text())
        let run = func () -> unit!: chosen.trigger()
        result.append(MarkItem(command.text(), icon, command.is_enabled(), run, group = group))
    return result

func node_items(kinds: list[str], add: func(str) -> unit!, group: str = "") -> list[MarkItem]:
    let result = no_items()
    for kind in kinds:
        let chosen = kind
        let run = func () -> unit!: add(chosen)
        result.append(MarkItem(kind, asset_name(kind), true, run, group = group))
    return result

func no_items() -> list[MarkItem]:
    let result: list[MarkItem] = []
    return result