diff --git a/.vscode/launch.json b/.vscode/launch.json index 9b57d4c..0628366 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "type": "lldb", "request": "launch", "name": "Debug", - "program": "${workspaceFolder}/bin/editor", + "program": "${workspaceFolder}/build/bin/editor", "args": [], "cwd": "${workspaceFolder}", } diff --git a/editor/mainwindow.cpp b/editor/mainwindow.cpp index a1ae3ab..917091d 100644 --- a/editor/mainwindow.cpp +++ b/editor/mainwindow.cpp @@ -13,10 +13,14 @@ #include #include #include +#include #include #include +#include #include #include +#include +#include #include #include "common.h" @@ -33,6 +37,10 @@ #include "qobject.h" #include "qsysinfo.h" +#ifdef _NDEBUG +#define NDEBUG +#endif + bool simulationPlaying = false; bool worldSpaceTransforms = false; @@ -114,6 +122,53 @@ MainWindow::MainWindow(QWidget *parent) } }); + connect(ui->actionNew, &QAction::triggered, this, [&]() { + // 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 (!dataModel->HasFile()) + path = openFileDialog("Openblocks Level (*.obl)", ".obl", QFileDialog::AcceptSave, QString::fromStdString("Save " + dataModel->name)); + if (!path || path == "") return; + + dataModel->SaveToFile(path); + } + #endif + + // TODO: Also remove this (the reason this is here is because all parts are expected to + // be deleted before their parent DataModel is. So it expects rigidBody to not be null, and tries + // to destroy it, causing a segfault since it's already been destroyed. This is important for the later code. + // TL;DR: This stinks and I need to fix it.) + ui->mainWidget->lastPart = Part::New(); + + dataModel = DataModel::New(); + dataModel->Init(); + ui->explorerView->updateRoot(dataModel); + + // TODO: Remove this and use a proper fix. This *WILL* cause a leak and memory issues in the future + simulationInit(); + + // Baseplate + workspace()->AddChild(ui->mainWidget->lastPart = Part::New({ + .position = glm::vec3(0, -5, 0), + .rotation = glm::vec3(0), + .size = glm::vec3(512, 1.2, 512), + .color = glm::vec3(0.388235, 0.372549, 0.384314), + .anchored = true, + })); + ui->mainWidget->lastPart->name = "Baseplate"; + syncPartPhysics(ui->mainWidget->lastPart); + }); + connect(ui->actionSave, &QAction::triggered, this, [&]() { std::optional path; if (!dataModel->HasFile()) @@ -286,6 +341,28 @@ MainWindow::MainWindow(QWidget *parent) syncPartPhysics(ui->mainWidget->lastPart); } +void MainWindow::closeEvent(QCloseEvent* evt) { + #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 evt->ignore(); + if (result == QMessageBox::Save) { + std::optional path; + if (!dataModel->HasFile()) + path = openFileDialog("Openblocks Level (*.obl)", ".obl", QFileDialog::AcceptSave, QString::fromStdString("Save " + dataModel->name)); + if (!path || path == "") return evt->ignore(); + + dataModel->SaveToFile(path); + } + #endif +} + void MainWindow::handleLog(Logger::LogLevel logLevel, std::string message) { if (logLevel == Logger::LogLevel::DEBUG) return; diff --git a/editor/mainwindow.h b/editor/mainwindow.h index 03bc13d..57a35b6 100644 --- a/editor/mainwindow.h +++ b/editor/mainwindow.h @@ -46,6 +46,7 @@ private: void updateToolbars(); void timerEvent(QTimerEvent*) override; + void closeEvent(QCloseEvent* evt) override; void handleLog(Logger::LogLevel, std::string); std::optional openFileDialog(QString filter, QString defaultExtension, QFileDialog::AcceptMode acceptMode, QString title = "");