Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.2

0.11.2 / tests / editor / ui_tests.luc

from app import Luce3D
from luce_ui.ui import Event
from luce_window.input import EventKind, Key, ScrollUnit
from luce_gpu.gpu import Frame
from luce_window import window
from geometry_tests import near
from view_projection import project, axis
import node_commands
import selection_commands

func click(editor: Luce3D, x: float, y: float) -> unit!:
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = x, y = y, button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = x, y = y, button = 0))
    editor.panels.refresh()

pub func ui_tests() -> unit!:
    let editor = Luce3D()
    let w = editor.workspace
    editor.app.layout(1400.0, 900.0)
    let graph = editor.panels.graph
    editor.app.focus(graph.layout())
    let graph_bounds = graph.layout().bounds()
    assert(graph_bounds.width > 400.0 and graph_bounds.height > 160.0)
    let anchor = graph.to_graph(300.0, 120.0)
    graph.zoom_about(1.4, 300.0, 120.0)
    let anchored = graph.to_graph(300.0, 120.0)
    near(anchor.x, anchored.x)
    near(anchor.y, anchored.y)
    let wheel_zoom = graph.zoom
    editor.app.dispatch(Event(kind = EventKind.scroll, x = graph_bounds.x + 300.0, y = graph_bounds.y + 120.0, scroll_y = 1.0, scroll_unit = ScrollUnit.lines))
    assert(graph.zoom > wheel_zoom)
    let pan = graph.pan_x
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = graph_bounds.x + 300.0, y = graph_bounds.y + 120.0, button = 2))
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = graph_bounds.x + 350.0, y = graph_bounds.y + 140.0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = graph_bounds.x + 350.0, y = graph_bounds.y + 140.0, button = 2))
    near(graph.pan_x, pan + 50.0)
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.tab))
    editor.app.layout(1400.0, 900.0)
    assert(editor.panels.search.is_open())
    for scalar in [116, 114, 97, 110, 115]:
        editor.app.dispatch(Event(kind = EventKind.text_input, codepoint = scalar))
    assert(editor.panels.search.result_count() >= 1)
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert((w.selected() else trap("node")).kind == "Transform")
    assert(w.network.nodes.length == 2 and not editor.panels.search.is_open())
    editor.panels.refresh()
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.z, control = true))
    assert(w.network.nodes.length == 1)
    editor.panels.refresh()
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.z, control = true, shift = true))
    assert(w.network.nodes.length == 2)
    node_commands.add_node(w, "Edit Mesh", 600.0, 80.0)
    editor.panels.refresh()
    editor.app.layout(1400.0, 900.0)
    editor.app.focus(editor.panels.view.layout())
    let area = editor.panels.view.layout().bounds()
    let mesh = w.geometry else trap("mesh")
    let top = project(w, mesh.face_center(2), area.width, area.height)
    let before_hover = w.history.cursor
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + top[0], y = area.y + top[1]))
    assert(editor.panels.view.tools.hovered == 2 and w.history.cursor == before_hover)
    click(editor, area.x + top[0], area.y + top[1])
    assert(w.selected_ids() == [2])
    # Hold S: the Modify marking menu around the pointer; a click on
    # Extrude extrudes the face.
    editor.panels.refresh()
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.s))
    let marking = editor.panels.view.marking
    assert(marking.is_open() and marking.title == "Modify" and marking.items[0].name == "Extrude faces")
    let extrude_at = marking.center_of(0, area.width, area.height)
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + extrude_at[0], y = area.y + extrude_at[1]))
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = area.x + extrude_at[0], y = area.y + extrude_at[1], button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = area.x + extrude_at[0], y = area.y + extrude_at[1], button = 0))
    assert(not marking.is_open())
    editor.app.dispatch(Event(kind = EventKind.key_up, key = Key.s))
    assert((w.geometry else trap("mesh")).face_count() == 10)
    # A shows the Create menu while held; letting it go closes it.
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.a))
    assert(marking.is_open() and marking.title == "Create")
    editor.app.dispatch(Event(kind = EventKind.key_up, key = Key.a))
    assert(not marking.is_open())
    editor.panels.refresh()
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.w))
    assert(w.edit_tool == "Move")
    let center = w.selection_center()
    let end = project(w, center.add(axis(1).multiply_scalar(w.view.distance * 0.14)), area.width, area.height)
    let before = w.history.cursor
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = area.x + end[0], y = area.y + end[1], button = 0))
    assert(editor.panels.view.tools.dragging)
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + end[0], y = area.y + end[1] - 30.0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = area.x + end[0], y = area.y + end[1] - 30.0, button = 0))
    assert(w.history.cursor == before + 1)
    assert((w.geometry else trap("mesh")).bounds().maximum.y > 2.5)
    w.undo()
    near((w.geometry else trap("mesh")).bounds().maximum.y, 2.5)
    # Point/edge/face buttons and actual occlusion-aware point picking.
    click(editor, area.x + 67.0, area.y + 23.0)
    assert(w.component_mode == 1)
    let edited_mesh = w.geometry else trap("mesh")
    let point_id = edited_mesh.face_point(2, 2)
    let point = project(w, edited_mesh.point(point_id), area.width, area.height)
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + point[0], y = area.y + point[1]))
    assert(editor.panels.view.tools.hovered == point_id)
    click(editor, area.x + point[0], area.y + point[1])
    assert(w.selected_ids() == [point_id])
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.digit_3))
    assert(w.component_mode == 2)
    let other_id = edited_mesh.face_point(2, 3)
    var edge_id = -1
    for edge in 0..<(edited_mesh.edge_count()):
        let a = edited_mesh.edge_point(edge, 0)
        let b = edited_mesh.edge_point(edge, 1)
        if (a == point_id and b == other_id) or (b == point_id and a == other_id):
            edge_id = edge
    assert(edge_id >= 0)
    let edge_at = project(w, edited_mesh.point(point_id).add(edited_mesh.point(other_id)).multiply_scalar(0.5), area.width, area.height)
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + edge_at[0], y = area.y + edge_at[1]))
    assert(editor.panels.view.tools.hovered == edge_id)
    click(editor, area.x + edge_at[0], area.y + edge_at[1])
    assert(w.selected_ids() == [edge_id])
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.digit_4))
    assert(w.component_mode == 3)
    # Losing capture during a gizmo drag restores the original geometry.
    selection_commands.choose_component(w, 2, false)
    editor.panels.refresh()
    let after_undo = w.selection_center()
    let handle = project(w, after_undo.add(axis(1).multiply_scalar(w.view.distance * 0.14)), area.width, area.height)
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = area.x + handle[0], y = area.y + handle[1], button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + handle[0], y = area.y + handle[1] - 20.0))
    editor.app.dispatch(Event(kind = EventKind.focus_lost))
    assert(not editor.panels.view.tools.dragging and not w.in_transaction())
    near((w.geometry else trap("mesh")).bounds().maximum.y, 2.5)
    # Maya navigation: Alt-left orbit, Alt-middle pan, Alt-right dolly.
    let yaw = w.view.yaw
    let distance = w.view.distance
    let target = w.view.target
    for button in [0, 2, 1]:
        editor.app.dispatch(Event(kind = EventKind.pointer_down, x = area.x + 300.0, y = area.y + 220.0, button = button, alt = true))
        editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + 330.0, y = area.y + 230.0, alt = true))
        editor.app.dispatch(Event(kind = EventKind.pointer_up, x = area.x + 330.0, y = area.y + 230.0, button = button, alt = true))
    assert(w.view.yaw != yaw and w.view.distance != distance and w.view.target.x != target.x)
    assert(editor.panels.view.tools.hovered == -1)
    editor.panels.refresh()
    for width in [1400.0, 1100.0]:
        editor.app.layout(width, 900.0)
        let frame = Frame(window.Size(width, 900.0, width, 900.0, 1.0))
        editor.app.render(frame.target())
        frame.close()
    editor.close()
    print("PASS node canvas, Tab search, shortcuts, face picking, extrusion, gizmo transaction and render")
    wiring_and_fields()

