dymokomi/luced-3d
Source of release 0.11.7
0.11.7 / src / build_labels.luc
## Alias's Boundary Labels for the surface builders (ยง7.3 of the surfacing
## research doc; Plasticity's black circles): while an Edit CAD node's last
## step is Square, Skin or Rail, or such a node is selected and displayed,
## each boundary with a continuity of its own carries a label at its middle
## ("G1", "Implied tangent"...); a click cycles it to the next choice, one
## undo step, the same value as its row in the parameters. The labels come
## from the cook's state lines (build_ops.labels_of), so the worker's
## results serve and the viewport never cooks. While the builder waits for
## more picks, those it has are lit along their length. A Fill's conflicts
## (fill_ops.marks_of: where the boundaries' demands disagree, one mark a
## corner or a boundary's runs) are drawn in the failure color, a run along
## its boundary and a corner as a ring, each mark with one plate kept clear
## of the others, as xNURBS 7 marks them.
from luce_ui.ui import Painter, Rect, Point
from workspace import Workspace
from node import Node
import edit_cad
import tool_picks
import pick_kinds
import build_ops
import blend_ops
import fill_ops
import surface_ops
import view_projection as gm
import theme
from luce_std import math
pub class BuildLabels:
let w: Workspace
## The label under the pointer (-1: none).
pub var hovered: int = -1
pub func init(self, w: Workspace):
self.w = w
## The node whose builder the labels belong to, and whether it is an
## Edit CAD step (else a builder node); none when no builder shows.
func target(self) -> (Node, bool)?:
let node = self.w.selected() else return none
if node.id != self.w.network.display or self.w.sketching():
return none
if node.kind == edit_cad.kind_name:
if node.edits.length == 0 or not self.w.editing() or not (node.edits[-1].verb in build_ops.verbs() or node.edits[-1].verb in blend_ops.verbs()):
return none
return (node, true)
if node.kind in build_ops.verbs() or node.kind in blend_ops.verbs():
return (node, false)
return none
## The labels of the target's cook.
func labels(self) -> list[build_ops.BuildLabel]:
let found = self.target() else return []
let (node, _) = found
return build_ops.labels_of(node.sketch_state)
## Whether labels show (the viewport routes the pointer here).
pub func active(self) -> bool:
return self.labels().length > 0
## A label's plate on screen.
func plate(self, label: build_ops.BuildLabel, width: float, height: float) -> Rect?!:
let s = gm.project(self.w, label.point, width, height)
if s[2] <= 0.0:
return none
# Beside the boundary's middle, up and to the left, clear of the
# Continuity Check's label there.
let w = float(label.text.length) * 7.5 + 14.0
return Rect(s[0] - w - 18.0, s[1] - 44.0, w, 22.0)
## A label's boundary point on screen.
func anchor(self, label: build_ops.BuildLabel, width: float, height: float) -> list[float]?!:
let s = gm.project(self.w, label.point, width, height)
if s[2] <= 0.0:
return none
return s
## The pointer at (`x`, `y`): the label under it; whether that changed.
pub func hover(self, x: float, y: float, width: float, height: float) -> bool!:
let was = self.hovered
self.hovered = -1
let shown = self.labels()
for k in 0..<shown.length:
if let box = self.plate(shown[k], width, height):
if x >= box.x and x <= box.x + box.width and y >= box.y and y <= box.y + box.height:
self.hovered = k
# Its dot on the boundary takes the click too.
if let at = self.anchor(shown[k], width, height):
if math.hypot(x - at[0], y - at[1]) <= 8.0:
self.hovered = k
return self.hovered != was
## A click on a label: its value to the next choice (one undo).
pub func click(self, x: float, y: float, width: float, height: float) -> bool!:
_ = self.hover(x, y, width, height)
if self.hovered < 0:
return false
let found = self.target() else return false
let (node, step) = found
let label = self.labels()[self.hovered]
let values = node.edits[-1].numbers.copy() if step else node.values.copy()
let verb = node.edits[-1].verb if step else node.kind
let start = blend_ops.starting(verb) if verb in blend_ops.verbs() else build_ops.starting(verb)
while values.length < start.length:
values.append(start[values.length])
if label.slot < 0 or label.slot >= values.length:
return false
values[label.slot] = float((int(values[label.slot]) + 1) % (label.choices if label.choices > 1 else 1))
self.w.perform("Cycle continuity", func () -> unit!:
if step:
node.edits[-1] = node.edits[-1].with_numbers(values)
else:
for k in 0..<values.length:
if k < node.values.length:
node.values[k] = values[k]
self.w.network.invalidate(node.id)
self.w.rebuild())
return true
## Whether the target waits for picks (a step's or a node's prompt).
func waiting(self) -> bool:
let kind = tool_picks.waiting_kind(self.w) catch failure:
return false
return kind == pick_kinds.words
## The picks lit, each its curve along its length (the accent, wide).
func draw_picks(self, painter: Painter, width: float, height: float) -> unit!:
if not self.waiting():
return
let found = self.target() else return
let (node, _) = found
for line in node.sketch_state.split("\n"):
if not line.starts_with(build_ops.pick_head):
continue
var previous: list[float]? = none
for p in surface_ops.points_of(line, build_ops.pick_head):
let s = gm.project(self.w, p, width, height)
if s[2] <= 0.0:
previous = none
continue
if let before = previous:
painter.line(Point(before[0], before[1]), Point(s[0], s[1]), 3.0, theme.accent())
previous = s
## A Fill's conflicts gathered into marks (fill_ops.marks_of: one a
## corner, one a boundary's runs of an order): each run a thick red
## line, a corner a ring, and one plate a mark with its worst amount,
## placed at the first spot round its anchor clear of the boundary
## labels and the plates placed before it (else the least crowded), a
## leader back to the anchor.
func draw_conflicts(self, painter: Painter, width: float, height: float) -> unit!:
let found = self.target() else return
let (node, _) = found
let taken: list[Rect] = []
for label in self.labels():
if let box = self.plate(label, width, height):
taken.append(box)
for m in fill_ops.marks_of(fill_ops.conflicts_of(node.sketch_state)):
for (start, finish) in m.runs:
let a = gm.project(self.w, start, width, height)
let b = gm.project(self.w, finish, width, height)
if a[2] <= 0.0 or b[2] <= 0.0:
continue
if math.hypot(a[0] - b[0], a[1] - b[1]) < 2.0:
painter.rounded_rectangle(Rect(a[0] - 5.0, a[1] - 5.0, 10.0, 10.0), theme.failure(), 5.0)
else:
painter.line(Point(a[0], a[1]), Point(b[0], b[1]), 4.0, theme.failure())
let at = gm.project(self.w, m.anchor, width, height)
if at[2] <= 0.0:
continue
if m.corner:
painter.rounded_rectangle(Rect(at[0] - 7.0, at[1] - 7.0, 14.0, 14.0), theme.failure(), 7.0)
painter.rounded_rectangle(Rect(at[0] - 4.0, at[1] - 4.0, 8.0, 8.0), theme.grey(24.0), 4.0)
let text = m.text()
let box = clear_spot(at[0], at[1], float(text.length) * 7.5 + 14.0, 22.0, taken)
taken.append(box)
let near_x = box.x if box.x > at[0] else box.x + box.width
let near_y = box.y if box.y > at[1] else box.y + box.height
painter.line(Point(at[0], at[1]), Point(near_x, near_y), 1.0, theme.failure())
painter.rounded_rectangle(Rect(box.x - 1.0, box.y - 1.0, box.width + 2.0, box.height + 2.0), theme.failure(), 4.0)
painter.rounded_rectangle(box, theme.grey(24.0), 3.0)
painter.text(text, box.x + 7.0, box.y + 3.0, theme.text())
## The labels: a dark plate with the continuity, the hovered one lit.
pub func draw(self, painter: Painter, width: float, height: float) -> unit!:
self.draw_picks(painter, width, height)
self.draw_conflicts(painter, width, height)
let shown = self.labels()
for k in 0..<shown.length:
let box = self.plate(shown[k], width, height) else continue
let lit = k == self.hovered
if let at = self.anchor(shown[k], width, height):
painter.line(Point(at[0], at[1]), Point(box.x + box.width, box.y + box.height), 1.0, theme.accent() if lit else theme.grey(110.0))
painter.rounded_rectangle(Rect(at[0] - 3.0, at[1] - 3.0, 6.0, 6.0), theme.accent() if lit else theme.grey(200.0), 3.0)
painter.rounded_rectangle(Rect(box.x - 1.0, box.y - 1.0, box.width + 2.0, box.height + 2.0), theme.accent() if lit else theme.grey(70.0), 4.0)
painter.rounded_rectangle(box, theme.grey(24.0), 3.0)
painter.text(shown[k].text, box.x + 7.0, box.y + 3.0, theme.accent() if lit else theme.text())
## A `w` by `h` plate near (`x`, `y`): the first of the spots round it
## (right and left, below and above, stepping out) that overlaps none of
## `taken`, else the one overlapping least.
pub func clear_spot(x: float, y: float, w: float, h: float, taken: list[Rect]) -> Rect:
var best = Rect(x + 14.0, y + 10.0, w, h)
var best_overlap = -1.0
for ring in 0..<4:
let out = float(ring) * (h + 8.0)
for (dx, dy) in [(14.0, 10.0 + out), (-w - 14.0, 10.0 + out), (14.0, -h - 10.0 - out), (-w - 14.0, -h - 10.0 - out)]:
let spot = Rect(x + dx, y + dy, w, h)
var overlap = 0.0
for other in taken:
let ox = math.min(spot.x + spot.width, other.x + other.width) - math.max(spot.x, other.x)
let oy = math.min(spot.y + spot.height, other.y + other.height) - math.max(spot.y, other.y)
if ox > -4.0 and oy > -4.0:
overlap += (ox + 4.0) * (oy + 4.0)
if overlap == 0.0:
return spot
if best_overlap < 0.0 or overlap < best_overlap:
best = spot
best_overlap = overlap
return best