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. ## The default and smallest widths (the player can drag it wider) and the ## collapsed strip's width. const WIDTH := 340.0 const MIN_WIDTH := 280.0 const STRIP := 30.0 var width := WIDTH ## The drag handle on the inspector's left edge, shown with it when open. var splitter: PanelSplitter 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] = [] var _title_label: Label # 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 = {} ## Named fields of the active section ("name", "dice", "text", ...). var fields: Dictionary = {} ## 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 = {} 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 visibility_changed.connect(_sync_splitter) InspectorFrame.build(self) rebuild() # ---------- Selection ---------- ## Selecting a card or result on the spread opens its section and closes the rest. func select(sel: Dictionary) -> void: if collapsed: set_collapsed(false) selection = sel var kind := str(sel.get("kind", "")) if kind == "card" or kind == "result": open_sections = {InspectorList.key_of(sel): true} rebuild() _mark_cards() 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) func clear() -> void: selection = {} rebuild() _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 var tid := str(selection.get("tid", "")) var eid := str(selection.get("eid", "")) if str(selection.get("kind", "")) == "result" else "" 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 "") card.set_inspected(card.table.id == tid) func set_collapsed(on: bool) -> void: collapsed = on custom_minimum_size.x = STRIP if on else width _body.visible = not on _strip.visible = on _sync_splitter() if main and main.state: main.state.set_setting("inspector_collapsed", on) ## 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 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() func _signature() -> String: return InspectorSync.signature(self) # ---------- 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() InspectorFrame.refresh_title(self) if rebuild_panel: rebuild() ## 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(), "dice": t.dice, "dice_mod": t.dice_mod, "card_mode": t.card_mode, "deck_empty": t.deck_empty} 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 "" ## A collection card's members: Array of [item, value] (item without the stem). func members(t: RollTable) -> Array: return InspectorSync.members(self, t) func set_pipe_when(to: String, s: String) -> void: InspectorThread.set_when(self, to, s) func set_pipe_otherwise(to: String, on: bool) -> void: InspectorThread.set_otherwise(self, to, on) ## 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: InspectorThread.set_input(self, to, input_name, expr) func set_pipe_count(to: String, s: String) -> void: InspectorThread.set_count(self, to, s) func set_pipe_pause(to: String, on: bool) -> void: InspectorThread.set_pause(self, to, on) func set_pipe_combine(to: String, mode: String) -> void: InspectorThread.set_combine(self, to, mode) func set_pipe_save(to: String, s: String) -> void: InspectorThread.set_save(self, to, s) func set_pipe_color(hex: String, to: String) -> void: InspectorThread.set_color(self, hex, to) 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) 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 ---------- func set_entry_text(s: String) -> void: InspectorResult.set_entry_text(self, s) 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) # ---------- 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(): _content.remove_child(c) c.queue_free() _live.clear() effects = null show_if_builder = null case_builder = null pipe_parts = {} fields = {} section_parts.clear() section_boxes.clear() var t := selected_table() var e := selected_entry() if selection.has("eid") and e == null: t = null if t == null: selection = {} InspectorFrame.show_empty(self) else: match str(selection.get("kind", "card")): "pipe": InspectorThread.section(self, t, e, str(selection.get("to", ""))) _: InspectorList.build(self, t) _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: return InspectorFrame.head(self, kind, title) 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)