TableTower/ui/pipe_popover.gd
RobinFiredrake 87e578b85a Play-side UI: BBCode rendering, hover details, card decks, variables, copy, ref pipes
- Cards, result chips and the Earlier list render BBCode with RichTextLabel;
  Play rows show [[refs]] as teal chips and {vars} in brass, lit rows show the
  rolled text; missing/unset/too-deep markers are red.
- Hover pop-up (DetailsPopup) for table headers, entries with details, result
  chips and [url] metas in rendered text; items with details get a small ⓘ.
- Card-mode tables show cards left and Reshuffle; chips note reshuffles.
- Local state: decks, persistent variables and history per collection via
  StateStore (memory only when saving is off); delete_table forgets its state.
- Result tray: Copy menu (text / BBCode / with details) and a Variables tab
  (edit, re-roll, clear; last roll's chain variables read-only).
- Reference pipes drawn dashed from TextExpander.references; lit when used.
- Build mode: weight field, per-entry and table details editors, a gear
  button for table settings, pipe popover (count, save as, remove), range
  clamps use min_value(); Build cards are 300px wide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:43:01 -06:00

160 lines
5.6 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, 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 _save: LineEdit
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.COPPER
add_theme_stylebox_override("panel", sb)
popup_hide.connect(func() -> void:
_commit_save()
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.COPPER)
_save_ok = field_ok
_save_bad = Style.bordered(Style.box(Color.WHITE, 5, [7, 3, 7, 3]), 1, Style.DANGER)
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(_entry.count_for(_to) - 1 if _entry else 1))
count_row.add_child(minus)
_count = LineEdit.new()
_count.custom_minimum_size = Vector2(44, 0)
_count.alignment = HORIZONTAL_ALIGNMENT_CENTER
_count.max_length = 2
_count.add_theme_stylebox_override("normal", field_ok)
_count.add_theme_stylebox_override("focus", field_focus)
_count.text_submitted.connect(func(s: String) -> void: _set_count(s.to_int()))
_count.focus_exited.connect(func() -> void: _set_count(_count.text.to_int()))
count_row.add_child(_count)
var plus := _small_button("+")
plus.pressed.connect(func() -> void: _set_count(_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))
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(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 = "Remove pipe"
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
_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)
_title.text = "%s [color=#%s]→[/color] [b]%s[/b]" % [RichText.preview(_entry.text, main.data), Style.COPPER.to_html(false), RichText.plain(Formatter.strip_tags(target.name)) if target else _to]
_count.text = str(_entry.count_for(_to))
_save.text = _entry.save_for(_to)
_save.add_theme_stylebox_override("normal", _save_ok)
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))
func _set_count(n: int) -> void:
if _entry == null:
return
_entry.set_pipe_count(_to, n)
_count.text = str(_entry.count_for(_to))
main.mark_dirty()
func _commit_save() -> void:
if _entry == null:
return
var s := _save.text.strip_edges()
if s != "" and _re.search(s) == null:
return
if s != _entry.save_for(_to):
_entry.set_pipe_save(_to, s)
main.mark_dirty()