dymokomi/luced-3d
Source of release 0.11.6
0.11.6 / src / feature_handles.luc
## The selected CAD feature's handles in the viewport, Fusion's
## manipulators, where its cook put them (handle_lines.luc, Node.handles):
## Extrude's arrow per side and its taper rings, Revolve's angle rings, the
## arrow of an Offset Face (Press Pull on a face) and of a Fillet's or
## Chamfer's radius, a Shell's thickness arrows, and a Move's arrows and
## rings at its pivot. An arrow is drawn at least least_arrow points long, as
## Fusion's manipulators keep their size on screen. Dragging one sets its
## value: an arrow by how far along its line the pointer's ray passes since
## the press, snapped to the grid's 1, 2 or
## 5 × 10ⁿ for the zoom; a taper ring's angle by how far it is pulled
## sideways (out: positive, Fusion's sign); an angle ring round its axis;
## whole degrees (Cmd or Ctrl drags freely). One undo step each; the worker
## cooks each step as the parameter panel's drags do. A click on a handle
## without dragging opens its value box there (panels.luc), the panel's
## row, the box and the handle being one value. In Edit CAD the same
## handles drag the last step's values (its Extrude, Revolve, Offset Face,
## Fillet, Chamfer or Shell), the step's tool bar and box being that value.
## Alias's Blend Curve handles: at each end the tangent's length and, at G2
## or more, the curvature's lean (edit_cad_curves.blend_handles), on the
## Blend Curve node and on Edit CAD's Blend Curve step.
## A Move node of Free move or Translate shows Fusion's whole manipulator
## instead (move_manipulator.luc), at its pivot moved by the translation.
from luce_ui.ui import Painter, Point, Event
from luce_gpu.gpu import Rect, Color
from luce_window.input import EventKind, Key
from luce_3d.graphics import Vector3
from workspace import Workspace
from node import Node
from handle_lines import HandleLine
from snapping import grid_step, stepped
from angles import radians, degrees
import handle_lines
from move_manipulator import MoveManipulator, first_ring
import node_catalog
import tool_registry
from edit_kinds import is_edit
import view_projection
import theme
from luce_std import math
## How near a handle a press takes it, in points.
let reach = 12.0
## How far a taper ring stands beside its arrow, in points.
let taper_gap = 36.0
## The shortest an arrow is drawn, in points: Fusion's manipulators keep
## their size on screen, so a small radius's arrow stays one to grab.
let least_arrow = 44.0
## What the handles set: the selected feature node's values, or the
## displayed Edit CAD node's last step's numbers (`step`). `kind` names the
## rows (the node's kind, the step's verb).
pub class HandleTarget:
pub let node: Node
pub let kind: str
pub let values: list[float]
pub let step: bool
pub func init(self, node: Node, kind: str, values: list[float], step: bool):
self.node = node
self.kind = kind
self.values = values
self.step = step
## Whether node kind `name` (an Edit CAD step's verb: `step`) has handles
## its values drag: a CAD tool's declaration says (Tool.handles); a move's
## step is dragged by the gizmo instead.
func handled(name: str, step: bool) -> bool:
let tool = (tool_registry.cad_step(name) if step else tool_registry.cad_node(name)) else return false
return tool.handles and not (step and tool.gesture == "move")
pub class FeatureHandles:
let w: Workspace
## The handles as last parsed, and the text they came from.
var parsed: list[HandleLine]
var parsed_from: str = ""
## The handle under the pointer or dragged (an index into parsed), -1
## none.
pub var hovered: int = -1
pub var dragging: bool = false
## Whether the drag moved (a press and release in place is a click).
var moved: bool = false
## Where along its line an arrow was pressed, and its value then: a drag
## moves the value by how far the pointer goes (the arrow may be drawn
## longer than its value).
var pressed_along: float = 0.0
var pressed_value: float = 0.0
var pressed_x: float = 0.0
var pressed_y: float = 0.0
## A handle clicked: its value box is wanted (panels reads and clears).
pub var clicked: int = -1
## A Move node's manipulator and its part hovered or dragged (-1 none).
let manipulator: MoveManipulator
pub var part: int = -1
pub func init(self, w: Workspace):
self.w = w
self.parsed = []
self.manipulator = MoveManipulator(w)
## Where a Move node's manipulator turns (its pivot, as the cook
## reports it: its arrows' foot), when the node moves freely or
## translates; none for its other types and everything else.
func move_pivot(self, target: HandleTarget) -> Vector3?!:
if target.step or target.kind != "Move" or target.values.length <= 16 or int(target.values[16]) > 1:
return none
for h in self.handles(target):
if h.kind == "arrow":
return h.foot
return none
## The manipulator's events (see move_event).
func move_event(self, target: HandleTarget, pivot: Vector3, event: Event, width: float, height: float) -> bool!:
let rings = int(target.values[16]) == 0
if self.dragging:
if event.cancelled or event.kind in [EventKind.focus_lost, EventKind.overflow] or (event.kind == EventKind.key_down and event.key == Key.escape):
self.w.cancel()
self.dragging = false
return true
if event.kind == EventKind.pointer_up:
self.dragging = false
if self.moved:
self.w.commit()
else:
self.w.cancel()
return true
if event.kind == EventKind.pointer_moved:
if math.hypot(event.x - self.pressed_x, event.y - self.pressed_y) < 3.0 and not self.moved:
return true
self.moved = true
self.w.preview_parameters(self.manipulator.dragged(event.x, event.y, width, height, not (event.meta or event.control)))
return true
return false
if event.alt:
return false
if event.kind == EventKind.pointer_moved:
let at = self.manipulator.hit(pivot, target.values, rings, event.x, event.y, width, height)
let changed = at != self.part
self.part = at
return changed
if event.kind == EventKind.pointer_down and event.button == 0 and event.inside:
let at = self.manipulator.hit(pivot, target.values, rings, event.x, event.y, width, height)
if at < 0:
return false
self.part = at
self.w.begin("Move rotate" if at >= first_ring else "Move translate")
self.dragging = true
self.moved = false
self.pressed_x = event.x
self.pressed_y = event.y
self.manipulator.press(at, pivot, target.values, rings, event.x, event.y, width, height)
return true
return false
## What the handles set, when they show: the selected feature (enabled,
## no prompt open), or the edited Edit CAD model's last step.
pub func feature(self) -> HandleTarget?:
let node = self.w.selected() else return none
if not node.enabled or self.w.prompt != none:
return none
if handled(node.kind, false):
return HandleTarget(node, node.kind, node.values, false)
if not is_edit(node.kind) or node.edits.length == 0 or node.id != self.w.network.display or self.w.sketching():
return none
let last = node.edits[-1]
if not handled(last.verb, true):
return none
# Its rows are its tool's node's (Blend Curve between edges has the
# curves' rows).
let tool = tool_registry.cad_step(last.verb) else return none
return HandleTarget(node, tool.label, last.numbers, true)
## Its handles, parsed once per report.
func handles(self, target: HandleTarget) -> list[HandleLine]!:
let node = target.node
if node.handles != self.parsed_from:
self.parsed = handle_lines.parse(node.handles)
self.parsed_from = node.handles
if self.hovered >= self.parsed.length:
self.hovered = -1
return self.parsed
## Whether any handle shows.
pub func active(self) -> bool!:
let node = self.feature() else return false
return self.handles(node).length > 0
## The value handle `h` shows: its slot's; a pair's the mean of its two
## (Align's middle Tangent Scale arrow over the start and end scales).
func value_of(self, node: HandleTarget, h: HandleLine) -> float:
if h.kind == "pair":
return (node.values[h.slot] + node.values[h.reference]) * 0.5
return node.values[h.slot]
## Handle `h`'s grip (the arrow's tip, the ring) in the world.
func grip(self, node: HandleTarget, h: HandleLine, width: float, height: float) -> Vector3!:
let value = node.values[h.slot]
if h.kind == "angle":
let r = h.side.length()
let along = h.side.multiply_scalar(1.0 / math.max(r, 1e-12))
let turn = radians(value * h.scale)
return h.foot.add(along.multiply_scalar(r * math.cos(turn))).add(h.way.cross(along).multiply_scalar(r * math.sin(turn)))
if h.kind == "taper":
let (base, sideways, length, shift) = self.taper_frame(node, h, width, height)
return base.add(sideways.multiply_scalar(shift + length * math.tan(radians(math.max(-80.0, math.min(80.0, value))))))
return h.foot.add(h.way.multiply_scalar(self.arrow_length(self.value_of(node, h) * h.scale, h.foot, width, height)))
## An arrow's length in the world for `length` (its value times its
## scale), at least least_arrow points on screen at `foot`, its sign kept.
func arrow_length(self, length: float, foot: Vector3, width: float, height: float) -> float!:
let least = least_arrow * self.pixel(foot, width, height)
if math.abs(length) >= least:
return length
return -least if length < 0.0 else least
## A point's size in the world at `at` on a viewport `width` by `height`.
func pixel(self, at: Vector3, width: float, height: float) -> float!:
let lens = self.w.view.camera.projection(math.max(1.0, width) / math.max(1.0, height))
return lens.pixel(lens.depth(at), height)
## A taper ring's frame: the arrow's tip it leans from, the way out
## (square to the arrow, toward the screen's right), the arrow's length,
## and the gap that keeps the ring beside the arrow (in world units).
func taper_frame(self, node: HandleTarget, h: HandleLine, width: float, height: float) -> (Vector3, Vector3, float, float)!:
let lens = self.w.view.camera.projection(math.max(1.0, width) / math.max(1.0, height))
let length = math.abs(self.arrow_length(node.values[h.reference] * h.scale, h.foot, width, height))
let base = h.foot.add(h.way.multiply_scalar(length))
var sideways = lens.right.subtract(h.way.multiply_scalar(lens.right.dot(h.way)))
if sideways.length() < 1e-6:
sideways = lens.up.subtract(h.way.multiply_scalar(lens.up.dot(h.way)))
let along = sideways.multiply_scalar(1.0 / math.max(sideways.length(), 1e-12))
let pixel = lens.pixel(lens.depth(base), height)
return (base, along, length, taper_gap * pixel)
## Handle `slot`'s grip on a viewport `width` by `height`, in its points
## (none when no handle of that slot shows); -1: the first handle's.
pub func tip(self, width: float, height: float, slot: int = -1) -> list[float]?!:
let node = self.feature() else return none
for h in self.handles(node):
if slot < 0 or h.slot == slot:
let s = view_projection.project(self.w, self.grip(node, h, width, height), width, height)
return [s[0], s[1]]
return none
## The slot of the handle a click wants a value box for (-1 none); asking
## clears it.
pub func take_click(self) -> int:
let slot = self.clicked
self.clicked = -1
return slot
## The handle near (`x`, `y`), or -1.
func near(self, node: HandleTarget, x: float, y: float, width: float, height: float) -> int!:
let shown = self.handles(node)
var best = -1
var nearest = reach
for at in 0..<shown.length:
let h = shown[at]
if h.kind == "taper" and math.abs(node.values[h.reference]) * h.scale < 1e-9:
continue
let s = view_projection.project(self.w, self.grip(node, h, width, height), width, height)
let gap = math.hypot(x - s[0], y - s[1])
if s[2] >= 0.0 and gap <= nearest:
nearest = gap
best = at
return best
## Pointer events; whether one was used.
pub func event(self, event: Event, width: float, height: float) -> bool!:
let node = self.feature() else return false
if let pivot = self.move_pivot(node):
return self.move_event(node, pivot, event, width, height)
let shown = self.handles(node)
if self.dragging:
if event.cancelled or event.kind in [EventKind.focus_lost, EventKind.overflow] or (event.kind == EventKind.key_down and event.key == Key.escape):
self.w.cancel()
self.dragging = false
return true
if event.kind == EventKind.pointer_up:
self.dragging = false
if self.moved:
self.w.commit()
else:
self.w.cancel()
if self.hovered >= 0 and self.hovered < shown.length:
self.clicked = shown[self.hovered].slot
self.w.revision += 1
return true
if event.kind == EventKind.pointer_moved:
if math.hypot(event.x - self.pressed_x, event.y - self.pressed_y) < 3.0 and not self.moved:
return true
self.moved = true
if self.hovered >= 0 and self.hovered < shown.length:
self.drag_to(node, shown[self.hovered], event.x, event.y, width, height, not (event.meta or event.control))
return true
return false
if event.alt:
return false
if event.kind == EventKind.pointer_moved:
let at = self.near(node, event.x, event.y, width, height)
let changed = at != self.hovered
self.hovered = at
return changed
if event.kind == EventKind.pointer_down and event.button == 0 and event.inside:
let at = self.near(node, event.x, event.y, width, height)
if at < 0:
return false
self.hovered = at
self.w.begin(f"{node.kind} {self.row_name(node.kind, shown[at].slot).lower()}")
self.dragging = true
self.moved = false
let pressed = shown[at]
self.pressed_value = self.value_of(node, pressed)
self.pressed_along = self.along(pressed, event.x, event.y, width, height)
self.pressed_x = event.x
self.pressed_y = event.y
return true
return false
## The name of `kind`'s row on value slot `slot` (a node's or a step's).
pub func row_name(self, kind: str, slot: int) -> str!:
for row in node_catalog.parameters(kind):
if row.slot == slot and row.valued():
return row.name
return "Value"
## Whether the value on `slot` of what the handles set is an angle (its
## handle a ring).
pub func is_angle(self, slot: int) -> bool!:
let node = self.feature() else return false
for h in self.handles(node):
if h.slot == slot:
return h.kind != "arrow" and h.kind != "pair"
return false
## The slot of the first handle (-1 none).
pub func first_slot(self) -> int!:
let node = self.feature() else return -1
let shown = self.handles(node)
return shown[0].slot if shown.length > 0 else -1
## Handle `h` dragged to the pointer at (`x`, `y`).
func drag_to(self, node: HandleTarget, h: HandleLine, x: float, y: float, width: float, height: float, snap: bool) -> unit!:
var value = self.value_of(node, h)
if h.kind == "arrow" or h.kind == "pair":
# Moved by how far along its line the pointer went since the
# press.
let t = self.along(h, x, y, width, height)
# A blend's handles are chord multiples: twentieths.
let step = 0.05 if node.kind in ["Blend Curve", "Align"] else grid_step(self.w.view.distance)
value = stepped(self.pressed_value + (t - self.pressed_along) / h.scale, step, snap)
elif h.kind == "taper":
let (base, sideways, length, shift) = self.taper_frame(node, h, width, height)
let q = view_projection.plane(self.w, x, y, width, height, base, h.way.cross(sideways))
value = degrees(math.atan2(q.subtract(base).dot(sideways) - shift, math.max(length, 1e-12)))
value = stepped(value, 1.0, snap)
else:
let r = h.side.length()
let along = h.side.multiply_scalar(1.0 / math.max(r, 1e-12))
let q = view_projection.plane(self.w, x, y, width, height, h.foot, h.way).subtract(h.foot)
var turn = degrees(math.atan2(q.dot(h.way.cross(along)), q.dot(along))) * h.scale
if turn <= 0.0:
turn += 360.0
value = stepped(turn, 1.0, snap)
let values = node.values.copy()
if h.kind == "pair":
# The middle arrow slides the whole row: both ends' scales move
# by what the mean moved.
let shift = value - self.value_of(node, h)
values[h.slot] = self.clamped(node, h.slot, node.values[h.slot] + shift)
values[h.reference] = self.clamped(node, h.reference, node.values[h.reference] + shift)
else:
values[h.slot] = self.clamped(node, h.slot, value)
if node.step:
self.w.preview_step(values)
else:
self.w.preview_parameters(values)
## Where along arrow `h`'s line (from its foot) the pointer's ray at
## (`x`, `y`) passes nearest it.
func along(self, h: HandleLine, x: float, y: float, width: float, height: float) -> float!:
let lens = self.w.view.camera.projection(math.max(1.0, width) / math.max(1.0, height))
let n = h.way
let r = view_projection.ray(self.w, x, y, width, height)
let gap = h.foot.subtract(lens.ray_origin(x, y, width, height))
let b = n.dot(r)
let c = r.dot(r)
let denominator = c - b * b
if math.abs(denominator) < 1e-9:
return 0.0
# The parameter along the arrow from its foot (n is a unit vector).
return (b * gap.dot(r) - c * n.dot(gap)) / denominator
## The least and most the row on `slot` of `kind` takes.
pub func range(self, kind: str, slot: int) -> (float, float)!:
for row in node_catalog.parameters(kind):
if row.slot == slot and row.valued():
return (row.minimum, row.maximum)
return (-1.0e12, 1.0e12)
## `value` within the range of the row on `slot`.
func clamped(self, node: HandleTarget, slot: int, value: float) -> float!:
for row in node_catalog.parameters(node.kind):
if row.slot == slot and row.valued():
return math.max(row.minimum, math.min(row.maximum, value))
return value
## The handles over the scene, lit when hovered or dragged.
pub func draw(self, p: Painter, width: float, height: float) -> unit!:
let node = self.feature() else return
if let pivot = self.move_pivot(node):
self.manipulator.draw(p, pivot, node.values, int(node.values[16]) == 0, self.part, width, height)
return
let shown = self.handles(node)
for at in 0..<shown.length:
let h = shown[at]
let color = theme.grey(245.0) if at == self.hovered else theme.accent()
if h.kind == "arrow" or h.kind == "pair":
self.draw_arrow(p, node, h, color, width, height)
elif h.kind == "taper":
self.draw_taper(p, node, h, color, width, height)
else:
self.draw_angle(p, node, h, color, width, height)
## A shaft from the foot to the tip, a head, and the tip's handle.
func draw_arrow(self, p: Painter, node: HandleTarget, h: HandleLine, color: Color, width: float, height: float) -> unit!:
let foot = view_projection.project(self.w, h.foot, width, height)
let tip = view_projection.project(self.w, self.grip(node, h, width, height), width, height)
if foot[2] < 0.0 or tip[2] < 0.0:
return
p.line(Point(foot[0], foot[1]), Point(tip[0], tip[1]), 2.5, color)
# The head: a triangle pointing along the shaft as seen.
let dx = tip[0] - foot[0]
let dy = tip[1] - foot[1]
let length = math.max(1e-6, math.hypot(dx, dy))
let ux = dx / length
let uy = dy / length
if length > 4.0:
let back = Point(tip[0] - ux * 14.0, tip[1] - uy * 14.0)
p.triangle(Point(tip[0], tip[1]), Point(back.x - uy * 7.0, back.y + ux * 7.0), Point(back.x + uy * 7.0, back.y - ux * 7.0), color)
p.rounded_rectangle(Rect(tip[0] - 6.0, tip[1] - 6.0, 12.0, 12.0), color, 6.0)
## A thin line from beside the arrow's tip leaning by the angle, and a
## ring at its end.
func draw_taper(self, p: Painter, node: HandleTarget, h: HandleLine, color: Color, width: float, height: float) -> unit!:
let (base, sideways, length, shift) = self.taper_frame(node, h, width, height)
if length < 1e-9:
return
let start = view_projection.project(self.w, h.foot.add(sideways.multiply_scalar(shift)), width, height)
let ring = view_projection.project(self.w, self.grip(node, h, width, height), width, height)
let corner = view_projection.project(self.w, base.add(sideways.multiply_scalar(shift)), width, height)
if start[2] < 0.0 or ring[2] < 0.0:
return
p.line(Point(start[0], start[1]), Point(corner[0], corner[1]), 1.0, theme.grey(150.0))
p.line(Point(start[0], start[1]), Point(ring[0], ring[1]), 1.5, color)
ring_at(p, ring[0], ring[1], color)
## An arc round the axis from the profiles to the ring.
func draw_angle(self, p: Painter, node: HandleTarget, h: HandleLine, color: Color, width: float, height: float) -> unit!:
let r = h.side.length()
let along = h.side.multiply_scalar(1.0 / math.max(r, 1e-12))
let across = h.way.cross(along)
let turn = radians(node.values[h.slot] * h.scale)
let steps = 48
var last = view_projection.project(self.w, h.foot.add(h.side), width, height)
for step in 1..<steps + 1:
let a = turn * float(step) / float(steps)
let next = view_projection.project(self.w, h.foot.add(along.multiply_scalar(r * math.cos(a))).add(across.multiply_scalar(r * math.sin(a))), width, height)
if last[2] >= 0.0 and next[2] >= 0.0:
p.line(Point(last[0], last[1]), Point(next[0], next[1]), 2.0, color)
last = next
if last[2] >= 0.0:
ring_at(p, last[0], last[1], color)
## A ring at (`x`, `y`): a disc of `color` with a dark middle.
func ring_at(p: Painter, x: float, y: float, color: Color) -> unit!:
p.rounded_rectangle(Rect(x - 7.0, y - 7.0, 14.0, 14.0), color, 7.0)
p.rounded_rectangle(Rect(x - 4.0, y - 4.0, 8.0, 8.0), theme.grey(28.0), 4.0)