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>
591 lines
17 KiB
GDScript
591 lines
17 KiB
GDScript
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)
|
|
|
|
const HISTORY_SIZE := 6
|
|
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 pipe to edit it in the inspector. Drag from a ● to another card to lay a pipe.",
|
|
}
|
|
|
|
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, Ctrl-drag on the felt).
|
|
var selection: Dictionary = {}
|
|
|
|
var _save_timer: Timer
|
|
var _saving_enabled := true
|
|
|
|
# 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 _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
|
|
var cli: MainCli
|
|
|
|
|
|
func _init() -> void:
|
|
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)
|
|
prompt_hook = rolling._ask_player
|
|
|
|
|
|
func _ready() -> void:
|
|
rng.randomize()
|
|
theme = Style.app_theme()
|
|
var args := OS.get_cmdline_user_args()
|
|
if args.has("--sample") or MainCli._arg_value(args, "--screenshot") != "":
|
|
_saving_enabled = false
|
|
if _saving_enabled:
|
|
_open_library()
|
|
if data == null:
|
|
data = SampleBoard.create()
|
|
data.id = "fantasy-loot"
|
|
data.name = "Fantasy Loot"
|
|
|
|
_build_ui()
|
|
_rebuild_cards()
|
|
rolling._seed_session()
|
|
_update_collapse_all()
|
|
# Let the cards lay out before fitting them to the window.
|
|
await get_tree().process_frame
|
|
await get_tree().process_frame
|
|
board.fit()
|
|
cli._run_cli(args)
|
|
|
|
|
|
func _exit_tree() -> void:
|
|
_save_now()
|
|
|
|
|
|
## Migrates the old board.json if needed, then opens the last collection (or the
|
|
## first one, or a new "Fantasy Loot" made from the sample tables).
|
|
func _open_library() -> void:
|
|
state = StateStore.new()
|
|
library = LibraryStore.new("user://", state)
|
|
library.migrate_legacy()
|
|
var last := str(state.get_setting("last_collection", ""))
|
|
if last != "":
|
|
data = library.load_collection(last)
|
|
if data == null:
|
|
for entry in library.list():
|
|
data = library.load_collection(str(entry["id"]))
|
|
if data:
|
|
break
|
|
if data == null:
|
|
data = library.create("Fantasy Loot")
|
|
data.tables = SampleBoard.create().tables
|
|
library.save(data)
|
|
state.set_setting("last_collection", data.id)
|
|
|
|
|
|
func _save_now() -> void:
|
|
if _saving_enabled and data and library:
|
|
library.save(data)
|
|
state.set_setting("last_collection", data.id)
|
|
state.save()
|
|
|
|
|
|
# ---------- UI ----------
|
|
|
|
func _build_ui() -> void:
|
|
set_anchors_and_offsets_preset(PRESET_FULL_RECT)
|
|
var root := VBoxContainer.new()
|
|
root.set_anchors_and_offsets_preset(PRESET_FULL_RECT)
|
|
root.add_theme_constant_override("separation", 0)
|
|
add_child(root)
|
|
root.add_child(top_bar._build_top_bar())
|
|
var middle := HBoxContainer.new()
|
|
middle.add_theme_constant_override("separation", 0)
|
|
middle.size_flags_vertical = SIZE_EXPAND_FILL
|
|
root.add_child(middle)
|
|
board = BoardView.new()
|
|
board.setup(self)
|
|
board.size_flags_horizontal = SIZE_EXPAND_FILL
|
|
middle.add_child(board)
|
|
inspector = Inspector.new()
|
|
inspector.setup(self)
|
|
inspector.visible = false
|
|
if state and bool(state.get_setting("inspector_collapsed", false)):
|
|
inspector.set_collapsed(true)
|
|
middle.add_child(inspector)
|
|
editing._build_selection_bar()
|
|
tray = ResultTray.new()
|
|
tray.setup(self)
|
|
root.add_child(tray)
|
|
top_bar._build_toast()
|
|
undo_redo._build_edit_timer()
|
|
|
|
_save_timer = Timer.new()
|
|
_save_timer.one_shot = true
|
|
_save_timer.wait_time = SAVE_DELAY
|
|
_save_timer.timeout.connect(func() -> void:
|
|
_save_now()
|
|
)
|
|
add_child(_save_timer)
|
|
resized.connect(top_bar._place_toast)
|
|
|
|
|
|
# ---------- 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))
|
|
|
|
|
|
## Rebuilds every card. Cards hidden in collapsed groups aren't built at all
|
|
## (card_for gives null for them) until their group expands; see
|
|
## _apply_group_visibility. Called from outside an edit (a board loaded or
|
|
## replaced), it also takes the board as the edit history's new baseline.
|
|
func _rebuild_cards() -> void:
|
|
if not undo_redo._keep_history:
|
|
undo_redo._sync_history()
|
|
for c in board.cards.get_children():
|
|
board.cards.remove_child(c)
|
|
c.queue_free()
|
|
cards.clear()
|
|
selection.clear()
|
|
grouping._rebuild_groups()
|
|
editing._update_selection_bar()
|
|
_update_collapse_all()
|
|
# A board loaded or replaced starts with nothing in the inspector; an
|
|
# undo keeps the selection (the inspector follows the board).
|
|
if inspector and not undo_redo._keep_history:
|
|
inspector.clear()
|
|
elif inspector:
|
|
inspector.sync()
|
|
|
|
|
|
func _refresh_lit() -> void:
|
|
view_rev += 1
|
|
for card in cards.values():
|
|
(card as TableCard).refresh_lit()
|
|
grouping._refresh_group_lits()
|
|
if inspector:
|
|
inspector.sync()
|
|
|
|
|
|
func set_mode(m: Mode) -> void:
|
|
if m == Mode.BUILD and rolling._choice_waiting("Answer the waiting question before building"):
|
|
top_bar._build_button.set_pressed_no_signal(mode == Mode.BUILD)
|
|
top_bar._play_button.set_pressed_no_signal(mode == Mode.PLAY)
|
|
return
|
|
mode = m
|
|
top_bar._play_button.set_pressed_no_signal(m == Mode.PLAY)
|
|
top_bar._build_button.set_pressed_no_signal(m == Mode.BUILD)
|
|
# Build mode's bar is full of tools, so its hint moves into the tooltip;
|
|
# the (empty) label still stretches to push the right-hand buttons over.
|
|
top_bar._hint.text = HINTS[m] if m == Mode.PLAY else ""
|
|
top_bar._hint.tooltip_text = HINTS[m]
|
|
top_bar._hint.custom_minimum_size.x = 60.0 if m == Mode.PLAY else 0.0
|
|
# Build's bar has no room for Collapse all; it's in Arrange ▾ there.
|
|
top_bar._collapse_all.visible = m != Mode.BUILD
|
|
top_bar._build_tools.visible = m == Mode.BUILD
|
|
board.pipes.hover_key = ""
|
|
clear_selection()
|
|
inspector.visible = m == Mode.BUILD
|
|
if m != Mode.BUILD:
|
|
inspector.clear()
|
|
for card in cards.values():
|
|
(card as TableCard).rebuild()
|
|
for n in grouping._group_nodes.values():
|
|
(n as GroupCard).rebuild()
|
|
top_bar._update_endless_chip()
|
|
nav._nav_mode_changed()
|
|
|
|
|
|
## What every change to the board needs: a save soon, fresh caches and redraws.
|
|
func _board_changed() -> void:
|
|
if _saving_enabled:
|
|
_save_timer.start()
|
|
local_state._ref_cache.clear()
|
|
view_rev += 1
|
|
edit_rev += 1
|
|
_update_collapse_all()
|
|
top_bar._schedule_endless_check()
|
|
if mode == Mode.BUILD:
|
|
refresh_live()
|
|
if inspector:
|
|
inspector.sync()
|
|
|
|
|
|
## True when an expanded group, or a table that isn't hidden in a collapsed
|
|
## group, could still be collapsed.
|
|
func _any_expanded() -> bool:
|
|
for g in data.groups:
|
|
if not g.collapsed:
|
|
return true
|
|
var hidden := hidden_groups()
|
|
for t in data.tables:
|
|
if not t.collapsed and not hidden.has(t.id):
|
|
return true
|
|
return false
|
|
|
|
|
|
func _update_collapse_all() -> void:
|
|
if top_bar._collapse_all:
|
|
top_bar._collapse_all.text = "Collapse all" if _any_expanded() else "Expand all"
|
|
|
|
|
|
# ---------- Collapsing ----------
|
|
|
|
func toggle_collapse(tid: String) -> void:
|
|
var t := data.get_table(tid)
|
|
if t == null:
|
|
return
|
|
t.collapsed = not t.collapsed
|
|
card_for(tid).rebuild()
|
|
mark_dirty("", false)
|
|
|
|
|
|
## Collapses or expands every table and every group.
|
|
func set_all_collapsed(collapsed: bool) -> void:
|
|
for t in data.tables:
|
|
if t.collapsed != collapsed:
|
|
t.collapsed = collapsed
|
|
var card := card_for(t.id)
|
|
if card:
|
|
card.rebuild()
|
|
var changed := false
|
|
for g in data.groups:
|
|
if g.collapsed != collapsed:
|
|
g.collapsed = collapsed
|
|
changed = true
|
|
if changed:
|
|
grouping._rebuild_groups()
|
|
mark_dirty("", false)
|
|
|
|
|
|
## 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()
|
|
|
|
|
|
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
|
|
|
|
|
|
# ---------- Input ----------
|
|
|
|
func _shortcut_input(ev: InputEvent) -> void:
|
|
var k := ev as InputEventKey
|
|
if k == null or not k.pressed:
|
|
return
|
|
if undo_redo._history_shortcut(k):
|
|
get_viewport().set_input_as_handled()
|
|
return
|
|
if k.echo:
|
|
return
|
|
if k.keycode == KEY_F and (k.ctrl_pressed or k.meta_pressed) and not k.alt_pressed:
|
|
nav.open_search()
|
|
get_viewport().set_input_as_handled()
|
|
elif k.keycode == KEY_M and not (k.ctrl_pressed or k.meta_pressed or k.alt_pressed or k.shift_pressed):
|
|
var f := get_viewport().gui_get_focus_owner()
|
|
if f is LineEdit or f is TextEdit:
|
|
return
|
|
nav.set_minimap_visible(not board.minimap.visible, true)
|
|
get_viewport().set_input_as_handled()
|
|
|
|
|
|
func _unhandled_input(ev: InputEvent) -> void:
|
|
var k := ev as InputEventKey
|
|
if k == null or not k.pressed or k.echo or mode != Mode.BUILD:
|
|
return
|
|
if k.keycode == KEY_G and (k.ctrl_pressed or k.meta_pressed):
|
|
grouping.group_selection()
|
|
get_viewport().set_input_as_handled()
|
|
elif k.keycode == KEY_ESCAPE and not selection.is_empty():
|
|
clear_selection()
|
|
get_viewport().set_input_as_handled()
|
|
|
|
|
|
# ---------- Rules view (docs/PLAIN.md §6) ----------
|
|
|
|
## When there's no `state` (--sample), the rules view lives here.
|
|
var _rules_view_mem := "plain"
|
|
|
|
|
|
## "plain" (Describe's phrases) or "text" (the stored rule text).
|
|
func rules_view() -> String:
|
|
return state.rules_view() if state else _rules_view_mem
|
|
|
|
|
|
func rules_plain() -> bool:
|
|
return rules_view() == "plain"
|
|
|
|
|
|
## Shows rules as "plain" words or as "text"; saved as a setting. Emits
|
|
## rules_view_changed when it changes.
|
|
func set_rules_view(v: String) -> void:
|
|
if v != "plain" and v != "text":
|
|
return
|
|
if v == rules_view():
|
|
top_bar._sync_rules_switch()
|
|
return
|
|
if state:
|
|
state.set_rules_view(v)
|
|
state.save()
|
|
_rules_view_mem = v
|
|
top_bar._sync_rules_switch()
|
|
if inspector:
|
|
inspector.rebuild()
|
|
rules_view_changed.emit(v)
|
|
|
|
|
|
# ---------- Delegators: what other files call on Main ----------
|
|
|
|
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 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.begin_card_drag(card)
|
|
|
|
func drag_card(card: TableCard, to: Vector2) -> void:
|
|
grouping.drag_card(card, to)
|
|
|
|
func end_card_drag(_card: TableCard) -> void:
|
|
grouping.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() -> void:
|
|
collections.new_collection()
|
|
|
|
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)
|