Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.2

0.11.2 / 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
from edit_types import level_names, sketch_level_names
from tools import transform_tools, transform_keys
import modeling_commands
import selection_commands
import node_commands
from luce_geocore.geocore import Domain

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
    ## The document's length unit.
    pub let millimeters: Command
    pub let inches: 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 group: Command
    pub let enabled: Command
    pub let freeze: Command
    pub let delete: Command
    pub let extrude: Command
    pub let clear_edits: Command
    pub let select_all: Command
    ## The splat brush's radius ([ and ]).
    pub let smaller_brush: Command
    pub let larger_brush: Command
    ## Edit GSplats: select only the splats seen, or through the depth (V).
    pub let visible_splats: Command
    ## The timeline's (timeline_pane.luc connects them): ↑ play or stop,
    ## ←/→ a frame back or on, ↓ the range's first frame.
    pub let play: Command
    pub let previous_frame: Command
    pub let next_frame: Command
    pub let first_frame: Command
    pub let last_frame: Command
    pub let looping: 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.millimeters = Command("Units: Millimeters", id = "units_mm")
        self.inches = Command("Units: Inches", id = "units_in")
        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", Shortcut(Key.u), id = "parent_group")
        self.group = Command("Group", Shortcut(Key.g, primary = true), id = "group_nodes")
        self.enabled = Command("Enable / bypass", Shortcut(Key.b), id = "toggle_node")
        self.freeze = Command("Freeze / unfreeze", Shortcut(Key.b, shift = true), id = "freeze_node")
        self.play = Command("Play", Shortcut(Key.up), id = "timeline_play")
        self.previous_frame = Command("Previous frame", Shortcut(Key.left), id = "timeline_previous")
        self.next_frame = Command("Next frame", Shortcut(Key.right), id = "timeline_next")
        self.first_frame = Command("First frame", Shortcut(Key.down), id = "timeline_first")
        self.last_frame = Command("Last frame", id = "timeline_last")
        self.looping = Command("Loop", id = "timeline_loop")
        self.delete = Command("Delete node", id = "delete_node")
        self.extrude = Command("Extrude faces", 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.group.on_trigger(func () -> unit!: node_commands.group_selected(workspace)),
            self.frame.on_trigger(workspace.frame_cube), self.grid.on_trigger(workspace.toggle_grid), self.hulls.on_trigger(workspace.toggle_hulls),
            self.millimeters.on_trigger(func () -> unit!: workspace.set_units("mm")), self.inches.on_trigger(func () -> unit!: workspace.set_units("in")),
            self.display.on_trigger(func () -> unit!: node_commands.display_selected(workspace)), self.enabled.on_trigger(func () -> unit!: node_commands.toggle_enabled(workspace)),
            self.freeze.on_trigger(func () -> unit!: node_commands.toggle_freeze(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 (Object, Point, Edge, Face, Smart): their
        # labels follow the Edit kind's levels (Vertices, CVs, …).
        let keys = [Key.digit_1, Key.digit_2, Key.digit_3, Key.digit_4, Key.digit_5]
        self.modes = []
        for index in 0..<5:
            self.modes.append(Command(level_names()[index], Shortcut(keys[index]), id = f"select_level_{index + 1}"))
        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)))
        # The transform tools, Maya's keys: Select, Move, Rotate, Scale and
        # the selected node's own handles (Manipulator).
        let tool_keys = transform_keys()
        self.tools = []
        for index in 0..<transform_tools().length:
            let tool = transform_tools()[index]
            let command = Command(tool, Shortcut(tool_keys[index]), id = "tool_" + tool.lower())
            self.tools.append(command)
            self.connections.append(command.on_trigger(func (): modeling_commands.choose_tool(workspace, tool)))
        self.smaller_brush = Command("Smaller brush", Shortcut(Key.left_bracket), id = "smaller_brush")
        self.larger_brush = Command("Larger brush", Shortcut(Key.right_bracket), id = "larger_brush")
        self.connections.append(self.smaller_brush.on_trigger(func (): modeling_commands.scale_brush(workspace, 0.8)))
        self.connections.append(self.larger_brush.on_trigger(func (): modeling_commands.scale_brush(workspace, 1.25)))
        self.visible_splats = Command("Select visible splats only", Shortcut(Key.v), id = "visible_splats")
        self.connections.append(self.visible_splats.on_trigger(func (): modeling_commands.toggle_visible_splats(workspace)))
        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.group, self.enabled, self.freeze, self.play, self.previous_frame, self.next_frame, self.first_frame, self.last_frame, self.looping, self.delete, self.extrude, self.clear_edits, self.select_all, self.smaller_brush, self.larger_brush, self.visible_splats, self.millimeters, self.inches] + 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 Modify marking menu's tools: what the active Edit kind offers
    ## (Edit Mesh's on other nodes), Extrude first.
    pub func modify_commands(self) -> list[Command]:
        let kind = self.workspace.strip_kind()
        let result: list[Command] = []
        if kind.tool("Extrude") != none:
            result.append(self.extrude)
        for command in self.modeling:
            if kind.tool(command.text()) != none: result.append(command)
        return result

    ## The Create marking menu's primitives inside an Edit node.
    pub func create_commands(self) -> list[Command]:
        let kind = self.workspace.strip_kind()
        let result: list[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.freeze.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 and not w.sketching())
        # A sketch being edited takes single keys for its tools (D, O, …).
        if w.sketching():
            for command in [self.display, self.output, self.enabled] + self.tools:
                command.set_enabled(false)
        self.group.set_enabled(node != none and not w.network.in_material())
        if let selected = node:
            self.enabled.set_checked(selected.enabled)
            self.freeze.set_checked(selected.frozen != "")
            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.on_domain(Domain.face) and w.tool_available("Extrude"))
        self.select_all.set_enabled(selecting)
        let levels = w.levels()
        let sketching = w.sketching()
        for index in 0..<self.modes.length:
            self.modes[index].set_enabled((selecting and w.level_available(index)) or (sketching and index > 0))
            self.modes[index].set_checked(index == (w.sketch_level if sketching else w.component_mode))
            if sketching:
                self.modes[index].set_text(sketch_level_names()[index])
            elif index < levels.length and levels[index].available:
                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:
            if not w.sketching():
                tool.set_enabled(true)
            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)
        self.millimeters.set_checked(w.network.units == "mm")
        self.inches.set_checked(w.network.units == "in")

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