Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.5

0.11.5 / src / outliner.luc

## Outliner projection: exposed nodes plus Group containment, never wire ancestry.
from luce_ui.ui import Widget, Layout, Constraints, Size, Painter, Event, Font
from luce_gpu.gpu import RenderTarget, Rect
from luce_window.input import EventKind, Key
from workspace import Workspace
import theme
from luce_std import math
import time
from tool_icons import draw_icon
import node_commands
import take_commands
from network import Network
import node_catalog

pub class OutlineRow:
    pub let id: int
    pub let depth: int
    pub let visible: bool
    pub let path: str
    pub let branch: bool
    ## A path row hidden itself (not by an ancestor).
    pub let hidden: bool
    pub func init(self, id: int, depth: int, visible: bool, path: str = "", branch: bool = false, hidden: bool = false):
        self.id = id
        self.depth = depth
        self.visible = visible
        self.path = path
        self.branch = branch
        self.hidden = hidden

## The objects: exposed nodes and, inside Groups, theirs; under each node the
## object paths of its result, hidden ones too (they left the result). A
## collapsed Group or path (`node id:path`) hides its rows.
pub func object_rows(graph: Network, collapsed: list[int], collapsed_paths: list[str]) -> list[OutlineRow]!:
    let rows: list[OutlineRow] = []
    append_scope(graph, rows, collapsed, collapsed_paths, -1, 0, true)
    return rows

func append_scope(graph: Network, rows: list[OutlineRow], collapsed: list[int], collapsed_paths: list[str], parent: int, depth: int, visible: bool) -> unit!:
    for node in graph.outputs(parent):
        let shown = visible and node.visible
        rows.append(OutlineRow(node.id, depth, shown))
        if node_catalog.places_children(node.kind) and not (node.id in collapsed):
            append_scope(graph, rows, collapsed, collapsed_paths, node.id, depth + 1, shown and node.enabled)
        elif not node_catalog.places_children(node.kind):
            let paths = node.group_paths.copy()
            for path in node.hidden:
                if not (path in paths):
                    paths.append(path)
            append_paths(rows, collapsed_paths, node.id, node.hidden, paths, "", depth + 1, shown)

func append_paths(rows: list[OutlineRow], collapsed_paths: list[str], id: int, hidden: list[str], paths: list[str], parent: str, depth: int, visible: bool):
    let children: list[str] = []
    for path in paths:
        if parent != "" and not path.starts_with(parent + "/"): continue
        let remainder = path[parent.length + 1..<path.length] if parent != "" else path
        # Absolute paths (USD's "/World/...") start with an empty component:
        # the first named one is their top level.
        var component = ""
        for part in remainder.split("/"):
            if part != "":
                component = part
                break
        if component == "": continue
        if parent == "" and remainder.starts_with("/"): component = "/" + component
        if not (component in children): children.append(component)
    for component in children:
        let path = parent + "/" + component if parent != "" else component
        var branch = false
        for candidate in paths:
            if candidate.starts_with(path + "/"): branch = true
        let shown = visible and not (path in hidden)
        rows.append(OutlineRow(id, depth, shown, path, branch, hidden = path in hidden))
        if branch and not (f"{id}:{path}" in collapsed_paths):
            append_paths(rows, collapsed_paths, id, hidden, paths, path, depth + 1, shown)

