Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.1

0.11.1 / src / actions.luc

## The same Command instances drive toolbar buttons, menus and shortcuts.
from luce_ui.ui import Command, Shortcut
from luce_window.input import Key
from interop import Connection
from workspace import Workspace
from network import kinds
from edit_kinds import is_edit, edit_tool_names, edit_primitive_names
import modeling_commands
import selection_commands
import node_commands

pub class Actions:
    pub let new_project: Command
    pub let open_project: Command
    pub let save: Command
    pub let save_as: Command
    pub let reset_layout: Command
    pub let undo: Command
    pub let redo: Command
    pub let frame: Command
    pub let grid: Command
    pub let hulls: Command
    pub let display: Command
    pub let output: Command
    pub let auto_output: Command
    pub let visibility: Command
    pub let parent: Command
    pub let enabled: Command
    pub let delete: Command
    pub let extrude: Command
    pub let clear_edits: Command
    pub let select_all: Command
    pub let modes: list[Command]
    pub let tools: list[Command]
    pub let add: list[Command]
    pub let modeling: list[Command]
    ## Create a primitive in the displayed Edit node.
    pub let create: list[Command]
    ## Selection edits: invert, grow, shrink, border, flood, loop, ring.
    pub let selection_tools: list[Command]
    pub let all: list[Command]
    let connections: list[Connection]
    let workspace: Workspace

    pub func init(self, workspace: Workspace) -> unit!:
        self.workspace = workspace
        self.new_project = Command("New project", Shortcut(Key.n, primary = true), id = "new_project")
        self.open_project = Command("Open project…", Shortcut(Key.o, primary = true), id = "open_project")
        self.save = Command("Save", Shortcut(Key.s, primary = true), id = "save_project")
        self.save_as = Command("Save As…", Shortcut(Key.s, primary = true, shift = true), id = "save_project_as")
        self.reset_layout = Command("Reset layout", id = "reset_layout")
        self.undo = Command("Undo", Shortcut(Key.z, primary = true), id = "undo")
        self.redo = Command("Redo", Shortcut(Key.z, primary = true, shift = true), id = "redo")
        self.frame = Command("Frame geometry", Shortcut(Key.f), id = "frame")
        self.grid = Command("Grid", Shortcut(Key.g), id = "grid")
        self.hulls = Command("Control hulls", id = "control_hulls")
        self.display = Command("Preview selected", Shortcut(Key.d), id = "display_node")
        self.output = Command("Out", Shortcut(Key.o), id = "output_node")
        self.auto_output = Command("Auto Out", id = "auto_output")
        self.visibility = Command("Visible", id = "visible_node")
        self.parent = Command("Up", id = "parent_group")
        self.enabled = Command("Enable / bypass", Shortcut(Key.b), id = "toggle_node")
        self.delete = Command("Delete node", id = "delete_node")
        self.extrude = Command("Extrude faces", Shortcut(Key.e), id = "extrude_faces")
        self.clear_edits = Command("Clear edits", id = "clear_edits")
        self.select_all = Command("Select all", Shortcut(Key.a, primary = true), id = "select_all")
        self.connections = [self.undo.on_trigger(workspace.undo), self.redo.on_trigger(workspace.redo),
            self.output.on_trigger(func () -> unit!: node_commands.toggle_output(workspace)), self.auto_output.on_trigger(func () -> unit!: node_commands.auto_output(workspace)),
            self.visibility.on_trigger(func () -> unit!: node_commands.toggle_visible(workspace)), self.parent.on_trigger(func () -> unit!: node_commands.leave_group(workspace)),
            self.frame.on_trigger(workspace.frame_cube), self.grid.on_trigger(workspace.toggle_grid), self.hulls.on_trigger(workspace.toggle_hulls),
            self.display.on_trigger(func () -> unit!: node_commands.display_selected(workspace)), self.enabled.on_trigger(func () -> unit!: node_commands.toggle_enabled(workspace)),
            self.delete.on_trigger(func () -> unit!: node_commands.delete_selected(workspace)), self.extrude.on_trigger(func () -> unit!: modeling_commands.extrude(workspace)),
            self.clear_edits.on_trigger(func () -> unit!: modeling_commands.clear_edits(workspace)), self.select_all.on_trigger(func () -> unit!: selection_commands.select_all(workspace))]
        self.add = []
        self.modeling = []
        # Every Edit kind's tools and primitives, from the kind registry
        # (Extrude has its own command); the strip shows the active kind's.
        for operation in edit_tool_names():
            if operation == "Extrude": continue
            let name = operation
            let command = Command(operation, id = "edit_" + operation)
            self.modeling.append(command)
            self.connections.append(command.on_trigger(func () -> unit!: modeling_commands.model(workspace, name)))
        self.selection_tools = [Command("Invert selection", Shortcut(Key.i, primary = true), id = "select_invert")]
        self.connections.append(self.selection_tools[0].on_trigger(func () -> unit!: selection_commands.invert_selection(workspace)))
        let walks = ["Grow selection", "Shrink selection", "Select border", "Select connected", "Select loop", "Select ring"]
        for index in 0..<walks.length:
            let operation = index
            let command = Command(walks[index], id = "select_walk_" + str(index))
            self.selection_tools.append(command)
            self.connections.append(command.on_trigger(func () -> unit!: selection_commands.walk_selection(workspace, operation)))
        # Selection levels 1-5: their labels follow the Edit kind's levels.
        self.modes = [Command("Object", Shortcut(Key.digit_1), id = "select_level_1"), Command("Polygons", Shortcut(Key.digit_2), id = "select_level_2"),
            Command("Edges", Shortcut(Key.digit_3), id = "select_level_3"), Command("Vertices", Shortcut(Key.digit_4), id = "select_level_4"),
            Command("Corners", Shortcut(Key.digit_5), id = "select_level_5")]
        for index in 0..<self.modes.length:
            let mode = index
            self.connections.append(self.modes[index].on_trigger(func () -> unit!:
                selection_commands.set_mode(workspace, mode)))
        # Primitives an Edit node creates (each kind offers its own).
        self.create = []
        for primitive in edit_primitive_names():
            let name = primitive
            let command = Command(primitive, id = "create_" + primitive.lower().replace(" ", "_"))
            self.create.append(command)
            self.connections.append(command.on_trigger(func () -> unit!: modeling_commands.create(workspace, name)))
        self.tools = [Command("Select", Shortcut(Key.q), id = "tool_select"), Command("Move", Shortcut(Key.m), id = "tool_move")]
        self.connections.append(self.tools[0].on_trigger(func (): modeling_commands.choose_tool(workspace, "Select")))
        self.connections.append(self.tools[1].on_trigger(func (): modeling_commands.choose_tool(workspace, "Move")))
        for kind in kinds():
            self.add.append(Command(kind, id = "add_" + kind))
        self.all = [self.undo, self.redo, self.frame, self.grid, self.hulls, self.display, self.output, self.auto_output, self.visibility, self.parent, self.enabled, self.delete, self.extrude, self.clear_edits, self.select_all] + self.add + self.modes + self.tools + self.modeling + self.create + self.selection_tools
        self.all += [self.new_project, self.open_project, self.save, self.save_as, self.reset_layout]
        self.refresh()

    ## The viewport's tool strip: Select, Move, then what the active Edit
    ## kind offers (Edit Mesh's on other nodes): Extrude, its tools, its
    ## primitives.
    pub func strip(self) -> list[Command]:
        let kind = self.workspace.strip_kind()
        let result = self.tools.copy()
        if kind.tool("Extrude") != none:
            result.append(self.extrude)
        for command in self.modeling:
            if kind.tool(command.text()) != none: result.append(command)
        for command in self.create:
            if command.text() in kind.primitives: result.append(command)
        return result

    pub func refresh(self) -> unit!:
        let w = self.workspace
        self.undo.set_enabled(w.history.can_undo() or w.in_transaction())
        self.redo.set_enabled(w.history.can_redo())
        self.undo.set_text(w.history.undo_label())
        self.redo.set_text(w.history.redo_label())
        let node = w.selected()
        self.delete.set_enabled(node != none)
        self.enabled.set_enabled(node != none)
        self.display.set_enabled(node != none)
        self.output.set_enabled(node != none)
        self.auto_output.set_enabled(node != none)
        self.visibility.set_enabled(node != none)
        self.parent.set_enabled(w.network.scope >= 0)
        if let selected = node:
            self.enabled.set_checked(selected.enabled)
            self.display.set_checked(w.network.preview and selected.id == w.network.display)
            self.output.set_checked(w.network.is_output(selected.id))
            self.auto_output.set_checked(selected.output == 0)
            self.visibility.set_checked(selected.visible)
            self.clear_edits.set_enabled(is_edit(selected.kind) and selected.edits.length > 0)
        else:
            self.clear_edits.set_enabled(false)
        let selecting = w.selecting()
        self.extrude.set_enabled(selecting and w.domain() == 2 and w.tool_available("Extrude"))
        self.select_all.set_enabled(selecting)
        let levels = w.levels()
        for index in 0..<self.modes.length:
            self.modes[index].set_enabled(selecting and index < levels.length)
            self.modes[index].set_checked(index == w.component_mode)
            if index < levels.length:
                self.modes[index].set_text(levels[index].name)
        for command in self.create:
            command.set_enabled(w.editing() and w.tool_available(command.text()))
        for tool in self.tools:
            tool.set_enabled(selecting)
            tool.set_checked(tool.text() == w.edit_tool)
        # Tools apply to the selection, or prompt for one (Tool → Select);
        # on the Edit node they need a selection.
        for command in self.modeling:
            command.set_enabled(selecting and w.tool_available(command.text()) and (not w.editing() or w.selected_count() > 0 or command.text() in ["Subdivide", "Clean"] or command.text() in w.strip_kind().draw_tools))
        for command in self.selection_tools:
            command.set_enabled(selecting and w.selected_count() > 0)
        self.grid.set_checked(w.view.grid_visible)
        self.hulls.set_checked(w.view.hulls_visible)

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