- ChoiceDialog: card-styled pop-up for pick (option rows with details, Random,
1-9 / R keys), number (spin box with min/max/default, Enter) and text.
Cancel, Esc, freeing the dialog, a new roll or a collection switch all
answer null through a separate Reply object, so a roll never hangs.
- Main: real prompt hook; the steps so far light on the board and in the
tray before each question; a roll with questions shows the rest at once;
a new roll cancels an open question cleanly; refresh_live() re-renders
variable, sheet and question text only after rolls and edits.
- TableCard: "?" badge and question for choice cards, loop icon, rule chip
and Each time / Afterwards rows, compact live variable cards (reset in
Build), live sheet cards with one outlet, expression-dice subtitles,
effect chips in Play and a validated effects editor in Build.
- Build: "+ Table" menu for every kind; kind-specific settings (choice type,
question, save as, min/max; loop mode and rule; variable name, type,
starting value; sheet template) checked with Expr.check; expression pipe
counts in the pipe popover and on pipe labels.
- Tray: effect lines under chips, last 12 steps behind "+N earlier steps",
sheet headline, Cancelled / action-limit pill, capped indents and Earlier
rows; Variables tab lists every board variable with Reset all.
- RichText: error markers in red; previews show {@x_*} and {= expr}.
- examples/logic_demo.json and tests/test_logic_ui.gd.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
237 lines
8.1 KiB
GDScript
237 lines
8.1 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.
|
|
|
|
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.COPPER
|
|
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 or e.details.strip_edges() == "":
|
|
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)), e.details, Formatter.strip_tags(t.name))
|
|
|
|
|
|
## 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.
|
|
func show_step(owner: Control, step: Dictionary) -> 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 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 pipe'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 collection" 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:
|
|
return {"pick": "Choice · %d options" % t.entries.size(), "number": "Choice · a number", "text": "Choice · open ended"}.get(t.choice, "Choice")
|
|
RollTable.KIND_LOOP:
|
|
return "Loop · " + TableCard.loop_rule(t)
|
|
RollTable.KIND_VARIABLE:
|
|
return "Variable @%s · %s" % [t.var_name, t.var_type]
|
|
RollTable.KIND_SHEET:
|
|
return "Sheet"
|
|
var parts := PackedStringArray([t.dice_label()])
|
|
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)
|