TableTower/ui/choice_preview.gd

188 lines
6.2 KiB
GDScript3
Raw Permalink Normal View History

class_name ChoicePreview
extends RefCounted
## The hover/focus preview panel beside a ChoiceDialog: the option's details,
## effects and a TablePreview for each table it pipes to or mentions.
const PREVIEW_WIDTH := 350.0
var d: ChoiceDialog
var hover := -1
var shown_for := -2
func _init(dialog: ChoiceDialog) -> void:
d = dialog
func set_hover(k: int) -> void:
hover = k
refresh()
## Shows the preview for the hovered option, else the focused one, else (for
## number and text questions) where the Answer leads.
func refresh() -> void:
if d.minimized or d.preview_panel == null or not d.is_inside_tree():
return
var idx := -1
if d.kind == "pick":
idx = hover
if idx < 0:
for k in d._options.size():
if d._options[k].has_focus():
idx = k
break
else:
idx = 0
if idx == shown_for:
return
shown_for = idx
for c in d._preview_box.get_children():
d._preview_box.remove_child(c)
c.queue_free()
var content := preview_for_option(idx) if idx >= 0 else null
if content == null:
d.preview_panel.hide()
return
d._preview_box.add_child(content)
d.preview_panel.show()
place()
func hide() -> void:
shown_for = -2
if d.preview_panel:
d.preview_panel.hide()
## The entry behind option `k` (a position in the request's options; for
## number and text questions, the single Answer entry), or null.
func _entry_for(k: int) -> TableEntry:
if d.board == null:
return null
var t := d.board.get_table(str(d.request.get("table_id", "")))
if t == null:
return null
var index := k
if d.kind == "pick":
var opts: Array = d.request.get("options", [])
if k < 0 or k >= opts.size():
return null
var o: Dictionary = opts[k]
index = int(o.get("entry", o.get("index", k)))
return t.entries[index] if index >= 0 and index < t.entries.size() else null
## The preview content for option `k` as a Control, or null when there is
## nothing to say. It holds the option's details and effects, then a
## TablePreview for each table it pipes to (with its count) and each table
## its text mentions with [[Table]].
func preview_for_option(k: int) -> Control:
var e := _entry_for(k)
if e == null:
return null
var box := VBoxContainer.new()
box.add_theme_constant_override("separation", 8)
box.mouse_filter = Control.MOUSE_FILTER_IGNORE
box.custom_minimum_size.x = PREVIEW_WIDTH - 28.0
var heading := d.section(("Option %d" % (k + 1)) if d.kind == "pick" else "After you answer")
heading.mouse_filter = Control.MOUSE_FILTER_IGNORE
box.add_child(heading)
var any := false
var o: Dictionary = (d.request.get("options", []) as Array)[k] if d.kind == "pick" else {}
var t := d.board.get_table(str(d.request.get("table_id", "")))
if o.get("dynamic", false):
# The variable (or list item) behind a dynamic option, and its value.
any = true
var what := "option = %s" % str(o.get("option", ""))
if str(o.get("value_text", "")) != "":
what += " · value %s" % str(o["value_text"])
if d.plain:
var src := RulesView.options_line(t, d.board, true) if t != null else ""
what = "%s%s" % [Formatter.strip_tags(str(o.get("label", o.get("option", "")))), (" · level %s" % o["value_text"]) if str(o.get("value_text", "")) != "" else ""]
if src != "":
what += "\n" + src
var ol := Style.make_label(what, Style.body_bold(), 13, Style.VALUE_INK)
ol.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
ol.mouse_filter = Control.MOUSE_FILTER_IGNORE
box.add_child(ol)
var det := e.details.strip_edges()
if det != "":
any = true
var dt := RichText.label(RichText.decorate(det), 14, Style.INK, false)
dt.mouse_filter = Control.MOUSE_FILTER_IGNORE
box.add_child(dt)
var fx: Array = Describe.effects(e.effects, d.board, t) if d.plain else Array(Effects.split(e.effects))
if not fx.is_empty():
any = true
box.add_child(_rule())
box.add_child(d.section("When this comes up" if d.plain else "Effects"))
for st: Variant in fx:
var l := Style.make_label(str(st), Style.body_bold(), 13, Style.VALUE_INK)
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
box.add_child(l)
var shown: Array[String] = []
for tid in e.pipes:
shown.append(tid)
any = true
box.add_child(_rule())
var how := Describe.pipe(e, tid, d.board) if d.plain else PipeLayer._pipe_tag(e, tid)
if how != "":
var hl := Style.make_label(("Then: " if d.plain else "thread: ") + how, Style.body_bold(), 12, Style.TRIM_DARK)
hl.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
hl.mouse_filter = Control.MOUSE_FILTER_IGNORE
hl.set_meta("pipe_line", true)
box.add_child(hl)
box.add_child(TablePreview.build(d.board, tid, {"count": e.count_text(tid), "vars": d.vars, "plain": d.plain}))
var mentioned: Array[String] = []
for name in TextExpander.references(e.text, func(n: String) -> String: return Roller.resolve(d.board, n)):
var rid := Roller.resolve(d.board, name)
if rid != "" and not SpreadLinks.is_link(rid) and not shown.has(rid) and not mentioned.has(rid):
mentioned.append(rid)
if not mentioned.is_empty():
any = true
box.add_child(_rule())
box.add_child(d.section("Mentioned in the text"))
for rid in mentioned:
box.add_child(TablePreview.build(d.board, rid, {"vars": d.vars, "plain": d.plain}))
if not any:
box.free()
return null
return box
func _rule() -> Control:
var r := ColorRect.new()
r.color = Style.RULE
r.custom_minimum_size.y = 1
r.mouse_filter = Control.MOUSE_FILTER_IGNORE
return r
## Beside the card (right if there is room, else left), top-aligned with it
## and kept on screen. It never overlaps the card, so it never covers options.
func place() -> void:
if not d.preview_panel.visible:
return
var view := d.size
var cr := d.card.get_global_rect()
var gap := 18.0
var right := view.x - cr.end.x - gap - 10.0
var left := cr.position.x - gap - 10.0
var w := PREVIEW_WIDTH
var x: float
if right >= w or right >= left:
w = clampf(right, 180.0, PREVIEW_WIDTH)
x = cr.end.x + gap
else:
w = clampf(left, 180.0, PREVIEW_WIDTH)
x = cr.position.x - gap - w
d.preview_panel.custom_minimum_size.x = w
d.preview_panel.size = Vector2(w, 0)
var h := d.preview_panel.get_combined_minimum_size().y
var top := clampf(cr.position.y, 10.0, maxf(10.0, view.y - 10.0 - h))
h = minf(h, view.y - top - 10.0)
d.preview_panel.position = Vector2(x, top)
d.preview_panel.size = Vector2(w, h)