Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / src / viewport_menu.luc

## The viewport's right-click menu, Fusion's way: it offers what applies to
## what is under the pointer or selected (a click without dragging; Alt and
## the right button still zoom the camera; a component under the pointer,
## with nothing selected, is selected first: panels.luc).
##   In a sketch: Delete for selected entities or constraints (a glyph or
##     dimension right-clicked is selected first); for one dimension, Edit
##     value (its box on its label) and Driven or Driving; Extrude (its
##     picked profiles, else its closed shapes), Finish Sketch, Revolve
##     (its picked profiles, then the axis), and Create
##     with its CREATE tools; for selected projected curves, Re-link (the
##     next edge clicked) and Break link; while Project is the tool,
##     Project body (the input body's outline, Fusion's Bodies filter);
##     Slice (the body in front of the plane cut away from view).
##   In a CAD node: on selected faces Sketch on face, Extrude face and
##     Press Pull (an Offset Face), on selected edges Press Pull (a Fillet),
##     then the CAD tools the selection's level takes, by family (Faces,
##     Bodies, Blends, the surfacing tools…: tool_menus.luc); with nothing
##     selected, the Create tools and a Modify submenu holding every other,
##     each waiting for its picks.
##   In an edited Edit CAD model: its steps the selection's level takes, by
##     family (Add as fillet set while a Fillet is the last step; Select
##     blends on faces), or its sketches, curve drawing tools and primitives
##     (model_entries).
##   In Geo and an edited Edit Mesh, with components selected: Repeat, the
##     registry's Modify tools the level takes, by family, and Select (the
##     Select tools and selection commands: tool_menus.luc); with nothing
##     selected, Create (an Edit Mesh's primitives and drawing tools, else
##     the nodes) and Select.
##   Elsewhere: Create with the nodes the Tab menu offers here.
## The menu is a ContextMenu anchored on an empty label in the footer, its
## entries chosen as it opens.
from luce_ui.ui import Text, Font, Command, ContextMenu
from interop import Connection
from workspace import Workspace
from actions import Actions
from sketch_editor import SketchEditor
from top_menus import node_categories
import sketch_tools
import sketch_picking
import cad_commands
import edit_cad_commands
import component_moves
import tool_menus

