TableTower/ui/inspector_thread.gd
RobinFiredrake 1735ed09fd Card threads, Morgan's welcome, and typing/scroll/wheel papercuts
- 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>
2026-11-16 10:41:13 -06:00

310 lines
12 KiB
GDScript

class_name InspectorThread
extends RefCounted
## The Inspector's "Selected thread" section and the pipe edit rules behind it.
## Inspector keeps the public set_pipe_* functions and delegates here.
static func set_when(insp: Inspector, to: String, s: String) -> void:
var e := insp.selected_entry()
if e == null or PipePopover.when_error(s) != "" or s.strip_edges() == e.when_for(to):
return
e.set_pipe_when(to, s)
insp.wrote(insp.selected_table())
refresh_otherwise(insp, to)
static func set_otherwise(insp: Inspector, to: String, on: bool) -> void:
var e := insp.selected_entry()
if e == null or on == e.is_otherwise(to):
return
e.set_pipe_otherwise(to, on)
insp.wrote(insp.selected_table())
refresh_otherwise(insp, to)
## Sets one input value passed down the pipe ("" uses the card's default).
static func set_input(insp: Inspector, to: String, input_name: String, expr: String) -> void:
var e := insp.selected_entry()
if e == null:
return
var w := e.with_for(to)
var s := expr.strip_edges()
if s != "" and InspectorKit.expr_error(s) != "":
return
if s == "":
w.erase(input_name)
else:
w[input_name] = s
if w == e.with_for(to):
return
e.set_pipe_with(to, w)
insp.wrote(insp.selected_table())
static func set_count(insp: Inspector, to: String, s: String) -> void:
var e := insp.selected_entry()
if e == null or PipePopover._count_error(s) != "":
return
var before := e.count_text(to)
e.set_pipe_count(to, s.strip_edges() if s.strip_edges() != "" else "1")
if e.count_text(to) != before:
insp.wrote(insp.selected_table())
static func set_pause(insp: Inspector, to: String, on: bool) -> void:
var e := insp.selected_entry()
if e == null or e.pipe_pause.has(to) == on:
return
if on:
e.pipe_pause[to] = true
else:
e.pipe_pause.erase(to)
insp.wrote(insp.selected_table())
static func set_distinct(insp: Inspector, to: String, on: bool) -> void:
var e := insp.selected_entry()
if e == null or e.pipe_distinct.has(to) == on:
return
if on:
e.pipe_distinct[to] = true
else:
e.pipe_distinct.erase(to)
insp.wrote(insp.selected_table())
static func set_combine(insp: Inspector, to: String, mode: String) -> void:
var e := insp.selected_entry()
if e == null or e.combine_for(to) == mode:
return
if mode == "all":
e.pipe_combine.erase(to)
else:
e.pipe_combine[to] = mode
insp.wrote(insp.selected_table())
static func set_save(insp: Inspector, to: String, s: String) -> void:
var e := insp.selected_entry()
if e == null or Inspector._save_error(s) != "" or s.strip_edges() == e.save_for(to):
return
e.set_pipe_save(to, s)
insp.wrote(insp.selected_table())
static func set_color(insp: Inspector, hex: String, to: String) -> void:
var e := insp.selected_entry()
if e == null:
return
if hex == "":
e.pipe_color.erase(to)
else:
e.pipe_color[to] = hex
insp.wrote(insp.selected_table())
insp.main.board.geometry_rev += 1
static func section(insp: Inspector, t: RollTable, e: TableEntry, to: String) -> void:
if not e.pipes.has(to):
InspectorResult.section(insp, t, e)
return
var target := insp.main.data.get_table(to)
var card := e == t.outlet
insp._content.add_child(insp._head("Selected card thread" if card else "Selected thread", "%s → %s" % [source_title(t, e), Inspector._title_of(target.name, to) if target else to]))
var back := InspectorKit.link("Edit the whole card" if card else "Edit the whole result", "Show the card" if card else "Show the result this thread leaves from")
back.add_theme_color_override("font_color", Style.MUTED)
back.pressed.connect(func() -> void: insp.select({"kind": "card", "tid": t.id} if card else {"kind": "result", "tid": t.id, "eid": e.id}))
insp._content.add_child(back)
insp._content.add_child(box(insp, t, e, to))
insp._content.add_child(InspectorKit.field("Thread colour", ColorChoice.new(str(e.pipe_color.get(to, "")), func(h: String) -> void: set_color(insp, h, to))))
lint_lines(insp, t, e)
## Problems and hints the board check finds on this result's pipes.
static func lint_lines(insp: Inspector, t: RollTable, e: TableEntry) -> void:
var box := InspectorKit.vbox(3)
var fill := func() -> void:
for c in box.get_children():
box.remove_child(c)
c.queue_free()
for it: Dictionary in Roller.lint_pipes(insp.main.data):
if str(it["table_id"]) == t.id and str(it["entry_id"]) == e.id:
var hint := str(it.get("level", "error")) == "hint" or str(it["message"]).contains("otherwise")
box.add_child(InspectorKit.note(("Note: " if hint else "Problem: ") + str(it["message"]) + ".",
Style.MUTED if hint else Style.DANGER_INK, 13))
box.visible = box.get_child_count() > 0
fill.call()
insp._live.append(fill)
insp._content.add_child(box)
static func box(insp: Inspector, t: RollTable, e: TableEntry, to: String) -> Control:
var target := insp.main.data.get_table(to)
var parts := {"inputs": {}}
var taken := insp.main.lit_pipes.has(Roller.pipe_key(t.id, e.id, to))
var panel := PanelContainer.new()
var sb := Style.bordered(Style.box(Style.VALUE_BG if taken else Style.CARD_SOFT, 8, [10, 9, 10, 10]), 1, Style.ACCENT if taken else Style.RULE)
panel.add_theme_stylebox_override("panel", sb)
var v := InspectorKit.vbox(8)
panel.add_child(v)
parts["box"] = panel
parts["taken"] = taken
var head := InspectorKit.hbox(8)
head.add_child(InspectorKit.word("→", Style.TRIM, true))
var tn := Style.make_label(Inspector._title_of(target.name, to) if target else to + " (missing)", Style.display(), 19, Style.INK)
tn.size_flags_horizontal = Control.SIZE_EXPAND_FILL
tn.clip_text = true
head.add_child(tn)
if taken:
head.add_child(_chip("taken last roll", Style.ACCENT_DARK, Style.LIT))
elif insp.main.is_entry_lit(t.id, e.id):
head.add_child(Style.make_label("not taken", Style.body_bold(), 12, Style.MUTED))
v.add_child(head)
# Inputs the target declares (and any extra names the pipe passes).
var w := e.with_for(to)
var names: Array = target.input_names() if target else []
for nm: String in w:
if not names.has(nm):
names.append(nm)
for nm: String in names:
var decl: Dictionary = target.find_input(nm) if target else {}
var row := InspectorKit.hbox(6)
var lab := str(decl.get("label", "")) if not decl.is_empty() else ""
var cap := InspectorKit.word(lab if lab != "" else RollContext.pretty_name(nm))
cap.custom_minimum_size = Vector2(64, 0)
cap.tooltip_text = "Input %s of %s" % [nm, target.name if target else to]
row.add_child(cap)
var def := str(decl.get("default", ""))
var le := InspectorKit.amount(str(w.get(nm, "")), def if def != "" else "0")
le.tooltip_text = "An expression passed as %s. Empty uses the card's default%s." % [nm, (" (" + def + ")") if def != "" else ""]
InspectorKit.tag(le, "pipe.%s.in.%s" % [to, nm])
var err := InspectorKit.error_label()
var nm_c := nm
le.text_changed.connect(func(s: String) -> void:
var msg := InspectorKit.expr_error(s, "This value")
InspectorKit.show_error(err, msg)
InspectorKit.mark(le, msg == "")
set_input(insp, to, nm_c, s)
)
row.add_child(le)
if decl.is_empty():
row.add_child(InspectorKit.word("(not an input of this card)", Style.MUTED))
(parts["inputs"] as Dictionary)[nm] = le
v.add_child(row)
v.add_child(err)
# Only if / Otherwise.
var cond := ConditionBuilder.new()
cond.setup(insp.main, e.when_for(to), not insp.main.rules_plain(), "pipe.%s.when" % to,
{"add_label": "+ Only if…" if e.when_for(to) == "" else "+ Add condition"})
cond.changed.connect(func(s: String) -> void: set_when(insp, to, s))
parts["when"] = cond
var when_box := InspectorKit.vbox(4)
when_box.add_child(InspectorKit.flabel("Only if"))
when_box.add_child(cond)
v.add_child(when_box)
var other := InspectorKit.check_box("Otherwise", e.is_otherwise(to))
other.tooltip_text = otherwise_tip(t, e)
other.toggled.connect(func(on: bool) -> void: set_otherwise(insp, to, on))
parts["otherwise"] = other
v.add_child(other)
var pause := InspectorKit.check_box("Wait for me first", e.pipe_pause.has(to))
pause.tooltip_text = "The roll stops here with Continue ▸ in the tray, so you can reveal the next part when you're ready"
pause.toggled.connect(func(on: bool) -> void: set_pause(insp, to, on))
parts["pause"] = pause
v.add_child(pause)
# Count and saves as.
var cr := InspectorKit.flow()
cr.add_child(InspectorKit.word("Rolls"))
var count := InspectorKit.amount(e.count_text(to), "1")
count.tooltip_text = "A number, or an expression such as @terms, answer or 1d3 (worked out when the thread runs)"
InspectorKit.tag(count, "pipe.%s.count" % to)
var cerr := InspectorKit.error_label()
count.text_changed.connect(func(s: String) -> void:
var msg := PipePopover._count_error(s)
InspectorKit.show_error(cerr, msg)
InspectorKit.mark(count, msg == "")
set_count(insp, to, s)
)
cr.add_child(count)
cr.add_child(InspectorKit.word("times, then"))
var then := OptionButton.new()
ModalCard.style_popup(then.get_popup())
for i in RollCombine.LABELS.size():
then.add_item(RollCombine.LABELS[i], i)
then.select(maxi(0, RollCombine.MODES.find(e.combine_for(to))))
then.tooltip_text = "What to do with the rolls when there's more than one (rerolls, best of two, totals…)"
then.item_selected.connect(func(i: int) -> void: set_combine(insp, to, RollCombine.MODES[i]))
cr.add_child(then)
parts["combine"] = then
var distinct := InspectorKit.check_box("No repeats", e.pipe_distinct.has(to))
distinct.tooltip_text = "When it rolls more than once, a result that already came up is rolled again, so every roll is different"
InspectorKit.tag(distinct, "pipe.%s.distinct" % to)
distinct.toggled.connect(func(on: bool) -> void: set_distinct(insp, to, on))
parts["distinct"] = distinct
cr.add_child(distinct)
var sr := InspectorKit.hbox(6)
sr.add_child(InspectorKit.word("Saves as"))
var save := InspectorKit.line(e.save_for(to), "nothing (or a name)")
save.tooltip_text = "Keep the result as a value: a name lasts one roll, @name is kept between rolls"
InspectorKit.tag(save, "pipe.%s.save" % to)
save.text_changed.connect(func(s: String) -> void:
var msg := Inspector._save_error(s)
InspectorKit.show_error(cerr, msg)
InspectorKit.mark(save, msg == "")
set_save(insp, to, s)
)
sr.add_child(save)
parts["count"] = count
parts["save"] = save
v.add_child(cr)
v.add_child(sr)
v.add_child(cerr)
InspectorThreadRolls.rows(insp, e, to, target, v, parts)
var rm := InspectorKit.link("Delete thread", "Delete this thread")
rm.add_theme_color_override("font_color", Style.DANGER_INK)
rm.add_theme_color_override("font_hover_color", Style.DANGER_INK)
rm.pressed.connect(func() -> void: insp.remove_pipe(to))
v.add_child(rm)
insp.pipe_parts[to] = parts
refresh_otherwise(insp, to)
return panel
## What a thread leaves from: the result's text, or "<card> (card thread)".
static func source_title(t: RollTable, e: TableEntry) -> String:
if t != null and e == t.outlet:
return "%s (card thread)" % Inspector._title_of(t.name, "(no name)")
return Inspector._title_of(e.text, "(no text)")
static func otherwise_tip(t: RollTable, e: TableEntry) -> String:
if t != null and e == t.outlet:
return "Runs only when no thread of the card's result ran (and no \"Only if\" card thread was true). Off: runs after every result"
return "Runs only when none of this result's \"Only if\" threads is true"
## Otherwise is off and disabled while the pipe has an Only if.
static func refresh_otherwise(insp: Inspector, to: String) -> void:
var parts: Dictionary = insp.pipe_parts.get(to, {})
var e := insp.selected_entry()
if parts.is_empty() or e == null:
return
var cb := parts["otherwise"] as CheckBox
var has_when := e.when_for(to) != ""
cb.disabled = has_when
cb.set_pressed_no_signal(e.is_otherwise(to))
cb.tooltip_text = "Turn off \"Only if\" first: a thread with a condition isn't an otherwise thread" if has_when \
else otherwise_tip(insp.selected_table(), e)
static func _chip(text: String, fg: Color, bg: Color) -> Control:
var p := PanelContainer.new()
p.add_theme_stylebox_override("panel", Style.bordered(Style.box(bg, 9, [7, 1, 7, 2]), 1, Style.ACCENT))
p.add_child(Style.make_label(text, Style.body_bold(), 12, fg))
p.size_flags_vertical = Control.SIZE_SHRINK_CENTER
return p