Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
class_name MainTemplates
extends RefCounted
## Main's templates: saving cards as templates, inserting, import and export.
## Saved in user://templates (TemplateLibrary), shared by every collection.
## --sample runs keep them in memory only.
2026-11-10 18:24:23 -06:00
const TEMPLATE_FILTER : = " *.json ; Table Tarot template "
2026-11-09 08:21:26 -06:00
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
var m : Main
var _templates : TemplateLibrary
func _init ( p_main : Main ) - > void :
m = p_main
func templates ( ) - > TemplateLibrary :
if _templates == null :
_templates = TemplateLibrary . new ( " user:// " if m . _saving_enabled else " " )
return _templates
## Opens the save dialog for a template of these tables (and groups).
func save_template_from ( table_ids : Array , kind : String , group_ids : Array = [ ] ) - > void :
var tname : = " Template "
if kind == " card " and table_ids . size ( ) == 1 and m . data . get_table ( str ( table_ids [ 0 ] ) ) :
tname = Formatter . strip_tags ( m . data . get_table ( str ( table_ids [ 0 ] ) ) . name )
elif kind == " group " and group_ids . size ( ) == 1 and m . data . get_group ( str ( group_ids [ 0 ] ) ) :
tname = Formatter . strip_tags ( m . data . get_group ( str ( group_ids [ 0 ] ) ) . name )
var tpl : = TemplateKit . make ( m . data , table_ids , kind , tname , " " , group_ids )
if ( tpl [ " tables " ] as Array ) . is_empty ( ) :
m . toast ( " Nothing to save: the template would have no cards " )
return
if kind == " selection " :
tpl [ " name " ] = " %d cards " % ( tpl [ " tables " ] as Array ) . size ( )
2026-11-10 18:40:53 -06:00
var mine : Array = [ ]
for r : Dictionary in templates ( ) . list ( ) :
if not TemplateLibrary . is_builtin ( str ( r [ " id " ] ) ) :
mine . append ( [ str ( r [ " id " ] ) , str ( r [ " name " ] ) ] )
m . _add_modal ( TemplateForms . save_dialog ( tpl , store_template , mine ) )
## Saves a template from the save dialog: new, or over the one it names.
func store_template ( out : Dictionary ) - > void :
var rid : = str ( out . get ( " replace_id " , " " ) )
if rid != " " :
if templates ( ) . replace ( rid , out ) :
m . toast ( " Updated template “ %s ” " % out [ " name " ] )
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
else :
2026-11-10 18:40:53 -06:00
m . toast ( " Couldn ' t update the template " )
return
var id : = templates ( ) . add ( out )
if id == " " :
m . toast ( " Couldn ' t save the template " )
else :
2026-11-29 14:58:56 -06:00
m . toast ( " Saved template “ %s ”. Insert it from the Cards tab (Templates) " % out [ " name " ] )
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
## The selection as a template; groups whose cards are all selected come along.
func save_selection_as_template ( ) - > void :
2026-11-10 18:47:05 -06:00
if m . selection . is_empty ( ) and m . selecting . groups . is_empty ( ) :
m . toast ( " Select some cards or groups first " )
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
return
var gids : Array = [ ]
for g in m . data . groups :
if g . members . is_empty ( ) :
continue
var all : = true
for member in g . members :
if not m . selection . has ( member ) :
all = false
break
if all :
gids . append ( g . id )
2026-11-10 18:47:05 -06:00
for gid : String in m . selecting . groups :
if not gids . has ( gid ) :
gids . append ( gid )
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
save_template_from ( m . selection . keys ( ) , " selection " , gids )
func open_template_picker ( ) - > TemplatePicker :
var p : = TemplatePicker . new ( m , templates ( ) )
m . _add_modal ( p )
return p
## Asks for the template's parameters (if it has any), then inserts it.
2026-11-29 14:58:56 -06:00
## `at`: its top-left corner in world coordinates (INF = the view centre).
func begin_template_insert ( tpl : Dictionary , at : Vector2 = Vector2 . INF ) - > void :
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
if ( tpl . get ( " params " , [ ] ) as Array ) . is_empty ( ) :
2026-11-29 14:58:56 -06:00
insert_template ( tpl , { } , at )
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
return
2026-11-29 14:58:56 -06:00
m . _add_modal ( TemplateForms . params_dialog ( tpl , func ( values : Dictionary ) - > void : insert_template ( tpl , values , at ) ) )
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
2026-11-29 14:58:56 -06:00
## Inserts a template, centred in the view or with its top-left corner at
## `at` (world coordinates): parameters substituted, fresh ids, outside pipes
## relinked by name (or dropped and reported). Returns TemplateKit.instantiate's result.
func insert_template ( tpl : Dictionary , values : Dictionary , at : Vector2 = Vector2 . INF ) - > Dictionary :
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
m . undo_redo . _begin_edit ( )
2026-11-29 14:58:56 -06:00
var origin : = at
if at == Vector2 . INF :
var ext : = TemplateKit . extent ( tpl )
var center : = m . board . screen_to_world ( m . board . size / 2.0 )
origin = ( center - ext / 2.0 ) . round ( )
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
var r : = TemplateKit . insert ( tpl , values , m . data , origin )
for t : RollTable in r [ " tables " ] :
if t . kind == RollTable . KIND_VARIABLE and t . var_name == " " :
t . var_name = m . editing . _free_var_name ( " var " )
var card : = TableCard . new ( )
m . board . cards . add_child ( card )
card . setup ( m , t )
m . cards [ t . id ] = card
m . grouping . _groups_changed ( )
m . undo_redo . _end_edit ( " Insert template %s " % Formatter . strip_tags ( str ( tpl . get ( " name " , " template " ) ) ) )
var n : = ( r [ " tables " ] as Array ) . size ( )
var msg : = " Inserted “ %s ” ( %d %s ) " % [ str ( tpl . get ( " name " , " template " ) ) , n , " card " if n == 1 else " cards " ]
var relinked : Array = r [ " relinked " ]
if not relinked . is_empty ( ) :
msg += " · %d %s relinked by name " % [ relinked . size ( ) , " pipe " if relinked . size ( ) == 1 else " pipes " ]
var dropped : Array = r [ " dropped " ]
if dropped . is_empty ( ) :
m . toast ( msg , true )
else :
m . toast ( msg + " · %d left out " % dropped . size ( ) , true )
var d : = ModalCard . new ( " Inserted “ %s ” " % str ( tpl . get ( " name " , " template " ) ) , 540.0 )
2026-11-10 18:24:23 -06:00
d . body . add_child ( ModalCard . note ( " These threads led to cards outside the template, and this spread has no card with that name, so they were left out. Lay them again by hand, or rename a card and insert again. " , Style . INK ) )
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
d . body . add_child ( ModalCard . section ( " Left out ( %d ) " % dropped . size ( ) ) )
for what : String in dropped :
d . body . add_child ( ModalCard . note ( " • " + what , Style . INK ) )
if not relinked . is_empty ( ) :
d . body . add_child ( ModalCard . section ( " Relinked ( %d ) " % relinked . size ( ) ) )
for what : String in relinked :
d . body . add_child ( ModalCard . note ( " • " + what ) )
d . add_button ( " Done " , " primary " , d . close )
m . _add_modal ( d )
return r
func export_template ( id : String ) - > void :
var text : = templates ( ) . export_text ( id )
if text == " " :
m . toast ( " Couldn ' t read that template " )
return
2026-11-09 08:21:26 -06:00
m . export_dialog ( " Export template " , TEMPLATE_FILTER ,
id . trim_prefix ( TemplateLibrary . BUILTIN_PREFIX ) + " .template.json " , func ( ) - > String : return text )
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
func import_template_file ( picker : TemplatePicker = null ) - > void :
2026-11-09 08:21:26 -06:00
m . file_dialog ( " Import a template " , [ TEMPLATE_FILTER ] , FileDialog . FILE_MODE_OPEN_FILE , func ( path : String ) - > void :
Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):
main_cli.gd command-line and screenshot flags
main_top_bar.gd top bar, toast, endless-loop chip
main_editing.gd add/delete cards, results and pipes, selection bar
main_groups.gd groups and card drags
main_templates.gd templates
main_rolling.gd rolling, choice questions, playback, session seed, copy
main_collections.gd collections, import/export, table settings, test board
main_state.gd decks, variables, roll history (local state)
main_navigation.gd search, focus, minimap, arrange/pack
main_history.gd undo/redo and mark_dirty
main_inspector.gd inspector selection and refresh
Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00
var r : = templates ( ) . import_text ( FileAccess . get_file_as_string ( path ) )
if r [ " ok " ] :
m . toast ( " Imported template “ %s ” " % r [ " name " ] )
if is_instance_valid ( picker ) :
picker . refresh ( )
picker . select ( r [ " id " ] )
else :
m . _add_modal ( IoMenu . report_dialog ( " Couldn ' t import %s " % path . get_file ( ) , { " errors " : r [ " errors " ] } ) )
)