class_name VariablesPanel extends VBoxContainer ## The Variables tab of the result tray: every board variable ({@name}) — the ## ones that are set and the ones variable cards declare — each editable, and ## clearable or resettable. Text a roll table gave can be re-rolled from that ## table. "Reset all…" puts everything back to its starting value after an ## in-app confirmation. Below them, the last roll's chain variables (read ## only). ## Smallest height of the list; it fills the rest of the tray and scrolls. const HEIGHT := 40.0 var main: Main var _list: VBoxContainer var _reset_all: Button var _field: StyleBoxFlat var _field_focus: StyleBoxFlat func setup(p_main: Main) -> void: main = p_main add_theme_constant_override("separation", 6) size_flags_vertical = SIZE_EXPAND_FILL _field = Style.bordered(Style.box(Color(1, 1, 1, 0.05), 6, [8, 3, 8, 3]), 1, Color(1, 1, 1, 0.12)) _field_focus = Style.bordered(Style.box(Color(1, 1, 1, 0.1), 6, [8, 3, 8, 3]), 1, Style.ACCENT) var scroll := ScrollContainer.new() scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED # The list fills the tray's height and scrolls (the tray is resizable). scroll.custom_minimum_size = Vector2(0, HEIGHT) scroll.size_flags_vertical = SIZE_EXPAND_FILL add_child(scroll) _list = VBoxContainer.new() _list.add_theme_constant_override("separation", 5) _list.size_flags_horizontal = SIZE_EXPAND_FILL scroll.add_child(_list) var foot := HBoxContainer.new() foot.alignment = BoxContainer.ALIGNMENT_END add_child(foot) _reset_all = Button.new() _reset_all.text = "Reset all…" _reset_all.tooltip_text = "Put every variable back to its starting value (variables without a card are cleared)" _reset_all.add_theme_font_size_override("font_size", 13) var n := Style.bordered(Style.box(Color(1, 1, 1, 0.04), 6, [10, 2, 10, 3]), 1, Color(1, 1, 1, 0.12)) var h := Style.bordered(Style.box(Color(1, 1, 1, 0.1), 6, [10, 2, 10, 3]), 1, Color(Style.DANGER, 0.6)) Style.skin_button(_reset_all, n, h, h, Style.ON_BG_MUTED, Style.ON_BG) _reset_all.pressed.connect(confirm_reset_all) foot.add_child(_reset_all) ## Opens the "Reset all variables?" confirmation. func confirm_reset_all() -> ModalCard: var msg := "Every variable goes back to its starting value, and variables without a variable card are cleared. This can't be undone." var d := ModalCard.confirm("Reset all variables?", msg, "Reset all", main.reset_all_variables, true) main.add_child(d) return d func render() -> void: for c in _list.get_children(): _list.remove_child(c) c.queue_free() var names := main.variable_names() _reset_all.disabled = names.is_empty() if names.is_empty(): var empty := RichText.label("No saved variables yet. A pipe that saves as [color=#%s]@name[/color], a change such as [color=#%s]@hp = 10[/color], or [lb][lb]Table -> @name]] in a result, keeps its value here." % [Style.ACCENT.to_html(false), Style.ACCENT.to_html(false)], 14, Style.ON_BG_MUTED, false) _list.add_child(empty) for n in names: _list.add_child(_var_row(n)) var chain: Dictionary = main.current.get("chain_vars", {}) if not chain.is_empty(): var head := Style.make_label("THIS ROLL", Style.caps(), 11, Style.ON_BG_MUTED) head.add_theme_constant_override("line_spacing", 0) var gap := Control.new() gap.custom_minimum_size = Vector2(0, 4) _list.add_child(gap) _list.add_child(head) var keys: Array = chain.keys() keys.sort() for k in keys: var line := RichText.label("[color=#%s]{%s}[/color] %s" % [Style.VALUE_INK_ON_BG.to_html(false), k, Expr.format_value(chain[k])], 14, Style.ON_BG, false) _list.add_child(line) ## Shown text of a variable's value ("" when it isn't set). func _value_text(name: String) -> String: var raw: Variant = main.persistent_vars.get(name) if raw == null: return "" var v: Variant = (raw as Dictionary).get("value", "") if raw is Dictionary else raw return Expr.format_value(v) func _var_row(name: String) -> Control: var is_set := main.persistent_vars.has(name) var card := main.variable_card(name) var row := HBoxContainer.new() row.add_theme_constant_override("separation", 6) var label := Style.make_label("@" + name, Style.body_bold(), 14, Style.VALUE_INK_ON_BG) label.custom_minimum_size = Vector2(84, 0) label.clip_text = true label.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS label.tooltip_text = "{@%s}%s" % [name, (" · " + Formatter.strip_tags(card.name)) if card else ""] label.size_flags_vertical = SIZE_SHRINK_CENTER row.add_child(label) var edit := LineEdit.new() edit.text = _value_text(name) edit.size_flags_horizontal = SIZE_EXPAND_FILL edit.custom_minimum_size = Vector2(60, 0) edit.add_theme_font_size_override("font_size", 14) edit.add_theme_color_override("font_color", Style.ON_BG) edit.add_theme_color_override("caret_color", Style.ON_BG) edit.add_theme_color_override("selection_color", Color(Style.ACCENT, 0.4)) edit.add_theme_color_override("font_placeholder_color", Color(Style.ON_BG_MUTED, 0.7)) edit.add_theme_stylebox_override("normal", _field) edit.add_theme_stylebox_override("focus", _field_focus) if not is_set and card: edit.placeholder_text = "not set · starts at %s" % (card.initial if card.initial != "" else "0") edit.tooltip_text = "Edit the value (BBCode ok); Enter to save" var commit := func() -> void: if edit.text != _value_text(name) and (main.persistent_vars.has(name) or edit.text != ""): main.set_variable(name, edit.text) edit.text_submitted.connect(func(_t: String) -> void: commit.call() edit.release_focus()) edit.focus_exited.connect(commit) row.add_child(edit) if card: var reset := _icon_button("↺", "Reset @%s to its starting value" % name) reset.pressed.connect(func() -> void: main.reset_variable(name)) row.add_child(reset) else: var v: Variant = main.persistent_vars.get(name) var source := str(v.get("source", "")) if v is Dictionary else "" var st := main.data.get_table(source) if source != "" else null var can := main.can_reroll_variable(name) var again := _icon_button("↻", ("Re-roll from %s" % Formatter.strip_tags(st.name)) if can else "Set by a change or answer; no table to re-roll from") again.disabled = not can again.pressed.connect(func() -> void: main.reroll_variable(name)) row.add_child(again) var clear := _icon_button("×", "Clear {@%s}" % name) clear.disabled = not is_set clear.pressed.connect(func() -> void: main.clear_variable(name)) row.add_child(clear) return row func _icon_button(text: String, tip: String) -> Button: var b := Button.new() b.text = text b.tooltip_text = tip b.custom_minimum_size = Vector2(28, 26) b.add_theme_font_size_override("font_size", 16) var n := Style.bordered(Style.box(Color(1, 1, 1, 0.04), 6), 1, Color(1, 1, 1, 0.12)) var h := Style.bordered(Style.box(Color(1, 1, 1, 0.1), 6), 1, Color(1, 1, 1, 0.2)) Style.skin_button(b, n, h, h, Style.ON_BG_MUTED, Style.ON_BG) b.add_theme_color_override("font_disabled_color", Color(Style.ON_BG_MUTED, 0.35)) return b