dymokomi/luced-3d
Source of release 0.11.6
## Outliner projection of the root scene (docs/NETWORK-LEVELS.md): every
## object-level node, under each container what it yields, under a node the
## object paths of its result; never wire ancestry. While the network view
## is inside a container, the rows outside it are greyed; a click on any row
## takes the network view to that node's network. A right click on a node's
## row does that too and opens the network's node menu (on_menu).
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
from node import Node
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
import levels
from context_view import is_outside
pub class OutlineRow:
pub let id: int
pub let depth: int
pub let visible: bool
pub let path: str
pub let branch: bool
## On an object level: the row's eye toggles the node's display flag.
pub var object: bool = false
## 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: every node of an object level (the root, a scene Group),
## flagged or not; under a container what it yields (a data container's
## output node, a library's materials); under each other node the object
## paths of its result, hidden ones too (they left the result). A collapsed
## container 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!:
let object = graph.level_of(parent) == levels.object_level
let nodes = graph.children(parent) if object else graph.outputs(parent)
for node in nodes:
if object and node.kind == "Render":
continue
let shown = visible and (node.visible or not object)
let row = OutlineRow(node.id, depth, shown)
row.object = object
rows.append(row)
if node_catalog.collects_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.collects_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)
## The rows whose label holds `query` (any case) and the rows above them, so
## each match keeps its place in the hierarchy; every row when it is empty.
pub func filtered(graph: Network, rows: list[OutlineRow], query: str) -> list[OutlineRow]!:
let wanted = query.trim().lower()
if wanted == "":
return rows
let kept: list[bool] = []
for _ in rows:
kept.append(false)
for at in 0..<rows.length:
if not label_of(graph, rows[at]).lower().contains(wanted):
continue
kept[at] = true
# Its parents: each nearer row above it at a lower depth.
var depth = rows[at].depth
var up = at - 1
while up >= 0 and depth > 0:
if rows[up].depth < depth:
kept[up] = true
depth = rows[up].depth
up -= 1
let result: list[OutlineRow] = []
for at in 0..<rows.length:
if kept[at]:
result.append(rows[at])
return result
## A row's icon: its node's kind; an object path's a group's, a CAD
## curve's (Alias's curve icon) or an object's.
pub func row_icon(node: Node, row: OutlineRow) -> str:
if row.path == "":
return node.kind
if row.branch:
return "Group"
return "Curve" if row.path in node.curve_paths else "Cube"
## A row's label: its node's name, or the last part of its object path.
pub func label_of(graph: Network, row: OutlineRow) -> str!:
if row.path != "":
return row.path.split("/")[-1]
return graph.require(row.id).name
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
## The search field's text: only matching rows (and their parents) list,
## collapsed branches searched too.
var query: str = ""
## A right click on node `id`'s row, at (x, y) in window points.
pub var on_menu: func(int, float, float) -> unit!? = none
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
## Show only the rows matching `text` (the search field's).
pub func search(self, text: str) -> unit!:
self.query = text
self.scroll = 0.0
self.refresh()
pub func measure(self, available: Constraints) -> Size!:
return Size(300.0, 210.0)
pub func row_height(self) -> float:
return self.font.line_height() + 6.0
pub func refresh(self) -> unit!:
let searching = self.query.trim() != ""
let all = object_rows(self.workspace.network, [] if searching else self.collapsed, [] if searching else self.collapsed_paths)
self.rows = filtered(self.workspace.network, all, self.query)
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
# Rows outside the container the network view is in are greyed,
# below even a hidden row's muted ink.
let greyed = is_outside(self.workspace.network, row.id)
let ink = theme.grey(92.0) if greyed else (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.collects_children(node.kind):
p.text(">" if node.id in self.collapsed else "v", x, text_y, ink, self.font)
draw_icon(p, row_icon(node, row), 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 = label_of(self.workspace.network, row)
p.text(label if label.length <= limit else label[0..<limit] + "…", x + 40.0, text_y, ink, self.font)
if row.path != "" or row.object:
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 matches" if self.query.trim() != "" else "No objects", 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 == 1 and event.inside:
let index = int((event.y + self.scroll) / self.row_height())
if index >= 0 and index < self.rows.length and self.rows[index].path == "":
let id = self.rows[index].id
if not (id in self.workspace.network.selection()):
node_commands.reveal_node(self.workspace, id)
self.refresh()
if let menu = self.on_menu:
let bounds = self.box.bounds()
menu(id, bounds.x + event.x, bounds.y + event.y)
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.collects_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 and row.object:
node_commands.toggle_display(self.workspace, node.id)
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.holds_nodes(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()