TableTower/ui/pipe_popover.gd
RobinFiredrake 9fdf65c00b Group cards and templates
Groups: an optional groups list on a collection (frame rect, collapsed,
members, details), saved by TowerBoard and kept by JsonIO. Expanded groups
draw a frame behind their cards with a title bar; collapsed ones become one
compact card that lists the members' lit results, with pipes re-routed to
its inlet and right edge. Build mode adds multi-select (Ctrl/Shift-click,
Ctrl-drag), Group (Ctrl+G), drop in/out of frames, rename, ungroup, delete
with undo and a details editor.

Templates: TemplateKit saves cards, groups or selections with relative
positions, internal pipes and outside pipes by name, detects {$name}
parameters and inserts with fresh ids. TemplateLibrary keeps user templates
in user://templates and lists the built-in ones in templates/.

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

190 lines
6.9 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 _count_msg: Label
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(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 pipe 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 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 = "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 = _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)
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
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()