class_name ValuePicker extends Button ## A dropdown of the values a board uses (Builder.known_values), with search, ## grouped as variables, collections, other values and roll values. Each item ## shows its label and kind. "+ New value…" asks for a name and adds it, ## making a variable card when "Show on the board" is ticked. ## Used by the inspector's effect, condition, modifier and options builders. signal value_chosen(name: String) const GROUPS := ["", "Variables", "Ledgers", "Other values", "Roll values"] const KIND_WORDS := {"number": "number", "text": "text", "list": "list", "collection": "collection", "scale": "scale"} var main: Main ## The chosen value's name ("@luck", "@skill_*", "tier"), or "". var value := "" ## Kinds offered ([] for all): "number", "text", "list", "collection", "scale". var kinds: Array = [] ## Extra items listed first: Array of {name, label, kind?}. var extras: Array = [] ## Collection patterns (@skill_*) are only offered when true. var allow_patterns := false var allow_new := true var placeholder := "choose a value" var _popup: PopupPanel var _search: LineEdit var _list: VBoxContainer var _scroll: ScrollContainer var _new_button: Button ## Names currently listed (after the search), in order. var _shown: Array[String] = [] func _init(p_main: Main = null, p_value: String = "") -> void: main = p_main value = p_value var n := Style.bordered(Style.box(Style.VALUE_BG, 6, [8, 3, 8, 3]), 1, Style.VALUE_BORDER) var h := Style.bordered(Style.box(Style.VALUE_BG.darkened(0.04), 6, [8, 3, 8, 3]), 1, Style.TRIM) Style.skin_button(self, n, h, h, Style.INK) add_theme_font_override("font", Style.body_bold()) add_theme_font_size_override("font_size", 15) add_theme_color_override("font_focus_color", Style.INK) size_flags_vertical = Control.SIZE_SHRINK_CENTER pressed.connect(open_menu) _refresh_text() func set_value(n: String) -> void: value = n _refresh_text() func _refresh_text() -> void: if value == "": text = placeholder + " ▾" add_theme_color_override("font_color", Style.MUTED) else: text = label_of(value) + " ▾" add_theme_color_override("font_color", Style.INK) tooltip_text = value if value != "" else "Pick a value" custom_minimum_size = Vector2(0, 0) func label_of(n: String) -> String: for x: Dictionary in extras: if str(x["name"]) == n: return str(x["label"]) return Describe.label(n, main.data if main else null) ## Every value offered, before the search: Array of {name, label, kind, group}. func items() -> Array: var out: Array = [] var seen := {} for x: Dictionary in extras: out.append({"name": x["name"], "label": x["label"], "kind": str(x.get("kind", "")), "group": ""}) seen[x["name"]] = true var board: TowerBoard = main.data if main else null var known: Array = Builder.known_values(board) if board else [] # Members of a collection that only exist as saved values (not yet named # in any rule) are offered too. if main: var stems: Array = [] for t in board.tables: if t.is_collection() and t.var_name != "": stems.append(t) var names: Array = main.persistent_vars.keys() names.sort() for n: String in names: for t: RollTable in stems: if n.begins_with(t.var_name + "_") and n.length() > t.var_name.length() + 1: known.append({"name": "@" + n, "label": Describe.label("@" + n, board), "kind": "number", "source": "member", "collection": "@%s_*" % t.var_name}) break for k: Dictionary in known: var n := str(k["name"]) if seen.has(n): continue var kind := str(k["kind"]) if n.ends_with("*") and not allow_patterns: continue if not kinds.is_empty() and not kinds.has(kind): continue seen[n] = true var g := "Other values" match str(k["source"]): "variable": g = "Variables" "collection": g = "Ledgers" "chain": g = "Roll values" "input": g = "Roll values" if k.has("collection"): g = "Ledgers" out.append({"name": n, "label": str(k["label"]), "kind": kind, "group": g}) var ordered: Array = [] for g: String in GROUPS: for it: Dictionary in out: if it["group"] == g: ordered.append(it) return ordered ## Items matching `query` (label or name, any case). func filtered(query: String) -> Array: var q := query.strip_edges().to_lower() var out: Array = [] for it: Dictionary in items(): if q == "" or str(it["label"]).to_lower().contains(q) or str(it["name"]).to_lower().contains(q): out.append(it) return out ## Names listed in the open menu. func shown_names() -> Array[String]: return _shown func open_menu() -> void: if _popup == null: _build_popup() _search.text = "" _fill("") var r := get_global_rect() _popup.reset_size() var view := get_viewport_rect().size var p := Vector2(r.position.x, r.end.y + 4) var sz := Vector2(260, 0) p.x = clampf(p.x, 8.0, view.x - sz.x - 12.0) if p.y + 340.0 > view.y: p.y = maxf(8.0, r.position.y - 344.0) _popup.popup(Rect2i(Vector2i(p), Vector2i(260, 0))) _search.grab_focus.call_deferred() func is_menu_open() -> bool: return _popup != null and _popup.visible func close_menu() -> void: if _popup: _popup.hide() func _build_popup() -> void: _popup = PopupPanel.new() _popup.theme = Style.card_theme() var sb := Style.bordered(Style.box(Style.CARD, 8, [6, 6, 6, 6]), 1, Style.DASH) sb.shadow_color = Color(0, 0, 0, 0.3) sb.shadow_size = 12 sb.shadow_offset = Vector2(0, 5) _popup.add_theme_stylebox_override("panel", sb) var v := VBoxContainer.new() v.add_theme_constant_override("separation", 4) v.custom_minimum_size = Vector2(248, 0) _popup.add_child(v) _search = InspectorKit.line("", "Search values") _search.text_changed.connect(_fill) _search.text_submitted.connect(func(s: String) -> void: if not _shown.is_empty(): choose(_shown[0]) elif allow_new and s.strip_edges() != "": _ask_new(s.strip_edges()) ) v.add_child(_search) _scroll = ScrollContainer.new() _scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED _scroll.custom_minimum_size = Vector2(0, 40) v.add_child(_scroll) _list = VBoxContainer.new() _list.add_theme_constant_override("separation", 0) _list.size_flags_horizontal = Control.SIZE_EXPAND_FILL _scroll.add_child(_list) _new_button = _row_button("+ New value…", "", true) _new_button.pressed.connect(func() -> void: _ask_new(_search.text.strip_edges())) v.add_child(_new_button) add_child(_popup) func _fill(query: String) -> void: for c in _list.get_children(): _list.remove_child(c) c.queue_free() _shown.clear() var last_group := "-" for it: Dictionary in filtered(query): if it["group"] != last_group: last_group = str(it["group"]) if last_group != "": var h := InspectorKit.flabel(last_group) h.add_theme_constant_override("line_spacing", 0) var m := MarginContainer.new() m.add_theme_constant_override("margin_left", 6) m.add_theme_constant_override("margin_top", 6) m.add_child(h) _list.add_child(m) _list.add_child(_item_button(it)) _shown.append(str(it["name"])) var q := query.strip_edges() _new_button.visible = allow_new _new_button.text = "+ New value…" if q == "" else "+ New value “%s”…" % q if _shown.is_empty(): _list.add_child(InspectorKit.note("Nothing matches.", Style.MUTED, 14)) _scroll.custom_minimum_size.y = clampf(_list.get_combined_minimum_size().y, 40.0, 300.0) if _popup.visible: _popup.reset_size() func _item_button(it: Dictionary) -> Button: var kind := str(it.get("kind", "")) var b := _row_button(str(it["label"]), KIND_WORDS.get(kind, kind), false) if str(it["name"]) == value: b.add_theme_stylebox_override("normal", Style.box(Style.LIT, 5, [8, 4, 8, 4])) b.tooltip_text = str(it["name"]) var n := str(it["name"]) b.pressed.connect(func() -> void: choose(n)) return b func _row_button(label: String, right: String, accent: bool) -> Button: var b := Button.new() b.alignment = HORIZONTAL_ALIGNMENT_LEFT b.text = label b.clip_text = true var n := Style.box(Color(0, 0, 0, 0), 5, [8, 4, 8, 4]) var h := Style.box(Style.HOVER_ROW, 5, [8, 4, 8, 4]) Style.skin_button(b, n, h, h, Style.TRIM_DARK if accent else Style.INK) b.add_theme_font_override("font", Style.body_bold() if accent else Style.body()) b.add_theme_font_size_override("font_size", 15) b.focus_mode = Control.FOCUS_NONE if right != "": var k := Style.make_label(right, Style.body(), 12, Style.MUTED) k.set_anchors_and_offsets_preset(Control.PRESET_CENTER_RIGHT) k.offset_left = -80 k.offset_right = -8 k.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT k.mouse_filter = Control.MOUSE_FILTER_IGNORE b.add_child(k) return b ## Picks `n` (as if clicked). func choose(n: String) -> void: close_menu() set_value(n) value_chosen.emit(n) func _ask_new(initial: String) -> void: close_menu() var d := ModalCard.new("New value", 420.0) var le := LineEdit.new() le.text = initial le.placeholder_text = "e.g. Luck" ModalCard.style_line_edit(le) d.body.add_child(ModalCard.field("Name", le)) var cb := InspectorKit.check_box("Show on the spread", true) cb.tooltip_text = "Adds a variable card, so the value has a label and a starting value, and shows while you play" d.body.add_child(cb) var hint := ModalCard.note("") d.body.add_child(hint) var upd := func(_s: String = "") -> void: var nm := JsonIO.slug(le.text.strip_edges(), "") hint.text = "In rules: @%s" % nm if nm != "" else "Give it a name." le.text_changed.connect(upd) upd.call() var accept := func() -> void: if le.text.strip_edges() == "": return d.close() create_value(le.text.strip_edges(), cb.button_pressed) le.text_submitted.connect(func(_t: String) -> void: accept.call()) d.add_button("Cancel", "plain", d.close) d.add_button("Add", "primary", accept) d.ready.connect(func() -> void: le.grab_focus() le.select_all() ) if main: main.add_child(d) ## Adds a value named `label_text` ("Luck" -> @luck) and picks it. With ## `on_board`, a variable card declares it (Main.add_variable_card). ## Returns the value's name. func create_value(label_text: String, on_board: bool) -> String: var nm := JsonIO.slug(label_text.strip_edges(), "value") if on_board and main: # A value made from a picker that only takes lists (Add to list) is a list. var type := str(kinds[0]) if not kinds.is_empty() and RollTable.VAR_TYPES.has(str(kinds[0])) else "number" var t := main.add_variable_card(label_text.strip_edges(), nm, type) nm = t.var_name var full := "@" + nm set_value(full) value_chosen.emit(full) return full