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