Style's colours become static vars named by role (BG, PANEL, CARD, THREAD, ACCENT, TRIM, GLOW…) instead of materials (felt, copper, brass), so a theme can replace them. Stray hard-coded colours moved into tokens. ThemeStore applies built-in sets (Tarot, the new default: jewel-purple cloth with a shine; Felt, the old look) and the user's own sets from user://themes/. Fills for the background and panels can be gradients; the background pattern (dots, stars, none) and logo are per set. View ▾ → Theme… edits a set; changes to a built-in save as a new set. Using a theme reloads the UI and keeps the open spread. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
223 lines
8.3 KiB
GDScript
223 lines
8.3 KiB
GDScript
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
|
||
static var _list_re: RegEx
|
||
|
||
|
||
## 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_BG_MUTED if on_felt else Style.FAINT
|
||
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 == "{":
|
||
# {= expr}: a computed value, shown as written (red if it can't parse).
|
||
if i + 1 < n and text[i + 1] == "=":
|
||
var close := text.find("}", i + 2)
|
||
var nl := text.find("\n", i + 2)
|
||
if close >= 0 and (nl < 0 or close < nl):
|
||
var src := text.substr(i + 2, close - i - 2).strip_edges()
|
||
var err := Expr.check(src)
|
||
var ink := Style.DANGER_INK if err != "" else Style.VALUE_INK
|
||
out += "[url=%s][color=#%s]{= %s}[/color][/url]" % ["error:expr" if err != "" else "expr:" + plain(src).replace("]", ""),
|
||
ink.to_html(false), plain(src)]
|
||
i = close + 1
|
||
continue
|
||
# {@prefix_*}: a list of every variable with the prefix.
|
||
var lm := _lre().search(text, i)
|
||
if lm != null and lm.get_start() == i:
|
||
out += "[url=var:@%s*][color=#%s]{@%s*}[/color][/url]" % [lm.get_string(1), Style.VALUE_INK.to_html(false), lm.get_string(1)]
|
||
i = lm.get_end()
|
||
continue
|
||
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, Style.VALUE_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, Style.REF_BG.to_html(false), Style.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 (Style.REF_INK_ON_BG if on_felt else Style.REF_INK).to_html(false)
|
||
if meta.begins_with("var:"):
|
||
return (Style.VALUE_INK_ON_BG if on_felt else Style.VALUE_INK).to_html(false)
|
||
if meta.begins_with("missing:") or meta.begins_with("unset:") or meta.begins_with("deep:") or meta.begins_with("error:"):
|
||
return (Style.BAD_INK_ON_BG 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 _lre() -> RegEx:
|
||
if _list_re == null:
|
||
_list_re = RegEx.new()
|
||
_list_re.compile("\\{@([A-Za-z0-9_]*)\\*\\}")
|
||
return _list_re
|
||
|
||
|
||
static func _cre() -> RegEx:
|
||
if _count_re == null:
|
||
_count_re = RegEx.new()
|
||
_count_re.compile(_COUNT_PATTERN)
|
||
return _count_re
|