pub class Outliner: Widget:
    let box: Layout
    let workspace: Workspace
    let font: Font
    pub var rows: list[OutlineRow]
    var collapsed: list[int]
    var collapsed_paths: list[str]
    var scroll: float = 0.0
    var hovered: int = -1
    var last_node: int = -1
    var last_click: int = 0

    pub func init(self, workspace: Workspace, font: Font? = none) -> unit!:
        self.workspace = workspace
        self.font = font else Font()
        self.rows = []
        self.collapsed = []
        self.collapsed_paths = []
        self.box = Layout()
        self.box.set_flex()
        self.box.set_interactive(true)
        self.box.set_focusable(true)
        self.box.set_takes_scroll(true)

    pub func layout(self) -> Layout:
        return self.box
    pub func measure(self, available: Constraints) -> Size!:
        return Size(300.0, 210.0)

    func row_height(self) -> float:
        return self.font.line_height() + 6.0

    pub func refresh(self) -> unit!:
        self.rows = object_rows(self.workspace.network, self.collapsed, self.collapsed_paths)
        self.scroll = math.clamp(self.scroll, 0.0, math.max(0.0, float(self.rows.length) * self.row_height() - self.box.bounds().height))
        self.box.invalidate()

    pub func draw(self, target: RenderTarget) -> unit!:
        let size = target.size()
        let p = Painter(target)
        p.rectangle(Rect(0.0, 0.0, size.width, size.height), theme.backdrop())
        let picked = self.workspace.network.selection()
        for index in 0..<self.rows.length:
            let row = self.rows[index]
            let y = float(index) * self.row_height() - self.scroll
            if y + self.row_height() < 0.0 or y > size.height:
                continue
            let node = self.workspace.network.require(row.id)
            let selected = row.path == "" and node.id in picked
            if selected or self.hovered == index:
                p.rectangle(Rect(0.0, y, size.width, self.row_height() - 1.0), theme.surface() if not selected else theme.grey(74.0))
            if selected:
                p.rectangle(Rect(0.0, y, 3.0, self.row_height() - 1.0), theme.accent())
            let x = 10.0 + float(row.depth) * 18.0
            let text_y = y + (self.row_height() - self.font.height()) * 0.5
            let ink = theme.text() if row.visible else theme.text_muted()
            if row.path != "" and row.branch:
                p.text(">" if f"{node.id}:{row.path}" in self.collapsed_paths else "v", x, text_y, ink, self.font)
            elif row.path == "" and node_catalog.places_children(node.kind):
                p.text(">" if node.id in self.collapsed else "v", x, text_y, ink, self.font)
            draw_icon(p, ("Group" if row.branch else "Cube") if row.path != "" else node.kind, x + 17.0, y + (self.row_height() - self.font.height()) * 0.5, self.font.height(), ink)
            let limit = int(math.max(1.0, (size.width - x - 75.0) / self.font.advance()))
            let label = row.path.split("/")[-1] if row.path != "" else node.name
            p.text(label if label.length <= limit else label[0..<limit] + "…", x + 40.0, text_y, ink, self.font)
            p.text(("o" if node.visible else "-") if row.path == "" else ("-" if row.hidden else "o"), size.width - 23.0, text_y, ink, self.font)
        if self.rows.length == 0:
            p.text("No exposed outputs", 12.0, 12.0, theme.text_muted(), self.font)
        p.close()

    pub func event(self, event: Event) -> unit!:
        if event.cancelled:
            self.last_node = -1
            return
        if event.kind == EventKind.scroll:
            self.scroll = math.clamp(self.scroll - event.scroll_y * 24.0, 0.0, math.max(0.0, float(self.rows.length) * self.row_height() - self.box.bounds().height))
        elif event.kind == EventKind.key_down and event.key == Key.enter:
            node_commands.enter_group(self.workspace, self.workspace.network.selected)
        elif event.kind == EventKind.pointer_down and event.button == 0 and event.inside:
            let index = int((event.y + self.scroll) / self.row_height())
            if index >= 0 and index < self.rows.length:
                let row = self.rows[index]
                let node = self.workspace.network.require(row.id)
                let x = 10.0 + float(row.depth) * 18.0
                if row.path != "":
                    if event.x > self.box.bounds().width - 32.0:
                        take_commands.toggle_path(self.workspace, row.id, row.path)
                    elif row.branch and event.x >= x and event.x < x + 16.0:
                        let key = f"{row.id}:{row.path}"
                        if let at = self.collapsed_paths.index_of(key): _ = self.collapsed_paths.remove_at(at)
                        else: self.collapsed_paths.append(key)
                    else:
                        node_commands.reveal_node(self.workspace, node.id)
                    self.refresh()
                    return
                if node_catalog.places_children(node.kind) and event.x >= x and event.x < x + 16.0:
                    if let at = self.collapsed.index_of(node.id):
                        _ = self.collapsed.remove_at(at)
                    else:
                        self.collapsed.append(node.id)
                elif event.x > self.box.bounds().width - 32.0:
                    node_commands.select_node(self.workspace, node.id)
                    node_commands.toggle_visible(self.workspace)
                else:
                    let now = time.now()
                    let double = self.last_node == node.id and now - self.last_click < 400000000
                    node_commands.reveal_node(self.workspace, node.id)
                    if double and node_catalog.places_children(node.kind):
                        node_commands.enter_group(self.workspace, node.id)
                    self.last_node = -1 if double else node.id
                    self.last_click = now
                self.refresh()
        elif event.kind == EventKind.pointer_moved:
            let index = int((event.y + self.scroll) / self.row_height())
            self.hovered = index if event.inside and index >= 0 and index < self.rows.length else -1
        elif event.kind == EventKind.pointer_left:
            self.hovered = -1
        self.box.invalidate()