class_name MainCollections extends RefCounted ## Main's app shell: collections (switch, new, rename, duplicate, delete), ## import and export, the table settings dialog and the Test board. Pure rules ## live in CollectionSwitcher, IoMenu and TableSettingsDialog. var m: Main func _init(p_main: Main) -> void: m = p_main ## True when collections can be listed and saved (not --sample / --screenshot). func collections_enabled() -> bool: return m._saving_enabled and m.library != null and m.state != null ## Opens the settings dialog for a table (Build mode editor). func open_table_settings(table_id: String) -> void: var t := m.data.get_table(table_id) if t == null: return var d := TableSettingsDialog.new(t) d.template_requested.connect(func() -> void: m.save_template_from([table_id], "card")) m.undo_redo._begin_edit() d.applied.connect(func(result: Dictionary) -> void: if result.get("deck_reset", false) and m.state and m.data.id != "": m.state.clear_deck(m.data.id, t.id) if t.kind == RollTable.KIND_VARIABLE: # Effect chips on other cards name variables by their card. for c in m.cards.values(): (c as TableCard).rebuild() else: var card := m.card_for(t.id) if card: card.rebuild() m.undo_redo._end_edit("Edit settings of %s" % Formatter.strip_tags(t.name)) m.refresh_live() m.toast("Updated %s" % t.name, true) ) m._add_modal(d) # ----- Collections ----- func switch_collection(id: String) -> void: if not collections_enabled() or id == m.data.id: return m.undo_redo._commit_edits() m._save_timer.stop() var b := CollectionSwitcher.switch_collection(m.library, m.data, id) if b == null: m.toast("Couldn't open that collection") return _show_collection(b) ## Swaps the board to `b`: loads that collection's decks, variables and roll ## history (lighting its last roll), rebuilds the cards, fits the view and ## remembers it as the last collection. func _show_collection(b: TowerBoard) -> void: # Waiting edits belong to the collection being left. m.undo_redo._commit_edits() m.data = b m.rolling._token += 1 m.rolling.cancel_choice() m.rolling_ctx = null m.board.pipes.pulse_key = "" m.local_state.load_local_state() if not m.current.is_empty(): m.rolling._light_steps(m.current.get("all", [])) m._rebuild_cards() m.tray.render() if m.state: m.state.set_setting("last_collection", m.data.id) m.state.save() m.top_bar._switcher.refresh() m.top_bar._update_endless_chip() await m.get_tree().process_frame await m.get_tree().process_frame m.board.fit() func new_collection() -> void: if not collections_enabled(): return m._add_modal(ModalCard.ask_text("New collection", "Name", "", "Create", func(n: String) -> void: _show_collection(CollectionSwitcher.create_collection(m.library, m.data, n)) m.toast("Created “%s”" % m.data.name) )) func rename_collection() -> void: if not collections_enabled(): return m._add_modal(ModalCard.ask_text("Rename collection", "Name", m.data.name, "Rename", func(n: String) -> void: if CollectionSwitcher.rename_collection(m.library, m.data, n): m.top_bar._switcher.refresh() )) func duplicate_collection() -> void: if not collections_enabled(): return var copy := CollectionSwitcher.duplicate_collection(m.library, m.data, m.data.name + " copy") if copy == null: m.toast("Couldn't duplicate the collection") return _show_collection(copy) m.toast("Duplicated as “%s”" % copy.name) func delete_collection() -> void: if not collections_enabled(): return var msg := "Delete “%s” and its saved decks, variables and history? This can't be undone." % m.data.name m._add_modal(ModalCard.confirm("Delete collection", msg, "Delete", func() -> void: var gone := m.data.name var gone_id := m.data.id var next := CollectionSwitcher.delete_collection(m.library, m.data) if next: m.undo_redo._histories.erase(gone_id) _show_collection(next) m.toast("Deleted “%s”" % gone) , true)) # ----- Import and export ----- func export_collection(format: Dictionary) -> void: var dlg := FileDialog.new() dlg.use_native_dialog = true dlg.access = FileDialog.ACCESS_FILESYSTEM dlg.file_mode = FileDialog.FILE_MODE_SAVE_FILE dlg.title = "Export as " + str(format["label"]) dlg.filters = PackedStringArray([format["filter"]]) dlg.current_file = IoMenu.default_file_name(m.data, format) dlg.file_selected.connect(func(path: String) -> void: var err := TowerBoard.write_text_atomic(path, IoMenu.export_text(str(format["key"]), m.data)) m.toast("Exported to %s" % path.get_file() if err == OK else "Couldn't write %s" % path.get_file()) ) dlg.visibility_changed.connect(func() -> void: if not dlg.visible: dlg.queue_free.call_deferred() ) m.add_child(dlg) dlg.popup_centered_ratio(0.6) func import_collection() -> void: if not collections_enabled(): return var dlg := FileDialog.new() dlg.use_native_dialog = true dlg.access = FileDialog.ACCESS_FILESYSTEM dlg.file_mode = FileDialog.FILE_MODE_OPEN_FILE dlg.title = "Import as a new collection" dlg.filters = PackedStringArray(IoMenu.IMPORT_FILTERS) dlg.file_selected.connect(_import_file) dlg.visibility_changed.connect(func() -> void: if not dlg.visible: dlg.queue_free.call_deferred() ) m.add_child(dlg) dlg.popup_centered_ratio(0.6) func _import_file(path: String) -> void: if not FileAccess.file_exists(path): m.toast("Couldn't read %s" % path.get_file()) return m._save_now() var r := IoMenu.import_as_collection(m.library, FileAccess.get_file_as_string(path), path.get_file()) if r["ok"]: _show_collection(r["board"]) var has_report: bool = not (r["errors"] as Array).is_empty() or not (r["warnings"] as Array).is_empty() \ or not (r["guesses"] as Array).is_empty() if has_report or not r["ok"]: m._add_modal(IoMenu.report_dialog( "Imported “%s”" % r["board"].name if r["ok"] else "Couldn't import %s" % path.get_file(), r)) elif r["ok"]: m.toast("Imported “%s” as a new collection" % r["board"].name) # ----- Test board ----- ## Build: opens the Test board dialog (core/board_tester.gd), which keeps its ## last report while the app is open. func open_test_board() -> void: m._add_modal(TestBoardDialog.new(m))