dymokomi/luced-3d
Source of release 0.11.8
0.11.8 / 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
## (bisect_stroke.luc), object picking (object_box.luc), component boxes
## (component_box.luc), splat selection (splat_select.luc) and the node
## gizmos
## (node_gizmos.luc), a tool's drag after it runs (tool_drag.luc) and the
## hover gesture's preview and click (hover_gesture.luc), primitives drawn
## (create_gesture.luc) and Poly Pen (pen_gesture.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, Key
import retopology
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 bisect_stroke import BisectStroke
from object_box import ObjectBox
from component_box import ComponentBox
from splat_select import SplatSelect
from tool_drag import ToolDrag
from hover_gesture import HoverGesture
from create_gesture import CreateGesture
from pen_gesture import PenGesture
from sculpt_gesture import SculptGesture
from lattice_gesture import LatticeGesture
from mesh_check_view import MeshCheckView
import selection_commands
import selection_gestures
from luce_geocore.geocore import Selection
import component_moves
import edit_cad_nets
import edit_cad_commands
from edit_types import edge_level
from luce_std import math
import time
pub class ViewportTools:
let workspace: Workspace
pub var dragging: bool = false
pub let gizmos: NodeGizmos
pub let chrome: ViewportChrome
pub let picker: ComponentPicker
let bisect: BisectStroke
pub let objects: ObjectBox
## A press at a component level: a click, or a box when dragged.
pub let components: ComponentBox
## Box, lasso and brush selection of splats (Edit GSplats).
pub let splats: SplatSelect
## The drag of the tool run last, and a hover tool's preview.
pub let drag: ToolDrag
pub let hovering: HoverGesture
## A primitive drawn in the viewport, and Poly Pen.
pub let creating: CreateGesture
pub let pen: PenGesture
## Sculpt's brush (sculpt_gesture.luc).
pub let sculpt: SculptGesture
## A Lattice's cage over its result (lattice_gesture.luc).
pub let lattice: LatticeGesture
## The Mesh Check overlay and its panel (mesh_check_view.luc).
pub let checking: MeshCheckView
## 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
var last_click: int = 0
## Clicks in a row on one CV (Edit CAD's hull picking), and which.
var cv_clicks: int = 0
var last_cv: int = -1
var last_x: float = 0.0
var last_y: float = 0.0
var last_node: int = -1
var last_level: int = -1
## The component the last click picked, and the undo position after it:
## a double-click takes that click back (selection_gestures.luc).
var last_id: int = -1
var click_mark: int = -1
## Whether the click just handled finished a double-click (the next
## one starts afresh).
var double: bool = false
## What a double-click on the hovered edge would take (its loop, or with
## Cmd/Ctrl its ring), and what it was made for.
pub var preview: Selection? = none
var preview_for: list[int]
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.bisect = BisectStroke(workspace)
self.objects = ObjectBox(workspace)
self.components = ComponentBox(workspace)
self.splats = SplatSelect(workspace)
self.drag = ToolDrag(workspace)
self.hovering = HoverGesture(workspace)
self.creating = CreateGesture(workspace, self.overlay)
self.pen = PenGesture(workspace, self.overlay, self.picker)
self.sculpt = SculptGesture(workspace)
self.lattice = LatticeGesture(workspace)
self.checking = MeshCheckView(workspace)
self.preview_for = []
# The renderer tints selected faces (Renderer.set_face_selection).
self.overlay.set_fill(false)
## Over a pane `width` by `height`; the `active` pane (the pointer's)
## also draws the boxes and strokes being dragged.
pub func draw(self, painter: Painter, width: float, height: float, active: bool = true) -> unit!:
let w = self.workspace
if self.hover_revision != w.revision:
self.hovered = -1
self.preview = none
# Docked over four panes, the chrome draws once (view.luc).
if not self.chrome.docked():
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()
let cad_hover = w.picking_brep() and self.hovered >= 0
self.overlay.set_fill(w.picking_brep())
self.overlay.show(mesh, w.overlay_mode(), w.selection, self.hovered if own and not cad_hover else -1)
# A hover tool shows what it would make; else what a
# double-click would take.
if let made = self.hovering.made:
self.overlay.set_preview(made, self.hovering.preview)
else:
self.overlay.set_preview(mesh, self.preview)
self.overlay.set_hidden(mesh, w.hidden_faces())
# A kind whose pick geometry the viewport does not draw (Edit
# SDF's proxies) shows its selection through surfaces, dimmed.
let kind = w.selection_kind()
let through = kind != none and (kind else trap("kind")).see_through
# Edge marks (seams, hard edges) show where meshes are edited.
let marks = w.editing() and not w.picking_brep()
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, preview = theme.preview(),
seam = theme.seam() if marks else none, sharp = theme.hard_edge() if marks else none)
self.placing.draw(painter)
self.pen.draw(painter, width, height)
self.sculpt.draw(painter, width, height)
if cad_hover:
self.picker.draw_cad_hover(painter, w.level_index() if own else self.hover_level, self.hovered)
elif 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, active)
if active:
self.components.draw(painter)
self.bisect.draw(painter)
self.drag.draw(painter, width, height)
self.hovering.draw(painter)
self.creating.draw(painter, width, height)
self.lattice.draw(painter, width, height)
self.checking.draw(painter, width, height)
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 or self.creating.changed or self.pen.changed or self.sculpt.changed or self.lattice.changed or self.checking.changed
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
# The tool run last takes drags anywhere (not on the buttons).
if button < 0 and self.drag.event(event, width, height):
return true
if self.hovering.event(event):
return true
if w.picking_target:
if event.kind == EventKind.pointer_down and event.inside and event.button == 0 and not event.alt:
_ = retopology.picked(w, event.x, event.y, width, height)
return true
if event.kind == EventKind.key_down and event.key == Key.escape:
retopology.clear(w)
return true
if button < 0 and self.checking.event(event):
return true
if button < 0 and self.lattice.event(event, width, height):
return true
if button < 0 and self.creating.event(event, width, height, self.picker.renderer):
return true
if button < 0 and self.pen.event(event, width, height):
return true
if button < 0 and self.sculpt.event(event, width, height):
return true
if self.bisect.event(event, width, height, button >= 0):
return true
if self.objects.event(event, width, height):
return true
let press = self.components.event(event, width, height, self.picker.renderer)
if press == 2:
let at = self.components.box
self.click(at[0], at[1], self.components.operation, width, height)
if press > 0:
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, event.control or event.meta, event.shift)
elif w.picking_smart():
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)
self.follow_hover(event.control or event.meta)
self.hovering.hover(self.hovered)
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, event.control or event.meta, event.shift)
return true
if w.picking_objects():
self.objects.start(event)
return true
if splats:
self.splats.start(event, width, height)
return true
if w.selecting():
# The click (or the box, when it is dragged) comes on release.
self.components.start(event)
return true
return false
## The loop (or ring, with Cmd/Ctrl) a double-click on the hovered edge
## would take, made again only when the edge or the modifier changed.
func follow_hover(self, command: bool) -> unit!:
let w = self.workspace
let own = self.hover_level < 0 or self.hover_level == w.level_index()
let wanted = [self.hovered if own else -1, 1 if command else 0, w.revision]
if wanted == self.preview_for:
return
self.preview_for = wanted
self.preview = selection_gestures.preview(w, wanted[0], command)
## A press released where it started, at (x, y) of a component level:
## the component under it (Smart's level's under Smart), alone (operation
## 0), added (1, Shift) or taken away (2, Ctrl or Cmd); a second click on
## a CAD edge takes its tangent chain.
func click(self, x: float, y: float, operation: int, width: float, height: float) -> unit!:
let w = self.workspace
let now = time.now()
let chain = w.network.context() == "cad" and self.last_node == w.network.selected and self.last_level == edge_level and now - self.last_click < 400000000 and math.hypot(x - self.last_x, y - self.last_y) < 5.0
if w.picking_smart():
let found = self.picker.smart_pick(x, y, width, height)
let twice = now - self.last_click < 400000000 and math.hypot(x - self.last_x, y - self.last_y) < 5.0 and found[1] >= 0 and found[1] == self.last_id and found[0] == self.last_level
if chain and found[0] == edge_level:
selection_commands.choose_component(w, found[1], operation == 1, chain = true, remove = operation == 2)
elif selection_gestures.apply(w) and found[0] == w.level_index() and (twice or (self.components.shift and self.components.command)):
self.gesture(found[1], twice)
else:
selection_commands.choose_smart(w, found[0], found[1], operation)
self.last_id = found[1]
self.click_mark = w.history.cursor
elif component_moves.at_cvs(w):
# Hull picking: clicks again on one CV take its row along V,
# then along U, then its net (with Pick hull rows, from the
# first click).
let id = self.pick(x, y, width, height)
let again = id >= 0 and id == self.last_cv and now - self.last_click < 400000000 and math.hypot(x - self.last_x, y - self.last_y) < 5.0
self.cv_clicks = self.cv_clicks + 1 if again else 1
self.last_cv = id
var rows = 0
if let node = w.selected():
rows = 1 if node.values.length > edit_cad_nets.hull_pick_slot and node.values[edit_cad_nets.hull_pick_slot] != 0.0 else 0
let taken = self.cv_clicks - 1 + rows
selection_commands.choose_component(w, id, operation == 1, remove = operation == 2, row = (taken - 1) % 3 if taken > 0 else -1)
else:
let id = self.pick(x, y, width, height)
# At Edit CAD's Curves level a double-click on a surface, no
# curve under it, picks the isoparm there (Alias's).
let twice = now - self.last_click < 400000000 and math.hypot(x - self.last_x, y - self.last_y) < 5.0
if id < 0 and operation == 0 and twice and edit_cad_commands.pick_isoparm(w, x, y, width, height):
self.last_click = 0
return
if selection_gestures.apply(w) and not chain:
self.gesture(id, twice and id >= 0 and id == self.last_id and self.last_level == w.level_index())
else:
selection_commands.choose_component(w, id, operation == 1, chain = chain, remove = operation == 2)
self.last_click = 0 if chain or self.double else now
self.double = false
self.last_x = x
self.last_y = y
self.last_node = w.network.selected
self.last_level = w.level_index()
## A click on component `id` of a mesh (selection_gestures.luc):
## the second of a double-click (`twice`) takes the first back and runs
## the double-click gesture; Shift+Cmd/Ctrl adds the shortest path;
## else Shift adds, Cmd/Ctrl takes away, a plain click selects.
func gesture(self, id: int, twice: bool) -> unit!:
let w = self.workspace
let shift = self.components.shift
let command = self.components.command
if twice:
# The first click is one undo step; the gesture replaces it.
if w.prompt == none and w.history.cursor == self.click_mark and self.click_mark > 0:
w.undo()
selection_gestures.double_click(w, id, shift, command)
self.double = true
elif shift and command:
selection_gestures.path_click(w, id)
else:
selection_commands.choose_component(w, id, shift, remove = command and not shift)
self.last_id = id
self.click_mark = w.history.cursor