TableTower/ui/format_menu.gd

117 lines
4.7 KiB
GDScript3
Raw Permalink Normal View History

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)
_submenu(menu, "Colour", colours(), FIRST_ID + 100, field)
_submenu(menu, "Size", SIZES, FIRST_ID + 200, field)
_submenu(menu, "Effect", EFFECTS, FIRST_ID + 300, field)
## The colour presets as [label, open tag, close tag], like STYLES.
static func colours() -> Array:
var out: Array = []
for preset: Array in ColorChoice.PRESETS:
out.append([preset[0], "[color=%s]" % preset[1], "[/color]"])
return out
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 result := TableSettingsRules.wrap_text(edit.text, selection(edit), open, close_tag)
edit.text = str(result["text"])
edit.text_changed.emit()
## A TextEdit's selection as character offsets (from, to); both are the caret
## when nothing is selected.
static func selection(edit: TextEdit) -> Vector2i:
if edit.has_selection():
return 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 caret := _offset(edit, edit.get_caret_line(), edit.get_caret_column())
return Vector2i(caret, caret)
## 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