class_name SheetKindSettings extends RefCounted ## The settings each kind of box has in the inspector (Build): number limits, ## dropdown options, bar maximum, clock segments, the formula, the dice, a ## list's style, columns and point pool, slots, spell levels, XP levels, ## coins. Lists of things are typed one per line, in plain words. ## groups() returns [[label, [controls]], …] for SheetInspector. static func groups(ed: SheetEditor, box: SheetBox) -> Array: var cfg := func(label: String, change: Callable) -> void: ed.edit_config(box, label, change) match box.kind: SheetKinds.NUMBER: return [["Limits", [_pair( _line("Lowest", str(box.config.get("min", "")), func(t: String) -> void: cfg.call("Lowest", func() -> void: _set_or_clear(box, "min", t))), _line("Highest", str(box.config.get("max", "")), func(t: String) -> void: cfg.call("Highest", func() -> void: _set_or_clear(box, "max", t))))]]] SheetKinds.DROPDOWN, SheetKinds.CONDITIONS: return [["Options, one per line", [_lines(box.config.get("options", []), func(rows: Array) -> void: cfg.call("Options", func() -> void: box.config["options"] = rows))]]] SheetKinds.RESOURCE: var v: Dictionary = box.value if box.value is Dictionary else {"cur": 0, "max": 10} return [["Maximum", [_line("10", SheetValues.show(v.get("max", 10)), func(t: String) -> void: cfg.call("Maximum", func() -> void: var next := v.duplicate() next["max"] = maxi(1, int(SheetValues.number(t))) next["cur"] = mini(int(SheetValues.number(next.get("cur", 0))), next["max"]) box.value = next))]], ["Bar colour", [ColorChoice.new(str(box.config.get("color", "")), func(hex: String) -> void: cfg.call("Bar colour", func() -> void: box.config["color"] = hex if hex != "" else "#a33a2c"))]]] SheetKinds.TICKS: return [["Boxes", [_spin(ed, box, "count", 1, 40)]]] SheetKinds.DOTS: return [["Dots", [_spin(ed, box, "max", 1, 20)]]] SheetKinds.CLOCK: return [["Segments", [_spin(ed, box, "segments", 2, 24)]]] SheetKinds.SLOTS: return [["Slots", [_spin(ed, box, "slots", 1, 60)]]] SheetKinds.LADDER: return [["Steps, lowest first, one per line", [_lines(box.config.get("steps", SheetKinds.FATE_LADDER), func(rows: Array) -> void: cfg.call("Steps", func() -> void: box.config["steps"] = rows))]]] SheetKinds.FORMULA: var result := SheetValues.formula(ed.doc, box) return [["Formula", [_line("(@str - 10) / 2", str(box.config.get("expr", "")), func(t: String) -> void: cfg.call("Formula", func() -> void: box.config["expr"] = t)), _note("Use other boxes by their name in tables, like @str. Now: %s" % (result["error"] if result["error"] != "" else SheetValues.show(result["value"])))]]] SheetKinds.DICE: return [["Dice", [_line("1d20 + @str", str(box.config.get("dice", "")), func(t: String) -> void: cfg.call("Dice", func() -> void: box.config["dice"] = t)), _note("Dice like 2d6 plus any box by its name in tables, like @str.")]]] SheetKinds.LIST: return _list_groups(ed, box) SheetKinds.SPELL_SLOTS: var rows: Array = [] for lv: Dictionary in box.config.get("levels", []): rows.append("%s: %d" % [lv.get("name", ""), int(SheetValues.number(lv.get("max", 0)))]) return [["Levels, one per line (name: slots)", [_lines(rows, func(lines: Array) -> void: cfg.call("Spell levels", func() -> void: box.config["levels"] = _parse_pairs(lines, ":", "max")))]]] SheetKinds.XP: var levels: Array = box.config.get("levels", []) return [["XP for each level, from level 1", [_line("0, 2000, 4000", ", ".join(levels.map(func(n: Variant) -> String: return SheetValues.show(n))), func(t: String) -> void: cfg.call("XP levels", func() -> void: var out: Array = [] for part in t.split(",", false): out.append(int(SheetValues.number(part))) box.config["levels"] = out if not out.is_empty() else [0]))]]] SheetKinds.CURRENCY: var rows: Array = [] for coin: Dictionary in box.config.get("coins", []): rows.append("%s = %s" % [coin.get("name", ""), SheetValues.show(coin.get("rate", 1))]) return [["Coins, one per line (name = worth in the first coin)", [_lines(rows, func(lines: Array) -> void: cfg.call("Coins", func() -> void: box.config["coins"] = _parse_pairs(lines, "=", "rate")))]]] SheetKinds.PICTURE: var choose := ModalCard.make_button("Choose a picture…", "plain") choose.pressed.connect(SheetFieldViews.pick_picture.bind(ed, box)) return [["Picture", [choose]]] return [] ## A list's style (Rows | Mini table | Chips), its columns and its point pool. static func _list_groups(ed: SheetEditor, box: SheetBox) -> Array: var seg := HBoxContainer.new() seg.name = "ListStyle" seg.add_theme_constant_override("separation", 3) for pair: Array in [["rows", "Rows"], ["table", "Mini table"], ["chips", "Chips"]]: var on: bool = str(box.config.get("style", "rows")) == pair[0] var b := Button.new() b.name = str(pair[0]) b.text = pair[1] b.focus_mode = Control.FOCUS_NONE b.size_flags_horizontal = Control.SIZE_EXPAND_FILL b.add_theme_font_override("font", Style.body_bold()) b.add_theme_font_size_override("font_size", 13) var bg := Style.box(Style.INK if on else Style.CARD_HEAD, 6, [4, 6, 4, 6]) Style.skin_button(b, bg, bg, bg, Style.CARD if on else Style.INK) var style: String = pair[0] b.pressed.connect(func() -> void: ed.edit("List style", func() -> void: box.config["style"] = style)) seg.add_child(b) var helps := {"rows": "One thin row per entry: name on the left, value on the right.", "table": "A row per entry, a cell per column.", "chips": "Short entries side by side, like tags."} var col_lines: Array = [] for c: Dictionary in SheetFieldsList.columns(box): col_lines.append("%s (number)" % c["name"] if c.get("type", "text") == "number" else str(c["name"])) var pool := _line("0 = no pool", SheetValues.show(box.config.get("pool", 0)), func(t: String) -> void: ed.edit_config(box, "Point pool", func() -> void: box.config["pool"] = int(SheetValues.number(t)))) pool.name = "PoolTotal" var cost := _line("Cost", str(box.config.get("cost_column", "Cost")), func(t: String) -> void: ed.edit_config(box, "Cost column", func() -> void: box.config["cost_column"] = t.strip_edges())) return [["Style", [seg, _note(str(helps.get(str(box.config.get("style", "rows")), "")))]], ["Columns, one per line (add “(number)” for numbers)", [_lines(col_lines, func(lines: Array) -> void: ed.edit_config(box, "Columns", func() -> void: box.config["columns"] = _parse_columns(lines)))]], ["Point pool", [_pair(pool, cost), _note("Each entry's cost comes off the pool. Going over is allowed and shows red.")]]] static func _parse_columns(lines: Array) -> Array: var out: Array = [] for line: String in lines: var number := line.to_lower().contains("(number)") var name := line.replacen("(number)", "").strip_edges() if name != "": out.append({"name": name, "type": "number" if number else "text"}) return out if not out.is_empty() else [{"name": "Name", "type": "text"}] ## "1st: 4" lines → [{"name": "1st", key: 4}]. static func _parse_pairs(lines: Array, sep: String, key: String) -> Array: var out: Array = [] for line: String in lines: var parts := line.split(sep, false, 1) if parts.size() == 2 and parts[0].strip_edges() != "": out.append({"name": parts[0].strip_edges(), key: SheetValues.number(parts[1].strip_edges())}) return out static func _set_or_clear(box: SheetBox, key: String, text: String) -> void: if text.strip_edges() == "": box.config.erase(key) else: box.config[key] = SheetFieldViews.parse_number(text, 0) static func _spin(ed: SheetEditor, box: SheetBox, key: String, lo: int, hi: int) -> SpinBox: var s := SpinBox.new() s.name = key.capitalize() s.min_value = lo s.max_value = hi s.value = SheetValues.number(box.config.get(key, lo)) s.value_changed.connect(func(v: float) -> void: ed.edit_config(box, key.capitalize(), func() -> void: box.config[key] = int(v))) return s static func _line(placeholder: String, text: String, on_change: Callable) -> LineEdit: var e := LineEdit.new() e.placeholder_text = placeholder e.text = text ModalCard.style_line_edit(e) e.text_changed.connect(on_change) return e static func _lines(rows: Array, on_change: Callable) -> TextEdit: var t := TextEdit.new() t.name = "Lines" t.text = "\n".join(rows.map(func(r: Variant) -> String: return str(r))) t.custom_minimum_size.y = 96 ModalCard.style_text_edit(t) t.text_changed.connect(func() -> void: var out: Array = [] for line in t.text.split("\n", false): if line.strip_edges() != "": out.append(line.strip_edges()) on_change.call(out)) return t static func _pair(a: Control, b: Control) -> HBoxContainer: var row := HBoxContainer.new() row.add_theme_constant_override("separation", 6) a.size_flags_horizontal = Control.SIZE_EXPAND_FILL b.size_flags_horizontal = Control.SIZE_EXPAND_FILL row.add_child(a) row.add_child(b) return row static func _note(text: String) -> Label: var l := Style.make_label(text, Style.body(), 13, Style.MUTED) l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART return l