Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.8

0.11.8 / tools / preview_select.luc

## Stage 2's captures for preview.py (docs/research/POLY-MODELING.md):
## selection in Edit Mesh.
##   select_d_edges: an edge of a 6 by 6 grid selected, D held: the Select
##     menu at Edges, labeled, Loops first, then Region, Find, Similar,
##     Pattern, Symmetry, Convert, Selection, the sets and Hide.
##   select_d_faces: a face selected and a set saved, D held: Region first,
##     the set listed under Sets.
##   select_loop: a torus with the loop a double-click took, and the
##     pointer over another edge: its loop previewed.
##   select_ring: the same with Cmd held: the ring previewed.
##   select_path: Shift+Cmd/Ctrl+click from one corner of the grid to the
##     other: the shortest path, the straightest.
##   select_similar: a box and a grid, Similar by area run from one of the
##     box's faces: its six faces, Similar's rows on the tool bar.
##   select_hide: three of the box's faces hidden, the inside of the box
##     and the grid under it in sight, four grid faces selected.
from app import Luce3D
from luce_ui.ui import Event
from luce_window.input import EventKind, Key
from luce_3d.graphics import Vector3
from luce_geocore.geocore import Selection
from view_projection import project
from edit_types import edge_level, face_level, point_level
import node_commands
import modeling_commands
import selection_commands
import selection_gestures
import selection_sets
import selection_hide
import preview_support

pub func scenes() -> list[str]:
    return ["select_d_edges", "select_d_faces", "select_loop", "select_ring", "select_path", "select_similar", "select_hide"]

## An Edit Mesh holding primitive `name` (its size or radius `size`, count
## `count`), looked at from above the front.
func modeled(editor: Luce3D, name: str, count: float) -> unit!:
    let w = editor.workspace
    node_commands.delete_selected(w)
    node_commands.add_node(w, "Edit Mesh", 40.0, 40.0)
    preview_support.settle(w)
    modeling_commands.create(w, name)
    preview_support.settle(w)
    preview_support.counts(w, name, count)

## Scene `scene` before its first frame.
pub func setup(editor: Luce3D, scene: str) -> unit!:
    let w = editor.workspace
    if scene in ["select_loop", "select_ring"]:
        modeled(editor, "Torus", 16.0)
    elif scene in ["select_similar", "select_hide"]:
        modeled(editor, "Box", 1.0)
        modeling_commands.create(w, "Grid")
        preview_support.settle(w)
        modeling_commands.set_last_step(w, 1, 8.0, 1.0, 1000.0)
        preview_support.settle(w)
        modeling_commands.set_last_step(w, 0, 4.0, 0.001, 1000.0)
        preview_support.settle(w)
        modeling_commands.set_last_step(w, 4, -1.0, -100.0, 100.0)
        preview_support.settle(w)
    else:
        modeled(editor, "Grid", 6.0)
    let mesh = w.geometry else trap("mesh")
    if scene == "select_d_edges":
        selection_commands.set_mode(w, edge_level)
        w.selection = Selection.of(mesh, "p8-9", 4)
    elif scene == "select_d_faces":
        selection_commands.set_mode(w, face_level)
        w.selection = Selection.of(mesh, "14 15 20 21", 2)
        _ = selection_sets.save(w)
        preview_support.settle(w)
        selection_commands.set_mode(w, face_level)
        w.selection = Selection.of(w.geometry else trap("set"), "14", 2)
    elif scene in ["select_loop", "select_ring"]:
        selection_commands.set_mode(w, edge_level)
        selection_gestures.double_click(w, 0, false, false)
    elif scene == "select_path":
        selection_commands.set_mode(w, point_level)
        w.selection = Selection.empty(mesh, 0).changed(0, 0)
        selection_gestures.path_click(w, 48)
    elif scene == "select_similar":
        selection_commands.set_mode(w, face_level)
        w.selection = Selection.of(mesh, "0", 2)
        modeling_commands.model(w, "Similar", [1.0, 0.0, 0.01])
    elif scene == "select_hide":
        selection_commands.set_mode(w, face_level)
        w.selection = Selection.of(mesh, "0 2 4", 2)
        selection_hide.hide(w, false)
        w.selection = Selection.of(w.geometry else trap("hidden"), "6-9", 2)
    w.view.yaw = 0.55
    w.view.pitch = 0.75
    w.view.distance = 4.5 if scene in ["select_loop", "select_ring"] else 6.0
    w.view.target = Vector3(0.0, 0.3, 0.0)
    w.view.update_camera()
    editor.panels.refresh()

## Frame `frame` of scene `scene` (from 2; `last` is the frame captured).
pub func act(editor: Luce3D, scene: str, frame: int, last: int) -> unit!:
    let w = editor.workspace
    if frame != last - 1:
        return
    let area = editor.panels.view.layout().bounds()
    let mesh = w.geometry else return
    editor.app.focus(editor.panels.view.layout())
    if scene in ["select_loop", "select_ring"]:
        # Over an edge of the torus nearest the eye, on its outside: its
        # loop (ring with Cmd).
        var nearest = 0
        var depth = 1000000.0
        for point in 0..<mesh.point_count():
            let on = project(w, mesh.point(point), area.width, area.height)
            if on[2] < depth:
                depth = on[2]
                nearest = point
        let ring = nearest - nearest % 16
        let edge = mesh.find_edge(nearest, ring + (nearest % 16 + 1) % 16)
        let middle = mesh.point(mesh.edge_point(edge, 0)).add(mesh.point(mesh.edge_point(edge, 1))).multiply_scalar(0.5)
        let at = project(w, middle, area.width, area.height)
        editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + at[0], y = area.y + at[1], meta = scene == "select_ring"))
        return
    # Right of the model, so the menu leaves it in sight.
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + area.width * 0.72, y = area.y + area.height * 0.5))
    if scene in ["select_d_edges", "select_d_faces"]:
        editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.d))