Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / tool_menus.luc

## The registry's tools as the menus show them: the viewport's context's
## tools (tool_registry.luc's: an Edit kind's, Edit Mesh's in Geo, a CAD
## network's nodes) by section and family, at the selection level, and the
## Select section with the selection commands. The A, S and D marking
## menus, the right-click menu and Modify › the context's tools read these,
## so a tool reaches every surface from its declaration alone.
from luce_ui.ui import Command
from workspace import Workspace
from actions import Actions
from edit_types import edge_level, face_level
import pick_kinds
from tool_record import Tool
import tool_registry

## A family's commands, in the kind's tool order, and the registry tool
## each runs (none for a command outside the registry: Select All, Hide).
pub class Family:
    pub let name: str
    pub let commands: list[Command]
    pub let tools: list[Tool?]

    pub func init(self, name: str):
        self.name = name
        self.commands = []
        self.tools = []

    ## Command `command` (of registry tool `tool`, if any) at the end.
    pub func add(self, command: Command, tool: Tool? = none):
        self.commands.append(command)
        self.tools.append(tool)

## Whether the viewport's kind takes its tools from the registry (all of
## them; Edit SDF's and Edit GSplats' are its own).
pub func from_registry(w: Workspace) -> bool:
    for tool in w.strip_kind().tools:
        if tool.tool == none:
            return false
    return true

## Whether the viewport's kind has Select tools of the registry (Edit
## Mesh's and Geo's).
pub func selects(w: Workspace) -> bool:
    for tool in w.strip_kind().tools:
        if let registered = tool.tool:
            if registered.section == "Select":
                return true
    return false

## The context's tools: the viewport's Edit kind's registry tools, each
## with whether it is offered at level `level` (-1: any); in a CAD network
## (no Edit node edited) its nodes' tools.
func context_tools(w: Workspace, level: int) -> list[(Tool, bool)]!:
    let result: list[(Tool, bool)] = []
    if w.network.context() == "cad" and not w.editing():
        # A CAD network picks faces and edges of its bodies: a tool on
        # bodies takes those of the faces picked.
        for entry in tool_registry.cad_nodes():
            let tool = entry.make()
            let bodies = pick_kinds.whole_bodies(tool.first_pick().kind) and level == face_level
            # One that picks nothing (Mirror Body) works on every body: it
            # is offered when nothing is picked.
            let picking = tool.picks.length > 0 and (tool.offered(level) or bodies)
            result.append((tool, level < 0 or picking))
        return result
    for tool in w.strip_kind().tools:
        let registered = tool.tool else continue
        result.append((registered, level < 0 or tool.offered(level)))
    return result

## The context's registry tools of `section` offered at level `level` (-1:
## at any level), each its command, by family in the registry's family
## order (tool_registry.family_order), each family's tools in their
## declaration order. A tool of two steps (Blend Curve on curves and on
## edges) is one command.
pub func families(w: Workspace, actions: Actions, section: str, level: int = -1) -> list[Family]!:
    let order = select_order(level) if section == "Select" else tool_registry.family_order()
    let found: list[Family] = []
    let commands = actions.modify_commands()
    let named_already: list[str] = []
    for (registered, offered) in context_tools(w, level):
        if registered.section != section or not offered or registered.label in named_already:
            continue
        named_already.append(registered.label)
        let command = named(commands, registered.label) else continue
        var family: Family? = none
        for each in found:
            if each.name == registered.family:
                family = each
        if family == none:
            let made = Family(registered.family)
            found.append(made)
            family = made
        (family else trap("family")).add(command, registered)
    let result: list[Family] = []
    for name in order:
        for each in found:
            if each.name == name:
                result.append(each)
    for each in found:
        if not (each.name in order):
            result.append(each)
    return result

## The Select families in the order the level reaches for them: loops
## first at Edges, regions first at Points and Faces.
pub func select_order(level: int) -> list[str]:
    if level == edge_level:
        return ["Loops", "Region", "Find", "Similar", "Pattern", "Symmetry", "Convert"]
    return ["Region", "Loops", "Similar", "Find", "Pattern", "Symmetry", "Convert"]

## The commands of `groups` in order, one list.
pub func flat(groups: list[Family]) -> list[Command]:
    let result: list[Command] = []
    for family in groups:
        result.extend(family.commands)
    return result

## Repeat, when a registry tool has run.
pub func repeat(w: Workspace, actions: Actions) -> list[Command]:
    let result: list[Command] = []
    if w.last_tool != "":
        result.append(actions.repeat_tool)
    return result

## What the Select menus offer at the level: the Select tools there by
## family (select_order), then Selection (all, none, invert, through the
## depth), the sets and Hide. D lists them as columns, the right-click
## Select submenu with a divider between families.
pub func select_families(w: Workspace, actions: Actions) -> list[Family]!:
    return select_families_at(w, actions, w.level_index())

## select_families at level `level` (-1: every Select tool, as the tool
## shelf lists them).
pub func select_families_at(w: Workspace, actions: Actions, level: int) -> list[Family]!:
    let result = families(w, actions, "Select", level)
    let selection = Family("Selection")
    for command in [actions.select_all, actions.selection.none_selected, actions.selection.invert, actions.through_depth]:
        selection.add(command)
    result.append(selection)
    let hiding = Family("Hide")
    for command in [actions.selection.hide, actions.selection.hide_unselected, actions.selection.unhide]:
        hiding.add(command)
    result.append(hiding)
    return result

## The Select menus' commands in one list, families apart by dividers and
## the sets' submenu after Selection.
pub func select_commands(w: Workspace, actions: Actions) -> list[Command]!:
    let result: list[Command] = []
    for family in select_families(w, actions):
        if result.length > 0:
            result.append(Command("", divider = true))
        result.extend(family.commands)
        if family.name == "Selection":
            result.append(actions.selection.sets)
    return result

## Modify › Selection: Selection's commands, a submenu per Select family
## (every tool, enabled at the levels it takes), the sets and Hide.
pub func select_menu(w: Workspace, actions: Actions) -> list[Command]!:
    let titles: list[Command] = []
    for family in families(w, actions, "Select"):
        let title = Command(family.name)
        title.set_items(family.commands)
        titles.append(title)
    let selection = actions.selection
    return [actions.select_all, selection.none_selected, selection.invert, actions.through_depth, Command("", divider = true)] + titles + [Command("", divider = true), selection.sets, selection.hide, selection.hide_unselected, selection.unhide]

func named(commands: list[Command], name: str) -> Command?:
    for command in commands:
        if command.text() == name:
            return command
    return none