Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.1

0.11.1 / tests / outliner_tests.luc

from network import Network
from workspace import Workspace
from geometry_tests import near
from app import Luce3D
from luce_ui.ui import Event
from luce_window.input import EventKind
import node_commands

func ids(graph: Network, parent: int = -1) -> list[int]:
    let result: list[int] = []
    for node in graph.outputs(parent):
        result.append(node.id)
    return result

pub func outliner_tests() -> unit!:
    let graph = Network(false)
    let a = graph.add("Cube", 0.0, 0.0)
    let b = graph.add("Sphere", 200.0, 0.0)
    let transform = graph.add("Transform", 0.0, 100.0)
    graph.connect(a.id, transform.id, 0)
    assert(ids(graph) == [b.id, transform.id])
    a.output = 1
    assert(ids(graph) == [a.id, b.id])
    transform.output = 1
    assert(ids(graph) == [a.id, b.id, transform.id])
    a.output = -1
    transform.output = -1
    assert(ids(graph) == [b.id])
    transform.output = 0
    assert(ids(graph) == [b.id, transform.id])
    let group = graph.add("Group", 400.0, 0.0)
    graph.scope = group.id
    let child = graph.add("Cube", 0.0, 0.0)
    let nested = graph.add("Group", 200.0, 0.0)
    graph.scope = nested.id
    let inner = graph.add("Cube", 0.0, 0.0)
    graph.scope = -1
    group.values[1] = 5.0
    nested.values[2] = 4.0
    let data = graph.evaluate_data(group.id)
    assert(data.mesh == none and data.instance_count() == 1)
    let before = child.evaluations
    let realized = data.require_mesh()
    assert(realized.face_count() == 12)
    near(realized.bounds().minimum.x, 4.0)
    near(realized.bounds().maximum.y, 6.0)
    group.values[1] = 10.0
    graph.invalidate(group.id)
    near(graph.evaluate(group.id).bounds().minimum.x, 9.0)
    assert(child.evaluations == before)
    group.values[7] = 2.0
    graph.invalidate(group.id)
    near(graph.evaluate(group.id).bounds().minimum.x, 8.0)
    assert(child.evaluations == before)
    inner.values[0] = 4.0
    graph.invalidate(inner.id)
    near(graph.evaluate(group.id).bounds().maximum.y, 8.0)
    var rejected = false
    graph.connect(child.id, transform.id, 0) catch failure:
        rejected = true
    assert(rejected and transform.inputs[0] == a.id)
    let snapshot = graph.copy()
    graph.remove(group.id)
    assert(graph.find(child.id) == none and graph.find(inner.id) == none)
    assert(snapshot.require(child.id).parent == group.id)

    let w = Workspace()
    node_commands.add_node(w, "Group", 300.0, 40.0)
    let root = w.network.selected
    node_commands.enter_group(w, root)
    assert(w.network.scope == root and w.network.local_nodes().length == 0)
    node_commands.add_node(w, "Cube", 40.0, 50.0)
    let leaf = w.network.selected
    node_commands.toggle_visible(w)
    assert(not w.network.require(leaf).visible and w.network.is_output(leaf))
    w.undo()
    assert(w.network.require(leaf).visible)
    node_commands.toggle_output(w)
    assert(ids(w.network, root).length == 0)
    w.undo()
    assert(ids(w.network, root) == [leaf])
    node_commands.leave_group(w)
    node_commands.set_parameter(w, 1, 3.0)
    near(w.network.evaluate(root).bounds().minimum.x, 2.0)
    w.undo()
    near(w.network.evaluate(root).bounds().minimum.x, -1.0)
    node_commands.select_node(w, root)
    node_commands.delete_selected(w)
    assert(w.network.find(leaf) == none)
    w.undo()
    assert(w.network.require(leaf).parent == root)
    let exposed = ids(w.network)
    node_commands.display_selected(w)
    assert(w.network.preview and ids(w.network) == exposed)
    node_commands.display_selected(w)
    assert(not w.network.preview and ids(w.network) == exposed)

    let editor = Luce3D()
    node_commands.add_node(editor.workspace, "Group", 280.0, 40.0)
    let container = editor.workspace.network.selected
    node_commands.enter_group(editor.workspace, container)
    node_commands.add_node(editor.workspace, "Cube", 40.0, 60.0)
    let item = editor.workspace.network.selected
    node_commands.leave_group(editor.workspace)
    editor.panels.refresh()
    editor.app.layout(1400.0, 1000.0)
    let outline = editor.panels.outliner
    assert(outline.rows.length == 3)
    node_commands.reveal_node(editor.workspace, container)
    editor.panels.refresh()
    editor.app.layout(1400.0, 1000.0)
    let graph_view = editor.panels.graph
    let node = editor.workspace.network.require(container)
    let at = graph_view.to_screen(node.x + 45.0, node.y + 20.0)
    let graph_bounds = graph_view.layout().bounds()
    for click in 0..<2:
        editor.app.dispatch(Event(kind = EventKind.pointer_down, x = graph_bounds.x + at.x, y = graph_bounds.y + at.y, button = 0))
        editor.app.dispatch(Event(kind = EventKind.pointer_up, x = graph_bounds.x + at.x, y = graph_bounds.y + at.y, button = 0))
    assert(editor.workspace.network.scope == container)
    node_commands.leave_group(editor.workspace)
    editor.panels.refresh()
    editor.app.layout(1400.0, 1000.0)
    let outline_bounds = outline.layout().bounds()
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = outline_bounds.x + 12.0, y = outline_bounds.y + 40.0, button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = outline_bounds.x + 12.0, y = outline_bounds.y + 40.0, button = 0))
    assert(outline.rows.length == 2)
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = outline_bounds.x + outline_bounds.width - 16.0, y = outline_bounds.y + 40.0, button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = outline_bounds.x + outline_bounds.width - 16.0, y = outline_bounds.y + 40.0, button = 0))
    assert(not editor.workspace.network.require(container).visible and outline.rows.length == 2)
    editor.workspace.undo()
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = outline_bounds.x + 12.0, y = outline_bounds.y + 40.0, button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = outline_bounds.x + 12.0, y = outline_bounds.y + 40.0, button = 0))
    assert(outline.rows.length == 3)
    assert(outline.rows[1].id == container and outline.rows[2].id == item and outline.rows[2].depth == 1)
    let bounds = outline.layout().bounds()
    for click in 0..<2:
        editor.app.dispatch(Event(kind = EventKind.pointer_down, x = bounds.x + 70.0, y = bounds.y + 40.0, button = 0))
        editor.app.dispatch(Event(kind = EventKind.pointer_up, x = bounds.x + 70.0, y = bounds.y + 40.0, button = 0))
    assert(editor.workspace.network.scope == container)
    node_commands.leave_group(editor.workspace)
    node_commands.set_output(editor.workspace, container, -1)
    editor.panels.refresh()
    assert(outline.rows.length == 1)
    editor.workspace.undo()
    editor.panels.refresh()
    assert(outline.rows.length == 3)
    editor.close()
    print("PASS automatic/explicit multi-output islands, Group hierarchy, cached transforms, Outliner navigation and undo")