diff --git a/editor/mainwindow.cpp b/editor/mainwindow.cpp index bea8f20..cd1d1b1 100644 --- a/editor/mainwindow.cpp +++ b/editor/mainwindow.cpp @@ -438,6 +438,39 @@ void MainWindow::connectActionHandlers() { }); } +void MainWindow::openFile(std::string path) { + // Don't ask for confirmation if running a debug build (makes development easier) + #ifdef NDEBUG + // Ask if the user wants to save their changes + // https://stackoverflow.com/a/33890731 + QMessageBox msgBox; + msgBox.setText("Save changes before creating new document?"); + msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); + msgBox.setDefaultButton(QMessageBox::Save); + int result = msgBox.exec(); + + if (result == QMessageBox::Cancel) return; + if (result == QMessageBox::Save) { + std::optional path; + if (!gDataModel->HasFile()) + path = openFileDialog("Openblocks Level (*.obl)", ".obl", QFileDialog::AcceptSave, QString::fromStdString("Save " + gDataModel->name)); + if (!path || path == "") return; + + gDataModel->SaveToFile(path); + } + #endif + + std::shared_ptr newModel = DataModel::LoadFromFile(path); + editModeDataModel = newModel; + gDataModel = newModel; + newModel->Init(); + ui->explorerView->updateRoot(newModel); + + // Reset running state + placeDocument->setRunState(RUN_STOPPED); + updateToolbars(); +} + void MainWindow::updateToolbars() { ui->actionToolSelect->setChecked(selectedTool == TOOL_SELECT); ui->actionToolMove->setChecked(selectedTool == TOOL_MOVE); diff --git a/editor/mainwindow.h b/editor/mainwindow.h index a6503e9..5ee1e2d 100644 --- a/editor/mainwindow.h +++ b/editor/mainwindow.h @@ -57,6 +57,8 @@ public: void openScriptDocument(std::shared_ptr