- Card threads: a port on each card's bottom edge. Plain runs after any result (once the result's own threads finish); Otherwise runs only when no thread of the result ran. Saved as card_threads (LOGIC.md 3g). - Typing on long cards no longer rebuilds the inspector or the card per key (~175 ms -> ~7 ms). Timing tool: tests/perf_typing.gd. - "+ Add result" scrolls to the new row and focuses it. - Mouse wheel zooms; Shift+wheel pans sideways; scrolling lists keep it. - "SPREAD" caption over the switcher; Morgan's first-run welcome. - docs/MECHANICS.md: checklist of table mechanics for planning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
210 lines
6.7 KiB
GDScript
210 lines
6.7 KiB
GDScript
class_name PortLayer
|
|
extends Control
|
|
## Draws pipe ends on top of the cards: an inlet on each card, an outlet on
|
|
## each result and one on each card's bottom edge (card threads). In Build
|
|
## mode, dragging from an outlet lays a new pipe.
|
|
## Only the ports themselves take mouse input; everything else passes through.
|
|
|
|
const GRAB_RADIUS := 10.0
|
|
|
|
var main: Main
|
|
var _grab: Array = [] # [{pos, tid, eid}] outlets that can start a pipe
|
|
var _linking := false
|
|
var _ghost_from := Vector2.ZERO
|
|
var _ghost_to := Vector2.ZERO
|
|
var _ghost_down := false
|
|
var _drawn_rev := -1
|
|
var _drawn_hover := -1
|
|
var _drawn_mode := -1
|
|
var _drawn_far := false
|
|
## [stack index, rect] of each visible card, for hiding ports under other cards.
|
|
var _stack: Array = []
|
|
|
|
|
|
func _init() -> void:
|
|
mouse_filter = MOUSE_FILTER_STOP
|
|
mouse_default_cursor_shape = CURSOR_CROSS
|
|
# Controls only draw inside their own rect, so cover the whole world.
|
|
size = Vector2(BoardView.WORLD_SIZE, BoardView.WORLD_SIZE)
|
|
|
|
|
|
## Redraws only when the board's geometry changed (BoardView.geometry_rev),
|
|
## the outlet under the mouse changed (Build), or while a pipe is being laid.
|
|
func _process(_delta: float) -> void:
|
|
if main == null or main.board == null:
|
|
return
|
|
var hover := _hovered_grab()
|
|
var far := main.board.cards.far
|
|
if _linking or main.board.geometry_rev != _drawn_rev or hover != _drawn_hover or main.mode != _drawn_mode or far != _drawn_far:
|
|
_drawn_far = far
|
|
_drawn_rev = main.board.geometry_rev
|
|
_drawn_hover = hover
|
|
_drawn_mode = main.mode
|
|
queue_redraw()
|
|
|
|
|
|
## Index in _grab of the outlet under the mouse, or -1 (always -1 in Play).
|
|
func _hovered_grab() -> int:
|
|
if main.mode != Main.Mode.BUILD or _grab.is_empty():
|
|
return -1
|
|
var mouse := get_local_mouse_position()
|
|
for i in _grab.size():
|
|
if mouse.distance_to(_grab[i]["pos"]) <= GRAB_RADIUS:
|
|
return i
|
|
return -1
|
|
|
|
|
|
func _has_point(p: Vector2) -> bool:
|
|
if _linking:
|
|
return true
|
|
if main == null or main.mode != Main.Mode.BUILD:
|
|
return false
|
|
for g in _grab:
|
|
if p.distance_to(g["pos"]) <= GRAB_RADIUS:
|
|
return true
|
|
return false
|
|
|
|
|
|
func _draw() -> void:
|
|
_grab.clear()
|
|
# Zoomed far out (BoardView.FAR_ZOOM) the ports would be specks.
|
|
if main.board.cards.far and not _linking:
|
|
return
|
|
var build := main.mode == Main.Mode.BUILD
|
|
var mouse := get_local_mouse_position()
|
|
_note_stack()
|
|
for t in main.data.tables:
|
|
var card := main.card_for(t.id)
|
|
if card == null or not card.is_inside_tree() or not card.visible:
|
|
continue
|
|
var below := card.get_index()
|
|
# Variable cards are never rolled, so nothing pipes into them.
|
|
if t.kind != RollTable.KIND_VARIABLE and not _covered(card.inlet_point(), below):
|
|
_port(card.inlet_point(), Style.THREAD, Style.THREAD_DARK, 1.0)
|
|
for e in t.entries:
|
|
if not card.rows.has(e.id):
|
|
continue
|
|
var a := card.anchor_for(e.id)
|
|
if a.is_empty() or not a["visible"]:
|
|
continue
|
|
var on := not e.pipes.is_empty() or not main.ref_targets_for(e).is_empty()
|
|
if not build and not on:
|
|
continue
|
|
var pos: Vector2 = a["pos"]
|
|
if _covered(pos, below):
|
|
continue
|
|
var fill := Style.THREAD if on else Style.CARD
|
|
var ring := Style.THREAD_DARK
|
|
var s := 1.0
|
|
if on and main.is_entry_lit(t.id, e.id):
|
|
fill = Style.ACCENT
|
|
ring = Style.ACCENT_DARK
|
|
if build and pos.distance_to(mouse) <= GRAB_RADIUS and not _linking:
|
|
fill = Style.ACCENT
|
|
s = 1.2
|
|
_port(pos, fill, ring, s)
|
|
if build:
|
|
_grab.append({"pos": pos, "tid": t.id, "eid": e.id})
|
|
if t.kind != RollTable.KIND_VARIABLE:
|
|
_outlet_port(t, card, build, mouse, below)
|
|
if card.more_row != null:
|
|
var m := card.anchor_for("")
|
|
var on_hidden := card.hidden_has_pipes()
|
|
if not m.is_empty() and (on_hidden or build) and not _covered(m["pos"], below):
|
|
_port(m["pos"], Style.THREAD if on_hidden else Style.CARD, Style.THREAD_DARK, 1.0)
|
|
# Collapsed groups: an inlet, and an outlet wherever member pipes leave.
|
|
for g in main.data.groups:
|
|
if g.collapsed:
|
|
var gn := main.group_node(g.id)
|
|
if gn and gn.is_inside_tree():
|
|
_port(gn.inlet_point(), Style.THREAD, Style.THREAD_DARK, 1.0)
|
|
var gp: Dictionary = main.board.pipes.group_ports
|
|
for p: Vector2i in gp:
|
|
if gp[p]:
|
|
_port(Vector2(p), Style.ACCENT, Style.ACCENT_DARK, 1.0)
|
|
else:
|
|
_port(Vector2(p), Style.THREAD, Style.THREAD_DARK, 1.0)
|
|
if _linking:
|
|
_draw_ghost(PipeLayer.curve(_ghost_from, _ghost_to, _ghost_down))
|
|
|
|
|
|
## The port on a card's bottom edge for card threads (CardThreads): in Build
|
|
## always (drag from it to add one), in Play only when the card has some.
|
|
func _outlet_port(t: RollTable, card: TableCard, build: bool, mouse: Vector2, below: int) -> void:
|
|
var on := not t.outlet.pipes.is_empty()
|
|
var pos: Vector2 = card.anchor_for(RollTable.OUTLET_ID)["pos"]
|
|
if not (build or on) or _covered(pos - Vector2(0, 2), below):
|
|
return
|
|
var fill := Style.THREAD if on else Style.CARD
|
|
var ring := Style.THREAD_DARK
|
|
var s := 1.0
|
|
for to in t.outlet.pipes:
|
|
if main.lit_pipes.has(Roller.pipe_key(t.id, RollTable.OUTLET_ID, to)):
|
|
fill = Style.ACCENT
|
|
ring = Style.ACCENT_DARK
|
|
if build and pos.distance_to(mouse) <= GRAB_RADIUS and not _linking:
|
|
fill = Style.ACCENT
|
|
s = 1.2
|
|
_port(pos, fill, ring, s)
|
|
if build:
|
|
_grab.append({"pos": pos, "tid": t.id, "eid": RollTable.OUTLET_ID})
|
|
|
|
|
|
## Remembers where each visible card sits and how high it is stacked.
|
|
func _note_stack() -> void:
|
|
_stack.clear()
|
|
for c in main.board.cards.get_children():
|
|
var tc := c as TableCard
|
|
if tc and tc.visible:
|
|
_stack.append([tc.get_index(), Rect2(tc.position, tc.size)])
|
|
|
|
|
|
## True when a card stacked above index `below` covers the point.
|
|
func _covered(pos: Vector2, below: int) -> bool:
|
|
for s: Array in _stack:
|
|
if int(s[0]) > below and (s[1] as Rect2).has_point(pos):
|
|
return true
|
|
return false
|
|
|
|
|
|
func _port(pos: Vector2, fill: Color, ring: Color, s: float) -> void:
|
|
draw_circle(pos, 8.0 * s, ring)
|
|
draw_circle(pos, 5.0 * s, fill)
|
|
|
|
|
|
func _draw_ghost(pts: PackedVector2Array) -> void:
|
|
var carry := 0.0
|
|
for i in range(1, pts.size()):
|
|
var a := pts[i - 1]
|
|
var b := pts[i]
|
|
var seg := a.distance_to(b)
|
|
var d := carry
|
|
while d < seg:
|
|
draw_circle(a.lerp(b, d / seg), 2.5, Style.ACCENT)
|
|
d += 11.0
|
|
carry = d - seg
|
|
|
|
|
|
func _gui_input(ev: InputEvent) -> void:
|
|
var mb := ev as InputEventMouseButton
|
|
if mb and mb.button_index == MOUSE_BUTTON_LEFT:
|
|
if mb.pressed:
|
|
for g in _grab:
|
|
if mb.position.distance_to(g["pos"]) <= GRAB_RADIUS:
|
|
_linking = true
|
|
_ghost_from = g["pos"]
|
|
_ghost_down = g["eid"] == RollTable.OUTLET_ID
|
|
_ghost_to = mb.position
|
|
main.start_link(g["tid"], g["eid"])
|
|
accept_event()
|
|
return
|
|
elif _linking:
|
|
_linking = false
|
|
main.finish_link()
|
|
accept_event()
|
|
return
|
|
var mm := ev as InputEventMouseMotion
|
|
if mm and _linking:
|
|
_ghost_to = mm.position
|
|
main.update_link()
|
|
accept_event()
|