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 = { }
## Named fields of the card section ("name", "dice", "check_mod", ...).
var fields : 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
2026-11-09 10:01:10 -06:00
visibility_changed . connect ( _sync_splitter )
2026-11-08 21:24:25 -06:00
var sb : = Style . box ( Style . CARD , 0 , [ 0 , 0 , 0 , 0 ] )
sb . border_width_left = 1
sb . border_color = Color ( 0 , 0 , 0 , 0.25 )
add_theme_stylebox_override ( " panel " , sb )
var stack : = Control . new ( )
stack . size_flags_vertical = Control . SIZE_EXPAND_FILL
add_child ( stack )
_body = VBoxContainer . new ( )
_body . set_anchors_and_offsets_preset ( Control . PRESET_FULL_RECT )
_body . add_theme_constant_override ( " separation " , 0 )
stack . add_child ( _body )
_scroll = ScrollContainer . new ( )
_scroll . horizontal_scroll_mode = ScrollContainer . SCROLL_MODE_DISABLED
_scroll . size_flags_vertical = Control . SIZE_EXPAND_FILL
_body . add_child ( _scroll )
var pad : = MarginContainer . new ( )
pad . size_flags_horizontal = Control . SIZE_EXPAND_FILL
for k in [ " margin_left " , " margin_right " ] :
pad . add_theme_constant_override ( k , 16 )
pad . add_theme_constant_override ( " margin_top " , 14 )
pad . add_theme_constant_override ( " margin_bottom " , 18 )
_scroll . add_child ( pad )
_content = InspectorKit . vbox ( 14 )
pad . add_child ( _content )
_strip = PanelContainer . new ( )
_strip . set_anchors_and_offsets_preset ( Control . PRESET_FULL_RECT )
_strip . add_theme_stylebox_override ( " panel " , Style . box ( Style . CARD , 0 , [ 2 , 8 , 2 , 8 ] ) )
var open : = Button . new ( )
open . text = " « "
open . tooltip_text = " Show the inspector "
open . size_flags_vertical = Control . SIZE_SHRINK_BEGIN
Style . skin_button ( open , Style . box ( Color ( 0 , 0 , 0 , 0 ) , 5 , [ 4 , 2 , 4 , 2 ] ) , Style . box ( Style . HOVER_ROW , 5 , [ 4 , 2 , 4 , 2 ] ) ,
Theme system: runtime colour tokens, theme sets and a Theme dialog
Style's colours become static vars named by role (BG, PANEL, CARD, THREAD,
ACCENT, TRIM, GLOW…) instead of materials (felt, copper, brass), so a theme
can replace them. Stray hard-coded colours moved into tokens.
ThemeStore applies built-in sets (Tarot, the new default: jewel-purple
cloth with a shine; Felt, the old look) and the user's own sets from
user://themes/. Fills for the background and panels can be gradients; the
background pattern (dots, stars, none) and logo are per set. View ▾ →
Theme… edits a set; changes to a built-in save as a new set. Using a theme
reloads the UI and keeps the open spread.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:10:16 -06:00
Style . box ( Style . HOVER_ROW , 5 , [ 4 , 2 , 4 , 2 ] ) , Style . TRIM_DARK )
2026-11-08 21:24:25 -06:00
open . add_theme_font_size_override ( " font_size " , 18 )
open . pressed . connect ( func ( ) - > void : set_collapsed ( false ) )
_strip . add_child ( open )
_strip . hide ( )
stack . add_child ( _strip )
rebuild ( )
# ---------- Selection ----------
func select ( sel : Dictionary ) - > void :
if collapsed :
set_collapsed ( false )
selection = sel
rebuild ( )
2026-11-08 21:36:29 -06:00
_mark_cards ( )
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
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 " " )
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 ( )
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-08 21:29:59 -06:00
_refresh_title ( )
2026-11-08 21:24:25 -06:00
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 ( ) ,
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-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 ( ) :
_content . remove_child ( c )
c . queue_free ( )
_live . clear ( )
effects = null
show_if_builder = null
case_builder = null
pipe_parts . clear ( )
fields . clear ( )
var t : = selected_table ( )
var e : = selected_entry ( )
if selection . has ( " eid " ) and e == null :
t = null
if t == null :
selection = { }
_empty ( )
else :
match str ( selection . get ( " kind " , " card " ) ) :
" result " :
2026-11-10 19:51:10 -06:00
InspectorResult . section ( self , t , e )
2026-11-08 21:24:25 -06:00
" pipe " :
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
_ :
InspectorCard . build ( self , _content , 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 :
var h : = InspectorKit . hbox ( 6 )
var v : = InspectorKit . vbox ( 2 )
v . size_flags_horizontal = Control . SIZE_EXPAND_FILL
v . add_child ( InspectorKit . flabel ( kind ) )
var tl : = Style . make_label ( title , Style . display ( ) , 22 , Style . INK )
2026-11-08 21:29:59 -06:00
_title_label = tl
2026-11-08 21:24:25 -06:00
tl . autowrap_mode = TextServer . AUTOWRAP_WORD_SMART
tl . custom_minimum_size = Vector2 ( 120 , 0 )
v . add_child ( tl )
h . add_child ( v )
var hide_b : = Button . new ( )
hide_b . text = " » "
hide_b . tooltip_text = " Hide the inspector "
hide_b . size_flags_vertical = Control . SIZE_SHRINK_BEGIN
Style . skin_button ( hide_b , Style . box ( Color ( 0 , 0 , 0 , 0 ) , 5 , [ 6 , 0 , 6 , 2 ] ) , Style . box ( Style . HOVER_ROW , 5 , [ 6 , 0 , 6 , 2 ] ) ,
Style . box ( Style . HOVER_ROW , 5 , [ 6 , 0 , 6 , 2 ] ) , Style . MUTED , Style . INK )
hide_b . add_theme_font_size_override ( " font_size " , 18 )
hide_b . focus_mode = Control . FOCUS_NONE
hide_b . pressed . connect ( func ( ) - > void : set_collapsed ( true ) )
h . add_child ( hide_b )
return h
2026-11-08 21:29:59 -06:00
## Keeps the heading in step with the name or text being typed.
func _refresh_title ( ) - > void :
if not is_instance_valid ( _title_label ) :
return
var t : = selected_table ( )
match str ( selection . get ( " kind " , " " ) ) :
" card " :
if t :
_title_label . text = _title_of ( t . name , " (no name) " )
" result " :
var e : = selected_entry ( )
if e :
_title_label . text = _title_of ( e . text , " (no text) " )
2026-11-08 21:24:25 -06:00
func _empty ( ) - > void :
_content . add_child ( _head ( " Inspector " , " Nothing selected " ) )
2026-11-10 18:24:23 -06:00
_content . add_child ( InspectorKit . note ( " Select a card, a result or a thread to edit it. " , Style . MUTED , 15 ) )
_content . add_child ( InspectorKit . note ( " Click a card ' s title for the card, a result ' s text for the result, or a thread on the felt for its options. " , Style . MUTED , 13 ) )
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 )