diff --git a/include/core/maplayout.h b/include/core/maplayout.h index da58c4b7..fb4c0118 100644 --- a/include/core/maplayout.h +++ b/include/core/maplayout.h @@ -28,6 +28,7 @@ public: QString id; QString name; + QString newFolderPath; int width; int height; diff --git a/src/core/maplayout.cpp b/src/core/maplayout.cpp index 2b52a80f..79e3b832 100644 --- a/src/core/maplayout.cpp +++ b/src/core/maplayout.cpp @@ -438,5 +438,5 @@ QPixmap Layout::getLayoutItemPixmap() { } bool Layout::hasUnsavedChanges() const { - return !this->editHistory.isClean(); + return !this->editHistory.isClean() || !this->newFolderPath.isEmpty(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 483004f8..90b4b676 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1315,12 +1315,7 @@ void MainWindow::onNewMapCreated(Map *newMap, const QString &groupName) { ui->comboBox_EmergeMap->insertItem(mapIndex, newMap->name()); } - if (userSetMap(newMap->name())) { - // TODO: Creating a new map shouldn't be automatically saved. - // For one, it takes away the option to discard the new map. - // For two, if the new map uses an existing layout, any unsaved changes to that layout will also be saved. - save(true); - } + userSetMap(newMap->name()); } // Called any time a new layout is created (including as a byproduct of creating a new map) diff --git a/src/project.cpp b/src/project.cpp index 6810e7d7..ac5d37e6 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -385,20 +385,10 @@ Layout *Project::createNewLayout(const Layout::Settings &settings, const Layout // Otherwise the new layout's folder name will just be the layout's name. const QString folderName = !settings.folderName.isEmpty() ? settings.folderName : layout->name; const QString folderPath = projectConfig.getFilePath(ProjectFilePath::data_layouts_folders) + folderName; + layout->newFolderPath = folderPath; layout->border_path = folderPath + "/border.bin"; layout->blockdata_path = folderPath + "/map.bin"; - // Create a new directory for the layout, if it doesn't already exist. - const QString fullPath = QString("%1/%2").arg(this->root).arg(folderPath); - if (!QDir::root().mkpath(fullPath)) { - logError(QString("Failed to create directory for new layout: '%1'").arg(fullPath)); - delete layout; - return nullptr; - } - - this->mapLayouts.insert(layout->id, layout); - this->layoutIds.append(layout->id); - if (layout->blockdata.isEmpty()) { // Fill layout using default fill settings setNewLayoutBlockdata(layout); @@ -408,7 +398,15 @@ Layout *Project::createNewLayout(const Layout::Settings &settings, const Layout setNewLayoutBorder(layout); } - saveLayout(layout); // TODO: Ideally we shouldn't automatically save new layouts + // No need for a full load, we already have all the blockdata. + layout->loaded = loadLayoutTilesets(layout); + if (!layout->loaded) { + delete layout; + return nullptr; + } + + this->mapLayouts.insert(layout->id, layout); + this->layoutIds.append(layout->id); emit layoutCreated(layout); @@ -948,25 +946,25 @@ bool Project::loadLayoutTilesets(Layout *layout) { layout->tileset_primary = getTileset(layout->tileset_primary_label); if (!layout->tileset_primary) { QString defaultTileset = this->getDefaultPrimaryTilesetLabel(); - logWarn(QString("%1 has invalid primary tileset '%2'. Using default '%3'").arg(layout->name).arg(layout->tileset_primary_label).arg(defaultTileset)); layout->tileset_primary_label = defaultTileset; layout->tileset_primary = getTileset(layout->tileset_primary_label); if (!layout->tileset_primary) { - logError(QString("Failed to set default primary tileset.")); + logError(QString("%1 has invalid primary tileset '%2'.").arg(layout->name).arg(layout->tileset_primary_label)); return false; } + logWarn(QString("%1 has invalid primary tileset '%2'. Using default '%3'").arg(layout->name).arg(layout->tileset_primary_label).arg(defaultTileset)); } layout->tileset_secondary = getTileset(layout->tileset_secondary_label); if (!layout->tileset_secondary) { QString defaultTileset = this->getDefaultSecondaryTilesetLabel(); - logWarn(QString("%1 has invalid secondary tileset '%2'. Using default '%3'").arg(layout->name).arg(layout->tileset_secondary_label).arg(defaultTileset)); layout->tileset_secondary_label = defaultTileset; layout->tileset_secondary = getTileset(layout->tileset_secondary_label); if (!layout->tileset_secondary) { - logError(QString("Failed to set default secondary tileset.")); + logError(QString("%1 has invalid secondary tileset '%2'.").arg(layout->name).arg(layout->tileset_secondary_label)); return false; } + logWarn(QString("%1 has invalid secondary tileset '%2'. Using default '%3'").arg(layout->name).arg(layout->tileset_secondary_label).arg(defaultTileset)); } return true; } @@ -1266,6 +1264,16 @@ void Project::saveLayout(Layout *layout) { if (!layout || !layout->loaded) return; + if (!layout->newFolderPath.isEmpty()) { + // Layout directory doesn't exist yet, create it now. + const QString fullPath = QString("%1/%2").arg(this->root).arg(layout->newFolderPath); + if (!QDir::root().mkpath(fullPath)) { + logError(QString("Failed to create directory for new layout: '%1'").arg(fullPath)); + return; + } + layout->newFolderPath = QString(); + } + saveLayoutBorder(layout); saveLayoutBlockdata(layout);