Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.5

0.11.5 / tests / editor / chrome_tests.luc

## The viewport's chrome, clicked as a user would: the snapping button
## opens its options, a click on one turns it off and on, a click away
## closes them; the tool column's slot below Q–T picks the last sketch
## tool again; the marking menus offer each context's own (the scene's
## primitives, a CAD node's sketches and solids).
from app import Luce3D
from luce_ui.ui import Event
from luce_window.input import EventKind, Key
from viewport_chrome import ViewportChrome
from sketch_support import open_sketch, render

pub func chrome_tests() -> unit!:
    let editor = Luce3D()
    editor.app.layout(1600.0, 1000.0)
    let w = editor.workspace
    render(editor)
    let view = editor.panels.view
    let chrome = view.tools.chrome
    let area = view.layout().bounds()
    # The marking menu outside an Edit node and a sketch: the scene's
    # primitives (A).
    editor.app.focus(view.layout())
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.a))
    assert(view.marking.is_open() and view.marking.items[0].name == "Cube")
    editor.app.dispatch(Event(kind = EventKind.key_up, key = Key.a))
    # Snapping: the button opens its options; the third (Points) turns off
    # and on; a click away closes them.
    press(editor, area.x + 32.0, area.y + area.height - 25.0)
    assert(chrome.snaps_open)
    let points_row = find_button(chrome, area.width, area.height, 412)
    assert(points_row.length == 2)
    let was = w.snaps.flags[2]
    press(editor, area.x + points_row[0], area.y + points_row[1])
    assert(w.snaps.flags[2] != was)
    press(editor, area.x + points_row[0], area.y + points_row[1])
    assert(w.snaps.flags[2] == was)
    press(editor, area.x + area.width * 0.5, area.y + area.height * 0.5)
    assert(not chrome.snaps_open)
    # In a CAD node A offers sketches and solids; in a sketch, the slot
    # below Q–T picks the last tool again.
    _ = open_sketch(editor, 0.0, 0.0, 200.0)
    let e = view.sketch.editor
    e.choose("Center Diameter Circle")
    e.choose("Select")
    render(editor)
    let slot = find_button(chrome, area.width, area.height, 305)
    assert(slot.length == 2)
    press(editor, area.x + slot[0], area.y + slot[1])
    assert(e.tool == "Center Diameter Circle")
    e.choose("Select")
    # Nothing selected inside the CAD node: its sketches and solids.
    w.network.selected = -1
    render(editor)
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.a))
    assert(view.marking.is_open() and view.marking.items[0].name == "Sketch")
    editor.app.dispatch(Event(kind = EventKind.key_up, key = Key.a))

## A click (down and up) at window point (`x`, `y`).
func press(editor: Luce3D, x: float, y: float) -> unit!:
    for kind in [EventKind.pointer_moved, EventKind.pointer_down, EventKind.pointer_up]:
        editor.app.dispatch(Event(kind = kind, x = x, y = y, button = 0))

## A viewport point on chrome button `button` (see its hover_tool), or [].
func find_button(chrome: ViewportChrome, width: float, height: float, button: int) -> list[float]:
    var y = 4.0
    while y < height:
        var x = 4.0
        while x < 240.0:
            if chrome.button_at(x, y, width, height) == button:
                return [x + 4.0, y + 4.0]
            x += 8.0
        y += 4.0
    return []