User-facing text only; code names, JSON keys and the project's internal name (and so the user:// folder) are unchanged. The Collection card kind keeps its name until a new one is chosen. The window title says Table Tarot; the top bar shows the theme's logo (the fortune teller, drawn from a copy of the art without its background square); the app icon is the mascot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
314 lines
12 KiB
GDScript
314 lines
12 KiB
GDScript
class_name DetailsPopup
|
||
extends PanelContainer
|
||
## The hover pop-up for table and entry details, result chips and the
|
||
## [[references]] / {variables} inside rendered text.
|
||
##
|
||
## It ignores the mouse, so it never blocks clicks, stays inside the window,
|
||
## and hides itself as soon as the control that opened it is no longer hovered.
|
||
##
|
||
## Hover summaries follow RulesView.plain(main): an entry's effects, show_if,
|
||
## router condition and pipe labels read in plain words or as written, and a
|
||
## step of the last roll (a chip, or a lit row on a card) adds its "Why this?"
|
||
## reasons (WhyPopover.why_lines).
|
||
|
||
const WIDTH := 300.0
|
||
const DELAY := 0.28
|
||
const OFFSET := Vector2(14, 20)
|
||
|
||
var main: Main
|
||
var _owner_ctrl: Control
|
||
var _title: Label
|
||
var _sub: Label
|
||
var _body: RichTextLabel
|
||
var _wait := 0.0
|
||
var _pending := false
|
||
|
||
|
||
func setup(p_main: Main) -> void:
|
||
main = p_main
|
||
mouse_filter = MOUSE_FILTER_IGNORE
|
||
z_index = 100
|
||
top_level = true
|
||
var sb := Style.box(Style.CARD, 9, [13, 10, 13, 11])
|
||
sb.border_width_top = 3
|
||
sb.border_color = Style.TRIM
|
||
sb.shadow_color = Color(0, 0, 0, 0.35)
|
||
sb.shadow_size = 14
|
||
sb.shadow_offset = Vector2(0, 6)
|
||
add_theme_stylebox_override("panel", sb)
|
||
var v := VBoxContainer.new()
|
||
v.add_theme_constant_override("separation", 3)
|
||
v.mouse_filter = MOUSE_FILTER_IGNORE
|
||
add_child(v)
|
||
_title = Style.make_label("", Style.display(), 18, Style.INK)
|
||
_title.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
||
_title.custom_minimum_size = Vector2(WIDTH, 0)
|
||
_title.mouse_filter = MOUSE_FILTER_IGNORE
|
||
v.add_child(_title)
|
||
_sub = Style.make_label("", Style.body(), 12, Style.MUTED)
|
||
_sub.mouse_filter = MOUSE_FILTER_IGNORE
|
||
v.add_child(_sub)
|
||
_body = RichText.label("", 14, Style.INK, false)
|
||
_body.custom_minimum_size = Vector2(WIDTH, 0)
|
||
_body.mouse_filter = MOUSE_FILTER_IGNORE
|
||
v.add_child(_body)
|
||
hide()
|
||
|
||
|
||
## Shows the pop-up for `owner` after a short delay. Empty parts are hidden.
|
||
func show_for(owner: Control, title: String, body: String, sub: String = "") -> void:
|
||
if owner == null:
|
||
return
|
||
if not owner.has_meta("_details_hooked"):
|
||
owner.set_meta("_details_hooked", true)
|
||
owner.mouse_exited.connect(_on_owner_exited.bind(owner))
|
||
_owner_ctrl = owner
|
||
_title.text = title
|
||
_title.visible = title != ""
|
||
_sub.text = sub
|
||
_sub.visible = sub != ""
|
||
_body.text = body
|
||
_body.visible = body != ""
|
||
_wait = DELAY if not visible else 0.0
|
||
_pending = true
|
||
if visible:
|
||
_place()
|
||
|
||
|
||
## Hides the pop-up if `owner` opened it (or always, when owner is null).
|
||
func hide_for(owner: Control = null) -> void:
|
||
if owner == null or owner == _owner_ctrl:
|
||
_pending = false
|
||
_owner_ctrl = null
|
||
hide()
|
||
|
||
|
||
func _on_owner_exited(owner: Control) -> void:
|
||
hide_for(owner)
|
||
|
||
|
||
## Shows the prepared pop-up at a fixed spot until hide_for() (screenshots).
|
||
func pin(at: Vector2) -> void:
|
||
_owner_ctrl = null
|
||
_pending = false
|
||
show()
|
||
reset_size()
|
||
global_position = at
|
||
|
||
|
||
func _process(delta: float) -> void:
|
||
if _owner_ctrl == null:
|
||
return
|
||
# The owner may be rebuilt away (a card re-rendering) without a mouse_exited.
|
||
if not is_instance_valid(_owner_ctrl) or not _owner_ctrl.is_visible_in_tree():
|
||
hide_for()
|
||
return
|
||
if _pending:
|
||
_wait -= delta
|
||
if _wait <= 0.0:
|
||
_pending = false
|
||
show()
|
||
_place()
|
||
elif visible:
|
||
_place()
|
||
|
||
|
||
func _place() -> void:
|
||
reset_size()
|
||
var view := get_viewport_rect().size
|
||
var p := get_global_mouse_position() + OFFSET
|
||
if p.x + size.x > view.x - 8.0:
|
||
p.x = view.x - size.x - 8.0
|
||
if p.y + size.y > view.y - 8.0:
|
||
p.y = get_global_mouse_position().y - size.y - 12.0
|
||
global_position = Vector2(maxf(8.0, p.x), maxf(8.0, p.y))
|
||
|
||
|
||
# ---------- Content ----------
|
||
|
||
func show_table(owner: Control, tid: String, only_if_details: bool = false) -> void:
|
||
var t := main.data.get_table(tid) if main.data else null
|
||
if t == null:
|
||
return
|
||
if only_if_details and t.details.strip_edges() == "":
|
||
return
|
||
show_for(owner, Formatter.strip_tags(t.name), t.details, table_summary(t))
|
||
|
||
|
||
func show_entry(owner: Control, tid: String, eid: String) -> void:
|
||
var t := main.data.get_table(tid)
|
||
var e := t.find_entry(eid) if t else null
|
||
if e == null:
|
||
return
|
||
var plain := RulesView.plain(main)
|
||
var parts := PackedStringArray()
|
||
if e.details.strip_edges() != "":
|
||
parts.append(e.details)
|
||
var rules := rules_bbcode(t, e, main.data, plain)
|
||
if rules != "":
|
||
parts.append(rules)
|
||
if main.is_entry_lit(tid, eid):
|
||
var found := step_for(tid, eid)
|
||
if not found.is_empty():
|
||
var why := why_bbcode(found["steps"], int(found["index"]), main.data, plain)
|
||
if why != "":
|
||
parts.append(why)
|
||
if parts.is_empty():
|
||
return
|
||
var shown := main.lit_text(tid, eid)
|
||
if shown == "":
|
||
shown = RichText.preview(e.text, main.data)
|
||
show_for(owner, _short(Formatter.strip_tags(shown)), "\n\n".join(parts), Formatter.strip_tags(t.name))
|
||
|
||
|
||
## An entry's rules as short BBCode lines: "only if …", "Changes: Luck −1 ·
|
||
## Gold + 2D", and "→ Loot: Tier 3 · only if Luck is 2 or more" for each pipe
|
||
## with options. Router conditions are on the row itself, so they're left out.
|
||
static func rules_bbcode(t: RollTable, e: TableEntry, board: TowerBoard, plain: bool) -> String:
|
||
var muted := Style.MUTED.to_html(false)
|
||
var lines := PackedStringArray()
|
||
if t.kind == RollTable.KIND_CHOICE and t.choice == "pick" and e.show_if.strip_edges() != "":
|
||
lines.append("[color=#%s]Offered[/color] %s" % [muted, RichText.plain(RulesView.show_if_line(e, board, plain))])
|
||
if e.effects.strip_edges() != "":
|
||
var fx: Array = Describe.effects(e.effects, board, t) if plain else Array(Effects.split(e.effects))
|
||
var ss := PackedStringArray()
|
||
for f: Variant in fx:
|
||
ss.append(RichText.plain(str(f)))
|
||
lines.append("[color=#%s]Changes:[/color] [b]%s[/b]" % [muted, " · ".join(ss)])
|
||
for to in e.pipes:
|
||
var lab := Describe.pipe(e, to, board) if plain else PipeLayer._pipe_tag(e, to)
|
||
if lab == "":
|
||
continue
|
||
var target := board.get_table(to) if board else null
|
||
var nm := Formatter.strip_tags(target.name) if target else to
|
||
lines.append("[color=#%s]→ %s:[/color] %s" % [muted, RichText.plain(nm), RichText.plain(lab)])
|
||
return "\n".join(lines)
|
||
|
||
|
||
## The "Why this?" reasons for steps[index] as a small BBCode section, or "".
|
||
static func why_bbcode(steps: Array, index: int, board: TowerBoard, plain: bool) -> String:
|
||
var ls := WhyPopover.why_lines(steps, index, board, plain)
|
||
if ls.is_empty():
|
||
return ""
|
||
return "[color=#%s][b]WHY THIS?[/b][/color]\n%s" % [Style.MUTED.to_html(false), WhyPopover.bbcode_list(ls)]
|
||
|
||
|
||
## The last step of the current roll for an entry: {"steps", "index"} or {}.
|
||
func step_for(tid: String, eid: String) -> Dictionary:
|
||
var steps: Array = main.current.get("steps", [])
|
||
for k in range(steps.size() - 1, -1, -1):
|
||
var s: Dictionary = steps[k]
|
||
if str(s.get("table_id", "")) == tid and str(s.get("entry_id", "")) == eid:
|
||
return {"steps": steps, "index": k}
|
||
return {}
|
||
|
||
|
||
## Plain one-line title, cut down to a readable length.
|
||
static func _short(s: String) -> String:
|
||
s = s.strip_edges().replace("\n", " ")
|
||
while s.contains(" "):
|
||
s = s.replace(" ", " ")
|
||
return s if s.length() <= 90 else s.substr(0, 88).strip_edges() + "…"
|
||
|
||
|
||
## A result chip: the entry's details, else the table's, then the step's
|
||
## "Why this?" reasons when `steps` and `index` are given.
|
||
func show_step(owner: Control, step: Dictionary, steps: Array = [], index: int = -1) -> void:
|
||
var t := main.data.get_table(str(step.get("table_id", "")))
|
||
if t == null:
|
||
return
|
||
var e := t.find_entry(str(step.get("entry_id", "")))
|
||
var body := e.details if e and e.details.strip_edges() != "" else t.details
|
||
if index >= 0:
|
||
var why := why_bbcode(steps, index, main.data, RulesView.plain(main))
|
||
if why != "":
|
||
body = (body.strip_edges() + "\n\n" + why) if body.strip_edges() != "" else why
|
||
if body.strip_edges() == "":
|
||
return
|
||
var title := _short(Formatter.strip_tags(str(step.get("text", "")))) if e and e.details.strip_edges() != "" else Formatter.strip_tags(t.name)
|
||
show_for(owner, title, body, Formatter.strip_tags(t.name) if e and e.details.strip_edges() != "" else table_summary(t))
|
||
|
||
|
||
## True when a result chip has something to show.
|
||
func step_has_details(step: Dictionary) -> bool:
|
||
var t := main.data.get_table(str(step.get("table_id", "")))
|
||
if t == null:
|
||
return false
|
||
var e := t.find_entry(str(step.get("entry_id", "")))
|
||
return t.details.strip_edges() != "" or (e != null and e.details.strip_edges() != "")
|
||
|
||
|
||
## A [url] meta inside rendered text (see TextExpander's marker formats).
|
||
func show_meta(owner: Control, meta: String) -> void:
|
||
var kind := meta.get_slice(":", 0)
|
||
var arg := meta.substr(kind.length() + 1)
|
||
match kind:
|
||
"table":
|
||
show_table(owner, arg)
|
||
"var":
|
||
_show_var(owner, arg)
|
||
"missing":
|
||
show_for(owner, "Missing table", "There's no table called [b]%s[/b]. Check the spelling, or add a table with that name." % RichText.plain(arg))
|
||
"unset":
|
||
show_for(owner, "{%s} isn't set" % arg, "Nothing has saved this variable yet. Save it earlier in the roll with [lb][lb]Table -> %s]] or a thread's [i]Save as[/i]." % RichText.plain(arg))
|
||
"deep":
|
||
show_for(owner, "Chain too deep", "The roll stopped after %d levels so a loop can't run forever." % Roller.MAX_DEPTH)
|
||
"error":
|
||
show_for(owner, "Can't work this out", "This expression has a mistake; the message after ⚠ says what. Expressions use @variables, dice such as 2d6, + - * / and functions such as max() and dm().")
|
||
"expr":
|
||
show_for(owner, "{= %s}" % RichText.plain(arg), "A computed value, worked out each time the text is shown.")
|
||
|
||
|
||
func _show_var(owner: Control, name: String) -> void:
|
||
var persistent := name.begins_with("@")
|
||
var value: Variant = null
|
||
var source := ""
|
||
if persistent and name.ends_with("*"):
|
||
var parts := PackedStringArray()
|
||
for v: Dictionary in RollContext.vars_with_prefix(main.persistent_vars, name.substr(1)):
|
||
parts.append("%s %s" % [v["label"], Expr.format_value(v["value"])])
|
||
show_for(owner, "{%s}" % name, ", ".join(parts) if not parts.is_empty() else "[i]None set yet[/i]",
|
||
"Every variable starting with %s" % name.trim_suffix("*"))
|
||
return
|
||
if persistent:
|
||
var v: Variant = main.persistent_vars.get(name.substr(1))
|
||
if v is Dictionary:
|
||
value = Expr.format_value(v.get("value", ""))
|
||
source = str(v.get("source", ""))
|
||
else:
|
||
value = (main.current.get("chain_vars", {}) as Dictionary).get(name)
|
||
var sub := "Saved for this spread" if persistent else "Saved for this roll"
|
||
var st := main.data.get_table(source) if source != "" else null
|
||
if st:
|
||
sub += " · from %s" % Formatter.strip_tags(st.name)
|
||
var body := str(value) if value != null else "[i]Not set[/i]"
|
||
show_for(owner, "{%s}" % name, body, sub)
|
||
|
||
|
||
## "d100 · 6 ranges · 4 / 6 cards left"
|
||
func table_summary(t: RollTable) -> String:
|
||
match t.kind:
|
||
RollTable.KIND_CHOICE:
|
||
var sub := "Choice · " + Describe.choice_label(t.choice).to_lower()
|
||
return sub + " · %d options" % t.entries.size() if t.choice == "pick" else sub
|
||
RollTable.KIND_LOOP:
|
||
return "Repeat · " + RulesView.loop_rule(t, main.data, RulesView.plain(main))
|
||
RollTable.KIND_VARIABLE:
|
||
if RulesView.plain(main):
|
||
return "Collection · kept between rolls" if t.is_collection() else "%s · kept between rolls" % t.var_type.capitalize()
|
||
return "Variable @%s · %s" % [t.var_name, t.var_type]
|
||
RollTable.KIND_CHECK:
|
||
return RulesView.check_line(t, main.data, RulesView.plain(main))
|
||
RollTable.KIND_SHEET:
|
||
return "Sheet"
|
||
RollTable.KIND_ROUTER:
|
||
return "Branch · %d cases · the first true case is taken" % t.entries.size()
|
||
var dl := t.dice_label()
|
||
if t.clamps():
|
||
dl = RulesView.roll_line(t, main.data, RulesView.plain(main))
|
||
var parts := PackedStringArray([dl])
|
||
parts.append("%d %s" % [t.entries.size(), "ranges" if t.is_ranged() else "results"])
|
||
if t.card_mode:
|
||
var left := main.cards_left(t.id)
|
||
parts.append("%d / %d cards left" % [left.x, left.y])
|
||
return " · ".join(parts)
|