From 8dcb66ca52bd6fc345b78705a18f9999a4da7a7a Mon Sep 17 00:00:00 2001 From: GriffinR Date: Sun, 16 Mar 2025 19:29:26 -0400 Subject: [PATCH] Move map/layout loaded state to Project --- include/core/map.h | 3 --- include/core/maplayout.h | 1 - include/project.h | 14 ++++++++++++++ src/project.cpp | 25 +++++++++++++------------ src/ui/maplistmodels.cpp | 4 ++-- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/include/core/map.h b/include/core/map.h index a90105b0..c1a14b04 100644 --- a/include/core/map.h +++ b/include/core/map.h @@ -68,12 +68,10 @@ public: void setNeedsHealLocation(bool needsHealLocation) { m_needsHealLocation = needsHealLocation; } void setIsPersistedToFile(bool persistedToFile) { m_isPersistedToFile = persistedToFile; } void setHasUnsavedDataChanges(bool unsavedDataChanges) { m_hasUnsavedDataChanges = unsavedDataChanges; } - void setLoaded(bool loaded) { m_loaded = loaded; } bool needsHealLocation() const { return m_needsHealLocation; } bool isPersistedToFile() const { return m_isPersistedToFile; } bool hasUnsavedDataChanges() const { return m_hasUnsavedDataChanges; } - bool loaded() const { return m_loaded; } void resetEvents(); QList getEvents(Event::Group group = Event::Group::None) const; @@ -121,7 +119,6 @@ private: bool m_hasUnsavedDataChanges = false; bool m_needsHealLocation = false; bool m_scriptsLoaded = false; - bool m_loaded = false; QMap> m_events; QSet m_ownedEvents; // for memory management diff --git a/include/core/maplayout.h b/include/core/maplayout.h index 9b45a689..57da8840 100644 --- a/include/core/maplayout.h +++ b/include/core/maplayout.h @@ -22,7 +22,6 @@ public: static QString layoutConstantFromName(const QString &name); - bool loaded = false; bool hasUnsavedDataChanges = false; QString id; diff --git a/include/project.h b/include/project.h index 23cd0f57..eca85273 100644 --- a/include/project.h +++ b/include/project.h @@ -92,6 +92,11 @@ public: // Note: This does not guarantee the map is loaded. Map* getMap(const QString &mapName) { return this->maps.value(mapName); } + bool isMapLoaded(const Map *map) const { return map && isMapLoaded(map->name()); } + bool isMapLoaded(const QString &mapName) const { return this->loadedMapNames.contains(mapName); } + bool isLayoutLoaded(const Layout *layout) const { return layout && isLayoutLoaded(layout->id); } + bool isLayoutLoaded(const QString &layoutId) const { return this->loadedLayoutIds.contains(layoutId); } + QMap tilesetCache; Tileset* loadTileset(QString, Tileset *tileset = nullptr); Tileset* getTileset(QString, bool forceLoad = false); @@ -260,6 +265,15 @@ private: QHash speciesToIconPath; QHash maps; + // Maps/layouts represented in these sets have been fully loaded from the project. + // If a valid map name / layout id is not in these sets, a Map / Layout object exists + // for it in Project::maps / Project::mapLayouts, but it has been minimally populated + // (i.e. for a map layout it only has the data read from layouts.json, none of its assets + // have been loaded, and for a map it only has the data needed to identify it in the map + // list, none of the rest of its data in map.json). + QSet loadedMapNames; + QSet loadedLayoutIds; + const QRegularExpression re_gbapalExtension; const QRegularExpression re_bppExtension; diff --git a/src/project.cpp b/src/project.cpp index 5dc4e67d..cd9d13d7 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -130,6 +130,7 @@ QString Project::getProjectTitle() const { void Project::clearMaps() { qDeleteAll(this->maps); this->maps.clear(); + this->loadedMapNames.clear(); } void Project::clearTilesetCache() { @@ -142,14 +143,15 @@ Map* Project::loadMap(const QString &mapName) { if (!map) return nullptr; - if (map->loaded()) + if (isMapLoaded(map)) return map; if (!(loadMapData(map) && loadMapLayout(map))) return nullptr; - map->setLoaded(true); + this->loadedMapNames.insert(mapName); emit mapLoaded(map); + return map; } @@ -393,14 +395,14 @@ Layout *Project::createNewLayout(const Layout::Settings &settings, const Layout } // No need for a full load, we already have all the blockdata. - layout->loaded = loadLayoutTilesets(layout); - if (!layout->loaded) { + if (!loadLayoutTilesets(layout)) { delete layout; return nullptr; } this->mapLayouts.insert(layout->id, layout); this->layoutIds.append(layout->id); + this->loadedLayoutIds.insert(layout->id); emit layoutCreated(layout); @@ -408,14 +410,14 @@ Layout *Project::createNewLayout(const Layout::Settings &settings, const Layout } bool Project::loadLayout(Layout *layout) { - if (!layout->loaded) { + if (!isLayoutLoaded(layout)) { // Force these to run even if one fails bool loadedTilesets = loadLayoutTilesets(layout); bool loadedBlockdata = loadBlockdata(layout); bool loadedBorder = loadLayoutBorder(layout); if (loadedTilesets && loadedBlockdata && loadedBorder) { - layout->loaded = true; + this->loadedLayoutIds.insert(layout->id); return true; } else { return false; @@ -448,6 +450,7 @@ void Project::clearMapLayouts() { this->mapLayoutsMaster.clear(); this->layoutIds.clear(); this->layoutIdsMaster.clear(); + this->loadedLayoutIds.clear(); } bool Project::readMapLayouts() { @@ -1115,7 +1118,7 @@ void Project::saveAll() { } void Project::saveMap(Map *map, bool skipLayout) { - if (!map || !map->loaded()) return; + if (!map || !isMapLoaded(map)) return; // Create/Modify a few collateral files for brand new maps. const QString folderPath = projectConfig.getFilePath(ProjectFilePath::data_map_folders) + map->name(); @@ -1255,7 +1258,7 @@ void Project::saveMap(Map *map, bool skipLayout) { } void Project::saveLayout(Layout *layout) { - if (!layout || !layout->loaded) + if (!layout || !isLayoutLoaded(layout)) return; if (!layout->newFolderPath.isEmpty()) { @@ -1912,10 +1915,8 @@ bool Project::isIdentifierUnique(const QString &identifier) const { if (this->encounterGroupLabels.contains(identifier)) return false; // Check event IDs - for (const auto &map : this->maps) { - if (!map->loaded()) continue; - auto events = map->getEvents(); - for (const auto &event : events) { + for (const auto &mapName : this->loadedMapNames) { + for (const auto &event : this->maps.value(mapName)->getEvents()) { QString idName = event->getIdName(); if (!idName.isEmpty() && idName == identifier) return false; diff --git a/src/ui/maplistmodels.cpp b/src/ui/maplistmodels.cpp index 1bdc43aa..118c7c3d 100644 --- a/src/ui/maplistmodels.cpp +++ b/src/ui/maplistmodels.cpp @@ -165,7 +165,7 @@ QVariant MapListModel::data(const QModelIndex &index, int role) const { return this->mapOpenedIcon; const Map* map = this->project->getMap(name); - if (!map || !map->loaded()) + if (!this->project->isMapLoaded(map)) return this->mapGrayIcon; return map->hasUnsavedChanges() ? this->mapEditedIcon : this->mapIcon; } else if (type == this->folderTypeName) { @@ -510,7 +510,7 @@ QVariant LayoutTreeModel::data(const QModelIndex &index, int role) const { return this->mapOpenedIcon; const Layout* layout = this->project->mapLayouts.value(name); - if (!layout || !layout->loaded) + if (!this->project->isLayoutLoaded(layout)) return this->mapGrayIcon; return layout->hasUnsavedChanges() ? this->mapEditedIcon : this->mapIcon; }