Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / src / top_menus.luc

## The menu bar's Create and Modify menus, for those who prefer menus to Tab
## and the marking menus.
##   Create: the Tab menu's nodes for the network shown, in its categories
##     (node_catalog.offered, one submenu each), and inside a displayed Edit
##     node its primitives and drawing tools (Edit CAD's curves) first; in a CAD node Sketch on face, Extrude face,
##     Extrude and Revolve (a sketch's profiles) and Finish Sketch first.
##     Rebuilt when the network
##     level changes.
##   Modify: what applies to the selection: the node's flags, Output, grouping,
##     renaming, cutting, copying, pasting, duplicating and deletion; the transform
##     tools; the context's tools (tool_menus.luc): Repeat and a submenu per
##     family of the registry's Modify tools (an Edit kind's steps, a Geo
##     tool's or a CAD network's nodes), in CAD led by Press Pull and Extrude
##     face; the selection's edits; Selection: select all or none, invert,
##     through the depth, a submenu per family of the registry's Select
##     tools, the selection sets and Hide/Unhide (tool_menus.select_menu).
## Every item is one of Actions' commands, so choosing one is choosing it in
## Tab or a marking menu, its shortcut shows, and it is disabled when it
## does not apply (Actions.refresh).
from luce_ui.ui import Menu, Command, Font
from actions import Actions
from workspace import Workspace
import node_catalog
import tool_menus

