Profiling showed the delays come from Godot's own calls, not GDScript: - Right-click format menus are made on the first right-click, not for every text field at build time. - Dashed reference threads draw all their dashes in one call. - Inspector rebuilds keep the closed sections and only remake open ones. - Card rows are styled before they join the card. - A Play/Build switch rebuilds cards in view at once and the rest a few per frame (CardRefreshQueue); threads redraw only now and then meanwhile. Adds tests/perf_actions.gd, a timing tool for everyday actions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
106 lines
4.4 KiB
GDScript
106 lines
4.4 KiB
GDScript
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
|
|
|
|
|
|
## Gives `field` the formatting items, added the first time its menu is about
|
|
## to open: a menu with three submenus per field made long cards slow to build.
|
|
static func attach(field: Control) -> void:
|
|
field.gui_input.connect(_add_before_opening.bind(field))
|
|
|
|
|
|
## Runs before the field's own input handling, which opens the menu.
|
|
static func _add_before_opening(event: InputEvent, field: Control) -> void:
|
|
var click := event as InputEventMouseButton
|
|
var right_click := click != null and click.pressed and click.button_index == MOUSE_BUTTON_RIGHT
|
|
if right_click or event.is_action_pressed("ui_menu", true):
|
|
_add_to(field)
|
|
|
|
|
|
static func _add_to(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
|