Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.1

0.11.1 / src / inspector.luc

## The Parameters pane: a compact node header (name, type and flags, what it
## does), then the node's parameters as one ParameterPanel built from the
## catalog, one line each after Houdini's parameter pane. The catalog's rows
## name the value or text slot each edits; an Edit node adds its component
## edit rows. A drag is live and one undo step on release.
from luce_ui.ui import VStack, HStack, Text, TextField, Font, Button, ParameterPanel, ParameterSpec, ParameterKind, Rect
from interop import Connection
from workspace import Workspace
from actions import Actions
from node_catalog import Parameter
import node_catalog
from edit_kinds import is_edit
from usd_nodes import Composition, compose_usd
import modeling_commands
import node_commands

pub class Inspector:
    pub var on_browse: func() -> unit!? = none
    pub let body: VStack
    ## The selected node's parameters.
    pub let parameters: ParameterPanel
    let workspace: Workspace
    let actions: Actions
    let name: TextField
    let kind: Text
    let stats: Text
    let description: Text
    let state_rows: VStack
    let delete_button: Button
    let generic_rows: VStack
    let connections: list[Connection]
    ## The catalog rows the panel shows, and what each row is to the inspector:
    ## "value", "text", "info", "reload", "extrude", "clear", "last", "edits" or "operation".
    var rows: list[Parameter]
    var roles: list[str]
    ## What the panel was built for: node kind, file format and edit count.
    var built: str = ""
    ## A USD File node's composed stage, and the file, texts and revision
    ## it was composed for.
    var stage: Composition? = none
    var stage_key: str = ""
    var showing: int = -2

    pub func init(self, workspace: Workspace, actions: Actions, font: Font) -> unit!:
        self.workspace = workspace
        self.actions = actions
        self.connections = []
        self.rows = []
        self.roles = []
        self.name = TextField("", font = font, placeholder = "Node name", cells = 18.0)
        self.connections.append(self.name.on_commit(func (name: str) -> unit!: node_commands.rename(workspace, name)))
        self.kind = Text("", font = font, muted = true)
        self.stats = Text("", font = font, muted = true)
        self.description = Text("", font = font, muted = true)
        self.parameters = ParameterPanel([], font = font)
        self.connections.append(self.parameters.on_begin(func (index: int) -> unit!:
            if self.roles[index] in ["value", "last"]:
                workspace.begin("Change " + self.rows[index].name)))
        self.connections.append(self.parameters.on_change(self.changed))
        self.connections.append(self.parameters.on_commit(func (index: int) -> unit!:
            workspace.commit()))
        self.connections.append(self.parameters.on_action(self.acted))
        self.state_rows = VStack([
            HStack([Button(action = actions.output, font = font), Button(action = actions.auto_output, font = font), Button(action = actions.visibility, font = font)], spacing = 4.0),
            HStack([Button(action = actions.display, font = font), Button(action = actions.enabled, font = font)], spacing = 4.0)], spacing = 4.0)
        self.delete_button = Button(action = actions.delete, font = font)
        let header = VStack([self.name, self.kind, self.state_rows, self.description], spacing = 4.0, padding = 6.0)
        let footer = VStack([self.stats, self.delete_button], spacing = 4.0, padding = 6.0)
        self.generic_rows = VStack([header, self.parameters, footer], spacing = 0.0)
        self.body = VStack([self.generic_rows], spacing = 0.0)
        self.refresh()

    ## A parameter changed in the panel: its slots take the values (a vector
    ## its components), a text row its text.
    func changed(self, index: int) -> unit!:
        let role = self.roles[index]
        let row = self.rows[index]
        if role == "value":
            for component in 0..<row.components:
                node_commands.set_parameter(self.workspace, row.slot + component, self.parameters.component(index, component))
        elif role == "text":
            node_commands.set_text(self.workspace, row.slot, self.parameters.text(index))
        elif role == "last":
            modeling_commands.set_last_extrusion(self.workspace, self.parameters.value(index))

    func acted(self, index: int) -> unit!:
        let role = self.roles[index]
        if role == "text":
            if let callback = self.on_browse:
                callback()
        elif role == "reload":
            node_commands.reload_file(self.workspace)
        elif role == "extrude":
            self.actions.extrude.trigger()
        elif role == "clear":
            self.actions.clear_edits.trigger()
        elif role == "reselect":
            if let node = self.workspace.selected(): modeling_commands.reselect(self.workspace, node.id)
        elif role == "keep_ids":
            if let node = self.workspace.selected(): node_commands.keep_ids(self.workspace, node.id)

    ## The field showing value slot `slot`, in window points; empty when none shows it.
    pub func field_bounds(self, slot: int) -> Rect!:
        let origin = self.parameters.layout().bounds()
        for index in 0..<self.rows.length:
            let row = self.rows[index]
            if self.roles[index] == "value" and slot >= row.slot and slot < row.slot + row.components:
                let field = self.parameters.field_bounds(index, slot - row.slot)
                return Rect(origin.x + field.x, origin.y + field.y, field.width, field.height)
        return Rect()

    ## The value the panel shows for slot `slot`.
    pub func shown_value(self, slot: int) -> float!:
        for index in 0..<self.rows.length:
            let row = self.rows[index]
            if self.roles[index] == "value" and slot >= row.slot and slot < row.slot + row.components:
                return self.parameters.component(index, slot - row.slot)
        return 0.0

    ## Rebuild the panel's rows for `node`: the catalog's, and an Edit node's edits.
    func build(self, kind: str, path: str) -> unit!:
        self.rows = []
        self.roles = []
        var specs: list[ParameterSpec] = []
        for row in node_catalog.fields(kind, path, self.stage):
            specs.append(spec_of(row))
            self.rows.append(row)
            self.roles.append("text" if row.kind in ["text", "path"] else ("reload" if row.kind == "button" else ("info" if row.kind == "info" else "value")))
        if is_edit(kind):
            self.add(specs, "edits", ParameterSpec(name = "edits", label = "Edits", kind = ParameterKind.info))
            self.add(specs, "extrude", ParameterSpec(name = "extrude", label = "Extrude faces", kind = ParameterKind.button, join = true,
                                                      doc = "Extrude the selected faces by the tool amount (E)."))
            self.add(specs, "clear", ParameterSpec(name = "clear", label = "Clear edits", kind = ParameterKind.button,
                                                    doc = "Remove every component edit of this node."))
            self.add(specs, "last", ParameterSpec(name = "last", label = "Last extrusion", minimum = -100.0, maximum = 100.0, step = 0.1, decimals = 2,
                                                   doc = "Change the distance of the most recent extrusion."))
        # A verb node made from a selection: pick its components again, or keep
        # its numbers on a changed topology (Houdini's behaviour).
        if node_catalog.is_mesh_verb(kind):
            self.add(specs, "reselect", ParameterSpec(name = "reselect", label = "Reselect", kind = ParameterKind.button, join = true,
                                                      doc = "Show the input and pick the group's components again (Enter accepts)."))
            self.add(specs, "keep_ids", ParameterSpec(name = "keep_ids", label = "Keep IDs", kind = ParameterKind.button,
                                                      doc = "Use the group's numbers on the input's current topology."))
        self.parameters.set_specs(specs)

    ## An inspector row of the Edit node; `slot` is an operation's index.
    func add(self, specs: list[ParameterSpec], role: str, spec: ParameterSpec, slot: int = -1):
        specs.append(spec)
        self.rows.append(Parameter(spec.label, slot, 0.0))
        self.roles.append(role)

    pub func refresh(self) -> unit!:
        self.generic_rows.layout().set_visible(self.workspace.selected() != none)
        let node = self.workspace.selected() else return
        let changed = node.id != self.showing
        let file = node.kind == "File"
        let format = node_catalog.file_format(node.texts[0]) if file else ""
        let key = f"{node.kind}|{format}|{self.compose(node.texts, node.source_revision) if format == "usd" else ""}"
        if key != self.built:
            self.build(node.kind, node.texts[0])
            self.built = key
        self.showing = node.id
        self.state_rows.layout().set_visible(not file)
        self.delete_button.layout().set_visible(not file)
        if changed or not self.name.layout().is_focused():
            self.name.set_text(node.name)
        let network = self.workspace.network
        self.kind.set_text(node.kind + (" · Out" if network.is_output(node.id) else " · internal") + (" · Auto" if node.output == 0 else "") + (" · preview" if network.preview and node.id == network.display else "") + ("" if node.enabled else " · bypassed"))
        self.description.set_text(node.failure if node.failure != "" else ("Warning: " + node.warning if node.warning != "" else node_catalog.description(node.kind)))
        self.description.layout().set_visible(not file or node.failure != "" or node.warning != "")
        self.parameters.layout().set_enabled(node.enabled or file)
        for index in 0..<self.rows.length:
            let role = self.roles[index]
            let row = self.rows[index]
            if role == "value":
                for component in 0..<row.components:
                    self.parameters.set_component(index, component, node.values[row.slot + component])
            elif role == "text":
                # A selection too large to read as text shows as a summary
                # (the group itself stays exact); Reselect changes it.
                let text = node.texts[row.slot]
                let baked = row.name == "Group" and text.byte_count > 16384
                self.parameters.set_text(index, f"‹selection of {text.byte_count // 1024} KB›" if baked else text)
                self.parameters.set_enabled(index, not baked)
            elif role == "info":
                self.parameters.set_text(index, row.text)
            elif role == "edits":
                self.parameters.set_text(index, f"{node.edits.length} edits / {self.workspace.selected_count()} selected")
            elif role == "last":
                let last = node.edits.length > 0 and node.edits[-1].verb == "PolyExtrude"
                self.parameters.set_hidden(index, not last)
                if last:
                    self.parameters.set_value(index, node.edits[-1].numbers[0])
            elif role == "clear":
                self.parameters.set_enabled(index, node.edits.length > 0)
            elif role == "extrude":
                self.parameters.set_enabled(index, self.actions.extrude.is_enabled())
            elif role == "keep_ids":
                self.parameters.set_enabled(index, node.guard != 0)
        if node.kind == "Group":
            self.stats.set_text(f"{network.outputs(node.id).length} exposed children")
        elif self.workspace.compute != none:
            self.stats.set_text(node.phase if node.computing else node.summary)
        elif let data = node.cache:
            self.stats.set_text(data.description())
        else:
            self.stats.set_text("No evaluated geometry")

    ## Compose a USD File node's stage when its file, texts or revision
    ## changed (a failure shows as the node's cook error); the key composed.
    func compose(self, texts: list[str], revision: int) -> str:
        let key = f"{texts[0].trim()}|{texts[1]}|{texts[2]}|{revision}"
        if key != self.stage_key:
            self.stage_key = key
            self.stage = none
            let composed = compose_usd(texts[0].trim(), texts) catch failure:
                return key
            self.stage = composed
        return key

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

