From bd077ebadcd2f528aef58b43c276cab7bbab4eae Mon Sep 17 00:00:00 2001 From: RobinFiredrake Date: Sat, 14 Nov 2026 14:32:46 -0600 Subject: [PATCH] Formatting: right-click Format menu in text fields, more toolbar buttons, sparkle/shimmer/flash Every text field's right-click menu gains Format: Bold, Italic, Underline, Strike-through, Colour, Size and Effect (Godot's wave, tornado, shake, rainbow, pulse, fade plus the app's own sparkle, shimmer and flash, drawn by RichTextEffects in ui/text_effects). The details toolbar adds U, S and Effect. Co-Authored-By: Claude Opus 5.5 --- core/formatter.gd | 2 + help/topics.txt | 3 +- tests/test_text.gd | 23 +++++++ ui/format_menu.gd | 93 +++++++++++++++++++++++++++ ui/format_menu.gd.uid | 1 + ui/help_dialog.gd | 1 + ui/inspector_kit.gd | 1 + ui/inspector_result.gd | 1 + ui/rich_text.gd | 14 ++++ ui/search_panel.gd | 1 + ui/settings_kind_fields.gd | 1 + ui/table_card_columns.gd | 1 + ui/table_card_editing.gd | 1 + ui/table_card_kind_rows.gd | 1 + ui/table_settings_dialog.gd | 16 +++++ ui/text_effects/flash_effect.gd | 14 ++++ ui/text_effects/flash_effect.gd.uid | 1 + ui/text_effects/shimmer_effect.gd | 14 ++++ ui/text_effects/shimmer_effect.gd.uid | 1 + ui/text_effects/sparkle_effect.gd | 16 +++++ ui/text_effects/sparkle_effect.gd.uid | 1 + 21 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 ui/format_menu.gd create mode 100644 ui/format_menu.gd.uid create mode 100644 ui/text_effects/flash_effect.gd create mode 100644 ui/text_effects/flash_effect.gd.uid create mode 100644 ui/text_effects/shimmer_effect.gd create mode 100644 ui/text_effects/shimmer_effect.gd.uid create mode 100644 ui/text_effects/sparkle_effect.gd create mode 100644 ui/text_effects/sparkle_effect.gd.uid diff --git a/core/formatter.gd b/core/formatter.gd index b1b5dba..a4b28f2 100644 --- a/core/formatter.gd +++ b/core/formatter.gd @@ -20,6 +20,8 @@ const KNOWN_TAGS: Array[String] = [ "table", "cell", "ul", "ol", "lb", "rb", "br", "hr", "lrm", "rlm", "lre", "rle", "lro", "rlo", "pdf", "alm", "lri", "rli", "fsi", "pdi", "zwj", "zwnj", "wj", "shy", "wave", "tornado", "shake", "fade", "rainbow", "pulse", + # The app's own effects (ui/text_effects). + "sparkle", "shimmer", "flash", ] ## Tags that stand alone and never take a closing tag. diff --git a/help/topics.txt b/help/topics.txt index e8161d0..dbfff23 100644 --- a/help/topics.txt +++ b/help/topics.txt @@ -169,7 +169,8 @@ Result text can do more than plain words: - `[[Traveler Names -> patron]]` also saves it as patron, so later text can say `{patron}`. - `{@town}` shows a value. `{@town|Towns}` shows it, or rolls Towns once and remembers the answer if it isn't set yet. - `{= @gold * 2}` shows a worked-out number. -- Formatting: `[b]bold[/b]` and `[i]italic[/i]`. +- Formatting: `[b]bold[/b]` and `[i]italic[/i]`. [b]Right-click[/b] any text field and use [b]Format[/b] to wrap the selected words: Bold, Italic, Underline, Strike-through, Colour, Size and Effect. +- Moving text, for things such as loot rarity: [wave]Wave[/wave], [tornado]Tornado[/tornado], [shake]Shake[/shake], [rainbow]Rainbow[/rainbow], [pulse]Pulse[/pulse], Fade, [sparkle]Sparkle[/sparkle], [shimmer]Shimmer[/shimmer] and [flash]Flash[/flash]. As text: `[sparkle]Starlit ring[/sparkle]`. [b]Quest Hooks[/b] in the sample uses all of these. diff --git a/tests/test_text.gd b/tests/test_text.gd index 5a55297..ac0fa20 100644 --- a/tests/test_text.gd +++ b/tests/test_text.gd @@ -24,6 +24,7 @@ var _rolls: Dictionary = {} func _init() -> void: _test_expander() _test_formatter() + _test_format_menu() finish() @@ -206,3 +207,25 @@ func _test_formatter() -> void: _eq(RollContext.normalize_name(" [b]Gems[/b] "), "gems", "normalize_name") + +## Right-click formatting and the app's own text effects. +func _test_format_menu() -> void: + var line := LineEdit.new() + line.text = "A legendary sword" + FormatMenu.attach(line) + var menu := line.get_menu() + var labels: Array[String] = [] + for i in menu.item_count: + labels.append(menu.get_item_text(i)) + _check(labels.has("Bold") and labels.has("Effect"), "a text field's right-click menu has Bold and Effect: %s" % ", ".join(labels)) + var changed := [] + line.text_changed.connect(func(text: String) -> void: changed.append(text)) + line.select(2, 11) + FormatMenu.wrap_selection(line, "[sparkle]", "[/sparkle]") + _eq(line.text, "A [sparkle]legendary[/sparkle] sword", "it wraps the selected words") + _eq(changed.size(), 1, "and tells the field's listeners") + _eq(Formatter.strip_tags(line.text), "A legendary sword", "the app's own effects count as tags (stripped from plain text)") + var label := RichText.label("[shimmer]Rare[/shimmer]", 15, Color.WHITE, false) + _eq(label.get_parsed_text(), "Rare", "a label draws the shimmer effect instead of showing its tag") + label.free() + line.free() diff --git a/ui/format_menu.gd b/ui/format_menu.gd new file mode 100644 index 0000000..324c7cf --- /dev/null +++ b/ui/format_menu.gd @@ -0,0 +1,93 @@ +class_name FormatMenu +extends RefCounted +## Right-click formatting for any text field (LineEdit or TextEdit): its own +## menu (cut, copy, paste…) gains Bold, Italic, Underline, Strike-through and +## Colour, Size and Effect submenus. Each wraps the selected text in BBCode +## (or puts empty tags at the cursor), then tells the field's listeners. +## InspectorKit attaches it to its fields; other fields call attach(). + +## Text effects: [label, open tag, close tag]. Godot draws the first six; the +## app draws sparkle, shimmer and flash (ui/text_effects, RichText). +const EFFECTS := [ + ["Wave", "[wave amp=30 freq=4]", "[/wave]"], + ["Tornado", "[tornado radius=3 freq=3]", "[/tornado]"], + ["Shake", "[shake rate=12 level=6]", "[/shake]"], + ["Rainbow", "[rainbow freq=0.4 sat=0.7 val=1]", "[/rainbow]"], + ["Pulse", "[pulse freq=1.5 color=#ffffff60]", "[/pulse]"], + ["Fade", "[fade start=0 length=12]", "[/fade]"], + ["Sparkle", "[sparkle]", "[/sparkle]"], + ["Shimmer", "[shimmer]", "[/shimmer]"], + ["Flash", "[flash color=#ffd24a]", "[/flash]"], +] +const STYLES := [ + ["Bold", "[b]", "[/b]"], + ["Italic", "[i]", "[/i]"], + ["Underline", "[u]", "[/u]"], + ["Strike-through", "[s]", "[/s]"], +] +const SIZES := [["Small", "[font_size=12]", "[/font_size]"], ["Big", "[font_size=22]", "[/font_size]"]] +## Menu ids start here, clear of the field's own items. +const FIRST_ID := 2000 + + +## Adds the formatting items to `field`'s right-click menu. +static func attach(field: Control) -> void: + var menu: PopupMenu = field.call("get_menu") if field.has_method("get_menu") else null + if menu == null or menu.has_meta("format_menu"): + return + menu.set_meta("format_menu", true) + menu.add_separator("Format") + _add_items(menu, STYLES, FIRST_ID, field) + var colours: Array = [] + for preset: Array in ColorChoice.PRESETS: + colours.append([preset[0], "[color=%s]" % preset[1], "[/color]"]) + _submenu(menu, "Colour", colours, FIRST_ID + 100, field) + _submenu(menu, "Size", SIZES, FIRST_ID + 200, field) + _submenu(menu, "Effect", EFFECTS, FIRST_ID + 300, field) + + +static func _submenu(menu: PopupMenu, label: String, items: Array, first_id: int, field: Control) -> void: + var sub := PopupMenu.new() + ModalCard.style_popup(sub) + _add_items(sub, items, first_id, field) + menu.add_submenu_node_item(label, sub) + + +static func _add_items(menu: PopupMenu, items: Array, first_id: int, field: Control) -> void: + for i in items.size(): + menu.add_item(str(items[i][0]), first_id + i) + menu.id_pressed.connect(func(id: int) -> void: + var at := id - first_id + if at >= 0 and at < items.size(): + wrap_selection(field, str(items[at][1]), str(items[at][2]))) + + +## Wraps the field's selection (or the cursor) in `open` … `close_tag`. +static func wrap_selection(field: Control, open: String, close_tag: String) -> void: + if field is LineEdit: + var line := field as LineEdit + var sel := Vector2i(line.caret_column, line.caret_column) + if line.has_selection(): + sel = Vector2i(line.get_selection_from_column(), line.get_selection_to_column()) + var result := TableSettingsRules.wrap_text(line.text, sel, open, close_tag) + line.text = str(result["text"]) + line.caret_column = int(result["caret"]) + line.text_changed.emit(line.text) + elif field is TextEdit: + var edit := field as TextEdit + var sel := Vector2i(_offset(edit, edit.get_caret_line(), edit.get_caret_column()), 0) + sel.y = sel.x + if edit.has_selection(): + sel = Vector2i(_offset(edit, edit.get_selection_from_line(), edit.get_selection_from_column()), + _offset(edit, edit.get_selection_to_line(), edit.get_selection_to_column())) + var result := TableSettingsRules.wrap_text(edit.text, sel, open, close_tag) + edit.text = str(result["text"]) + edit.text_changed.emit() + + +## The character offset of a line and column in a TextEdit. +static func _offset(edit: TextEdit, line: int, column: int) -> int: + var offset := 0 + for i in line: + offset += edit.get_line(i).length() + 1 + return offset + column diff --git a/ui/format_menu.gd.uid b/ui/format_menu.gd.uid new file mode 100644 index 0000000..58b3a3d --- /dev/null +++ b/ui/format_menu.gd.uid @@ -0,0 +1 @@ +uid://caw30roqdmare diff --git a/ui/help_dialog.gd b/ui/help_dialog.gd index 5d1b0b5..bf511c5 100644 --- a/ui/help_dialog.gd +++ b/ui/help_dialog.gd @@ -39,6 +39,7 @@ func _init(start: String = "") -> void: list.add_child(b) _buttons[t["title"]] = b text = RichTextLabel.new() + RichText.install_effects(text) text.bbcode_enabled = true text.fit_content = true text.selection_enabled = true diff --git a/ui/inspector_kit.gd b/ui/inspector_kit.gd index e80c1ce..dac5fe5 100644 --- a/ui/inspector_kit.gd +++ b/ui/inspector_kit.gd @@ -47,6 +47,7 @@ static func text_box(text: String, placeholder: String = "", min_h: float = 70.0 var te := TextEdit.new() te.text = text te.placeholder_text = placeholder + FormatMenu.attach(te) te.custom_minimum_size = Vector2(0, min_h) te.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY te.scroll_fit_content_height = true diff --git a/ui/inspector_result.gd b/ui/inspector_result.gd index 3097d46..a355331 100644 --- a/ui/inspector_result.gd +++ b/ui/inspector_result.gd @@ -68,6 +68,7 @@ static func section(insp: Inspector, t: RollTable, e: TableEntry) -> void: if t.kind != RollTable.KIND_SHEET: var le := InspectorKit.line(e.text, "What this result says") + FormatMenu.attach(le) InspectorKit.tag(le, "res.text") le.text_changed.connect(insp.set_entry_text) insp.fields["text"] = le diff --git a/ui/rich_text.gd b/ui/rich_text.gd index 9746f71..e3e975c 100644 --- a/ui/rich_text.gd +++ b/ui/rich_text.gd @@ -15,11 +15,25 @@ static var _count_re: RegEx static var _list_re: RegEx +## The app's own text effects ([sparkle], [shimmer], [flash]), made once. +static var _effects: Array[RichTextEffect] = [] + + +## Lets `l` draw the app's own text effects (Godot's wave, tornado, shake, +## rainbow, pulse and fade work everywhere already). +static func install_effects(l: RichTextLabel) -> void: + if _effects.is_empty(): + _effects = [SparkleEffect.new(), ShimmerEffect.new(), FlashEffect.new()] + for effect in _effects: + l.install_effect(effect) + + ## A RichTextLabel that sizes itself to its text like a Label. ## `zoomable` picks the card (MSDF) fonts that stay sharp on the zoomed board. static func label(text: String, font_size: int, color: Color, zoomable: bool, wrap: bool = true, bold: bool = false) -> RichTextLabel: var l := RichTextLabel.new() l.bbcode_enabled = true + install_effects(l) l.fit_content = true l.scroll_active = false l.meta_underlined = false diff --git a/ui/search_panel.gd b/ui/search_panel.gd index 9a56995..3829d2d 100644 --- a/ui/search_panel.gd +++ b/ui/search_panel.gd @@ -198,6 +198,7 @@ static func _field_label(field: String) -> String: func _rich(bb: String, font: Font, fs: int) -> RichTextLabel: var l := RichTextLabel.new() + RichText.install_effects(l) l.bbcode_enabled = true l.fit_content = true l.scroll_active = false diff --git a/ui/settings_kind_fields.gd b/ui/settings_kind_fields.gd index 5888098..fa85365 100644 --- a/ui/settings_kind_fields.gd +++ b/ui/settings_kind_fields.gd @@ -175,6 +175,7 @@ func build() -> void: _template.text_changed.connect(d._refresh) box.add_child(ModalCard.field("Template (BBCode with {@var}, {@prefix_*} and {= expr})", _template)) _template_preview = RichTextLabel.new() + RichText.install_effects(_template_preview) _template_preview.bbcode_enabled = true _template_preview.fit_content = true _template_preview.scroll_active = false diff --git a/ui/table_card_columns.gd b/ui/table_card_columns.gd index d02e78c..89803fb 100644 --- a/ui/table_card_columns.gd +++ b/ui/table_card_columns.gd @@ -106,6 +106,7 @@ func add_cells(h: HBoxContainer, e: TableEntry, index: int, build: bool) -> void func _build_cell(e: TableEntry, k: int) -> Dictionary: var edit := LineEdit.new() edit.text = RollTable.cell_text(e, k) + FormatMenu.attach(edit) edit.placeholder_text = str(card.table.columns[k].get("name", "")) edit.custom_minimum_size = Vector2(CELL_W, 0) edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER diff --git a/ui/table_card_editing.gd b/ui/table_card_editing.gd index ba61e63..faddfbe 100644 --- a/ui/table_card_editing.gd +++ b/ui/table_card_editing.gd @@ -20,6 +20,7 @@ func text_edit(e: TableEntry, index: int, chosen: bool) -> LineEdit: var table := card.table var edit := LineEdit.new() edit.text = e.text + FormatMenu.attach(edit) if chosen: edit.placeholder_text = "(the option's name)" edit.tooltip_text = "The result text. Leave it as Chosen (or empty) to show the picked option's name; {option_label}, {option_value} and {@[option]} work" diff --git a/ui/table_card_kind_rows.gd b/ui/table_card_kind_rows.gd index 9c9cbfa..eab93da 100644 --- a/ui/table_card_kind_rows.gd +++ b/ui/table_card_kind_rows.gd @@ -68,6 +68,7 @@ func make_sheet_row(e: TableEntry, build: bool) -> Control: row.add_child(v) if build: var te := TextEdit.new() + FormatMenu.attach(te) te.text = e.text te.placeholder_text = "[b]Name[/b] {@name}\nHP {@hp} · Skills {@skill_*}" te.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY diff --git a/ui/table_settings_dialog.gd b/ui/table_settings_dialog.gd index 9de3e34..1c14684 100644 --- a/ui/table_settings_dialog.gd +++ b/ui/table_settings_dialog.gd @@ -75,6 +75,21 @@ func _build() -> void: 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]"))) + tools.add_child(_tool_button("U", "Underline", func() -> void: _wrap_selection("[u]", "[/u]"))) + tools.add_child(_tool_button("S", "Strike-through", func() -> void: _wrap_selection("[s]", "[/s]"))) + var effect := make_button("Effect ▾", "plain") + effect.tooltip_text = "Moving text: wave, shake, rainbow, sparkle…" + effect.add_theme_font_size_override("font_size", 14) + effect.pressed.connect(func() -> void: + var pm := PopupMenu.new() + style_popup(pm) + for i in FormatMenu.EFFECTS.size(): + pm.add_item(str(FormatMenu.EFFECTS[i][0]), i) + pm.id_pressed.connect(func(id: int) -> void: _wrap_selection(str(FormatMenu.EFFECTS[id][1]), str(FormatMenu.EFFECTS[id][2]))) + pm.popup_hide.connect(pm.queue_free.call_deferred) + add_child(pm) + pm.popup(Rect2i(Vector2i(effect.get_screen_position() + Vector2(0, effect.size.y)), Vector2i.ZERO))) + tools.add_child(effect) var color := make_button("Color ▾", "plain") color.pressed.connect(func() -> void: _color_menu(color)) color.add_theme_font_size_override("font_size", 14) @@ -85,6 +100,7 @@ func _build() -> void: dbox.add_child(tools) dbox.add_child(_details) _preview = RichTextLabel.new() + RichText.install_effects(_preview) _preview.bbcode_enabled = true _preview.fit_content = true _preview.scroll_active = false diff --git a/ui/text_effects/flash_effect.gd b/ui/text_effects/flash_effect.gd new file mode 100644 index 0000000..a2d714c --- /dev/null +++ b/ui/text_effects/flash_effect.gd @@ -0,0 +1,14 @@ +class_name FlashEffect +extends RichTextEffect +## [flash]…[/flash]: the text snaps between its own colour and another one. +## [flash color=#ffd24a freq=1.5] sets the other colour and how often. + +var bbcode := "flash" + + +func _process_custom_fx(char_fx: CharFXTransform) -> bool: + var freq := float(char_fx.env.get("freq", 1.5)) + var other := Color.from_string(str(char_fx.env.get("color", "#ffd24a")), Color("#ffd24a")) + if fmod(char_fx.elapsed_time * freq, 1.0) < 0.5: + char_fx.color = Color(other, char_fx.color.a) + return true diff --git a/ui/text_effects/flash_effect.gd.uid b/ui/text_effects/flash_effect.gd.uid new file mode 100644 index 0000000..7224c46 --- /dev/null +++ b/ui/text_effects/flash_effect.gd.uid @@ -0,0 +1 @@ +uid://wk8l2rnoa33t diff --git a/ui/text_effects/shimmer_effect.gd b/ui/text_effects/shimmer_effect.gd new file mode 100644 index 0000000..15246f1 --- /dev/null +++ b/ui/text_effects/shimmer_effect.gd @@ -0,0 +1,14 @@ +class_name ShimmerEffect +extends RichTextEffect +## [shimmer]…[/shimmer]: a band of golden light sweeps across the text, left to right. +## [shimmer speed=1.0] sets how fast. + +var bbcode := "shimmer" + + +func _process_custom_fx(char_fx: CharFXTransform) -> bool: + var speed := float(char_fx.env.get("speed", 1.0)) + var sweep := fmod(char_fx.elapsed_time * speed * 10.0, 24.0) - 4.0 + var near := clampf(1.0 - absf(float(char_fx.relative_index) - sweep) / 3.0, 0.0, 1.0) + char_fx.color = char_fx.color.lerp(Color(1.0, 0.85, 0.35, char_fx.color.a), near * 0.8) + return true diff --git a/ui/text_effects/shimmer_effect.gd.uid b/ui/text_effects/shimmer_effect.gd.uid new file mode 100644 index 0000000..91a3add --- /dev/null +++ b/ui/text_effects/shimmer_effect.gd.uid @@ -0,0 +1 @@ +uid://bvhru5ab24vgs diff --git a/ui/text_effects/sparkle_effect.gd b/ui/text_effects/sparkle_effect.gd new file mode 100644 index 0000000..a7f0fe9 --- /dev/null +++ b/ui/text_effects/sparkle_effect.gd @@ -0,0 +1,16 @@ +class_name SparkleEffect +extends RichTextEffect +## [sparkle]…[/sparkle]: letters glint gold at random moments, like gems (gold +## shows on light and dark cards alike). +## [sparkle freq=2.0] sets how often. + +var bbcode := "sparkle" + + +func _process_custom_fx(char_fx: CharFXTransform) -> bool: + var freq := float(char_fx.env.get("freq", 2.0)) + # Each letter has its own rhythm, so the glints don't line up. + var phase := fmod(char_fx.elapsed_time * freq + float(char_fx.relative_index) * 0.618, 1.0) + var glint := clampf(1.0 - absf(phase - 0.5) * 8.0, 0.0, 1.0) + char_fx.color = char_fx.color.lerp(Color(1.0, 0.78, 0.2, char_fx.color.a), glint * 0.9) + return true diff --git a/ui/text_effects/sparkle_effect.gd.uid b/ui/text_effects/sparkle_effect.gd.uid new file mode 100644 index 0000000..7094dc0 --- /dev/null +++ b/ui/text_effects/sparkle_effect.gd.uid @@ -0,0 +1 @@ +uid://cglhow121rffe