Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.3

0.11.3 / src / viewport_tools.luc

## The viewport's node tools over the scene: the chrome's buttons
## (viewport_chrome.luc), the edit overlay and component picking
## (component_pick.luc), click placing (viewport_placing.luc), the Knife
## (knife_stroke.luc), object picking (object_box.luc), splat selection
## (splat_select.luc) and the node gizmos
## (node_gizmos.luc). Topology and intersections remain in luce-3d.
from luce_ui.ui import Painter, Event
from luce_3d.graphics import EditOverlay
from luce_window.input import EventKind
from workspace import Workspace
from actions import Actions
import theme
from node_gizmos import NodeGizmos
from viewport_placing import ViewportPlacing
from viewport_chrome import ViewportChrome
from component_pick import ComponentPicker, draw_hover
from knife_stroke import KnifeStroke
from object_box import ObjectBox
from splat_select import SplatSelect
import selection_commands
from edit_types import smart_level

pub class ViewportTools:
    let workspace: Workspace
    pub var dragging: bool = false
    pub let gizmos: NodeGizmos
    pub let chrome: ViewportChrome
    pub let picker: ComponentPicker
    let knife: KnifeStroke
    let objects: ObjectBox
    ## Box, lasso and brush selection of splats (Edit GSplats).
    pub let splats: SplatSelect
    ## The hovered component, and under Smart its level (-1: the
    ## selection's).
    pub var hovered: int = -1
    var hover_level: int = -1
    var hover_revision: int = -1
    ## Wires, selection and point markers stay in Base, keyed by the mesh and
    ## the selection, so hover and camera frames never loop over the mesh here.
    pub let overlay: EditOverlay
    ## Faces and corners placed by clicking (Plane from Face, PolyDraw).
    pub let placing: ViewportPlacing
    pub var hover_changed: bool = false

    pub func init(self, workspace: Workspace, actions: Actions):
        self.workspace = workspace
        self.gizmos = NodeGizmos(workspace)
        self.overlay = EditOverlay()
        self.placing = ViewportPlacing(workspace)
        self.chrome = ViewportChrome(workspace, actions)
        self.picker = ComponentPicker(workspace, self.overlay)
        self.knife = KnifeStroke(workspace)
        self.objects = ObjectBox(workspace)
        self.splats = SplatSelect(workspace)
        # The renderer tints selected faces (Renderer.set_face_selection).
        self.overlay.set_fill(false)

    pub func draw(self, painter: Painter, width: float, height: float) -> unit!:
        let w = self.workspace
        if self.hover_revision != w.revision:
            self.hovered = -1
        self.chrome.draw(painter, width, height)
        if self.splats.applies():
            # Splats show their selection tinted (the view sets the marks).
            self.splats.draw(painter)
        elif w.selecting():
            let mesh = w.geometry else return
            # Points a soft selection reaches show its falloff (weighed in Base).
            let soft = w.soft_selection()
            self.overlay.set_soft(soft[0], int(soft[1]), int(soft[2]))
            # Smart's hover on another level than the selection's is drawn
            # here (one component), not by the overlay.
            let own = self.hover_level < 0 or self.hover_level == w.level_index()
            self.overlay.show(mesh, w.overlay_mode(), w.selection, self.hovered if own else -1)
            # A kind whose pick geometry the viewport does not draw (Edit
            # SDF's proxies) shows its selection through surfaces, dimmed.
            let kind = w.active_kind()
            let through = kind != none and (kind else trap("kind")).see_through
            self.overlay.draw(w.view.camera, painter.target(), theme.grey(90.0), theme.accent(), theme.text(), wires = false, hidden = theme.grey(150.0) if through else none)
            self.placing.draw(painter)
            if not own and self.hovered >= 0:
                draw_hover(painter, w, mesh, self.hover_level, self.hovered, width, height)
        if w.picking_objects():
            self.objects.draw(painter, width, height)
        self.knife.draw(painter)
        self.gizmos.draw(painter, width, height)
        self.chrome.draw_snap(painter, width, height)

    ## The component under (x, y) at the selection's level.
    pub func pick(self, x: float, y: float, width: float, height: float) -> int!:
        return self.picker.pick(x, y, width, height, self.hovered)

    ## Returns true when node tools consume the input; otherwise navigation owns it.
    ## Records whether the highlight changed, so an idle pointer move over the
    ## same face requests no frame and a hover change requests exactly one.
    pub func event(self, event: Event, width: float, height: float) -> bool!:
        let before = [self.hovered, self.chrome.hover_tool, self.gizmos.hovered]
        self.placing.moved = false
        let handled = self.handle(event, width, height)
        # The splat brush's circle follows the pointer.
        let brushing = event.kind == EventKind.pointer_moved and self.workspace.splat_shape == 2 and self.splats.applies()
        self.hover_changed = before != [self.hovered, self.chrome.hover_tool, self.gizmos.hovered] or self.placing.moved or brushing
        return handled

    func handle(self, event: Event, width: float, height: float) -> bool!:
        let w = self.workspace
        # The buttons keep their clicks from every tool.
        let button = self.chrome.button_at(event.x, event.y, width, height) if event.inside and not event.alt else -1
        if event.kind == EventKind.pointer_down and event.button == 0 and button >= 0 and not self.gizmos.dragging:
            self.chrome.press(button)
            return true
        let handled = self.gizmos.event(event, width, height)
        self.dragging = self.gizmos.dragging
        if handled:
            return true
        if self.knife.event(event, width, height, button >= 0):
            return true
        if self.objects.event(event, width, height):
            return true
        if self.splats.event(event, width, height):
            return true
        let splats = self.splats.applies()
        if event.kind == EventKind.pointer_moved:
            self.hovered = -1
            self.hover_level = -1
            self.chrome.hover_tool = button
            self.hover_revision = w.revision
            if button < 0 and event.inside and not event.alt and not self.dragging and w.selecting() and not w.picking_objects() and not splats:
                if let placing = w.placing:
                    self.placing.hover(placing, self.overlay, self.picker.renderer, event.x, event.y, width, height)
                elif w.component_mode == smart_level:
                    let found = self.picker.smart_pick(event.x, event.y, width, height)
                    self.hover_level = found[0]
                    self.hovered = found[1]
                else:
                    self.hovered = self.pick(event.x, event.y, width, height)
        if event.kind == EventKind.pointer_left or event.kind == EventKind.focus_lost or event.cancelled or event.alt:
            self.hovered = -1
            self.chrome.hover_tool = -1
        if event.kind != EventKind.pointer_down or not event.inside or event.button != 0 or event.alt:
            return false
        if let placing = w.placing:
            self.placing.click(placing, self.overlay, self.picker.renderer, event.x, event.y, width, height)
            return true
        if w.picking_objects():
            self.objects.start(event.x, event.y)
            return true
        if splats:
            self.splats.start(event, width, height)
            return true
        if w.selecting():
            if w.component_mode == smart_level:
                let found = self.picker.smart_pick(event.x, event.y, width, height)
                selection_commands.choose_smart(w, found[0], found[1], event.shift)
            else:
                selection_commands.choose_component(w, self.pick(event.x, event.y, width, height), event.shift)
            return true
        return false