Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / object_box.luc

## Object picking in the viewport: a click picks the object under it (on
## empty space, nothing), a drag every object the box touches, hidden or
## not (Houdini's rule), and a plain box over none selects nothing; Shift
## adds (a click on a picked one removes it), Ctrl or Cmd takes away (a
## Shift or Ctrl box over nothing changes nothing). Only the context's objects are picked, never the
## rest of the scene drawn around it. Picked objects have a thick contour
## of their rendered shape.
from luce_ui.ui import Painter, Event
from luce_geocore.geocore import Mesh
from luce_3d.graphics import Silhouette, GaussianSplats, FogVolume, Renderer
from luce_window.input import EventKind, Key
from workspace import Workspace
import theme
import node_commands
from viewport_objects import object_at, objects_in, helper_node
from marquee import operation_of, dragged, draw_box

pub class ObjectBox:
    let workspace: Workspace
    ## A pick in progress: where it started, the pointer now, and what it
    ## does (0 replace, 1 add, 2 remove).
    var boxing: bool = false
    var box: list[float]
    var operation: int = 0
    pub let silhouette: Silhouette
    ## The viewport's renderer: the outline's mask draws from the GPU copies
    ## it made this frame instead of sending the meshes again.
    pub var renderer: Renderer? = none

    pub func init(self, workspace: Workspace):
        self.workspace = workspace
        self.box = []
        self.silhouette = Silhouette()

    ## A press starts a pick at its point.
    pub func start(self, event: Event):
        self.boxing = true
        self.box = [event.x, event.y, event.x, event.y]
        self.operation = operation_of(event)

    ## The pick's events once started; whether one was used.
    pub func event(self, event: Event, width: float, height: float) -> bool!:
        let w = self.workspace
        if not self.boxing:
            return false
        if event.cancelled or event.kind in [EventKind.pointer_left, EventKind.focus_lost] or (event.kind == EventKind.key_down and event.key == Key.escape):
            self.boxing = false
            return true
        if event.kind == EventKind.pointer_moved:
            self.box = [self.box[0], self.box[1], event.x, event.y]
            return true
        if event.kind == EventKind.pointer_up:
            self.boxing = false
            self.box = [self.box[0], self.box[1], event.x, event.y]
            if dragged(self.box):
                let ids = objects_in(w, self.box[0], self.box[1], self.box[2], self.box[3], width, height)
                if ids.length > 0:
                    node_commands.pick_nodes(w, ids, self.operation)
                elif self.operation == 0:
                    node_commands.select_nothing(w)
                return true
            let id = object_at(w, event.x, event.y, width, height)
            if id >= 0 and self.operation == 2:
                node_commands.pick_nodes(w, [id], 2)
            elif id >= 0:
                node_commands.pick_node(w, id, self.operation == 1)
            elif self.operation == 0:
                node_commands.select_nothing(w)
        return true

    ## Only the selection marquee is rectangular. Object highlights follow
    ## their surfaces/opacity, while Camera and Light highlight their glyphs.
    pub func draw(self, painter: Painter, width: float, height: float, active: bool = true) -> unit!:
        let w = self.workspace
        let selected = w.network.selection()
        let (nodes, shown) = w.on_screen()
        let meshes: list[Mesh] = []
        let clouds: list[GaussianSplats] = []
        let volumes: list[FogVolume] = []
        for at in 0..<shown.length:
            if nodes[at] in selected: meshes.append(shown[at])
        for at in 0..<w.batches.clouds.length:
            if w.batches.cloud_nodes[at] in selected: clouds.append(w.batches.clouds[at])
        for at in 0..<w.batches.volumes.length:
            if w.batches.volume_nodes[at] in selected: volumes.append(w.batches.volumes[at])
        let key = (w.revision, w.batches.scene_keys, selected, w.display.splat_look(w.view.display_mode)).hash()
        self.silhouette.draw(meshes, clouds, volumes, w.view.camera, painter.target(), theme.accent(), key, w.display.splat_look(w.view.display_mode), renderer = self.renderer)
        if w.display.hulls_visible:
            for at in 0..<w.batches.scenes.length:
                let scene = w.batches.scenes[at].scene()
                let helpers = w.batches.helpers(at)
                for index in 0..<helpers.length:
                    if helper_node(w, scene.path(index), "Camera" if scene.is_camera(index) else "Light", w.batches.scene_nodes[at]) in selected:
                        helpers[index].draw(w.view.camera, painter.target(), theme.accent())
        if active and self.boxing and dragged(self.box):
            draw_box(painter, self.box)