Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / tests / editor / grouping_tests.luc

## Grouping: Group (Cmd/Ctrl+G, or Group in the Tab menu with nodes picked)
## moves the picked nodes and what feeds them into a new Group where they
## sat, rewiring their readers; the Group's transform travels with its
## result, so renders downstream follow it. Inside a Geo the Group yields the
## member read outside; at the root it yields every flagged member. Nodes are
## picked by Shift-click and boxes in the Network panel, and at the Object
## level by clicks and boxes in the viewport.
from workspace import Workspace
from app import Luce3D
from luce_ui.ui import Event
from luce_window.input import EventKind, Key
import node_commands
import selection_commands

pub func grouping_tests() -> unit!:
    let w = Workspace()
    let home = w.network.scope
    let cube = w.network.nodes[0].id
    node_commands.select_node(w, cube)
    node_commands.add_node(w, "Transform", 40.0, 140.0)
    let moved = w.network.selected
    node_commands.add_node(w, "Null", 40.0, 240.0)
    let reader = w.network.selected
    node_commands.select_node(w, moved)
    node_commands.group_selected(w)
    let group = w.network.require(w.network.selected)
    assert(group.kind == "Group" and group.parent == home and w.network.require(reader).inputs[0] == group.id)
    assert(w.network.require(moved).parent == group.id and w.network.require(cube).parent == group.id)
    assert(w.network.is_output(moved))
    var data = w.network.evaluate_data(reader)
    assert(data.instance_count() == 1 and data.instance_visible(0))

    # Moved: downstream sees it.
    node_commands.select_node(w, group.id)
    node_commands.set_parameter(w, 2, 2.0)
    data = w.network.evaluate_data(reader)
    assert(data.instance_translation(0).y == 2.0 and data.instance_visible(0))

    # A node whose input also feeds another node outside cannot go alone.
    w.undo()
    w.undo()
    assert(w.network.require(moved).parent == home and w.network.require(reader).inputs[0] == moved)
    node_commands.select_node(w, cube)
    node_commands.add_node(w, "Null", 240.0, 140.0)
    node_commands.select_node(w, moved)
    let count = w.network.nodes.length
    node_commands.group_selected(w)
    assert(w.network.nodes.length == count and w.notice.contains("also feeds"))
    picking_tests()
    print("PASS grouping: Group the picked nodes with their inputs, rewired readers, the Group's transform downstream, picking in both panels, Tab → Group")

func type_text(editor: Luce3D, text: str) -> unit!:
    let ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
    for at in 0..<text.length:
        let index = ascii.index_of(text[at..<at + 1]) else continue
        editor.app.dispatch(Event(kind = EventKind.text_input, codepoint = index + 32))

## A drag from (x0, y0) to (x1, y1), `shift` held or not.
func drag(editor: Luce3D, x0: float, y0: float, x1: float, y1: float) -> unit!:
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = x0, y = y0, button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = (x0 + x1) * 0.5, y = (y0 + y1) * 0.5))
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = x1, y = y1))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = x1, y = y1, button = 0))

## At the root: two Geo objects, the second a sphere moved 3 along X.
func picking_tests() -> unit!:
    let editor = Luce3D()
    editor.app.layout(1600.0, 1000.0)
    let w = editor.workspace
    node_commands.leave_group(w)
    let first = w.network.selected
    node_commands.add_node(w, "Geo", 300.0, 70.0)
    let second = w.network.selected
    node_commands.enter_group(w, second)
    node_commands.add_node(w, "Sphere", 0.0, 0.0)
    node_commands.add_node(w, "Transform", 0.0, 100.0)
    node_commands.set_parameter(w, 1, 3.0)
    node_commands.leave_group(w)
    assert(w.network.outputs(-1).length == 2)

    # Shift-click picks a second node; a click alone picks one again.
    node_commands.pick_node(w, first, false)
    node_commands.pick_node(w, second, true)
    assert(w.network.selection() == [first, second] and w.network.selected == second)
    node_commands.pick_node(w, first, true)
    assert(w.network.selection() == [second])

    # A box in the Network panel around every node picks them all.
    editor.panels.refresh()
    let graph = editor.panels.graph
    graph.frame_all()
    editor.app.layout(1600.0, 1000.0)
    let area = graph.layout().bounds()
    let a = graph.to_screen(30.0, 60.0)
    let b = graph.to_screen(450.0, 120.0)
    drag(editor, area.x + a.x, area.y + a.y, area.x + b.x, area.y + b.y)
    assert(w.network.selection().length == 2)

    # Tab, "Group", Enter: the picked objects in one Group, both displayed.
    editor.app.focus(graph.layout())
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.tab))
    type_text(editor, "Group")
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    let group = w.network.require(w.network.selected)
    assert(group.kind == "Group" and w.network.outputs(group.id).length == 2 and w.network.require(second).parent == group.id)
    # Inside it, U goes back up, as in Houdini.
    node_commands.enter_group(w, group.id)
    assert(w.network.scope == group.id)
    editor.panels.refresh()
    editor.app.focus(graph.layout())
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.u))
    assert(w.network.scope == -1 and w.network.selected == group.id)
    w.undo()
    assert(w.network.require(first).parent == -1)

    # The viewport at the Object level: a box over both objects picks them;
    # Tab → Group there groups them.
    selection_commands.set_mode(w, 0)
    w.frame_cube()
    editor.app.layout(1600.0, 1000.0)
    let view = editor.panels.view.layout().bounds()
    drag(editor, view.x + 110.0, view.y + 90.0, view.x + view.width - 20.0, view.y + view.height - 40.0)
    assert(w.network.selection().length == 2 and first in w.network.selection() and second in w.network.selection())
    # A click on empty space picks nothing.
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = view.x + 110.0, y = view.y + 90.0, button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = view.x + 110.0, y = view.y + 90.0, button = 0))
    assert(w.network.selection().length == 0 and w.selected() == none)
    drag(editor, view.x + 110.0, view.y + 90.0, view.x + view.width - 20.0, view.y + view.height - 40.0)
    editor.app.focus(editor.panels.view.layout())
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.tab))
    type_text(editor, "Group")
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    let viewed = w.network.require(w.network.selected)
    assert(viewed.kind == "Group" and w.network.outputs(viewed.id).length == 2 and w.network.require(second).parent == viewed.id)
    editor.close()