Pipes can carry "when" (Only if) and "otherwise". When an entry is
chosen, its effects run, its text expands, then every pipe condition and
count is evaluated once as a snapshot before any pipe runs; when pipes
run if true, otherwise pipes if no when pipe was true, plain pipes
always. Condition errors count as false and show as error lines.
New router card (kind "router"): its entries are cases with an
entry-level "when"; the first true case is chosen (empty = Otherwise),
and "No case matched" stops the node. One action. JSON round trip for
entry/pipe when and pipe otherwise, including JsonIO.canonical and
edge labels. Roller.lint_pipes() and Roller.lint_router() for board
checks. A pick choice's save_as is now stored before the snapshot.
UI: Only if / Otherwise in the pipe popover with live validation, pipe
labels ("if @str >= 8", "otherwise") and a source-end diamond on
conditional pipes, router card with fork icon, per-case condition rows,
case editing / reordering in Build, router settings, "+ Table" entry,
router cases in table previews. Logic demo now routes through a router.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
285 lines
10 KiB
GDScript
285 lines
10 KiB
GDScript
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.COPPER
|
||
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.COPPER)
|
||
_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 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 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 pipe runs only when this is true. Every condition on the entry is checked once, before any of its pipes 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\" pipes 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 = "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
|
||
_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)
|
||
_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)
|
||
_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()
|