TableTower/ui/pipe_popover.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

287 lines
11 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name PipePopover
extends PopupPanel
## Build mode: the options for one pipe, opened by clicking it. How many times
## it rolls, its condition ("Only if", or "Otherwise"), the variable it saves
## its result as, and Remove.
const SAVE_PATTERN := "^@?[A-Za-z_][A-Za-z0-9_]*$"
var main: Main
var key: String = ""
var _entry: TableEntry
var _to: String = ""
var _title: RichTextLabel
var _count: LineEdit
var _count_msg: Label
var _save: LineEdit
var _when: LineEdit
var _when_msg: Label
var _otherwise: CheckBox
var _field_focus: StyleBoxFlat
var _save_ok: StyleBoxFlat
var _save_bad: StyleBoxFlat
var _re := RegEx.new()
func setup(p_main: Main) -> void:
main = p_main
_re.compile(SAVE_PATTERN)
theme = Style.card_theme()
var sb := Style.box(Style.CARD, 9, [14, 11, 14, 12])
sb.border_width_top = 3
sb.border_color = Style.THREAD
add_theme_stylebox_override("panel", sb)
popup_hide.connect(func() -> void:
_commit_save()
_commit_when()
if main.board:
main.board.pipes.select_key = ""
)
var v := VBoxContainer.new()
v.add_theme_constant_override("separation", 9)
add_child(v)
_title = RichText.label("", 15, Style.INK, false)
_title.custom_minimum_size = Vector2(260, 0)
v.add_child(_title)
var field_ok := Style.bordered(Style.box(Color.WHITE, 5, [7, 3, 7, 3]), 1, Style.RULE)
var field_focus := Style.bordered(Style.box(Color.WHITE, 5, [7, 3, 7, 3]), 1, Style.THREAD)
_save_ok = field_ok
_save_bad = Style.bordered(Style.box(Color.WHITE, 5, [7, 3, 7, 3]), 1, Style.DANGER)
_field_focus = field_focus
var count_row := HBoxContainer.new()
count_row.add_theme_constant_override("separation", 6)
v.add_child(count_row)
var cl := Style.make_label("Rolls", Style.body(), 14, Style.MUTED)
cl.custom_minimum_size = Vector2(62, 0)
count_row.add_child(cl)
var minus := _small_button("−")
minus.pressed.connect(func() -> void: _set_count(str(_entry.count_for(_to) - 1) if _entry else "1"))
count_row.add_child(minus)
_count = LineEdit.new()
_count.custom_minimum_size = Vector2(64, 0)
_count.alignment = HORIZONTAL_ALIGNMENT_CENTER
_count.max_length = 60
_count.tooltip_text = "A number, or an expression such as @terms, answer or 1d3 (worked out when the thread runs)"
_count.add_theme_stylebox_override("normal", field_ok)
_count.add_theme_stylebox_override("focus", field_focus)
_count.text_changed.connect(func(s: String) -> void:
var ok := _count_error(s) == ""
_count.add_theme_stylebox_override("normal", field_ok if ok else _save_bad)
_count.add_theme_stylebox_override("focus", field_focus if ok else _save_bad)
_count_msg.text = _count_error(s)
_count_msg.visible = not ok
reset_size()
)
_count.text_submitted.connect(func(s: String) -> void: _set_count(s))
_count.focus_exited.connect(func() -> void: _set_count(_count.text))
count_row.add_child(_count)
var plus := _small_button("+")
plus.pressed.connect(func() -> void: _set_count(str(_entry.count_for(_to) + 1) if _entry else "1"))
count_row.add_child(plus)
count_row.add_child(Style.make_label("times", Style.body(), 14, Style.MUTED))
_count_msg = Style.make_label("", Style.body(), 12, Style.DANGER_INK)
_count_msg.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
_count_msg.custom_minimum_size = Vector2(260, 0)
_count_msg.hide()
v.add_child(_count_msg)
var when_row := HBoxContainer.new()
when_row.add_theme_constant_override("separation", 6)
v.add_child(when_row)
var wl := Style.make_label("Only if", Style.body(), 14, Style.MUTED)
wl.custom_minimum_size = Vector2(62, 0)
when_row.add_child(wl)
_when = LineEdit.new()
_when.placeholder_text = "always (or e.g. @str >= 8)"
_when.size_flags_horizontal = Control.SIZE_EXPAND_FILL
_when.max_length = 200
_when.tooltip_text = "The thread runs only when this is true. Every condition on the result is checked once, before any of its threads run."
_when.add_theme_stylebox_override("normal", field_ok)
_when.add_theme_stylebox_override("focus", field_focus)
_when.text_changed.connect(func(_s: String) -> void: _check_when())
_when.text_submitted.connect(func(_s: String) -> void:
_commit_when()
hide()
)
_when.focus_exited.connect(_commit_when)
when_row.add_child(_when)
_when_msg = Style.make_label("", Style.body(), 12, Style.DANGER_INK)
_when_msg.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
_when_msg.custom_minimum_size = Vector2(260, 0)
_when_msg.hide()
v.add_child(_when_msg)
var other_row := HBoxContainer.new()
other_row.add_theme_constant_override("separation", 6)
v.add_child(other_row)
var pad := Control.new()
pad.custom_minimum_size = Vector2(62, 0)
other_row.add_child(pad)
_otherwise = CheckBox.new()
_otherwise.text = "Otherwise"
_otherwise.tooltip_text = "Runs only when none of this result's \"Only if\" threads is true"
_otherwise.add_theme_font_override("font", Style.body())
_otherwise.add_theme_font_size_override("font_size", 14)
_otherwise.add_theme_color_override("font_color", Style.INK)
_otherwise.add_theme_color_override("font_hover_color", Style.INK)
_otherwise.add_theme_color_override("font_pressed_color", Style.INK)
_otherwise.add_theme_color_override("font_hover_pressed_color", Style.INK)
_otherwise.add_theme_color_override("font_disabled_color", Style.MUTED)
_otherwise.toggled.connect(func(on: bool) -> void:
if _entry == null or on == _entry.is_otherwise(_to):
return
_entry.set_pipe_otherwise(_to, on)
main.mark_dirty()
)
other_row.add_child(_otherwise)
var save_row := HBoxContainer.new()
save_row.add_theme_constant_override("separation", 6)
v.add_child(save_row)
var sl := Style.make_label("Save as", Style.body(), 14, Style.MUTED)
sl.custom_minimum_size = Vector2(62, 0)
save_row.add_child(sl)
_save = LineEdit.new()
_save.placeholder_text = "name, or @name to keep"
_save.size_flags_horizontal = Control.SIZE_EXPAND_FILL
_save.add_theme_stylebox_override("normal", field_ok)
_save.add_theme_stylebox_override("focus", field_focus)
_save.text_changed.connect(func(s: String) -> void:
var ok := s.strip_edges() == "" or _re.search(TemplateKit.probe(s.strip_edges())) != null
_save.add_theme_stylebox_override("normal", _save_ok if ok else _save_bad)
_save.add_theme_stylebox_override("focus", field_focus if ok else _save_bad)
)
_save.text_submitted.connect(func(_s: String) -> void:
_commit_save()
hide()
)
save_row.add_child(_save)
var hint := Style.make_label("{name} lasts one roll; {@name} is kept.", Style.body(), 12, Style.MUTED)
v.add_child(hint)
var remove := Button.new()
remove.text = "Delete thread"
remove.add_theme_font_size_override("font_size", 14)
var rn := Style.bordered(Style.box(Color(0, 0, 0, 0), 6, [10, 4, 10, 4]), 1, Style.DASH)
var rh := Style.bordered(Style.box(Style.WARN_BG, 6, [10, 4, 10, 4]), 1, Style.DANGER_INK)
Style.skin_button(remove, rn, rh, rh, Style.DANGER_INK)
remove.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
remove.pressed.connect(func() -> void:
var k := key
_commit_when()
_entry = null
hide()
main.remove_pipe(k)
)
v.add_child(remove)
func _small_button(text: String) -> Button:
var b := Button.new()
b.text = text
b.custom_minimum_size = Vector2(26, 26)
b.add_theme_font_size_override("font_size", 16)
var n := Style.bordered(Style.box(Color(0, 0, 0, 0), 5), 1, Style.RULE)
var h := Style.bordered(Style.box(Style.HOVER_ROW, 5), 1, Style.DASH)
Style.skin_button(b, n, h, h, Style.INK)
return b
## Opens for the pipe `pipe_key` near `at` (window coordinates).
func open(pipe_key: String, at: Vector2) -> void:
var from := pipe_key.get_slice(">", 0)
var t := main.data.get_table(from.get_slice(":", 0))
_to = pipe_key.get_slice(">", 1)
_entry = t.find_entry(from.get_slice(":", 1)) if t else null
if _entry == null:
return
key = pipe_key
var target := main.data.get_table(_to)
var source := RichText.plain(InspectorThread.source_title(t, _entry)) if _entry == t.outlet else RichText.preview(_entry.text, main.data)
_title.text = "%s [color=#%s]→[/color] [b]%s[/b]" % [source, Style.THREAD.to_html(false), RichText.plain(Formatter.strip_tags(target.name)) if target else _to]
_otherwise.tooltip_text = InspectorThread.otherwise_tip(t, _entry)
_count.text = _entry.count_text(_to)
_count.add_theme_stylebox_override("normal", _save_ok)
_count_msg.hide()
_save.text = _entry.save_for(_to)
_save.add_theme_stylebox_override("normal", _save_ok)
_when.text = _entry.when_for(_to)
_otherwise.set_pressed_no_signal(_entry.is_otherwise(_to))
_check_when()
main.board.pipes.select_key = pipe_key
reset_size()
var view := main.get_viewport_rect().size
var sz := get_contents_minimum_size()
var p := at + Vector2(12, 12)
p.x = clampf(p.x, 8.0, view.x - sz.x - 36.0)
p.y = clampf(p.y, 8.0, view.y - sz.y - 36.0)
popup(Rect2i(Vector2i(p), Vector2i.ZERO))
## Sets the count from typed text: a whole number (1–99) or an expression.
## Text that isn't a valid expression is left in the field, unsaved.
func _set_count(text: String) -> void:
if _entry == null or _count_error(text) != "":
return
var before := _entry.count_text(_to)
_entry.set_pipe_count(_to, text.strip_edges())
_count.text = _entry.count_text(_to)
if _entry.count_text(_to) != before:
main.mark_dirty()
## "" when `text` is a usable pipe count, else why not.
static func _count_error(text: String) -> String:
var s := text.strip_edges()
if s == "" or s.is_valid_int():
return ""
var err := Expr.check(TemplateKit.probe(s))
return "" if err == "" else "Not a number or expression: " + err
## "" when `text` is a usable condition (empty means always), else why not.
static func when_error(text: String) -> String:
var s := text.strip_edges()
if s == "":
return ""
var err := Expr.check(TemplateKit.probe(s))
return "" if err == "" else "Not a valid condition: " + err
## Live validation of the Only if field; Otherwise is off while it's filled.
func _check_when() -> void:
var err := when_error(_when.text)
var ok := err == ""
_when.add_theme_stylebox_override("normal", _save_ok if ok else _save_bad)
_when.add_theme_stylebox_override("focus", _field_focus if ok else _save_bad)
_when_msg.text = err
_when_msg.visible = not ok
var filled := _when.text.strip_edges() != ""
_otherwise.disabled = filled
if filled:
_otherwise.set_pressed_no_signal(false)
elif _entry != null:
_otherwise.set_pressed_no_signal(_entry.is_otherwise(_to))
reset_size()
func _commit_when() -> void:
if _entry == null or when_error(_when.text) != "":
return
var s := _when.text.strip_edges()
if s != _entry.when_for(_to):
_entry.set_pipe_when(_to, s)
main.mark_dirty()
func _commit_save() -> void:
if _entry == null:
return
var s := _save.text.strip_edges()
if s != "" and _re.search(TemplateKit.probe(s)) == null:
return
if s != _entry.save_for(_to):
_entry.set_pipe_save(_to, s)
main.mark_dirty()