class_name Main extends Control ## The app window: top bar, felt board and result tray. Owns the board data, ## the Play/Build mode, roll animation, and editing actions with undo and ## redo (EditHistory, one per collection; see ui/main_history.gd). ## ## Main is a shell: the shared state, start-up and the cards. Each area lives ## in a helper (ui/main_*.gd) holding its own state; the methods other files ## call stay here as one-line delegators (bottom of this file). enum Mode { PLAY, BUILD } ## The rules view changed ("plain" or "text"; see set_rules_view). signal rules_view_changed(view: String) ## Earlier rolls kept in the tray (a batch of rolls fills several). const HISTORY_SIZE := 30 const SAVE_DELAY := 0.6 const HINTS := { Mode.PLAY: "Click a die to roll. Shift-click skips straight to the result.", Mode.BUILD: "Click a card, a result or a thread to edit it in the inspector. Drag from a ● to another card to lay a thread.", } var data: TowerBoard var library: LibraryStore var state: StateStore var mode: Mode = Mode.PLAY var cards: Dictionary = {} # table id -> TableCard var lit_entries: Dictionary = {} # "tid:eid" -> true var lit_pipes: Dictionary = {} # "tid:eid>to" -> true var current: Dictionary = {} var history: Array = [] var rng := RandomNumberGenerator.new() ## Bumped by mark_dirty and _refresh_lit; BoardView redraws its layers when it changes. var view_rev := 0 ## Bumped by mark_dirty: the search index is rebuilt when it changes. var edit_rev := 0 var board: BoardView var tray: ResultTray ## The Build-mode inspector docked right of the board (ui/inspector.gd). var inspector: Inspector ## Table ids selected in Build mode (Ctrl/Shift-click, or a drag box on empty space). var selection: Dictionary = {} ## Groups in the Build selection (MainSelection). var selecting: MainSelection ## The List view panel (left dock). var list_view: ListView ## The left dock: Cards (the card palette) | List (list_view). var left_dock: LeftDock ## Main menu, spread or Sheets (MainScreens). var screens: MainScreens ## Sheets placed on the spread and their @variables (MainSheetLinks). var sheet_links: MainSheetLinks ## The phone layout, when it is on (MobileLayout.wanted). var mobile: MobileLayout ## The Book panel (reusable results). var book: BookPanel ## Help's demo spreads (shown unsaved). var demo: DemoMode var _save_timer: Timer var _saving_enabled := true ## When there's no `state` (--sample), the rules view lives here (docs/PLAIN.md §6). var _rules_view_mem := "plain" # Local state for the current collection (see load_local_state). Kept in memory # and written to `state` when there is one (saving disabled: memory only). var decks: Dictionary = {} # table id -> DeckState var persistent_vars: Dictionary = {} # name without "@" -> {"value", "source"} var lit_texts: Dictionary = {} # "tid:eid" -> expanded BBCode from the last roll var lit_columns: Dictionary = {} # "tid:eid" -> the rolled node's "columns" (TableCardColumns) var _details_popup: DetailsPopup ## Choice cards: func(request: Dictionary) -> Variant, awaited by the roller ## (docs/LOGIC.md §8). It opens the choice pop-up (_ask_player); tests swap in ## a script. var prompt_hook: Callable = Callable() ## The context of the roll in progress, or null; its root is the tree so far ## (Roller.progress()). var rolling_ctx: RollContext = null # The helpers, one per area. var top_bar: MainTopBar var editing: MainEditing var grouping: MainGroups var templating: MainTemplates var rolling: MainRolling var collections: MainCollections var local_state: MainState var nav: MainNavigation var undo_redo: MainHistory var inspecting: MainInspectorLink ## The theme's sounds (made in _ready, once the theme is applied). var sound: SoundBoard var cli: MainCli var layout: MainLayout var card_refresh: CardRefreshQueue func _init() -> void: layout = MainLayout.new(self) sheet_links = MainSheetLinks.new(self) card_refresh = CardRefreshQueue.new(self) top_bar = MainTopBar.new(self) editing = MainEditing.new(self) grouping = MainGroups.new(self) templating = MainTemplates.new(self) rolling = MainRolling.new(self) collections = MainCollections.new(self) local_state = MainState.new(self) nav = MainNavigation.new(self) undo_redo = MainHistory.new(self) inspecting = MainInspectorLink.new(self) cli = MainCli.new(self) selecting = MainSelection.new(self) demo = DemoMode.new(self) prompt_hook = rolling._ask_player func _ready() -> void: rng.randomize() # The project keeps its old internal name so the user:// folder (saved # spreads) doesn't move; the window shows the app's name. get_window().title = "Table Tarot" var args := OS.get_cmdline_user_args() if args.has("--sample") or MainCli._arg_value(args, "--screenshot") != "": _saving_enabled = false if _saving_enabled: layout._open_library() if ThemeStore.carry_board != null: if not _saving_enabled: data = ThemeStore.carry_board ThemeStore.carry_board = null var forced_theme := MainCli._arg_value(args, "--theme") if forced_theme != "": ThemeStore.apply(ThemeStore.find(forced_theme)) elif ThemeStore.current.is_empty(): ThemeStore.apply(ThemeStore.find(str(state.get_setting(ThemeStore.SETTING, ThemeStore.DEFAULT_ID)) if state else ThemeStore.DEFAULT_ID)) theme = Style.app_theme() sound = SoundBoard.new(self) add_child(sound) if data == null: data = SampleBoard.create() data.id = "fantasy-loot" data.name = "Fantasy Loot" layout._build_ui() add_child(TouchInput.new(self)) if MobileLayout.wanted(): mobile = MobileLayout.new(self) mobile.apply() _rebuild_cards() rolling._seed_session() # Let the cards lay out before fitting them to the window. await get_tree().process_frame await get_tree().process_frame board.fit() screens.start(args) cli._run_cli(args) if screens.showing(MainScreens.SPREAD): WelcomeDialog.maybe_show(self) func _exit_tree() -> void: # A roll still animating must not go on after this scene is freed (a theme # change mid-roll reloads it). rolling._token += 1 _save_now() if screens: screens.sheets.leave() Roller.sheet_text = Callable() # ---------- Cards ---------- func card_for(id: String) -> TableCard: return cards.get(id) func is_entry_lit(tid: String, eid: String) -> bool: return lit_entries.has(Roller.entry_key(tid, eid)) # ---------- Live cards and pop-ups ---------- ## Re-renders the live cards (variables, sheets, questions). Called after a ## roll, after variable edits and after edits in Build, never per frame, so ## dice inside {= } don't re-roll all the time. func refresh_live() -> void: for card in cards.values(): (card as TableCard).refresh_live() sheet_links.refresh_previews() func _add_modal(d: ModalCard) -> void: add_child(d) ## The shared hover pop-up for details. func details() -> DetailsPopup: if _details_popup == null: _details_popup = DetailsPopup.new() add_child(_details_popup) _details_popup.setup(self) return _details_popup # ---------- Delegators: what other files call on Main ---------- # One block per helper; each line pair passes the call straight on. func toast(message: String, undoable: bool = false) -> void: top_bar.toast(message, undoable) func add_variable_card(label: String, var_name: String, type: String = "number") -> RollTable: return editing.add_variable_card(label, var_name, type) func add_entry(tid: String) -> void: editing.add_entry(tid) func delete_entry(tid: String, eid: String) -> void: editing.delete_entry(tid, eid) func delete_table(tid: String) -> void: editing.delete_table(tid) func remove_pipe(key: String) -> void: editing.remove_pipe(key) func start_link(tid: String, eid: String) -> void: editing.start_link(tid, eid) func update_link() -> void: editing.update_link() func link_snap_point() -> Variant: return editing.link_snap_point() func finish_link() -> void: editing.finish_link() func is_selected(tid: String) -> bool: return editing.is_selected(tid) func toggle_selected(tid: String) -> void: editing.toggle_selected(tid) func clear_selection() -> void: editing.clear_selection() func select_in_rect(r: Rect2) -> void: editing.select_in_rect(r) func group_node(gid: String) -> GroupCard: return grouping.group_node(gid) func hidden_groups() -> Dictionary: return grouping.hidden_groups() func lit_keys_for_group(g: TableGroup) -> Array: return grouping.lit_keys_for_group(g) func toggle_group(gid: String) -> void: grouping.toggle_group(gid) func move_group(gid: String, delta: Vector2) -> void: grouping.move_group(gid, delta) func ungroup(gid: String) -> void: grouping.ungroup(gid) func confirm_delete_group(gid: String) -> void: grouping.confirm_delete_group(gid) func edit_group_details(gid: String) -> void: grouping.edit_group_details(gid) func begin_card_drag(card: TableCard) -> void: grouping.card_drag.begin_card_drag(card) func drag_card(card: TableCard, to: Vector2) -> void: grouping.card_drag.drag_card(card, to) func end_card_drag(_card: TableCard) -> void: grouping.card_drag.end_card_drag(_card) func save_template_from(table_ids: Array, kind: String, group_ids: Array = []) -> void: templating.save_template_from(table_ids, kind, group_ids) func begin_template_insert(tpl: Dictionary) -> void: templating.begin_template_insert(tpl) func export_template(id: String) -> void: templating.export_template(id) func import_template_file(picker: TemplatePicker = null) -> void: templating.import_template_file(picker) func start_roll(tid: String, fast: bool = false) -> void: await rolling.start_roll(tid, fast) func copy_current(kind: int) -> void: rolling.copy_current(kind) func collections_enabled() -> bool: return collections.collections_enabled() func open_table_settings(table_id: String) -> void: collections.open_table_settings(table_id) func switch_collection(id: String) -> void: collections.switch_collection(id) func new_collection(folder: String = "") -> void: collections.new_collection(folder) func rename_collection() -> void: collections.rename_collection() func duplicate_collection() -> void: collections.duplicate_collection() func delete_collection() -> void: collections.delete_collection() func cards_left(tid: String) -> Vector2i: return local_state.cards_left(tid) func reshuffle_table(tid: String) -> void: local_state.reshuffle_table(tid) func lit_text(tid: String, eid: String) -> String: return local_state.lit_text(tid, eid) func ref_targets_for(e: TableEntry) -> Array[String]: return local_state.ref_targets_for(e) func set_variable(name: String, value: String) -> void: local_state.set_variable(name, value) func clear_variable(name: String) -> void: local_state.clear_variable(name) func variable_card(name: String) -> RollTable: return local_state.variable_card(name) func variable_names() -> Array[String]: return local_state.variable_names() func reset_variable(name: String) -> void: local_state.reset_variable(name) func reset_all_variables() -> void: local_state.reset_all_variables() func reroll_variable(name: String) -> void: local_state.reroll_variable(name) func can_reroll_variable(name: String) -> bool: return local_state.can_reroll_variable(name) func set_collection_member(t: RollTable, item: String, value: String) -> void: local_state.set_collection_member(t, item, value) func remove_collection_member(t: RollTable, item: String) -> void: local_state.remove_collection_member(t, item) func focus_table(table_id: String, entry_id: String = "") -> void: await nav.focus_table(table_id, entry_id) func focus_group(gid: String) -> void: nav.focus_group(gid) func mark_dirty(label: String = "", undoable: bool = true) -> void: undo_redo.mark_dirty(label, undoable) func open_pipe_options(key: String, at: Vector2) -> void: inspecting.open_pipe_options(key, at) func request_table_settings(tid: String) -> void: inspecting.request_table_settings(tid) func select_card(table_id: String) -> void: inspecting.select_card(table_id) func select_result(table_id: String, entry_id: String) -> void: inspecting.select_result(table_id, entry_id) func clear_inspector() -> void: inspecting.clear_inspector() func inspector_selection() -> Dictionary: return inspecting.inspector_selection() func inspector_edited(tid: String, all_cards: bool = false) -> void: inspecting.inspector_edited(tid, all_cards) # ---------- Delegators to MainLayout ---------- func _save_now() -> void: layout._save_now() func _rebuild_cards() -> void: layout._rebuild_cards() func _refresh_lit() -> void: layout._refresh_lit() func set_mode(m: Mode) -> void: layout.set_mode(m) func _board_changed() -> void: layout._board_changed() func _any_expanded() -> bool: return layout._any_expanded() func toggle_collapse(tid: String) -> void: layout.toggle_collapse(tid) func set_all_collapsed(collapsed: bool) -> void: layout.set_all_collapsed(collapsed) ## Opens the system file dialog; `on_path` gets the chosen path. func file_dialog(title: String, filters: Array, mode: FileDialog.FileMode, on_path: Callable, file_name: String = "") -> void: layout.file_dialog(title, filters, mode, on_path, file_name) func export_dialog(title: String, filter: String, file_name: String, text_for: Callable) -> void: layout.export_dialog(title, filter, file_name, text_for) ## "plain" (Describe's phrases) or "text" (the stored rule text). func rules_view() -> String: return layout.rules_view() func rules_plain() -> bool: return layout.rules_plain() func set_rules_view(v: String) -> void: layout.set_rules_view(v) func _shortcut_input(ev: InputEvent) -> void: layout._shortcut_input(ev) func _unhandled_input(ev: InputEvent) -> void: layout._unhandled_input(ev)