pub class ViewportMenu:
    let w: Workspace
    let actions: Actions
    let editor: SketchEditor
    let anchor: Text
    pub let menu: ContextMenu
    ## The entries as last opened.
    pub let commands: list[Command]
    ## Delete in a sketch: its selected entities and constraints.
    pub let delete_sketch: Command
    ## A selected dimension's value box, and its Driven or Driving switch.
    pub let edit_dimension: Command
    pub let driven: Command
    ## Selected projected curves: follow another edge, or none.
    pub let relink: Command
    pub let break_link: Command
    pub let project_body: Command
    ## The sketch's CREATE tools, one command each.
    let sketch_create: Command
    let create: Command
    ## The CAD Modify features, with nothing selected.
    let modify: Command
    ## The Select tools and selection commands, with components selected.
    let select: Command
    let connections: list[Connection]

    pub func init(self, w: Workspace, actions: Actions, editor: SketchEditor, font: Font) -> unit!:
        self.w = w
        self.actions = actions
        self.editor = editor
        self.connections = []
        self.delete_sketch = Command("Delete", id = "delete_sketch_selection")
        self.connections.append(self.delete_sketch.on_trigger(func () -> unit!: editor.remove_selection()))
        self.edit_dimension = Command("Edit value", id = "edit_sketch_dimension")
        self.connections.append(self.edit_dimension.on_trigger(func () -> unit!:
            if editor.constraint_selection.length == 1:
                editor.edit_dimension(editor.constraint_selection[0])))
        self.relink = Command("Re-link", id = "sketch_relink")
        self.connections.append(self.relink.on_trigger(func () -> unit!: editor.relink_selection()))
        self.break_link = Command("Break link", id = "sketch_break_link")
        self.connections.append(self.break_link.on_trigger(func () -> unit!: editor.break_links()))
        self.project_body = Command("Project body", id = "sketch_project_body", icon = "project-geometry")
        self.connections.append(self.project_body.on_trigger(func () -> unit!: editor.project_body()))
        self.driven = Command("Make driven", id = "sketch_dimension_driven")
        self.connections.append(self.driven.on_trigger(func () -> unit!:
            let sketch = editor.sketch else return
            if editor.constraint_selection.length == 1:
                let chosen = editor.constraint_selection[0]
                sketch_picking.set_driven(editor, chosen, not sketch.driven(chosen))))
        self.sketch_create = Command("Create", id = "sketch_create")
        let tools: list[Command] = []
        for tool in sketch_tools.tools(0):
            for each in (tool.variants if tool.variants.length > 0 else [tool]):
                if not each.ready:
                    continue
                let name = each.name
                let command = Command(name, id = "sketch_tool_" + name.lower().replace(" ", "_"), icon = each.icon)
                tools.append(command)
                self.connections.append(command.on_trigger(func () -> unit!: editor.choose(name)))
        self.sketch_create.set_items(tools)
        self.create = Command("Create", id = "viewport_create")
        self.modify = Command("Modify", id = "viewport_modify")
        self.select = Command("Select", id = "viewport_select")
        self.anchor = Text("", font = font)
        self.commands = self.entries()
        self.menu = ContextMenu(self.anchor, self.commands, font = font)

    ## What applies now: in a sketch, to its selection; elsewhere, Create.
    pub func entries(self) -> list[Command]!:
        let result: list[Command] = []
        if self.w.sketching():
            let e = self.editor
            if e.selection.length > 0 or e.constraint_selection.length > 0:
                let one = e.selection.length == 0 and e.constraint_selection.length == 1
                let dimension = one and e.dimensions.find(e.constraint_selection[0]) != none
                if dimension:
                    let sketch = e.sketch else return result
                    self.driven.set_text("Make driving" if sketch.driven(e.constraint_selection[0]) else "Make driven")
                    result.append(self.edit_dimension)
                    result.append(self.driven)
                if e.linked_selection().length > 0:
                    result.append(self.relink)
                    result.append(self.break_link)
                self.delete_sketch.set_text("Delete dimension" if dimension else ("Delete constraint" if one else "Delete"))
                result.append(self.delete_sketch)
                result.append(Command("", divider = true))
            # Project's Bodies filter: the body's outline.
            if e.tool == "Project":
                result.append(self.project_body)
                result.append(Command("", divider = true))
            result.append(self.actions.extrude_profiles)
            result.append(self.actions.finish_sketch)
            result.append(self.actions.revolve_profiles)
            result.append(self.actions.redefine_plane)
            result.append(self.actions.slice)
            result.append(Command("", divider = true))
            result.append(self.sketch_create)
            return result
        if edit_cad_commands.active(self.w):
            return self.model_entries()
        if self.w.network.context() == "cad":
            let a = self.actions
            if self.w.selecting() and self.w.selected_count() > 0:
                if cad_commands.selected_faces(self.w).length > 0:
                    result.extend([a.sketch_on_face, a.extrude_face])
                result.append(a.press_pull)
                return result + divided(tool_menus.families(self.w, a, "Modify", self.w.level_index()), true)
            self.modify.set_items(divided(tool_menus.families(self.w, a, "Modify"), false))
            return divided(tool_menus.families(self.w, a, "Create"), false) + [Command("", divider = true), self.modify]
        if tool_menus.from_registry(self.w) and self.w.selecting():
            if self.w.selected_count() > 0:
                return self.tool_entries()
            # Nothing selected: Create, and Select (the tools that look at
            # everything, Select all, the sets, Unhide).
            if self.w.editing():
                self.create.set_items(divided(tool_menus.families(self.w, self.actions, "Create"), false))
            else:
                self.create.set_items(node_categories(self.w, self.actions))
            result.append(self.create)
            if self.w.editing():
                result.extend([self.actions.snap_target, self.actions.clear_snap_target])
            self.select.set_items(tool_menus.select_commands(self.w, self.actions))
            result.append(self.select)
            return result
        self.create.set_items(node_categories(self.w, self.actions))
        result.append(self.create)
        return result

    ## Components selected in Geo or an Edit Mesh: Repeat, the tools the
    ## level takes by family (a divider between families), then Select.
    func tool_entries(self) -> list[Command]!:
        let result = tool_menus.repeat(self.w, self.actions)
        if result.length > 0:
            result.append(Command("", divider = true))
        for family in tool_menus.families(self.w, self.actions, "Modify", self.w.level_index()):
            result.extend(family.commands)
            result.append(Command("", divider = true))
        self.select.set_items(tool_menus.select_commands(self.w, self.actions))
        result.append(self.select)
        return result

    ## In an edited Edit CAD model, Fusion's direct modeling on what is
    ## selected: faces (Create Sketch on it, Extrude face, Press Pull and
    ## the face tools), edges (Press Pull, Fillet, Chamfer), bodies
    ## (Move/Copy, Delete, Shell, Combine, Split Body), CVs (Noise, Flatten,
    ## Snap and the NURBS structure steps); with nothing
    ## selected, Create Sketch, Edit Sketch, Show/Hide Sketch and the
    ## primitives.
    func model_entries(self) -> list[Command]!:
        let a = self.actions
        let result: list[Command] = []
        # A sketch's curve right-clicked: edit it, or hide it.
        let sketch = self.w.hovered_sketch
        if sketch != "" and self.w.selected_count() == 0:
            for title in [a.model.edit_sketch, a.model.show_sketch, a.model.redefine_sketch]:
                for index in 0..<title.item_count():
                    let item = title.item(index)
                    if item.text() in ["Edit " + sketch, "Hide " + sketch, f"Redefine {sketch} plane"]:
                        result.append(item)
            if result.length > 0:
                return result
        let faces = edit_cad_commands.selected_faces(self.w).length > 0
        if faces:
            result.extend([a.sketch_on_face, a.extrude_face, a.press_pull, a.model.select_blends])
        elif edit_cad_commands.selected_edges(self.w):
            result.append(a.press_pull)
            # The last step a Fillet: the edges as its next selection set.
            if a.model.fillet_set.is_enabled():
                result.append(a.model.fillet_set)
        # CVs picked: the structure tools on their surface.
        let cvs = component_moves.at_cvs(self.w) and self.w.selected_count() > 0
        if faces or cvs or edit_cad_commands.selected_edges(self.w) or edit_cad_commands.selected_models(self.w) or edit_cad_commands.selected_curves(self.w):
            # The model's tools the level takes, by family.
            return result + divided(tool_menus.families(self.w, a, "Modify", self.w.level_index()), result.length > 0)
        let model = a.model
        result.append(model.create_sketch)
        result.append(model.select_blends)
        # Alias's curves: drawn with clicks.
        result.extend(a.draw_commands())
        if model.edit_sketch.is_enabled():
            result.append(model.edit_sketch)
            result.append(model.show_sketch)
            result.append(model.redefine_sketch)
        result.append(Command("", divider = true))
        self.create.set_items(a.create_commands())
        result.append(self.create)
        return result

    ## Open at (`x`, `y`) in window points.
    pub func open(self, x: float, y: float) -> unit!:
        self.actions.refresh()
        let entries = self.entries()
        # The new entries reach the menu before the old ones are let go.
        self.menu.set_commands(entries)
        self.commands.clear()
        self.commands.extend(entries)
        let origin = self.anchor.layout().bounds()
        self.menu.open_at(x - origin.x, y - origin.y)

    pub func is_open(self) -> bool:
        return self.menu.is_open()

    pub func close(self):
        for connection in self.connections:
            connection.disconnect()
        self.connections.clear()

## Families' commands in one list, a divider before each family (before
## the first too when `lead`: entries come before them).
func divided(families: list[tool_menus.Family], lead: bool) -> list[Command]!:
    let result: list[Command] = []
    for family in families:
        if result.length > 0 or lead:
            result.append(Command("", divider = true))
        result.extend(family.commands)
    return result