TableTower/ui/help_dialog.gd

153 lines
4.8 KiB
GDScript3
Raw Normal View History

class_name HelpDialog
extends ModalCard
## The Help window: topics down the left, the chosen topic on the right.
##
## The text lives in res://help/topics.txt, written in a small markup:
## # Title starts a topic
## ## Heading a heading inside a topic
## - item a bullet
## `code` rule text, shown as typed (brackets are safe inside)
## [topic]Title[/topic] a link to another topic
## Everything else is BBCode ([b], [i]) and blank lines split paragraphs.
## A topic's "Try a demo spread" link was clicked (key for DemoSpreads).
signal demo_requested(key: String)
const PATH := "res://help/topics.txt"
const LINK_COLOR := "#4f2b12"
const CODE_COLOR := "#8a4a1c"
static var _cache: Array = []
var current := ""
var text: RichTextLabel
var _buttons: Dictionary = {} # title -> Button
func _init(start: String = "") -> void:
super._init("Help", 920.0)
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 24)
body.add_child(row)
var list := VBoxContainer.new()
list.add_theme_constant_override("separation", 2)
list.custom_minimum_size.x = 220
row.add_child(list)
var group := ButtonGroup.new()
for t: Dictionary in topics():
var b := _topic_button(str(t["title"]), group)
list.add_child(b)
_buttons[t["title"]] = b
text = RichTextLabel.new()
text.bbcode_enabled = true
text.fit_content = true
text.selection_enabled = true
text.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
text.size_flags_horizontal = SIZE_EXPAND_FILL
text.add_theme_constant_override("line_separation", 3)
text.meta_clicked.connect(_on_meta)
row.add_child(text)
add_button("Close", "primary", close)
show_topic(start)
## Every topic, in file order: [{title, body}] with the body already turned
## into BBCode. Read once and cached.
static func topics() -> Array:
if _cache.is_empty():
_cache = parse(FileAccess.get_file_as_string(PATH))
if _cache.is_empty():
_cache = [{"title": "Help", "body": "The help text is missing (%s)." % PATH}]
return _cache
static func titles() -> Array[String]:
var out: Array[String] = []
for t: Dictionary in topics():
out.append(str(t["title"]))
return out
static func parse(source: String) -> Array:
var out: Array = []
var cur: Dictionary = {}
for line in source.split("\n"):
if line.begins_with("# "):
cur = {"title": line.substr(2).strip_edges(), "body": ""}
out.append(cur)
elif not cur.is_empty():
cur["body"] += _line(line) + "\n"
for t: Dictionary in out:
t["body"] = str(t["body"]).strip_edges()
return out
static func _line(line: String) -> String:
if line.begins_with("## "):
return "[font_size=19][b]%s[/b][/font_size]" % _inline(line.substr(3).strip_edges())
if line.begins_with("- "):
return "[indent]• %s[/indent]" % _inline(line.substr(2))
return _inline(line)
## `code` spans (brackets escaped) and [topic] links.
static func _inline(s: String) -> String:
var out := ""
var parts := s.split("`")
for i in parts.size():
if i % 2 == 1:
var code := ""
for ch in parts[i]:
code += "[lb]" if ch == "[" else ("[rb]" if ch == "]" else ch)
out += "[color=%s][b]%s[/b][/color]" % [CODE_COLOR, code]
else:
out += parts[i]
var dre := RegEx.create_from_string("\\[demo\\]([a-z_]+)\\[/demo\\]")
out = dre.sub(out, "[url=demo:$1][color=%s][b]▶ Try a demo spread[/b][/color][/url]" % LINK_COLOR, true)
var re := RegEx.create_from_string("\\[topic\\](.+?)\\[/topic\\]")
return re.sub(out, "[url=topic:$1][color=%s][u]$1[/u][/color][/url]" % LINK_COLOR, true)
func show_topic(title: String) -> void:
var all := topics()
var found: Dictionary = all[0]
for t: Dictionary in all:
if str(t["title"]).to_lower() == title.to_lower():
found = t
current = str(found["title"])
title_label.text = current
text.text = str(found["body"])
if _buttons.has(current):
(_buttons[current] as Button).set_pressed_no_signal(true)
_scroll.scroll_vertical = 0
func _on_meta(meta: Variant) -> void:
var s := str(meta)
if s.begins_with("topic:"):
show_topic(s.substr(6))
elif s.begins_with("demo:"):
close()
demo_requested.emit(s.substr(5))
## The window keeps one height while you move between topics.
func _fit() -> void:
card.custom_minimum_size.x = minf(_width, maxf(240.0, size.x - 32.0))
_scroll.custom_minimum_size.y = clampf(size.y - 200.0, 160.0, 640.0)
func _topic_button(title: String, group: ButtonGroup) -> Button:
var b := Button.new()
b.text = title
b.toggle_mode = true
b.button_group = group
b.alignment = HORIZONTAL_ALIGNMENT_LEFT
b.add_theme_font_size_override("font_size", 15)
var m := [10, 5, 10, 5]
var on := Style.box(Color(Style.ACCENT, 0.3), 6, m)
Style.skin_button(b, Style.box(Color(0, 0, 0, 0), 6, m), Style.box(Style.HOVER_ROW, 6, m), on, Style.INK)
b.add_theme_stylebox_override("hover_pressed", on)
b.pressed.connect(func() -> void: show_topic(title))
return b