pub class TopMenus:
    pub let create: Menu
    pub let modify: Menu
    let actions: Actions
    let workspace: Workspace
    ## Submenu titles: the displayed Edit node's primitives, the transform
    ## tools, the modeling tools and the selection's edits.
    let primitives: Command
    ## The displayed Edit node's drawing tools (Edit CAD's curves).
    let drawing: Command
    pub let transform: Command
    pub let modeling: Command
    pub let selecting: Command
    ## The modeling submenu's family submenus, as last built.
    var families: list[Command]
    ## Create's entries, as last built, and Modify's.
    pub var create_entries: list[Command]
    pub let modify_entries: list[Command]
    ## What the menus were last built for: they are rebuilt when it changes.
    var create_key: str = ""
    var modeling_key: str = ""

    pub func init(self, workspace: Workspace, actions: Actions, font: Font) -> unit!:
        self.workspace = workspace
        self.actions = actions
        self.primitives = Command("Primitives")
        self.drawing = Command("Curves")
        self.transform = Command("Transform tool")
        self.transform.set_items(actions.tools)
        self.modeling = Command("Modeling tools")
        self.selecting = Command("Selection")
        self.families = []
        self.create_key = self.network_key()
        self.create_entries = self.create_items()
        self.create = Menu("Create", self.create_entries, font = font)
        self.update_modeling()
        self.modify_entries = [actions.display, actions.enabled, actions.freeze, Command("", divider = true),
                               actions.set_output, actions.group, actions.rename, actions.cut, actions.copy, actions.paste, actions.duplicate, actions.delete, Command("", divider = true),
                               self.transform, self.modeling, actions.clear_edits, Command("", divider = true),
                               self.selecting, actions.snap_target, actions.clear_snap_target]
        self.modify = Menu("Modify", self.modify_entries, font = font)
        self.refresh()

    ## Follow the network shown and the selection: rebuild what changed and
    ## enable a submenu while any of its items applies. After Actions.refresh.
    pub func refresh(self) -> unit!:
        let key = self.network_key()
        if key != self.create_key:
            self.create_key = key
            self.create_entries = self.create_items()
            self.create.set_actions(self.create_entries)
        self.update_modeling()
        # Selection's submenus (a Select family, the sets) first: Selection
        # is enabled while any of them is.
        for index in 0..<self.selecting.item_count():
            let title = self.selecting.item(index)
            if title.item_count() > 0:
                enable_by_items(title)
        for title in self.families + [self.primitives, self.drawing, self.transform, self.modeling, self.selecting]:
            enable_by_items(title)

    ## The network level and whether an Edit node is displayed (and its kind).
    func network_key(self) -> str:
        let w = self.workspace
        let editing = w.strip_kind().name if w.editing() else ""
        return f"{w.network.scope} {w.network.context()} {w.network.has_inputs(w.network.scope)} {editing}"

    ## Create's entries: the Edit node's primitives, then the node categories.
    func create_items(self) -> list[Command]!:
        let w = self.workspace
        let result: list[Command] = []
        if w.editing() and tool_menus.from_registry(w):
            # Edit Mesh: the registry's Create tools, a submenu per family.
            for family in tool_menus.families(w, self.actions, "Create"):
                let title = Command(family.name)
                title.set_items(family.commands)
                result.append(title)
            result.append(Command("", divider = true))
        elif w.editing():
            let made = self.actions.create_commands()
            if made.length > 0:
                self.primitives.set_text(w.strip_kind().name + " primitives")
                self.primitives.set_items(made)
                result.append(self.primitives)
            let drawn = self.actions.draw_commands()
            if drawn.length > 0:
                self.drawing.set_text("Curves" if w.strip_kind().name == "Edit CAD" else "Draw")
                self.drawing.set_items(drawn)
                result.append(self.drawing)
            if made.length > 0 or drawn.length > 0:
                result.append(Command("", divider = true))
        if w.network.context() == "cad":
            result.append(self.actions.model.create_sketch)
            result.append(self.actions.model.edit_sketch)
            result.append(self.actions.model.show_sketch)
            result.append(self.actions.sketch_on_face)
            result.append(self.actions.extrude_face)
            result.append(self.actions.extrude_profiles)
            result.append(self.actions.revolve_profiles)
            result.append(self.actions.finish_sketch)
            result.append(Command("", divider = true))
        return result + node_categories(w, self.actions)

    ## The modeling submenu holds the active Edit kind's tools: the
    ## registry's Modify tools as Repeat and a submenu per family (Edit
    ## Mesh, Geo), else the kind's tools.
    func update_modeling(self) -> unit!:
        let tools = self.actions.modify_commands()
        let w = self.workspace
        let cad = w.network.context() == "cad"
        var key = f"{w.strip_kind().name} {cad and not w.editing()}"
        for tool in tools:
            key += "/" + tool.text()
        if key == self.modeling_key:
            return
        self.modeling_key = key
        self.selecting.set_items(tool_menus.select_menu(self.workspace, self.actions))
        self.modeling.set_text("CAD tools" if cad and not w.editing() else w.strip_kind().name + " tools")
        if not tool_menus.from_registry(w):
            self.families = []
            self.modeling.set_items(tools)
            return
        let titles: list[Command] = []
        for family in tool_menus.families(self.workspace, self.actions, "Modify"):
            let title = Command(family.name)
            title.set_items(family.commands)
            titles.append(title)
        let lead = [self.actions.press_pull, self.actions.extrude_face, Command("", divider = true)] if cad else []
        self.modeling.set_items(lead + [self.actions.repeat_tool] + titles)
        self.families = titles

## Enable submenu `title` while any of its items is enabled.
func enable_by_items(title: Command) -> unit!:
    var any = false
    for index in 0..<title.item_count():
        any = any or title.item(index).is_enabled()
    title.set_enabled(any)

## A submenu for each of the Tab menu's categories that offers something in
## the network shown, holding Actions' add commands: the Create menu's and
## the network's right-click Create.
pub func node_categories(w: Workspace, actions: Actions) -> list[Command]!:
    let context = w.network.context()
    let inputs = w.network.has_inputs(w.network.scope)
    let result: list[Command] = []
    for category in node_catalog.categories(context):
        if category == "All":
            continue
        let items: list[Command] = []
        for place in node_catalog.offered(context, inputs, category):
            items.append(actions.add[place])
        if items.length == 0:
            continue
        let title = Command(category)
        title.set_items(items)
        result.append(title)
    return result