class_name TableCardValue extends RefCounted ## The body of a variable card (its live value, big) or a collection card (its ## members with live values), re-rendered by refresh() after rolls and edits. var card: TableCard var var_value: Label var var_hint: Label ## Collection cards: the member list, refilled by refresh(). var members_box: VBoxContainer var members_cache := "" func _init(p_card: TableCard) -> void: card = p_card ## Drops the references to the previous build's labels. func clear() -> void: var_value = null var_hint = null members_box = null members_cache = "" ## A variable card's body: its live value, big, and in Build its starting value ## and a reset button. func make_variable_body(build: bool) -> Control: var table := card.table var main := card.main var wrap := MarginContainer.new() wrap.mouse_filter = Control.MOUSE_FILTER_PASS wrap.add_theme_constant_override("margin_left", 14) wrap.add_theme_constant_override("margin_right", 12) wrap.add_theme_constant_override("margin_top", 6) wrap.add_theme_constant_override("margin_bottom", 10) var v := TableCard.vbox(2) wrap.add_child(v) var_value = Style.make_label("", Style.display(true), 34, Style.INK) var_value.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART var_value.mouse_filter = Control.MOUSE_FILTER_PASS v.add_child(var_value) var_hint = Style.make_label("", Style.body(true), 12, Style.MUTED) var_hint.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART var_hint.mouse_filter = Control.MOUSE_FILTER_IGNORE v.add_child(var_hint) if build: var h := TableCard.hbox(8) var init := RulesView.dice(table.initial, main.data, card.plain) if table.initial.strip_edges() != "" else "" var start := Style.make_label("Starts at %s" % (init if init != "" else ("0" if table.var_type == "number" else "nothing")), Style.body(true), 12, Style.MUTED) start.size_flags_horizontal = Control.SIZE_EXPAND_FILL start.size_flags_vertical = Control.SIZE_SHRINK_CENTER h.add_child(start) var reset := Button.new() reset.text = "↺ Reset" reset.tooltip_text = "Set @%s back to its starting value" % table.var_name reset.add_theme_font_override("font", Style.body(true)) reset.add_theme_font_size_override("font_size", 12) var n := Style.box(Color(0, 0, 0, 0), 5, [5, 0, 5, 1]) var hv := Style.box(Color("#e6dfcd"), 5, [5, 0, 5, 1]) Style.skin_button(reset, n, hv, hv, Style.COPPER, Style.INK) reset.pressed.connect(func() -> void: main.reset_variable(table.var_name)) h.add_child(reset) v.add_child(h) return wrap ## A collection card's body: its members with their live values (refilled by ## refresh()), and in Build "+ Add …", which selects the card so the ## inspector can add a member. func make_collection_body(build: bool) -> Control: var table := card.table var main := card.main var wrap := MarginContainer.new() wrap.mouse_filter = Control.MOUSE_FILTER_PASS wrap.add_theme_constant_override("margin_left", 12) wrap.add_theme_constant_override("margin_right", 14) wrap.add_theme_constant_override("margin_top", 6) wrap.add_theme_constant_override("margin_bottom", 10) var v := TableCard.vbox(6) wrap.add_child(v) members_box = TableCard.vbox(0) v.add_child(members_box) if build: var add := TableCardEditing.footer_button("+ Add " + Describe._singular(Formatter.strip_tags(table.name).strip_edges().to_lower()), Style.INK) add.size_flags_horizontal = Control.SIZE_FILL add.tooltip_text = "Add a member in the inspector" add.set_meta("collection_add", true) add.pressed.connect(func() -> void: if main.has_method("select_card"): main.call("select_card", table.id) else: main.request_table_settings(table.id) ) v.add_child(add) return wrap ## A collection's members: [{name ("@skill_pilot"), label ("Pilot"), value ## (Variant or null when not set)}], set members first, then members only ## named by the board's rules, each sorted by label. static func collection_members(t: RollTable, board: TowerBoard, vars: Dictionary) -> Array[Dictionary]: var out: Array[Dictionary] = [] if t == null or not t.is_collection() or t.var_name == "": return out var seen := {} for v: Dictionary in RollContext.vars_with_prefix(vars, t.var_name + "_"): var nm := "@" + str(v["name"]) seen[nm] = true out.append({"name": nm, "label": Describe.label(nm, board), "value": v["value"]}) var named: Array[Dictionary] = [] for kv: Dictionary in Builder.known_values(board): if str(kv.get("collection", "")) == "@%s_*" % t.var_name and not seen.has(str(kv["name"])): seen[str(kv["name"])] = true named.append({"name": str(kv["name"]), "label": str(kv["label"]), "value": null}) var by_label := func(a: Dictionary, b: Dictionary) -> bool: return str(a["label"]).naturalnocasecmp_to(str(b["label"])) < 0 out.sort_custom(by_label) named.sort_custom(by_label) out.append_array(named) return out func _refresh_members() -> void: var plain := card.plain var list := collection_members(card.table, card.main.data, card.main.persistent_vars) var key := var_to_str(list) + str(plain) if key == members_cache: return members_cache = key for c in members_box.get_children(): members_box.remove_child(c) c.queue_free() if list.is_empty(): var none := Style.make_label("No members yet", Style.body_italic(true), 14, Style.MUTED) none.mouse_filter = Control.MOUSE_FILTER_IGNORE members_box.add_child(none) return for m: Dictionary in list: var h := TableCard.hbox(8) h.custom_minimum_size = Vector2(0, 28) h.set_meta("member", str(m["name"])) var nl := Style.make_label(str(m["label"]) if plain else str(m["name"]), Style.body(true), 15, Style.INK) nl.size_flags_horizontal = Control.SIZE_EXPAND_FILL nl.size_flags_vertical = Control.SIZE_SHRINK_CENTER nl.mouse_filter = Control.MOUSE_FILTER_IGNORE h.add_child(nl) var val: Variant = m["value"] var vl := Style.make_label("—" if val == null else Expr.format_value(val), Style.display(true), 17, Style.INK if val != null else Style.MUTED) vl.size_flags_vertical = Control.SIZE_SHRINK_CENTER vl.mouse_filter = Control.MOUSE_FILTER_IGNORE h.add_child(vl) members_box.add_child(h) ## Re-renders the member list or the variable's value and hint, touching only ## what changed. func refresh() -> void: if is_instance_valid(members_box): _refresh_members() if is_instance_valid(var_value): var d := Roller.variable_display(card.main.data, card.main.persistent_vars, card.table) var shown := str(d["text"]) if str(d["text"]) != "" else "—" if var_value.text != shown: var_value.text = shown var size_px := 34 if shown.length() <= 8 else (24 if shown.length() <= 18 else 17) var_value.add_theme_font_size_override("font_size", size_px) var_value.add_theme_color_override("font_color", Style.INK if d["set"] else Style.MUTED) var hint := "" if str(d["error"]) != "": hint = "⚠ " + str(d["error"]) elif not d["set"]: hint = "Not set yet · starts at this" var_hint.text = hint var_hint.add_theme_color_override("font_color", Style.DANGER_INK if str(d["error"]) != "" else Style.MUTED) var_hint.visible = hint != ""