2026-11-08 21:24:25 -06:00
|
|
|
class_name Inspector
|
|
|
|
|
extends PanelContainer
|
|
|
|
|
## The Build-mode inspector (docs/PLAIN.md §6): a panel docked right of the
|
|
|
|
|
## board that edits the selected card, result or pipe with fill-in builders.
|
|
|
|
|
## Plain words mode shows builders; Text mode shows the rule text.
|
|
|
|
|
##
|
|
|
|
|
## Every change goes through Main (mark_dirty), so it is saved and is an undo
|
|
|
|
|
## step; typing is one step once it pauses. The panel rebuilds when the
|
|
|
|
|
## selected card changes from outside (an undo, an edit on the card), and
|
|
|
|
|
## keeps focus in the field being typed in.
|
|
|
|
|
|
2026-11-09 10:01:10 -06:00
|
|
|
## The default and smallest widths (the player can drag it wider) and the
|
|
|
|
|
## collapsed strip's width.
|
2026-11-08 21:24:25 -06:00
|
|
|
const WIDTH := 340.0
|
2026-11-09 10:01:10 -06:00
|
|
|
const MIN_WIDTH := 280.0
|
2026-11-08 21:24:25 -06:00
|
|
|
const STRIP := 30.0
|
2026-11-09 10:01:10 -06:00
|
|
|
var width := WIDTH
|
|
|
|
|
## The drag handle on the inspector's left edge, shown with it when open.
|
|
|
|
|
var splitter: PanelSplitter
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
var main: Main
|
|
|
|
|
## {kind: "card" | "result" | "pipe", tid, eid?, to?}, or {} for nothing.
|
|
|
|
|
var selection: Dictionary = {}
|
|
|
|
|
var collapsed := false
|
|
|
|
|
|
|
|
|
|
var _body: VBoxContainer
|
|
|
|
|
var _scroll: ScrollContainer
|
|
|
|
|
var _content: VBoxContainer
|
|
|
|
|
var _strip: PanelContainer
|
|
|
|
|
var _sig := ""
|
|
|
|
|
var _sync_queued := false
|
|
|
|
|
var _writing := false
|
|
|
|
|
## Run after every write, to refresh live sentences and problem lines.
|
|
|
|
|
var _live: Array[Callable] = []
|
2026-11-08 21:29:59 -06:00
|
|
|
var _title_label: Label
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
# Parts the tests drive (rebuilt with the panel).
|
|
|
|
|
var effects: EffectsBuilder
|
|
|
|
|
var show_if_builder: ConditionBuilder
|
|
|
|
|
var case_builder: ConditionBuilder
|
|
|
|
|
## target id -> {box, when (ConditionBuilder), otherwise (CheckBox), count, save, inputs: {name: LineEdit}, taken}
|
|
|
|
|
var pipe_parts: Dictionary = {}
|
2026-11-15 12:35:27 -06:00
|
|
|
## Named fields of the active section ("name", "dice", "text", ...).
|
2026-11-08 21:24:25 -06:00
|
|
|
var fields: Dictionary = {}
|
2026-11-15 12:35:27 -06:00
|
|
|
## The card page's sections (InspectorList): which are open ("card" or a
|
|
|
|
|
## result id -> true), each one's parts, and each one's box.
|
|
|
|
|
var open_sections: Dictionary = {}
|
|
|
|
|
var section_parts: Dictionary = {}
|
|
|
|
|
var section_boxes: Dictionary = {}
|
2026-11-25 16:19:30 -06:00
|
|
|
## The box holding the sections and the card it shows, kept between rebuilds.
|
|
|
|
|
var section_list: VBoxContainer
|
|
|
|
|
var section_list_tid := ""
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func setup(p_main: Main) -> void:
|
|
|
|
|
main = p_main
|
|
|
|
|
theme = Style.card_theme()
|
|
|
|
|
custom_minimum_size = Vector2(WIDTH, 0)
|
|
|
|
|
size_flags_vertical = Control.SIZE_EXPAND_FILL
|
2026-11-09 10:01:10 -06:00
|
|
|
visibility_changed.connect(_sync_splitter)
|
2026-11-14 13:28:21 -06:00
|
|
|
InspectorFrame.build(self)
|
2026-11-08 21:24:25 -06:00
|
|
|
rebuild()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------- Selection ----------
|
|
|
|
|
|
2026-11-15 12:35:27 -06:00
|
|
|
## Selecting a card or result on the spread opens its section and closes the rest.
|
2026-11-08 21:24:25 -06:00
|
|
|
func select(sel: Dictionary) -> void:
|
|
|
|
|
if collapsed:
|
|
|
|
|
set_collapsed(false)
|
2026-11-15 12:46:05 -06:00
|
|
|
if main and main.book and main.book.visible:
|
|
|
|
|
SideTabs.show_tab(main, SideTabs.INSPECTOR)
|
2026-11-08 21:24:25 -06:00
|
|
|
selection = sel
|
2026-11-25 16:19:30 -06:00
|
|
|
main.card_refresh.ensure(str(sel.get("tid", "")))
|
2026-11-15 12:35:27 -06:00
|
|
|
var kind := str(sel.get("kind", ""))
|
|
|
|
|
if kind == "card" or kind == "result":
|
|
|
|
|
open_sections = {InspectorList.key_of(sel): true}
|
2026-11-08 21:24:25 -06:00
|
|
|
rebuild()
|
2026-11-08 21:36:29 -06:00
|
|
|
_mark_cards()
|
2026-11-15 12:35:27 -06:00
|
|
|
if kind == "card" or kind == "result":
|
|
|
|
|
InspectorList.scroll_to(self, InspectorList.key_of(sel))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
|
get_viewport().gui_focus_changed.connect(_on_focus_changed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## A click or focus inside a section makes it the one edits go to.
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
|
|
|
if event is InputEventMouseButton and (event as InputEventMouseButton).pressed and is_visible_in_tree() and not section_boxes.is_empty():
|
|
|
|
|
var key := InspectorList.key_at(self, (event as InputEventMouseButton).global_position)
|
|
|
|
|
if key != "":
|
|
|
|
|
InspectorList.activate(self, key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_focus_changed(c: Control) -> void:
|
|
|
|
|
if c and is_ancestor_of(c) and not section_boxes.is_empty():
|
|
|
|
|
var key := InspectorList.key_holding(self, c)
|
|
|
|
|
if key != "":
|
|
|
|
|
InspectorList.activate(self, key)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func clear() -> void:
|
|
|
|
|
selection = {}
|
|
|
|
|
rebuild()
|
2026-11-08 21:36:29 -06:00
|
|
|
_mark_cards()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Puts the copper selection bar on the selected result's row, and clears it
|
|
|
|
|
## everywhere else.
|
|
|
|
|
func _mark_cards() -> void:
|
|
|
|
|
if main == null:
|
|
|
|
|
return
|
2026-11-21 08:56:30 -06:00
|
|
|
var kind := str(selection.get("kind", ""))
|
|
|
|
|
# A selected thread is the selection itself; its source card is not.
|
|
|
|
|
var tid := str(selection.get("tid", "")) if kind != "pipe" else ""
|
|
|
|
|
var eid := str(selection.get("eid", "")) if kind == "result" else ""
|
2026-11-08 21:36:29 -06:00
|
|
|
for c in main.cards.values():
|
|
|
|
|
var card := c as TableCard
|
|
|
|
|
if card and card.has_method("set_selected_entry"):
|
|
|
|
|
card.set_selected_entry(eid if card.table.id == tid else "")
|
2026-11-15 12:39:37 -06:00
|
|
|
card.set_inspected(card.table.id == tid)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func set_collapsed(on: bool) -> void:
|
|
|
|
|
collapsed = on
|
2026-11-09 10:01:10 -06:00
|
|
|
custom_minimum_size.x = STRIP if on else width
|
2026-11-08 21:24:25 -06:00
|
|
|
_body.visible = not on
|
|
|
|
|
_strip.visible = on
|
2026-11-09 10:01:10 -06:00
|
|
|
_sync_splitter()
|
2026-11-08 21:24:25 -06:00
|
|
|
if main and main.state:
|
|
|
|
|
main.state.set_setting("inspector_collapsed", on)
|
|
|
|
|
|
|
|
|
|
|
2026-11-09 10:01:10 -06:00
|
|
|
## Sets the open width (from the PanelSplitter).
|
|
|
|
|
func set_width(px: float) -> void:
|
|
|
|
|
width = px
|
|
|
|
|
if not collapsed:
|
|
|
|
|
custom_minimum_size.x = px
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _sync_splitter() -> void:
|
|
|
|
|
if splitter:
|
|
|
|
|
splitter.visible = visible and not collapsed
|
|
|
|
|
|
|
|
|
|
|
2026-11-08 21:24:25 -06:00
|
|
|
func selected_table() -> RollTable:
|
|
|
|
|
if selection.is_empty() or main == null:
|
|
|
|
|
return null
|
|
|
|
|
return main.data.get_table(str(selection.get("tid", "")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func selected_entry() -> TableEntry:
|
|
|
|
|
var t := selected_table()
|
|
|
|
|
if t == null or not selection.has("eid"):
|
|
|
|
|
return null
|
|
|
|
|
return t.find_entry(str(selection["eid"]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Rebuilds once, at the end of the frame, if the selection changed from outside.
|
|
|
|
|
func sync() -> void:
|
|
|
|
|
if _writing or _sync_queued:
|
|
|
|
|
return
|
|
|
|
|
_sync_queued = true
|
|
|
|
|
_do_sync.call_deferred()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _do_sync() -> void:
|
|
|
|
|
_sync_queued = false
|
|
|
|
|
if _signature() != _sig:
|
|
|
|
|
rebuild()
|
2026-11-16 10:41:13 -06:00
|
|
|
else:
|
|
|
|
|
InspectorSync.sync_texts(self)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func _signature() -> String:
|
2026-11-10 19:51:10 -06:00
|
|
|
return InspectorSync.signature(self)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------- Writing ----------
|
|
|
|
|
|
|
|
|
|
## After the inspector changed table `t`: Main refreshes the card(s) and
|
|
|
|
|
## records the undo step. `rebuild_panel` redraws the inspector too (for
|
|
|
|
|
## changes that add or remove rows).
|
|
|
|
|
func wrote(t: RollTable, all_cards: bool = false, rebuild_panel: bool = false) -> void:
|
|
|
|
|
_writing = true
|
|
|
|
|
main.inspector_edited(t.id, all_cards)
|
|
|
|
|
main.mark_dirty()
|
|
|
|
|
_writing = false
|
|
|
|
|
_sig = _signature()
|
|
|
|
|
for f in _live:
|
|
|
|
|
if f.is_valid():
|
|
|
|
|
f.call()
|
2026-11-14 13:28:21 -06:00
|
|
|
InspectorFrame.refresh_title(self)
|
2026-11-08 21:24:25 -06:00
|
|
|
if rebuild_panel:
|
|
|
|
|
rebuild()
|
|
|
|
|
|
|
|
|
|
|
2026-11-16 10:41:13 -06:00
|
|
|
## After the inspector changed one result's text: the card's inline field
|
|
|
|
|
## takes the text in place (a rebuild of a long card took ~150 ms per key);
|
|
|
|
|
## otherwise as wrote().
|
|
|
|
|
func wrote_text(t: RollTable, eid: String) -> void:
|
|
|
|
|
var card := main.card_for(t.id)
|
|
|
|
|
if card == null or not card.edit_ui.sync_text(eid):
|
|
|
|
|
wrote(t)
|
|
|
|
|
return
|
|
|
|
|
_writing = true
|
|
|
|
|
main.local_state._ref_cache.clear()
|
|
|
|
|
main.mark_dirty()
|
|
|
|
|
_writing = false
|
|
|
|
|
_sig = _signature()
|
|
|
|
|
card.update_warning()
|
|
|
|
|
for f in _live:
|
|
|
|
|
if f.is_valid():
|
|
|
|
|
f.call()
|
|
|
|
|
InspectorFrame.refresh_title(self)
|
|
|
|
|
|
|
|
|
|
|
2026-11-08 21:24:25 -06:00
|
|
|
## Applies table settings through TableSettingsDialog.apply_settings (the
|
|
|
|
|
## same rules as the settings dialog). Returns "" or the problem.
|
|
|
|
|
func apply_settings(over: Dictionary, rebuild_panel: bool = false) -> String:
|
|
|
|
|
var t := selected_table()
|
|
|
|
|
if t == null:
|
|
|
|
|
return "Nothing selected"
|
|
|
|
|
var s := {"name": t.name, "details": t.details, "tags": ", ".join(t.tags), "ranged": t.is_ranged(),
|
2026-11-09 10:01:10 -06:00
|
|
|
"dice": t.dice, "dice_mod": t.dice_mod, "card_mode": t.card_mode, "deck_empty": t.deck_empty}
|
2026-11-08 21:24:25 -06:00
|
|
|
s.merge(over, true)
|
|
|
|
|
var r := TableSettingsDialog.apply_settings(t, s)
|
|
|
|
|
if not r["ok"]:
|
|
|
|
|
return str(r["error"])
|
|
|
|
|
if r.get("deck_reset", false) and main.state and main.data.id != "":
|
|
|
|
|
main.state.clear_deck(main.data.id, t.id)
|
|
|
|
|
wrote(t, t.kind == RollTable.KIND_VARIABLE, rebuild_panel)
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
2026-11-10 19:51:10 -06:00
|
|
|
## A collection card's members: Array of [item, value] (item without the stem).
|
|
|
|
|
func members(t: RollTable) -> Array:
|
|
|
|
|
return InspectorSync.members(self, t)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func set_pipe_when(to: String, s: String) -> void:
|
2026-11-10 19:51:10 -06:00
|
|
|
InspectorThread.set_when(self, to, s)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func set_pipe_otherwise(to: String, on: bool) -> void:
|
2026-11-10 19:51:10 -06:00
|
|
|
InspectorThread.set_otherwise(self, to, on)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
## Sets one input value passed down the pipe ("" uses the card's default).
|
|
|
|
|
func set_pipe_input(to: String, input_name: String, expr: String) -> void:
|
2026-11-10 19:51:10 -06:00
|
|
|
InspectorThread.set_input(self, to, input_name, expr)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func set_pipe_count(to: String, s: String) -> void:
|
2026-11-10 19:51:10 -06:00
|
|
|
InspectorThread.set_count(self, to, s)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
2026-11-10 19:10:11 -06:00
|
|
|
func set_pipe_pause(to: String, on: bool) -> void:
|
2026-11-10 19:51:10 -06:00
|
|
|
InspectorThread.set_pause(self, to, on)
|
2026-11-10 19:10:11 -06:00
|
|
|
|
|
|
|
|
|
2026-11-10 19:06:55 -06:00
|
|
|
func set_pipe_combine(to: String, mode: String) -> void:
|
2026-11-10 19:51:10 -06:00
|
|
|
InspectorThread.set_combine(self, to, mode)
|
2026-11-10 19:06:55 -06:00
|
|
|
|
|
|
|
|
|
2026-11-08 21:24:25 -06:00
|
|
|
func set_pipe_save(to: String, s: String) -> void:
|
2026-11-10 19:51:10 -06:00
|
|
|
InspectorThread.set_save(self, to, s)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
2026-11-10 18:20:46 -06:00
|
|
|
func set_pipe_color(hex: String, to: String) -> void:
|
2026-11-10 19:51:10 -06:00
|
|
|
InspectorThread.set_color(self, hex, to)
|
|
|
|
|
|
2026-11-10 18:20:46 -06:00
|
|
|
|
2026-11-10 21:18:29 -06:00
|
|
|
func set_pipe_dice(to: String, s: String) -> void:
|
|
|
|
|
InspectorThreadRolls.set_dice(self, to, s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func set_pipe_reroll(to: String, rid: String, on: bool) -> void:
|
|
|
|
|
InspectorThreadRolls.set_reroll(self, to, rid, on)
|
|
|
|
|
|
2026-11-10 18:20:46 -06:00
|
|
|
|
2026-11-08 21:24:25 -06:00
|
|
|
func remove_pipe(to: String) -> void:
|
|
|
|
|
var t := selected_table()
|
|
|
|
|
var e := selected_entry()
|
|
|
|
|
if t == null or e == null:
|
|
|
|
|
return
|
|
|
|
|
var was_pipe := str(selection.get("kind", "")) == "pipe"
|
|
|
|
|
main.remove_pipe(Roller.pipe_key(t.id, e.id, to))
|
|
|
|
|
if was_pipe:
|
|
|
|
|
select({"kind": "result", "tid": t.id, "eid": e.id})
|
|
|
|
|
else:
|
|
|
|
|
rebuild()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static func _save_error(s: String) -> String:
|
|
|
|
|
var t := TemplateKit.probe(s.strip_edges())
|
|
|
|
|
if t == "" or RegEx.create_from_string(PipePopover.SAVE_PATTERN).search(t) != null:
|
|
|
|
|
return ""
|
|
|
|
|
return "Use a name like weapon, or @weapon to keep it between rolls."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------- Collections ----------
|
|
|
|
|
|
2026-11-10 19:51:10 -06:00
|
|
|
func set_entry_text(s: String) -> void:
|
|
|
|
|
InspectorResult.set_entry_text(self, s)
|
|
|
|
|
|
2026-11-08 21:24:25 -06:00
|
|
|
|
2026-11-10 19:51:10 -06:00
|
|
|
func set_entry_details(s: String) -> void:
|
|
|
|
|
InspectorResult.set_entry_details(self, s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func set_entry_effects(s: String) -> void:
|
|
|
|
|
InspectorResult.set_entry_effects(self, s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func set_entry_show_if(s: String) -> void:
|
|
|
|
|
InspectorResult.set_entry_show_if(self, s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## A router case's condition ("" makes it the Otherwise case).
|
|
|
|
|
func set_case_when(eid: String, s: String) -> void:
|
|
|
|
|
InspectorResult.set_case_when(self, eid, s)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
# ---------- Building ----------
|
|
|
|
|
|
|
|
|
|
func rebuild() -> void:
|
|
|
|
|
var focus_key := ""
|
|
|
|
|
var caret := -1
|
|
|
|
|
var f := get_viewport().gui_get_focus_owner() if is_inside_tree() else null
|
|
|
|
|
if f and is_ancestor_of(f) and f.has_meta(InspectorKit.KEY):
|
|
|
|
|
focus_key = str(f.get_meta(InspectorKit.KEY))
|
|
|
|
|
if f is LineEdit:
|
|
|
|
|
caret = (f as LineEdit).caret_column
|
|
|
|
|
for c in _content.get_children():
|
2026-11-25 16:19:30 -06:00
|
|
|
if c != section_list:
|
|
|
|
|
_content.remove_child(c)
|
|
|
|
|
c.queue_free()
|
2026-11-08 21:24:25 -06:00
|
|
|
_live.clear()
|
|
|
|
|
effects = null
|
|
|
|
|
show_if_builder = null
|
|
|
|
|
case_builder = null
|
2026-11-15 12:35:27 -06:00
|
|
|
pipe_parts = {}
|
|
|
|
|
fields = {}
|
2026-11-08 21:24:25 -06:00
|
|
|
var t := selected_table()
|
|
|
|
|
var e := selected_entry()
|
|
|
|
|
if selection.has("eid") and e == null:
|
|
|
|
|
t = null
|
|
|
|
|
if t == null:
|
|
|
|
|
selection = {}
|
2026-11-25 16:19:30 -06:00
|
|
|
InspectorList.drop_list(self)
|
2026-11-14 13:28:21 -06:00
|
|
|
InspectorFrame.show_empty(self)
|
2026-11-08 21:24:25 -06:00
|
|
|
else:
|
|
|
|
|
match str(selection.get("kind", "card")):
|
|
|
|
|
"pipe":
|
2026-11-25 16:19:30 -06:00
|
|
|
InspectorList.drop_list(self)
|
2026-11-10 19:51:10 -06:00
|
|
|
InspectorThread.section(self, t, e, str(selection.get("to", "")))
|
2026-11-08 21:24:25 -06:00
|
|
|
_:
|
2026-11-15 12:35:27 -06:00
|
|
|
InspectorList.build(self, t)
|
2026-11-08 21:24:25 -06:00
|
|
|
_sig = _signature()
|
|
|
|
|
if main and main.board and main.board.pipes:
|
|
|
|
|
main.board.pipes.select_key = Roller.pipe_key(t.id, e.id, str(selection["to"])) if t and str(selection.get("kind", "")) == "pipe" else ""
|
|
|
|
|
if focus_key != "":
|
|
|
|
|
var c := InspectorKit.find_tagged(_content, focus_key)
|
|
|
|
|
if c:
|
|
|
|
|
c.grab_focus()
|
|
|
|
|
if c is LineEdit and caret >= 0:
|
|
|
|
|
(c as LineEdit).caret_column = caret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _head(kind: String, title: String) -> Control:
|
2026-11-14 13:28:21 -06:00
|
|
|
return InspectorFrame.head(self, kind, title)
|
2026-11-08 21:24:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
static func _title_of(s: String, fallback: String) -> String:
|
|
|
|
|
var t := Formatter.strip_tags(s).strip_edges()
|
|
|
|
|
return t if t != "" else fallback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Closes any open value-picker menu (before an undo rebuilds the board).
|
|
|
|
|
func close_menus() -> void:
|
|
|
|
|
_close_pickers(self)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _close_pickers(n: Node) -> void:
|
|
|
|
|
for c in n.get_children():
|
|
|
|
|
if c is ValuePicker:
|
|
|
|
|
(c as ValuePicker).close_menu()
|
|
|
|
|
_close_pickers(c)
|