dymokomi/luced-3d
Source of release 0.11.7
0.11.7 / src / node_gizmos.luc
## Node-owned, screen-sized interaction handles. Dragging edits parameters only;
## the worker computes geometry. One gesture is one undo transaction. The
## workspace's tool picks the gizmo: Move, Rotate and Scale act on a
## transforming node (Transform, Group, CAD) or on the selected components
## (a Transform Components step or node); Manipulator shows the selected
## node's own handles (a Box node's corners, in its placed frame). P toggles moving a transforming
## node's pivot.
import node_catalog
from workspace import Workspace
from luce_ui.ui import Painter, Point, Event
from luce_gpu.gpu import Rect
from luce_window.input import Key, EventKind
from luce_3d.graphics import Vector3, Matrix4
import view_projection as gm
from luce_std import math
import theme
from tools import gizmo_tool
from snapping import SnapMark, grid_step, on_grid, stepped
from angles import radians, degrees
import component_moves
from luce_geocore.geocore import Verbs
import edit_cad_nets
pub class NodeGizmos:
let w: Workspace
pub var dragging: bool = false
## Moving a transforming node's pivot instead of the node (P).
pub var pivot: bool = false
pub var size: float = 1.0
pub var hovered: int = -1
var handle: int = -1
var start_x: float = 0.0
var start_y: float = 0.0
var length: float = 1.0
var center: Vector3
var start_point: Vector3
var plane_normal: Vector3
var original: list[float]
var start_angle: float = 0.0
var rotation: float = 0.0
pub func init(self, workspace: Workspace):
self.w = workspace
self.center = Vector3()
self.start_point = Vector3()
self.plane_normal = Vector3()
self.original = []
## The gizmo in use: Move, Rotate, Scale or Pivot.
pub func mode(self) -> str:
if self.pivot:
if let node = self.w.selected():
if node.kind in transforming_kinds(): return "Pivot"
let tool = self.w.tool_in_use()
return tool if gizmo_tool(tool) else "Move"
pub func available(self) -> bool:
let node = self.w.selected() else return false
if not node.enabled:
return false
let tool = self.w.tool_in_use()
if tool == "Manipulator":
return node.kind == box_kind
if not gizmo_tool(tool):
return false
if node.kind in ["Camera", "Light"]:
return tool in ["Move", "Rotate"]
return node.kind in transforming_kinds() or (self.w.selecting() and self.w.selected_count() > 0)
## Whether the gizmo moves the selected components (not a node's values).
func components(self) -> bool:
let node = self.w.selected() else return false
return not node_catalog.moves_itself(node.kind)
pub func origin(self) -> Vector3!:
let node = self.w.selected() else return Vector3()
if node.kind in ["Camera", "Light"]:
# Their remaining values are lens/light settings, not scale or pivot.
return Vector3(node.values[1], node.values[2], node.values[3])
if node.kind in transforming_kinds():
let v = node.values
return Vector3(v[1] + v[10], v[2] + v[11], v[3] + v[12])
# Drawn every frame: the selection's center is a Base pass.
return self.w.selection_center()
## Box corner `id` (bit 1 +X, 2 the top, 4 +Z) of a Box node's values:
## its frame's (Position, Up, Turn) point of the corner of its Size.
func corner(self, id: int, values: list[float]) -> Vector3!:
let local = Vector3(values[0] * (0.5 if id % 2 == 1 else -0.5), values[1] * float((id // 2) % 2), values[2] * (0.5 if id // 4 == 1 else -0.5))
return box_point(values, local)
func axis(self, id: int) -> Vector3!:
let node = self.w.selected() else return gm.axis(id)
# Edit CAD's CV move modes turn the move gizmo into their frame.
if self.mode() == "Move" and self.components():
if let fixed = self.w.cv_frame:
return fixed[id]
if let frame = component_moves.cv_axes(self.w):
return frame[id]
if self.mode() != "Scale" or self.components():
return gm.axis(id)
let v = node.values
let rotation = Matrix4.compose(Vector3(), Vector3(radians(v[4]), radians(v[5]), radians(v[6])), Vector3(1.0, 1.0, 1.0))
return rotation.x if id == 0 else (rotation.y if id == 1 else rotation.z)
func planes(self, id: int, center: Vector3, length: float, width: float, height: float) -> list[list[float]]!:
let a = self.axis((id + 1) % 3)
let b = self.axis((id + 2) % 3)
let result: list[list[float]] = []
for pair in [[0.23, 0.23], [0.40, 0.23], [0.40, 0.40], [0.23, 0.40]]:
result.append(gm.project(self.w, center.add(a.multiply_scalar(pair[0] * length)).add(b.multiply_scalar(pair[1] * length)), width, height))
return result
func ring(self, axis: int, center: Vector3, length: float, width: float, height: float) -> list[list[float]]!:
let points: list[list[float]] = []
for at in 0..<65:
let angle = float(at) * math.pi / 32.0
points.append(gm.project(self.w, center.add(gm.axis((axis + 1) % 3).multiply_scalar(math.cos(angle) * length)).add(gm.axis((axis + 2) % 3).multiply_scalar(math.sin(angle) * length)), width, height))
return points
pub func hit(self, x: float, y: float, width: float, height: float) -> int!:
if not self.available():
return -1
let node = self.w.selected() else return -1
if node.kind == box_kind:
var best = 11.0
var found = -1
for id in 0..<8:
let p = gm.project(self.w, self.corner(id, node.values), width, height)
let distance = math.hypot(x - p[0], y - p[1])
if p[2] > 0.0 and distance < best:
best = distance
found = id + 10
return found
let center = self.origin()
let length = self.w.view.distance * 0.14 * self.size
let start = gm.project(self.w, center, width, height)
if start[2] <= 0.0:
return -1
if self.mode() == "Rotate":
var best = 8.0
var found = -1
for axis in 0..<3:
let points = self.ring(axis, center, length, width, height)
for at in 0..<64:
let distance = gm.segment_distance(x, y, points[at], points[at + 1])
if points[at][2] > 0.0 and points[at + 1][2] > 0.0 and distance < best:
best = distance
found = axis
return found
if math.hypot(x - start[0], y - start[1]) < 9.0:
return 6
for axis in 0..<3:
if gm.inside(x, y, self.planes(axis, center, length, width, height)):
return axis + 3
var best = 8.0
var found = -1
for axis in 0..<3:
let a = gm.project(self.w, center.add(self.axis(axis).multiply_scalar(length * 0.18)), width, height)
let b = gm.project(self.w, center.add(self.axis(axis).multiply_scalar(length)), width, height)
let distance = gm.segment_distance(x, y, a, b)
if a[2] > 0.0 and b[2] > 0.0 and distance < best:
best = distance
found = axis
return found
pub func draw(self, painter: Painter, width: float, height: float) -> unit!:
if not self.available():
return
let node = self.w.selected() else return
if node.kind == box_kind:
for id in 0..<8:
let p = gm.project(self.w, self.corner(id, node.values), width, height)
if p[2] <= 0.0:
continue
let hot = (self.handle if self.dragging else self.hovered) == id + 10
let radius = 5.0 if hot else 3.5
painter.rectangle(Rect(p[0] - radius - 1.0, p[1] - radius - 1.0, radius * 2.0 + 2.0, radius * 2.0 + 2.0), theme.backdrop())
painter.rectangle(Rect(p[0] - radius, p[1] - radius, radius * 2.0, radius * 2.0), theme.accent() if hot else theme.text())
return
let center = self.origin()
let length = self.w.view.distance * 0.14 * self.size
let start = gm.project(self.w, center, width, height)
if start[2] <= 0.0:
return
let hot = self.handle if self.dragging else self.hovered
for axis in 0..<3:
let color = theme.accent() if hot == axis else theme.axis(axis)
if self.mode() == "Rotate":
let points = self.ring(axis, center, length, width, height)
for at in 0..<64:
if points[at][2] > 0.0 and points[at + 1][2] > 0.0:
painter.line(Point(points[at][0], points[at][1]), Point(points[at + 1][0], points[at + 1][1]), 3.0 if hot == axis else 1.8, color)
continue
let end = gm.project(self.w, center.add(self.axis(axis).multiply_scalar(length)), width, height)
if end[2] <= 0.0:
continue
painter.line(Point(start[0], start[1]), Point(end[0], end[1]), 3.0 if hot == axis else 2.0, color)
if self.mode() == "Scale":
painter.rectangle(Rect(end[0] - 5.0, end[1] - 5.0, 10.0, 10.0), color)
else:
let dx = end[0] - start[0]
let dy = end[1] - start[1]
let size = math.max(0.001, math.hypot(dx, dy))
let ux = dx / size
let uy = dy / size
painter.mesh([Point(end[0], end[1]), Point(end[0] - ux * 12.0 - uy * 4.0, end[1] - uy * 12.0 + ux * 4.0), Point(end[0] - ux * 12.0 + uy * 4.0, end[1] - uy * 12.0 - ux * 4.0)], color)
# Mesh primitives are GPU-clipped, but text creates a bounded image
# region. A remote pivot can project far beyond that region's range
# while inspecting a tiny imported part. Cull only the label, not
# its axis (which may still cross the viewport).
let label_x = end[0] + 8.0
let label_y = end[1] - 9.0
if label_x >= -32.0 and label_x <= width and label_y >= -32.0 and label_y <= height:
painter.text(self.axis_names()[axis], label_x, label_y, color)
let corners = self.planes(axis, center, length, width, height)
let ink = theme.accent() if hot == axis + 3 else theme.axis(axis)
if hot == axis + 3:
let a = Point(corners[0][0], corners[0][1])
let b = Point(corners[1][0], corners[1][1])
let c = Point(corners[2][0], corners[2][1])
let d = Point(corners[3][0], corners[3][1])
painter.mesh([a,b,c,a,c,d], ink)
for at in 0..<4:
painter.line(Point(corners[at][0], corners[at][1]), Point(corners[(at + 1) % 4][0], corners[(at + 1) % 4][1]), 1.5, ink)
painter.rectangle(Rect(start[0] - 5.0, start[1] - 5.0, 10.0, 10.0), theme.accent() if hot == 6 or self.mode() == "Pivot" else theme.text())
pub func event(self, event: Event, width: float, height: float) -> bool!:
let node = self.w.selected() else return false
if not self.available() and not self.dragging:
return false
if event.kind == EventKind.key_down and not event.control and not event.meta:
if event.key in [Key.equal, Key.minus]:
self.size = math.clamp(self.size * (1.15 if event.key == Key.equal else 1.0 / 1.15), 0.35, 3.0)
return true
if not self.dragging and event.key == Key.p and node.kind in transforming_kinds():
self.pivot = not self.pivot
return true
if event.cancelled or event.kind in [EventKind.focus_lost, EventKind.overflow] or (event.kind == EventKind.key_down and event.key == Key.escape):
self.hovered = -1
if self.dragging:
self.w.cancel()
self.dragging = false
self.released()
return true
if self.dragging:
if event.kind == EventKind.pointer_up:
self.w.commit()
self.dragging = false
self.released()
return true
if event.kind == EventKind.pointer_moved:
self.update(event, width, height)
return true
if event.kind == EventKind.pointer_left or event.alt:
self.hovered = -1
if event.kind == EventKind.pointer_moved and event.inside and not event.alt:
self.hovered = self.hit(event.x, event.y, width, height)
if event.kind != EventKind.pointer_down or event.button != 0 or event.alt or not event.inside:
return false
let handle = self.hit(event.x, event.y, width, height)
if handle < 0:
return false
self.handle = handle
self.original = node.values.copy()
self.center = self.origin()
self.length = self.w.view.distance * 0.14 * self.size
self.start_x = event.x
self.start_y = event.y
self.plane_normal = self.axis(handle - 3) if handle >= 3 and handle <= 5 else self.w.view.camera.projection(width / height).backward
self.start_point = gm.plane(self.w, event.x, event.y, width, height, self.center, self.plane_normal)
let p = gm.project(self.w, self.center, width, height)
self.start_angle = math.atan2(event.y - p[1], event.x - p[0])
self.rotation = 0.0
self.w.begin("Resize Box" if node.kind == box_kind else self.mode() + " gizmo")
self.dragging = true
# A CV drag keeps its frame and shows the hulls as they were.
if self.components() and component_moves.at_cvs(self.w):
self.w.cv_frame = component_moves.cv_axes(self.w) if self.mode() == "Move" else none
self.w.ghost_hulls = self.w.batches.hull_batches.copy()
return true
## The axes' labels: X, Y, Z, or the CV move mode's frame (NUV's
## normal, U and V; the hull modes' U, V and normal; View's screen
## horizontal, vertical and depth).
func axis_names(self) -> list[str]!:
if self.mode() == "Move" and self.components() and component_moves.moves_cvs_by_mode(self.w):
let node = self.w.selected() else return ["X", "Y", "Z"]
let mode = int(node.values[edit_cad_nets.move_mode_slot])
if mode == 1: return ["N", "U", "V"]
if mode >= 2 and mode <= 4: return ["U", "V", "N"]
if mode == 5: return ["H", "V", "D"]
return ["X", "Y", "Z"]
## A drag over: no fixed frame, no ghost.
func released(self):
self.w.cv_frame = none
self.w.ghost_hulls = []
func update(self, event: Event, width: float, height: float) -> unit!:
let node = self.w.selected() else return
let values = self.original.copy()
var delta = gm.plane(self.w, event.x, event.y, width, height, self.center, self.plane_normal).subtract(self.start_point)
if self.handle < 3:
let a = gm.project(self.w, self.center, width, height)
let b = gm.project(self.w, self.center.add(self.axis(self.handle).multiply_scalar(self.length)), width, height)
let dx = b[0] - a[0]
let dy = b[1] - a[1]
let amount = ((event.x - self.start_x) * dx + (event.y - self.start_y) * dy) / math.max(0.000001, dx * dx + dy * dy) * self.length
delta = self.axis(self.handle).multiply_scalar(amount)
if (self.mode() == "Move" or self.mode() == "Pivot") and self.w.cv_frame == none and component_moves.cv_axes(self.w) == none:
delta = self.grid_moved(delta, event.meta or event.control)
if node.kind == box_kind:
# The corner dragged against the one across from it, in the
# box's own frame: its size, and its base's center as Position.
let id = self.handle - 10
let fixed = box_local(self.original, self.corner(7 - id, self.original))
let moved = box_local(self.original, self.corner(id, self.original).add(delta))
let a = [fixed.x, fixed.y, fixed.z]
let b = [moved.x, moved.y, moved.z]
for axis in 0..<3:
let positive = (id // (1 if axis == 0 else (2 if axis == 1 else 4))) % 2 == 1
b[axis] = math.max(a[axis] + 0.001, b[axis]) if positive else math.min(a[axis] - 0.001, b[axis])
values[axis] = math.abs(b[axis] - a[axis])
let base = box_point(self.original, Vector3((a[0] + b[0]) * 0.5, math.min(a[1], b[1]), (a[2] + b[2]) * 0.5))
values[box_position] = base.x
values[box_position + 1] = base.y
values[box_position + 2] = base.z
elif self.components():
# The selection's Transform Components: translate, rotate in
# degrees, scale (about its centroid).
let turn = self.turned(event, width, height) if self.mode() == "Rotate" else [0.0, 0.0, 0.0]
let factors = self.scaled(event, delta) if self.mode() == "Scale" else [1.0, 1.0, 1.0]
let moved = delta if self.mode() == "Move" else Vector3()
component_moves.preview_transform(self.w, moved, Vector3(turn[0], turn[1], turn[2]), Vector3(factors[0], factors[1], factors[2]))
return
elif self.mode() == "Rotate":
let turn = self.turned(event, width, height)
for axis in 0..<3:
values[4 + axis] += turn[axis]
elif self.mode() == "Scale":
let factors = self.scaled(event, delta)
for axis in 0..<3:
values[7 + axis] *= factors[axis]
elif self.mode() == "Pivot":
let transform = Matrix4.compose(Vector3(), Vector3(radians(values[4]), radians(values[5]), radians(values[6])), Vector3(values[7], values[8], values[9]))
let determinant = transform.determinant()
let parameter_delta = Vector3(delta.dot(transform.y.cross(transform.z)), delta.dot(transform.z.cross(transform.x)), delta.dot(transform.x.cross(transform.y))).multiply_scalar(1.0 / determinant)
let correction = delta.subtract(parameter_delta)
for axis in 0..<3:
values[10 + axis] += [parameter_delta.x, parameter_delta.y, parameter_delta.z][axis]
values[1 + axis] += [correction.x, correction.y, correction.z][axis]
else:
for axis in 0..<3:
values[1 + axis] += [delta.x, delta.y, delta.z][axis]
self.w.preview_parameters(values)
## The rotation dragged so far about the handle's axis, in degrees (15°
## steps with Shift), as X, Y and Z.
func turned(self, event: Event, width: float, height: float) -> list[float]!:
let p = gm.project(self.w, self.center, width, height)
let angle = math.atan2(event.y - p[1], event.x - p[0])
var change = angle - self.start_angle
while change > math.pi:
change -= math.pi * 2.0
while change < -math.pi:
change += math.pi * 2.0
self.rotation += change
self.start_angle = angle
let result = [0.0, 0.0, 0.0]
if self.handle < 3:
let direction = self.w.view.camera.projection(width / height).backward.dot(gm.axis(self.handle))
result[self.handle] = stepped(degrees(self.rotation) * (-1.0 if direction >= 0.0 else 1.0), 15.0, event.shift)
return result
## The scale dragged so far along each axis (the handle's axis, a
## plane's two, or all from the center), in 0.1 steps with Shift.
func scaled(self, event: Event, delta: Vector3) -> list[float]!:
let components = [delta.dot(self.axis(0)), delta.dot(self.axis(1)), delta.dot(self.axis(2))]
let result = [1.0, 1.0, 1.0]
for axis in 0..<3:
if self.handle == axis or self.handle == 6 or (self.handle >= 3 and self.handle < 6 and axis != self.handle - 3):
let amount = (event.x - self.start_x - event.y + self.start_y) / 100.0 if self.handle == 6 else components[axis] / self.length
result[axis] = math.max(0.001, stepped(1.0 + amount, 0.1, event.shift))
return result
## A move's `delta` with its origin landing on the snapping grid along
## the axes it moves (unless snapping is off, or `free`: Cmd held).
func grid_moved(self, delta: Vector3, free: bool) -> Vector3:
let snaps = self.w.snaps
if free or not snaps.grid():
return delta
let step = grid_step(self.w.view.distance)
let moved = [delta.x, delta.y, delta.z]
let origin = [self.center.x, self.center.y, self.center.z]
for axis in 0..<3:
if moved[axis] != 0.0:
moved[axis] = on_grid(origin[axis] + moved[axis], step) - origin[axis]
let result = Vector3(moved[0], moved[1], moved[2])
self.w.snap_mark = SnapMark(self.center.add(result), "Grid")
return result
## Nodes the gizmo transforms by their own values.
pub func transforming_kinds() -> list[str]:
return node_catalog.transforming_kinds()
## The Box node (luce-geocore's Box verb) and its Position's and Up's value
## slots (Turn follows Up).
let box_kind = "Box"
let box_position = 8
let box_up = 11
## A Box node's frame: its X, Y and Z.
func box_axes(values: list[float]) -> list[Vector3]!:
let up = Vector3(values[box_up], values[box_up + 1], values[box_up + 2])
let turn = values[box_up + 3]
return [Verbs.placement_axis(up, turn, 0), Verbs.placement_axis(up, turn, 1), Verbs.placement_axis(up, turn, 2)]
## Point `local` of a Box node's frame, in the world.
func box_point(values: list[float], local: Vector3) -> Vector3!:
let axes = box_axes(values)
let origin = Vector3(values[box_position], values[box_position + 1], values[box_position + 2])
return origin.add(axes[0].multiply_scalar(local.x)).add(axes[1].multiply_scalar(local.y)).add(axes[2].multiply_scalar(local.z))
## World point `p` in a Box node's frame.
func box_local(values: list[float], p: Vector3) -> Vector3!:
let axes = box_axes(values)
let offset = p.subtract(Vector3(values[box_position], values[box_position + 1], values[box_position + 2]))
return Vector3(offset.dot(axes[0]), offset.dot(axes[1]), offset.dot(axes[2]))