class_name TableSettingsDialog extends ModalCard ## Build-mode editor for one table's name, details, tags, type and card mode. ## The editing rules live in TableSettingsRules (static, tested without a ## window); the kind-specific fields in SettingsKindFields. signal applied(settings: Dictionary) ## The footer's "Save as template…" (the dialog closes first; Main opens the save dialog). signal template_requested const COLORS := [ ["Red", Color("#a33a2c")], ["Copper", Color("#b8703a")], ["Green", Color("#2f6b4f")], ["Blue", Color("#2f5d8a")], ["Purple", Color("#6b3f8a")], ] var table: RollTable var _name: LineEdit var _details: TextEdit var _preview: RichTextLabel var _tags: LineEdit var _dice: LineEdit var _dice_row: Control var _mod: LineEdit var _mod_note: Label var _type_note: Label var _problem: Label var _deck_block: Control var _ranged := false var _card_mode := false var _deck_empty := "reshuffle" var _type_box: Control var _card_box: Control var _kind: SettingsKindFields const OPTION_LABELS := ["Fixed list", "From variables (+ fixed extras)"] const LOOP_LABELS := ["Times", "While", "Until"] const LOOP_NOTES := { "times": "Runs Each time this many times (worked out once), then Afterwards. e.g. 3, @answer, 1d3", "while": "Checks the condition before each pass and stops when it's false. e.g. @hp > 0", "until": "Runs a pass, then stops once the condition is true (at least one pass). e.g. @gold >= 100", } func _init(p_table: RollTable = null) -> void: super._init(Describe.kind_label(p_table) + " settings" if p_table else "Card settings", 560.0) table = p_table _ranged = table.is_ranged() _card_mode = table.card_mode _deck_empty = table.deck_empty _kind = SettingsKindFields.new(self) _build() func _build() -> void: _name = LineEdit.new() _name.text = table.name _name.placeholder_text = "Table name" style_line_edit(_name) _name.text_changed.connect(func(_t: String) -> void: _refresh()) body.add_child(field("Name", _name)) _kind.build() _details = TextEdit.new() _details.text = table.details _details.placeholder_text = "What this table is for. BBCode is allowed." _details.custom_minimum_size.y = 84 _details.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY style_text_edit(_details) _details.text_changed.connect(_refresh) var tools := HBoxContainer.new() tools.add_theme_constant_override("separation", 6) tools.add_child(_tool_button("B", "Bold", func() -> void: _wrap_selection("[b]", "[/b]"))) tools.add_child(_tool_button("I", "Italic", func() -> void: _wrap_selection("[i]", "[/i]"))) var color := make_button("Color ▾", "plain") color.pressed.connect(func() -> void: _color_menu(color)) color.add_theme_font_size_override("font_size", 14) tools.add_child(color) var dbox := VBoxContainer.new() dbox.add_theme_constant_override("separation", 6) dbox.add_child(section("Details")) dbox.add_child(tools) dbox.add_child(_details) _preview = RichTextLabel.new() _preview.bbcode_enabled = true _preview.fit_content = true _preview.scroll_active = false _preview.custom_minimum_size.y = 24 var pbox := PanelContainer.new() pbox.add_theme_stylebox_override("panel", Style.box(Color(0, 0, 0, 0.05), 7, [10, 7, 10, 7])) pbox.add_child(_preview) dbox.add_child(pbox) body.add_child(dbox) _tags = LineEdit.new() _tags.text = ", ".join(table.tags) _tags.placeholder_text = "loot, treasure, magic" style_line_edit(_tags) body.add_child(field("Tags (comma-separated)", _tags)) var type_box := VBoxContainer.new() type_box.add_theme_constant_override("separation", 6) type_box.add_child(section("Table type")) type_box.add_child(segmented(["Plain", "Ranged (dice)"], 1 if _ranged else 0, func(i: int) -> void: _ranged = i == 1 _refresh() )) _dice = LineEdit.new() _dice.text = table.dice _dice.placeholder_text = "1d100" _dice.custom_minimum_size.x = 140 style_line_edit(_dice) _dice.size_flags_horizontal = SIZE_SHRINK_BEGIN _dice.text_changed.connect(func(_t: String) -> void: _refresh()) var drow := HBoxContainer.new() drow.add_theme_constant_override("separation", 10) var dl := Style.make_label("Dice", Style.body(), 15, Style.INK) dl.size_flags_vertical = SIZE_SHRINK_CENTER drow.add_child(dl) drow.add_child(_dice) var eg := note("e.g. 1d100, 2d6, 1d8+2") eg.size_flags_vertical = SIZE_SHRINK_CENTER drow.add_child(eg) # The modifier: added to every roll, e.g. @luck. The inspector has the # plain builder for it; here it's text with a plain reading under it. _mod = LineEdit.new() _mod.text = table.dice_mod _mod.placeholder_text = "none" _mod.custom_minimum_size.x = 140 style_line_edit(_mod) _mod.size_flags_horizontal = SIZE_SHRINK_BEGIN _mod.text_changed.connect(func(_t: String) -> void: _refresh()) var mrow := HBoxContainer.new() mrow.add_theme_constant_override("separation", 10) var ml := Style.make_label("Modifier", Style.body(), 15, Style.INK) ml.size_flags_vertical = SIZE_SHRINK_CENTER mrow.add_child(ml) mrow.add_child(_mod) _mod_note = note("") _mod_note.size_flags_vertical = SIZE_SHRINK_CENTER mrow.add_child(_mod_note) var dice_box := VBoxContainer.new() dice_box.add_theme_constant_override("separation", 6) dice_box.add_child(drow) dice_box.add_child(mrow) _dice_row = dice_box type_box.add_child(dice_box) _type_note = note("") type_box.add_child(_type_note) _problem = note("", Style.WARN_INK) type_box.add_child(_problem) body.add_child(type_box) _type_box = type_box var card_box := VBoxContainer.new() card_box.add_theme_constant_override("separation", 6) card_box.add_child(section("Card mode")) card_box.add_child(segmented(["Off", "On"], 1 if _card_mode else 0, func(i: int) -> void: _card_mode = i == 1 _refresh() )) card_box.add_child(note("Results are dealt like cards, without repeats, until the deck runs out.")) var deck := VBoxContainer.new() deck.add_theme_constant_override("separation", 6) deck.add_child(section("When the deck runs out")) deck.add_child(segmented(["Reshuffle", "Stop"], 1 if _deck_empty == "stop" else 0, func(i: int) -> void: _deck_empty = "stop" if i == 1 else "reshuffle" _refresh() )) _deck_block = deck card_box.add_child(deck) body.add_child(card_box) _card_box = card_box var tpl := add_button("Save as template…", "plain", func() -> void: close() template_requested.emit() ) tpl.tooltip_text = "Save this card as a reusable template ({$name} in any text becomes a parameter)" var gap := Control.new() gap.size_flags_horizontal = SIZE_EXPAND_FILL footer.add_child(gap) add_button("Close", "plain", close) add_button("Apply", "primary", _on_apply) _refresh() func _ready() -> void: super._ready() # Now that the dialog sits in Main, the sheet preview can use its variables. _refresh() func _tool_button(text: String, tip: String, on_press: Callable) -> Button: var b := make_button(text, "plain") b.tooltip_text = tip b.custom_minimum_size.x = 36 b.add_theme_font_size_override("font_size", 14) b.pressed.connect(on_press) return b func _color_menu(anchor: Control) -> void: var pm := PopupMenu.new() style_popup(pm) for i in COLORS.size(): pm.add_item(COLORS[i][0], i) pm.id_pressed.connect(func(id: int) -> void: _wrap_selection("[color=%s]" % (COLORS[id][1] as Color).to_html(false), "[/color]") ) pm.popup_hide.connect(pm.queue_free) add_child(pm) pm.popup(Rect2i(Vector2i(anchor.get_screen_position()) + Vector2i(0, int(anchor.size.y)), Vector2i(0, 0))) ## Wraps the selected text in the tags, or inserts an empty pair at the caret. func _wrap_selection(open: String, close_tag: String) -> void: var r := TableSettingsRules.wrap_text(_details.text, _selection_range(), open, close_tag) _details.text = r["text"] _set_caret_at_offset(int(r["caret"])) _details.grab_focus() _refresh() func _selection_range() -> Vector2i: if _details.has_selection(): return Vector2i(_offset_of(_details.get_selection_from_line(), _details.get_selection_from_column()), _offset_of(_details.get_selection_to_line(), _details.get_selection_to_column())) var c := _offset_of(_details.get_caret_line(), _details.get_caret_column()) return Vector2i(c, c) func _offset_of(line: int, col: int) -> int: var off := 0 for i in line: off += _details.get_line(i).length() + 1 return off + col func _set_caret_at_offset(off: int) -> void: var line := 0 while line < _details.get_line_count() - 1 and off > _details.get_line(line).length(): off -= _details.get_line(line).length() + 1 line += 1 _details.set_caret_line(line) _details.set_caret_column(clampi(off, 0, _details.get_line(line).length())) func settings() -> Dictionary: var s := { "name": _name.text, "details": _details.text, "tags": _tags.text, "ranged": _ranged, "dice": _dice.text.strip_edges(), "dice_mod": _mod.text.strip_edges(), "card_mode": _card_mode, "deck_empty": _deck_empty, } _kind.add_settings(s) return s func _refresh() -> void: if _preview == null: return var det := _details.text _preview.text = Formatter.close_open_tags(det) if det.strip_edges() != "" else "[color=#69726b]Preview appears here.[/color]" _dice_row.visible = _ranged _deck_block.visible = _card_mode var s := settings() var dice_ok := not _ranged or TableSettingsRules.dice_ok_text(s["dice"]) _dice.add_theme_color_override("font_color", Style.INK if dice_ok else Style.WARN_INK) var msgs := PackedStringArray() if _ranged and not table.is_ranged(): msgs.append("Each entry gets a range sized by its weight; adjust them on the card.") if _ranged and TableSettingsRules.extra_pipe_count(table) > 0: msgs.append("A range takes one pipe, so extra pipes on a result will be dropped.") if not _ranged and table.is_ranged(): msgs.append("Ranges are removed; results become plain picks.") _type_note.text = " ".join(msgs) _type_note.visible = not msgs.is_empty() var mod_err := TableSettingsRules.mod_error(s["dice_mod"]) if _ranged else "" _mod_note.text = "e.g. @luck, -@fatigue, dm(@str)" if s["dice_mod"] != "" and mod_err == "": _mod_note.text = Describe.roll(_preview_table(s), null).trim_prefix("Roll ") var problem := "" if not dice_ok: problem = "“%s” is not a valid dice expression." % s["dice"] elif mod_err != "": problem = mod_err elif _type_box == null or _type_box.visible: problem = _preview_table(s).range_problems() _problem.text = problem _problem.visible = problem != "" if _type_box != null: _kind.refresh() if is_inside_tree(): _fit() func _on_apply() -> void: var s := settings() var r := TableSettingsRules.apply_settings(table, s) if not r["ok"]: if _kind.problem and not TableSettingsRules.kind_problems(table, s).is_empty(): _kind.problem.text = r["error"] _kind.problem.visible = true else: _problem.text = r["error"] _problem.visible = true return applied.emit(r) close() ## A copy of the table with the settings `s` applied, for live wording. func _preview_table(s: Dictionary) -> RollTable: var clone := RollTable.from_dict(table.to_dict()) TableSettingsRules.apply_settings(clone, s) return clone ## TableSettingsRules.apply_settings. static func apply_settings(t: RollTable, s: Dictionary) -> Dictionary: return TableSettingsRules.apply_settings(t, s) ## TableSettingsRules.when_error. static func when_error(src: String) -> String: return TableSettingsRules.when_error(src) ## TableSettingsRules.dice_ok_text. static func dice_ok_text(text: String) -> bool: return TableSettingsRules.dice_ok_text(text)