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 <noreply@anthropic.com>
This commit is contained in:
RobinFiredrake 2026-11-14 14:32:46 -06:00
parent 05508e1c05
commit bd077ebadc
21 changed files with 206 additions and 1 deletions

View file

@ -20,6 +20,8 @@ const KNOWN_TAGS: Array[String] = [
"table", "cell", "ul", "ol", "lb", "rb", "br", "hr", "lrm", "rlm", "lre", "rle", "table", "cell", "ul", "ol", "lb", "rb", "br", "hr", "lrm", "rlm", "lre", "rle",
"lro", "rlo", "pdf", "alm", "lri", "rli", "fsi", "pdi", "zwj", "zwnj", "wj", "shy", "lro", "rlo", "pdf", "alm", "lri", "rli", "fsi", "pdi", "zwj", "zwnj", "wj", "shy",
"wave", "tornado", "shake", "fade", "rainbow", "pulse", "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. ## Tags that stand alone and never take a closing tag.

View file

@ -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}`. - `[[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. - `{@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. - `{= @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. [b]Quest Hooks[/b] in the sample uses all of these.

View file

@ -24,6 +24,7 @@ var _rolls: Dictionary = {}
func _init() -> void: func _init() -> void:
_test_expander() _test_expander()
_test_formatter() _test_formatter()
_test_format_menu()
finish() finish()
@ -206,3 +207,25 @@ func _test_formatter() -> void:
_eq(RollContext.normalize_name(" [b]Gems[/b] "), "gems", "normalize_name") _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()

93
ui/format_menu.gd Normal file
View file

@ -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

1
ui/format_menu.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://caw30roqdmare

View file

@ -39,6 +39,7 @@ func _init(start: String = "") -> void:
list.add_child(b) list.add_child(b)
_buttons[t["title"]] = b _buttons[t["title"]] = b
text = RichTextLabel.new() text = RichTextLabel.new()
RichText.install_effects(text)
text.bbcode_enabled = true text.bbcode_enabled = true
text.fit_content = true text.fit_content = true
text.selection_enabled = true text.selection_enabled = true

View file

@ -47,6 +47,7 @@ static func text_box(text: String, placeholder: String = "", min_h: float = 70.0
var te := TextEdit.new() var te := TextEdit.new()
te.text = text te.text = text
te.placeholder_text = placeholder te.placeholder_text = placeholder
FormatMenu.attach(te)
te.custom_minimum_size = Vector2(0, min_h) te.custom_minimum_size = Vector2(0, min_h)
te.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY te.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY
te.scroll_fit_content_height = true te.scroll_fit_content_height = true

View file

@ -68,6 +68,7 @@ static func section(insp: Inspector, t: RollTable, e: TableEntry) -> void:
if t.kind != RollTable.KIND_SHEET: if t.kind != RollTable.KIND_SHEET:
var le := InspectorKit.line(e.text, "What this result says") var le := InspectorKit.line(e.text, "What this result says")
FormatMenu.attach(le)
InspectorKit.tag(le, "res.text") InspectorKit.tag(le, "res.text")
le.text_changed.connect(insp.set_entry_text) le.text_changed.connect(insp.set_entry_text)
insp.fields["text"] = le insp.fields["text"] = le

View file

@ -15,11 +15,25 @@ static var _count_re: RegEx
static var _list_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. ## A RichTextLabel that sizes itself to its text like a Label.
## `zoomable` picks the card (MSDF) fonts that stay sharp on the zoomed board. ## `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: static func label(text: String, font_size: int, color: Color, zoomable: bool, wrap: bool = true, bold: bool = false) -> RichTextLabel:
var l := RichTextLabel.new() var l := RichTextLabel.new()
l.bbcode_enabled = true l.bbcode_enabled = true
install_effects(l)
l.fit_content = true l.fit_content = true
l.scroll_active = false l.scroll_active = false
l.meta_underlined = false l.meta_underlined = false

View file

@ -198,6 +198,7 @@ static func _field_label(field: String) -> String:
func _rich(bb: String, font: Font, fs: int) -> RichTextLabel: func _rich(bb: String, font: Font, fs: int) -> RichTextLabel:
var l := RichTextLabel.new() var l := RichTextLabel.new()
RichText.install_effects(l)
l.bbcode_enabled = true l.bbcode_enabled = true
l.fit_content = true l.fit_content = true
l.scroll_active = false l.scroll_active = false

View file

@ -175,6 +175,7 @@ func build() -> void:
_template.text_changed.connect(d._refresh) _template.text_changed.connect(d._refresh)
box.add_child(ModalCard.field("Template (BBCode with {@var}, {@prefix_*} and {= expr})", _template)) box.add_child(ModalCard.field("Template (BBCode with {@var}, {@prefix_*} and {= expr})", _template))
_template_preview = RichTextLabel.new() _template_preview = RichTextLabel.new()
RichText.install_effects(_template_preview)
_template_preview.bbcode_enabled = true _template_preview.bbcode_enabled = true
_template_preview.fit_content = true _template_preview.fit_content = true
_template_preview.scroll_active = false _template_preview.scroll_active = false

View file

@ -106,6 +106,7 @@ func add_cells(h: HBoxContainer, e: TableEntry, index: int, build: bool) -> void
func _build_cell(e: TableEntry, k: int) -> Dictionary: func _build_cell(e: TableEntry, k: int) -> Dictionary:
var edit := LineEdit.new() var edit := LineEdit.new()
edit.text = RollTable.cell_text(e, k) edit.text = RollTable.cell_text(e, k)
FormatMenu.attach(edit)
edit.placeholder_text = str(card.table.columns[k].get("name", "")) edit.placeholder_text = str(card.table.columns[k].get("name", ""))
edit.custom_minimum_size = Vector2(CELL_W, 0) edit.custom_minimum_size = Vector2(CELL_W, 0)
edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER

View file

@ -20,6 +20,7 @@ func text_edit(e: TableEntry, index: int, chosen: bool) -> LineEdit:
var table := card.table var table := card.table
var edit := LineEdit.new() var edit := LineEdit.new()
edit.text = e.text edit.text = e.text
FormatMenu.attach(edit)
if chosen: if chosen:
edit.placeholder_text = "(the option's name)" 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" 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"

View file

@ -68,6 +68,7 @@ func make_sheet_row(e: TableEntry, build: bool) -> Control:
row.add_child(v) row.add_child(v)
if build: if build:
var te := TextEdit.new() var te := TextEdit.new()
FormatMenu.attach(te)
te.text = e.text te.text = e.text
te.placeholder_text = "[b]Name[/b] {@name}\nHP {@hp} · Skills {@skill_*}" te.placeholder_text = "[b]Name[/b] {@name}\nHP {@hp} · Skills {@skill_*}"
te.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY te.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY

View file

@ -75,6 +75,21 @@ func _build() -> void:
tools.add_theme_constant_override("separation", 6) 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("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("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") var color := make_button("Color ▾", "plain")
color.pressed.connect(func() -> void: _color_menu(color)) color.pressed.connect(func() -> void: _color_menu(color))
color.add_theme_font_size_override("font_size", 14) color.add_theme_font_size_override("font_size", 14)
@ -85,6 +100,7 @@ func _build() -> void:
dbox.add_child(tools) dbox.add_child(tools)
dbox.add_child(_details) dbox.add_child(_details)
_preview = RichTextLabel.new() _preview = RichTextLabel.new()
RichText.install_effects(_preview)
_preview.bbcode_enabled = true _preview.bbcode_enabled = true
_preview.fit_content = true _preview.fit_content = true
_preview.scroll_active = false _preview.scroll_active = false

View file

@ -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

View file

@ -0,0 +1 @@
uid://wk8l2rnoa33t

View file

@ -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

View file

@ -0,0 +1 @@
uid://bvhru5ab24vgs

View file

@ -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

View file

@ -0,0 +1 @@
uid://cglhow121rffe