206 lines
7.4 KiB
GDScript3
206 lines
7.4 KiB
GDScript3
|
|
class_name RichText
|
|||
|
|
extends RefCounted
|
|||
|
|
## BBCode display helpers: RichTextLabels in the app's fonts, a readable preview
|
|||
|
|
## of entry text with [[references]] and {variables}, and colouring for
|
|||
|
|
## TextExpander's internal markers in rolled results.
|
|||
|
|
|
|||
|
|
## Marker shown after items that have hover details.
|
|||
|
|
const INFO_MARK := "ⓘ"
|
|||
|
|
|
|||
|
|
const _VAR_PATTERN := "\\{(@?[A-Za-z_][A-Za-z0-9_]*)(?:\\|([^{}\\[\\]\\n]+))?\\}"
|
|||
|
|
const _COUNT_PATTERN := "^(?:(\\d+)\\s*[x×]|(\\d*[dD]\\d+(?:\\s*[+-]\\s*\\d+)?))\\s+(\\S.*)$"
|
|||
|
|
|
|||
|
|
static var _var_re: RegEx
|
|||
|
|
static var _count_re: RegEx
|
|||
|
|
|
|||
|
|
## Colours for marker text, on cards (light) and on the felt (dark).
|
|||
|
|
const REF_INK := Color("#1d6656")
|
|||
|
|
const REF_BG := Color("#d6e9e2")
|
|||
|
|
const VAR_INK := Color("#7d5a0c")
|
|||
|
|
const REF_INK_FELT := Color("#9fdcc9")
|
|||
|
|
const VAR_INK_FELT := Color("#f3c450")
|
|||
|
|
const BAD_INK_FELT := Color("#ffb1a3")
|
|||
|
|
|
|||
|
|
|
|||
|
|
## A RichTextLabel that sizes itself to its text like a Label.
|
|||
|
|
## `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:
|
|||
|
|
var l := RichTextLabel.new()
|
|||
|
|
l.bbcode_enabled = true
|
|||
|
|
l.fit_content = true
|
|||
|
|
l.scroll_active = false
|
|||
|
|
l.meta_underlined = false
|
|||
|
|
l.hint_underlined = false
|
|||
|
|
l.shortcut_keys_enabled = false
|
|||
|
|
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART if wrap else TextServer.AUTOWRAP_OFF
|
|||
|
|
l.mouse_filter = Control.MOUSE_FILTER_PASS
|
|||
|
|
set_fonts(l, font_size, zoomable, bold)
|
|||
|
|
l.add_theme_color_override("default_color", color)
|
|||
|
|
l.text = text
|
|||
|
|
return l
|
|||
|
|
|
|||
|
|
|
|||
|
|
## Sets every font slot of a RichTextLabel. `bold` makes the normal text bold
|
|||
|
|
## (used for a lit row).
|
|||
|
|
static func set_fonts(l: RichTextLabel, font_size: int, zoomable: bool, bold: bool = false) -> void:
|
|||
|
|
l.add_theme_font_override("normal_font", Style.body_bold(zoomable) if bold else Style.body(zoomable))
|
|||
|
|
l.add_theme_font_override("bold_font", Style.body_bold(zoomable))
|
|||
|
|
l.add_theme_font_override("italics_font", Style.body_italic(zoomable))
|
|||
|
|
l.add_theme_font_override("bold_italics_font", Style.body_bold(zoomable))
|
|||
|
|
for slot in ["normal_font_size", "bold_font_size", "italics_font_size", "bold_italics_font_size", "mono_font_size"]:
|
|||
|
|
l.add_theme_font_size_override(slot, font_size)
|
|||
|
|
|
|||
|
|
|
|||
|
|
## Same as label() but in the Rokkitt display face (result chips).
|
|||
|
|
static func display_label(text: String, font_size: int, color: Color, zoomable: bool = false) -> RichTextLabel:
|
|||
|
|
var l := label(text, font_size, color, zoomable, false)
|
|||
|
|
var f := Style.display(zoomable)
|
|||
|
|
l.add_theme_font_override("normal_font", f)
|
|||
|
|
l.add_theme_font_override("bold_font", Style.display(zoomable, 900))
|
|||
|
|
return l
|
|||
|
|
|
|||
|
|
|
|||
|
|
## Connects a label's [url] metas to the details pop-up.
|
|||
|
|
static func hook_hover(l: RichTextLabel, main: Main) -> void:
|
|||
|
|
l.meta_hover_started.connect(func(meta: Variant) -> void:
|
|||
|
|
main.details().show_meta(l, str(meta)))
|
|||
|
|
l.meta_hover_ended.connect(func(_meta: Variant) -> void:
|
|||
|
|
main.details().hide_for(l))
|
|||
|
|
# A click (or a tap, where there's no hover) opens the same pop-up.
|
|||
|
|
l.meta_clicked.connect(func(meta: Variant) -> void:
|
|||
|
|
main.details().show_meta(l, str(meta)))
|
|||
|
|
|
|||
|
|
|
|||
|
|
## Small muted ⓘ appended to text that has hover details.
|
|||
|
|
static func info_mark(on_felt: bool = false) -> String:
|
|||
|
|
var c := Style.ON_FELT_MUTED if on_felt else Color("#9a9483")
|
|||
|
|
return " [font_size=12][color=#%s]%s[/color][/font_size]" % [c.to_html(false), INFO_MARK]
|
|||
|
|
|
|||
|
|
|
|||
|
|
## Escapes text so it shows exactly as typed.
|
|||
|
|
static func plain(text: String) -> String:
|
|||
|
|
return text.replace("[", "[lb]")
|
|||
|
|
|
|||
|
|
|
|||
|
|
## Readable BBCode for an entry's text as written (not rolled): each [[reference]]
|
|||
|
|
## becomes a small teal chip with the table's name, {variables} show in brass,
|
|||
|
|
## and a missing table shows the warning marker. References are wrapped in
|
|||
|
|
## [url=table:id] so hovering them shows the table's details.
|
|||
|
|
static func preview(text: String, board: TowerBoard) -> String:
|
|||
|
|
if not text.contains("[[") and not text.contains("{"):
|
|||
|
|
return text
|
|||
|
|
var out := ""
|
|||
|
|
var i := 0
|
|||
|
|
var n := text.length()
|
|||
|
|
while i < n:
|
|||
|
|
var c := text[i]
|
|||
|
|
if c == "[" and i + 1 < n and text[i + 1] == "[":
|
|||
|
|
var close := text.find("]]", i + 2)
|
|||
|
|
var body := text.substr(i + 2, close - i - 2) if close >= 0 else ""
|
|||
|
|
if close >= 0 and body.strip_edges() != "" and not body.contains("[") and not body.contains("]") and not body.contains("\n"):
|
|||
|
|
out += _ref_chip(body, board)
|
|||
|
|
i = close + 2
|
|||
|
|
continue
|
|||
|
|
out += c
|
|||
|
|
i += 1
|
|||
|
|
continue
|
|||
|
|
if c == "[":
|
|||
|
|
var tag := Formatter.tag_at(text, i)
|
|||
|
|
if not tag.is_empty():
|
|||
|
|
out += tag["raw"]
|
|||
|
|
i += (tag["raw"] as String).length()
|
|||
|
|
continue
|
|||
|
|
elif c == "{":
|
|||
|
|
var m := _vre().search(text, i)
|
|||
|
|
if m != null and m.get_start() == i:
|
|||
|
|
var vname := m.get_string(1)
|
|||
|
|
var shown := "{%s}" % vname
|
|||
|
|
out += "[url=var:%s][color=#%s]%s[/color][/url]" % [vname, VAR_INK.to_html(false), shown]
|
|||
|
|
var from := m.get_string(2).strip_edges()
|
|||
|
|
if from != "":
|
|||
|
|
var id := Roller.resolve(board, from)
|
|||
|
|
if id == "":
|
|||
|
|
out += " " + _bad(TextExpander.missing_marker(from), false)
|
|||
|
|
i = m.get_end()
|
|||
|
|
continue
|
|||
|
|
out += c
|
|||
|
|
i += 1
|
|||
|
|
return Formatter.close_open_tags(out)
|
|||
|
|
|
|||
|
|
|
|||
|
|
static func _ref_chip(body: String, board: TowerBoard) -> String:
|
|||
|
|
var name := body
|
|||
|
|
var save := ""
|
|||
|
|
var arrow := body.rfind("->")
|
|||
|
|
if arrow >= 0:
|
|||
|
|
save = body.substr(arrow + 2).strip_edges()
|
|||
|
|
name = body.substr(0, arrow)
|
|||
|
|
name = name.strip_edges()
|
|||
|
|
var count := ""
|
|||
|
|
var id := Roller.resolve(board, name)
|
|||
|
|
if id == "":
|
|||
|
|
var m := _cre().search(name)
|
|||
|
|
if m != null:
|
|||
|
|
count = (m.get_string(1) + "×") if m.get_string(1) != "" else (m.get_string(2) + "×")
|
|||
|
|
name = m.get_string(3).strip_edges()
|
|||
|
|
id = Roller.resolve(board, name)
|
|||
|
|
if id == "":
|
|||
|
|
return _bad(TextExpander.missing_marker(name), false)
|
|||
|
|
var t := board.get_table(id)
|
|||
|
|
var label_text := count + plain(Formatter.strip_tags(t.name))
|
|||
|
|
if save != "":
|
|||
|
|
label_text += " → " + plain(save)
|
|||
|
|
return "[url=table:%s][bgcolor=#%s][color=#%s] %s [/color][/bgcolor][/url]" % [id, REF_BG.to_html(false), REF_INK.to_html(false), label_text]
|
|||
|
|
|
|||
|
|
|
|||
|
|
## Colours the internal markers of rolled text: table rolls in teal, variables in
|
|||
|
|
## brass, and missing / unset / too-deep markers in red, so they're visible.
|
|||
|
|
static func decorate(bbcode: String, on_felt: bool = false) -> String:
|
|||
|
|
if not bbcode.contains("[url="):
|
|||
|
|
return bbcode
|
|||
|
|
var out := ""
|
|||
|
|
var open: Array[String] = []
|
|||
|
|
for t: Dictionary in Formatter.tokenize(bbcode):
|
|||
|
|
if t["name"] == "url":
|
|||
|
|
if t["kind"] == "open":
|
|||
|
|
var meta := (t["param"] as String).trim_prefix("=").strip_edges().trim_prefix("\"").trim_suffix("\"")
|
|||
|
|
var color := _marker_color(meta, on_felt)
|
|||
|
|
out += t["raw"]
|
|||
|
|
if color != "":
|
|||
|
|
out += "[color=#%s]" % color
|
|||
|
|
open.append(color)
|
|||
|
|
continue
|
|||
|
|
elif not open.is_empty():
|
|||
|
|
if open.pop_back() != "":
|
|||
|
|
out += "[/color]"
|
|||
|
|
out += t["raw"]
|
|||
|
|
return out
|
|||
|
|
|
|||
|
|
|
|||
|
|
static func _marker_color(meta: String, on_felt: bool) -> String:
|
|||
|
|
if meta.begins_with("table:"):
|
|||
|
|
return (REF_INK_FELT if on_felt else REF_INK).to_html(false)
|
|||
|
|
if meta.begins_with("var:"):
|
|||
|
|
return (VAR_INK_FELT if on_felt else VAR_INK).to_html(false)
|
|||
|
|
if meta.begins_with("missing:") or meta.begins_with("unset:") or meta.begins_with("deep:"):
|
|||
|
|
return (BAD_INK_FELT if on_felt else Style.DANGER_INK).to_html(false)
|
|||
|
|
return ""
|
|||
|
|
|
|||
|
|
|
|||
|
|
static func _bad(marker: String, on_felt: bool) -> String:
|
|||
|
|
return decorate(marker, on_felt)
|
|||
|
|
|
|||
|
|
|
|||
|
|
static func _vre() -> RegEx:
|
|||
|
|
if _var_re == null:
|
|||
|
|
_var_re = RegEx.new()
|
|||
|
|
_var_re.compile(_VAR_PATTERN)
|
|||
|
|
return _var_re
|
|||
|
|
|
|||
|
|
|
|||
|
|
static func _cre() -> RegEx:
|
|||
|
|
if _count_re == null:
|
|||
|
|
_count_re = RegEx.new()
|
|||
|
|
_count_re.compile(_COUNT_PATTERN)
|
|||
|
|
return _count_re
|