## The panel's description of a catalog row.
func spec_of(row: Parameter) -> ParameterSpec:
    let rest = row.rest + [0.0, 0.0, 0.0]
    let soft = row.soft_maximum > 0.0
    return ParameterSpec(name = row.key if row.key != "" else row.name.lower().replace(" ", "_"), label = row.name, kind = kind_of(row.kind),
                         components = row.components, value = row.initial, y = rest[0], z = rest[1], w = rest[2],
                         minimum = row.minimum, maximum = row.maximum, soft_minimum = (row.minimum if row.minimum >= 0.0 else -row.soft_maximum) if soft else 0.0,
                         soft_maximum = row.soft_maximum, step = row.step, decimals = row.decimals, suffix = row.suffix, slider = row.slider or soft,
                         items = row.items, doc = row.doc, hide_when = row.hide_when, disable_when = row.disable_when, join = row.join,
                         linear = row.kind == "color", tag = row.slot)

func kind_of(name: str) -> ParameterKind:
    if name == "integer":
        return ParameterKind.integer
    if name == "vector":
        return ParameterKind.vector
    if name == "toggle":
        return ParameterKind.toggle
    if name == "menu":
        return ParameterKind.menu
    if name == "text":
        return ParameterKind.text
    if name == "path":
        return ParameterKind.path
    if name == "button":
        return ParameterKind.button
    if name == "info":
        return ParameterKind.info
    if name == "folder":
        return ParameterKind.folder
    if name == "separator":
        return ParameterKind.separator
    if name == "color":
        return ParameterKind.color
    return ParameterKind.number