func wiring_and_fields() -> unit!:
    let editor = Luce3D()
    let w = editor.workspace
    node_commands.add_node(w, "Transform", 330.0, 60.0)
    let id = w.network.selected
    node_commands.disconnect(w, id, 0)
    editor.panels.refresh()
    editor.app.layout(1400.0, 900.0)
    let graph = editor.panels.graph
    graph.frame_all()
    let bounds = graph.layout().bounds()
    let output = graph.port_point(w.network.require(1), true)
    let input = graph.port_point(w.network.require(id), false)
    let before_wire = w.history.cursor
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = bounds.x + output.x, y = bounds.y + output.y, button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = bounds.x + input.x, y = bounds.y + input.y))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = bounds.x + input.x, y = bounds.y + input.y, button = 0))
    assert(w.network.require(id).inputs[0] == 1 and w.history.cursor == before_wire + 1)
    w.undo()
    assert(w.network.require(id).inputs[0] == -1)
    w.redo()
    assert(w.network.require(id).inputs[0] == 1)
    let own_output = graph.port_point(w.network.require(id), true)
    let before_cycle = w.history.cursor
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = bounds.x + own_output.x, y = bounds.y + own_output.y, button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = bounds.x + input.x, y = bounds.y + input.y, button = 0))
    assert(w.notice != "" and w.network.require(id).inputs[0] == 1 and w.history.cursor == before_cycle)
    let node = w.network.require(id)
    let at = graph.to_screen(node.x + 30.0, node.y + 15.0)
    let previous_x = node.x
    let before_move = w.history.cursor
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = bounds.x + at.x, y = bounds.y + at.y, button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = bounds.x + at.x + 35.0, y = bounds.y + at.y + 10.0))
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = bounds.x + at.x + 45.0, y = bounds.y + at.y + 20.0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = bounds.x + at.x + 45.0, y = bounds.y + at.y + 20.0, button = 0))
    assert(w.network.require(id).x > previous_x and w.history.cursor == before_move + 1)
    w.undo()
    near(w.network.require(id).x, previous_x)
    editor.panels.refresh()
    editor.app.layout(1400.0, 900.0)
    # A real parameter scrub must update geometry and coalesce its changes.
    let field = editor.panels.inspector.field_bounds(1)
    let before_scrub = w.history.cursor
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = field.x + 20.0, y = field.y + 10.0, button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = field.x + 40.0, y = field.y + 10.0))
    editor.panels.refresh()
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = field.x + 60.0, y = field.y + 10.0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = field.x + 60.0, y = field.y + 10.0, button = 0))
    assert(w.network.require(id).values[1] > 0.0 and w.history.cursor == before_scrub + 1)
    w.undo()
    near(w.network.require(id).values[1], 0.0)
    editor.panels.refresh()
    # Display and bypass are separate flags, clickable in the graph.
    let restored = w.network.require(id)
    let enable = graph.to_screen(restored.x + 7.0, restored.y + 24.0)
    click(editor, bounds.x + enable.x, bounds.y + enable.y)
    assert(not w.network.require(id).enabled)
    let cube = w.network.require(1)
    let display = graph.to_screen(cube.x + 89.0, cube.y + 24.0)
    click(editor, bounds.x + display.x, bounds.y + display.y)
    assert(w.network.require(1).output == 1 and w.network.is_output(1))
    node_commands.disconnect(w, id, 0)
    let click_input = graph.port_point(w.network.require(id), false)
    click(editor, bounds.x + output.x, bounds.y + output.y)
    click(editor, bounds.x + click_input.x, bounds.y + click_input.y)
    assert(w.network.require(id).inputs[0] == 1)
    editor.close()
    print("PASS actual wire gestures, cycle rollback, node movement, parameter scrub and display/bypass flags")