dymokomi/luced-3d
Source of release 0.11.1
0.11.1 / src / viewport_tools.luc
## Node-specific viewport UI. Topology and intersections remain in luce-3d.
from luce_ui.ui import Painter, Event, Point
from luce_gpu.gpu import Rect
from luce_3d.graphics import Vector3, EditOverlay, Renderer, Knife
from luce_window.input import EventKind, Key
from workspace import Workspace
from actions import Actions
from luce_std import math
import theme
from luce_ui import ui
from tool_icons import draw_icon
from node_gizmos import NodeGizmos
from viewport_placing import ViewportPlacing
import modeling_commands
import selection_commands
pub func projected(workspace: Workspace, point: Vector3, width: float, height: float) -> list[float]!:
let projection = workspace.view.camera.projection(math.max(1.0, width) / math.max(1.0, height))
let offset = point.subtract(projection.position)
let depth = -offset.dot(projection.backward)
if depth <= projection.near:
return [0.0, 0.0, -1.0]
return [(offset.dot(projection.right) * projection.focal / projection.aspect / depth + 1.0) * width * 0.5,
(1.0 - offset.dot(projection.up) * projection.focal / depth) * height * 0.5, depth]
pub func view_ray(workspace: Workspace, x: float, y: float, width: float, height: float) -> Vector3!:
let projection = workspace.view.camera.projection(math.max(1.0, width) / math.max(1.0, height))
return projection.right.multiply_scalar((x / width * 2.0 - 1.0) * projection.aspect / projection.focal).add(
projection.up.multiply_scalar((1.0 - y / height * 2.0) / projection.focal)).subtract(projection.backward).normalized()
pub func axis_vector(axis: int) -> Vector3:
return Vector3(1.0 if axis == 0 else 0.0, 1.0 if axis == 1 else 0.0, 1.0 if axis == 2 else 0.0)
pub class ViewportTools:
let workspace: Workspace
let actions: Actions
pub var dragging: bool = false
pub let gizmos: NodeGizmos
pub var hovered: int = -1
## A knife stroke being drawn: its start and end in the view.
var knifing: bool = false
var knife_line: list[float]
var hover_revision: int = -1
var hover_tool: int = -1
var tool_scroll: float = 0.0
## 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
## The viewport's renderer: faces are picked from its GPU id pass.
pub var renderer: Renderer? = none
pub func init(self, workspace: Workspace, actions: Actions):
self.knife_line = []
self.workspace = workspace
self.actions = actions
self.gizmos = NodeGizmos(workspace)
self.overlay = EditOverlay()
self.placing = ViewportPlacing(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
for index in 0..<2:
let x = width - 88.0 + float(index) * 38.0
painter.rounded_rectangle(Rect(x, 8.0, 32.0, 30.0), theme.surface(), 4.0)
draw_icon(painter, "Frame" if index == 0 else "Grid", x + 6.0, 13.0, 20.0, theme.accent() if self.hover_tool == index + 100 or (index == 1 and w.view.grid_visible) else theme.text())
if self.hover_tool == index + 100:
painter.rounded_rectangle(Rect(width - 184.0, 43.0, 168.0, 30.0), theme.surface(), 4.0)
painter.text("Frame geometry (F)" if index == 0 else "Toggle grid (G)", width - 176.0, 49.0, theme.text())
if self.hover_revision != w.revision:
self.hovered = -1
if 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]))
self.overlay.show(mesh, w.overlay_mode(), w.selection, self.hovered)
# 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)
self.draw_edit_controls(painter, height)
if self.knifing:
let line = self.knife_line
painter.line(Point(line[0], line[1]), Point(line[2], line[3]), 2.0, theme.accent())
self.gizmos.draw(painter, width, height)
func draw_edit_controls(self, painter: Painter, height: float) -> unit!:
let w = self.workspace
# The level switcher: the Edit kind's levels (Edit Mesh's elsewhere).
let levels = w.levels()
for mode in 0..<levels.length:
let x = 16.0 + float(mode) * 38.0
painter.rounded_rectangle(Rect(x, 44.0, 32.0, 30.0), theme.accent() if mode == w.component_mode else theme.surface(), 4.0)
let ink = theme.grey(0.0) if mode == w.component_mode else theme.text()
ui.draw_icon(painter.target(), levels[mode].icon, Rect(x + 5.0, 48.0, 22.0, 22.0), ink)
let caption = w.level().name
painter.text(caption + (f" / {w.selected_count()} selected" if w.selected_count() > 0 else ""), 24.0 + float(levels.length) * 38.0, 51.0, theme.text_muted())
if let prompt = w.prompt:
painter.rounded_rectangle(Rect(16.0, height - 40.0, 420.0, 28.0), theme.surface(), 4.0)
painter.text(f"{prompt.label}: select components, Enter to accept, Esc to cancel", 24.0, height - 35.0, theme.accent())
let commands = self.actions.strip()
for index in 0..<commands.length:
let label = "Extrude" if index == 2 else commands[index].text()
let y = 94.0 + float(index // 2) * 36.0 - self.tool_scroll
if y < 94.0 or y + 30.0 > height - 34.0:
continue
let x = 16.0 + float(index % 2) * 38.0
let available = commands[index].is_enabled()
painter.rounded_rectangle(Rect(x, y, 32.0, 30.0), theme.accent() if w.edit_tool == label else theme.surface(), 4.0)
draw_icon(painter, label, x + 6.0, y + 5.0, 20.0, theme.grey(0.0) if w.edit_tool == label else (theme.accent() if self.hover_tool == index and available else (theme.text() if available else theme.text_muted())))
if self.hover_tool == index:
painter.rounded_rectangle(Rect(94.0, y, 210.0, 30.0), theme.surface(), 4.0)
painter.text(label + ("" if available else " / select components"), 102.0, y + 6.0, theme.text())
func hit_tool(self, x: float, y: float, height: float) -> int:
if x < 16.0 or x > 86.0 or y < 94.0 or y > height - 34.0:
return -1
let column = int((x - 16.0) / 38.0)
let row = int((y - 94.0 + self.tool_scroll) / 36.0)
let id = row * 2 + column
return id if id < self.actions.strip().length else -1
## The component under (x, y): the face from the renderer's GPU id pass
## (a CPU ray before the first device frame), then for points and edges
## the nearest one on that face.
pub func pick(self, x: float, y: float, width: float, height: float) -> int!:
let w = self.workspace
let mesh = w.geometry else return -1
# A point cloud (CAD control vertices) is not drawn as a mesh: the
# nearest point on screen.
if mesh.face_count() == 0:
self.overlay.show(mesh, 0, w.selection, self.hovered)
return self.overlay.pick(w.view.camera, x, y, width, height, -1)
var face = -2
if let renderer = self.renderer:
face = renderer.pick_face(mesh, w.view.camera, x, y, width, height) catch failure:
recover -2
if w.domain() == 2:
if face >= -1:
return face
return mesh.ray_face(w.view.camera.position(), view_ray(w, x, y, width, height))
self.overlay.show(mesh, w.overlay_mode(), w.selection, self.hovered)
return self.overlay.pick(w.view.camera, x, y, width, height, face)
## 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.hover_tool, self.gizmos.hovered]
self.placing.moved = false
let handled = self.handle(event, width, height)
self.hover_changed = before != [self.hovered, self.hover_tool, self.gizmos.hovered] or self.placing.moved
return handled
## The Knife tool: press, drag a line across the model (Shift snaps it to
## 15°), release to cut; Escape or a lost pointer cancels.
func knife_event(self, event: Event, width: float, height: float) -> bool!:
let w = self.workspace
if not self.knifing:
if w.edit_tool != "Knife" or not w.selecting() or event.kind != EventKind.pointer_down or event.button != 0 or not event.inside or event.alt:
return false
# The level switcher and the tool strip keep their clicks.
if event.y <= 80.0 or (event.x <= 90.0 and event.y >= 90.0):
return false
self.knifing = true
self.knife_line = [event.x, event.y, event.x, event.y]
return true
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.knifing = false
return true
if event.kind == EventKind.pointer_moved or event.kind == EventKind.pointer_up:
var x = event.x
var y = event.y
if event.shift:
let snapped = Knife.snapped(self.knife_line[0], self.knife_line[1], x, y)
x = snapped.x
y = snapped.y
self.knife_line = [self.knife_line[0], self.knife_line[1], x, y]
if event.kind == EventKind.pointer_up:
self.knifing = false
let line = self.knife_line
modeling_commands.knife(w, line[0], line[1], line[2], line[3], width, height)
return true
return true
func handle(self, event: Event, width: float, height: float) -> bool!:
let w = self.workspace
let handled = self.gizmos.event(event, width, height)
self.dragging = self.gizmos.dragging
if handled:
return true
if self.knife_event(event, width, height):
return true
if event.kind == EventKind.scroll and w.selecting() and not event.alt and event.x >= 16.0 and event.x <= 86.0 and event.y >= 94.0:
let maximum = math.max(0.0, float((self.actions.strip().length + 1) // 2) * 36.0 - height + 144.0)
self.tool_scroll = math.max(0.0, math.min(maximum, self.tool_scroll - event.scroll_y * 12.0))
return true
if event.kind == EventKind.pointer_moved:
self.hovered = -1
self.hover_tool = -1
self.hover_revision = w.revision
if event.inside and not event.alt and event.y >= 8.0 and event.y <= 38.0 and event.x >= width - 88.0 and event.x <= width - 18.0:
self.hover_tool = 100 + int((event.x - width + 88.0) / 38.0)
elif event.inside and not event.alt and not self.dragging and w.selecting():
if event.x >= 16.0 and event.x <= 86.0 and event.y >= 94.0:
self.hover_tool = self.hit_tool(event.x, event.y, height)
elif not (event.y >= 44.0 and event.y <= 74.0 and event.x <= 250.0):
if let placing = w.placing:
self.placing.hover(placing, self.overlay, self.renderer, event.x, event.y, width, height)
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.hover_tool = -1
if event.kind != EventKind.pointer_down or not event.inside or event.button != 0 or event.alt:
return false
if event.y >= 8.0 and event.y <= 38.0 and event.x >= width - 88.0 and event.x <= width - 18.0:
if event.x < width - 50.0:
self.actions.frame.trigger()
else:
self.actions.grid.trigger()
return true
if w.selecting():
let levels = w.levels().length
if event.y >= 44.0 and event.y <= 74.0 and event.x >= 16.0 and event.x < 16.0 + float(levels) * 38.0 - 6.0:
let mode = int((event.x - 16.0) / 38.0)
self.actions.modes[mode].trigger()
return true
let tool = self.hit_tool(event.x, event.y, height)
if tool >= 0:
self.actions.strip()[tool].trigger()
return true
if let placing = w.placing:
self.placing.click(placing, self.overlay, self.renderer, event.x, event.y, width, height)
return true
if w.selecting():
selection_commands.choose_component(w, self.pick(event.x, event.y, width, height), event.shift)
return true
return false