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) func _show_var(owner: Control, name: String) -> void: var persistent := name.begins_with("@") var value: Variant = null var source := "" if persistent: var v: Variant = main.persistent_vars.get(name.substr(1)) if v is Dictionary: value = str(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: 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)