TableTower/ui/sheet_text_box.gd

77 lines
3.2 KiB
GDScript3
Raw Normal View History

class_name SheetTextBox
extends RefCounted
## The Long notes box (SheetKinds.TEXT): formatted text with live {@values}
## and Lore Entry links (SheetLoreLinks). In Play, click it to edit the BBCode
## with a formatting toolbar above (SheetTextToolbar); click away to see the
## text again. Hovering a Lore Entry link shows its start; clicking it opens
## the entry in the side panel instead of editing.
static func make(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var stack := Control.new()
var shown := RichTextLabel.new()
shown.name = "Shown"
shown.bbcode_enabled = true
shown.scroll_active = true
shown.meta_underlined = false
RichText.install_effects(shown)
shown.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
shown.add_theme_color_override("default_color", ink)
shown.add_theme_font_override("normal_font", Style.body())
shown.text = render(ed, str(box.value if box.value != null else ""))
if shown.text == "" and not ed.building():
shown.text = "[i]Click to write…[/i]"
stack.add_child(shown)
var editing := VBoxContainer.new()
editing.name = "Editing"
editing.add_theme_constant_override("separation", 4)
editing.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
editing.visible = false
stack.add_child(editing)
var edit := TextEdit.new()
edit.name = "Edit"
edit.text = str(box.value if box.value != null else "")
edit.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY
edit.size_flags_vertical = Control.SIZE_EXPAND_FILL
# Keep the selection while a toolbar menu is open.
edit.deselect_on_focus_loss_enabled = false
ModalCard.style_text_edit(edit)
FormatMenu.attach(edit)
var toolbar := SheetTextToolbar.new(ed, box, edit, ink)
editing.add_child(toolbar)
editing.add_child(edit)
# The link under the mouse ("" = none): a click there opens it, not the editor.
var hovered := {"meta": ""}
shown.meta_hover_started.connect(func(meta: Variant) -> void:
hovered["meta"] = str(meta)
ed.m.details().show_meta(shown, str(meta)))
shown.meta_hover_ended.connect(func(_meta: Variant) -> void:
hovered["meta"] = ""
ed.m.details().hide_for(shown))
shown.meta_clicked.connect(func(meta: Variant) -> void: open_link(ed, str(meta)))
shown.gui_input.connect(func(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.pressed and mb.button_index == MOUSE_BUTTON_LEFT and not ed.building() and hovered["meta"] == "":
shown.visible = false
editing.visible = true
edit.grab_focus())
edit.text_changed.connect(func() -> void: ed.edit_value(box, edit.text))
toolbar.shown = shown
# Deferred on the toolbar: a box redrawn meanwhile drops the call.
edit.focus_exited.connect(func() -> void: toolbar.call_deferred("stop_editing"))
toolbar.menu_closed.connect(func() -> void:
if editing.visible:
edit.grab_focus())
return stack
## The text as shown: live values filled in, links in the reference colours.
static func render(ed: SheetEditor, text: String) -> String:
return RichText.decorate(SheetFieldViews.expand(ed, text))
## A click on a link: a Lore Entry opens in the side panel.
static func open_link(ed: SheetEditor, meta: String) -> void:
if meta.begins_with(SheetLoreLinks.META):
ed.details.open_lore(meta.substr(SheetLoreLinks.META.length()))