TableTower/ui/pipe_geometry.gd
RobinFiredrake 7ea2bbf3a8 Speed: lazy format menus, one-call dashed threads, kept inspector sections, queued card rebuilds
Profiling showed the delays come from Godot's own calls, not GDScript:
- Right-click format menus are made on the first right-click, not for
  every text field at build time.
- Dashed reference threads draw all their dashes in one call.
- Inspector rebuilds keep the closed sections and only remake open ones.
- Card rows are styled before they join the card.
- A Play/Build switch rebuilds cards in view at once and the rest a few
  per frame (CardRefreshQueue); threads redraw only now and then meanwhile.

Adds tests/perf_actions.gd, a timing tool for everyday actions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 16:20:31 -06:00

84 lines
2.8 KiB
GDScript

class_name PipeGeometry
extends RefCounted
## The shape of a thread (pipe): the curve between two cards, a point part way
## along it, its length, and how to cut it into dashes. Only maths; it draws
## nothing. PipeLayer and PipeBatch use it.
## How many straight pieces make up one curve.
const SAMPLES := 40
## A smooth S-curve from `start` to `end`, as SAMPLES + 1 points. `down`:
## it leaves `start` downwards (a card thread, off a card's bottom edge).
static func curve(start: Vector2, end: Vector2, down: bool = false) -> PackedVector2Array:
var dx := maxf(70.0, absf(end.x - start.x) / 2.0)
var control1 := start + (Vector2(0, maxf(70.0, absf(end.y - start.y) / 2.0)) if down else Vector2(dx, 0))
var control2 := end - Vector2(dx, 0)
var pts := PackedVector2Array()
for i in SAMPLES + 1:
pts.append(start.bezier_interpolate(control1, control2, end, float(i) / SAMPLES))
return pts
## The length of a polyline.
static func length(pts: PackedVector2Array) -> float:
var total := 0.0
for i in range(1, pts.size()):
total += pts[i - 1].distance_to(pts[i])
return total
## The point a fraction `fraction` (0 to 1) of the way along a polyline.
static func point_along(pts: PackedVector2Array, fraction: float) -> Vector2:
if pts.size() < 2:
return pts[0] if pts.size() == 1 else Vector2.ZERO
var want := clampf(fraction, 0.0, 1.0) * length(pts)
for i in range(1, pts.size()):
var seg := pts[i - 1].distance_to(pts[i])
if want <= seg and seg > 0.0:
return pts[i - 1].lerp(pts[i], want / seg)
want -= seg
return pts[pts.size() - 1]
## The dashes of a polyline as point pairs, so all of them can be drawn in one
## call (draw_multiline): a call per dash made long dashed threads very slow.
static func dash_segments(pts: PackedVector2Array, on: float, off: float) -> PackedVector2Array:
var segments := PackedVector2Array()
for dash in dashes(pts, on, off):
for i in range(1, dash.size()):
segments.append(dash[i - 1])
segments.append(dash[i])
return segments
## Splits a polyline into dashes `on` long with gaps `off` long.
static func dashes(pts: PackedVector2Array, on: float, off: float) -> Array[PackedVector2Array]:
var out: Array[PackedVector2Array] = []
var current := PackedVector2Array()
var drawing := true
var left := on
for i in range(1, pts.size()):
var from := pts[i - 1]
var to := pts[i]
var seg := from.distance_to(to)
var pos := 0.0
if drawing and current.is_empty():
current.append(from)
while seg - pos > left:
pos += left
var cut := from.lerp(to, pos / seg)
if drawing:
current.append(cut)
out.append(current)
current = PackedVector2Array()
else:
current = PackedVector2Array([cut])
drawing = not drawing
left = on if drawing else off
left -= seg - pos
if drawing:
current.append(to)
if drawing and current.size() > 1:
out.append(current)
return out