From c4ad7d562015168a175dcb65868a59289ad8101c Mon Sep 17 00:00:00 2001 From: maelstrom Date: Sun, 20 Apr 2025 00:17:19 +0200 Subject: [PATCH] feat(editor): basics for run/edit mode --- core/src/common.cpp | 1 + core/src/objects/snap.cpp | 2 +- editor/mainwindow.cpp | 58 ++++++++++++++++++++++++++++++--------- editor/mainwindow.ui | 46 ++++++++++++++++++++++++++++++- 4 files changed, 92 insertions(+), 15 deletions(-) diff --git a/core/src/common.cpp b/core/src/common.cpp index ca4c815..bc5c1dd 100644 --- a/core/src/common.cpp +++ b/core/src/common.cpp @@ -6,6 +6,7 @@ Camera camera(glm::vec3(0.0, 0.0, 3.0)); //std::vector parts; std::shared_ptr gDataModel = DataModel::New(); +std::shared_ptr editModeDataModel = gDataModel; std::optional hierarchyPreUpdateHandler; std::optional hierarchyPostUpdateHandler; std::shared_ptr editorToolHandles = Handles::New(); diff --git a/core/src/objects/snap.cpp b/core/src/objects/snap.cpp index 03d34f2..8a3f2f9 100644 --- a/core/src/objects/snap.cpp +++ b/core/src/objects/snap.cpp @@ -111,7 +111,7 @@ std::optional> Snap::jointWorkspace() { if (workspace()) return workspace(); if (GetParent() && GetParent().value()->GetClass() == &JointsService::TYPE) - return std::dynamic_pointer_cast(GetParent().value()->GetParent().value())->GetService("Workspace"); + return std::dynamic_pointer_cast(GetParent().value()->GetParent().value())->GetService(); return {}; } \ No newline at end of file diff --git a/editor/mainwindow.cpp b/editor/mainwindow.cpp index 8efb439..dd77a09 100644 --- a/editor/mainwindow.cpp +++ b/editor/mainwindow.cpp @@ -17,7 +17,13 @@ #define NDEBUG #endif -bool simulationPlaying = false; +enum RunState { + RUN_STOPPED, + RUN_RUNNING, + RUN_PAUSED +}; + +RunState runState = RUN_STOPPED; bool worldSpaceTransforms = false; @@ -171,7 +177,7 @@ MainWindow::MainWindow(QWidget *parent) snap->c1 = part0->cframe; // gWorkspace()->AddChild(snap); - gDataModel->GetService("JointsService").expect()->AddChild(snap); + gDataModel->GetService().expect()->AddChild(snap); } void MainWindow::closeEvent(QCloseEvent* evt) { @@ -217,7 +223,7 @@ void MainWindow::timerEvent(QTimerEvent* evt) { float deltaTime = std::chrono::duration_cast>(std::chrono::steady_clock::now() - lastTime).count(); lastTime = std::chrono::steady_clock::now(); - if (simulationPlaying) + if (runState == RUN_RUNNING) gWorkspace()->PhysicsStep(deltaTime); ui->mainWidget->update(); ui->mainWidget->updateCycle(); @@ -254,19 +260,45 @@ void MainWindow::connectActionHandlers() { }); ui->actionToggleEditSounds->setChecked(true); - connect(ui->actionToggleSimulation, &QAction::triggered, this, [&]() { - simulationPlaying = !simulationPlaying; - if (simulationPlaying) { - ui->actionToggleSimulation->setText("Pause simulation"); - ui->actionToggleSimulation->setToolTip("Pause the simulation"); - ui->actionToggleSimulation->setIcon(QIcon::fromTheme("media-playback-pause")); - } else { - ui->actionToggleSimulation->setText("Resume simulation"); - ui->actionToggleSimulation->setToolTip("Resume the simulation"); - ui->actionToggleSimulation->setIcon(QIcon::fromTheme("media-playback-start")); + connect(ui->actionRunSimulation, &QAction::triggered, this, [&]() { + if (runState == RUN_RUNNING) return; + + if (runState == RUN_PAUSED) { + runState = RUN_RUNNING; + ui->actionRunSimulation->setEnabled(false); + ui->actionPauseSimulation->setEnabled(true); + return; } + + runState = RUN_RUNNING; + ui->actionRunSimulation->setEnabled(false); + ui->actionPauseSimulation->setEnabled(true); + ui->actionStopSimulation->setEnabled(true); }); + connect(ui->actionPauseSimulation, &QAction::triggered, this, [&]() { + if (runState != RUN_RUNNING) return; + + runState = RUN_PAUSED; + ui->actionRunSimulation->setEnabled(true); + ui->actionPauseSimulation->setEnabled(false); + return; + }); + + connect(ui->actionStopSimulation, &QAction::triggered, this, [&]() { + if (runState == RUN_STOPPED) return; + + runState = RUN_STOPPED; + ui->actionRunSimulation->setEnabled(true); + ui->actionPauseSimulation->setEnabled(false); + ui->actionStopSimulation->setEnabled(false); + return; + }); + + ui->actionRunSimulation->setEnabled(true); + ui->actionPauseSimulation->setEnabled(false); + ui->actionStopSimulation->setEnabled(false); + connect(ui->actionToggleSpace, &QAction::triggered, this, [&]() { worldSpaceTransforms = !worldSpaceTransforms; updateToolbars(); diff --git a/editor/mainwindow.ui b/editor/mainwindow.ui index 76029e9..25cff8a 100644 --- a/editor/mainwindow.ui +++ b/editor/mainwindow.ui @@ -188,7 +188,9 @@ false - + + + @@ -675,6 +677,48 @@ QAction::MenuRole::NoRole + + + + + + Run + + + Run simulation + + + QAction::MenuRole::NoRole + + + + + + + + Pause + + + Pause simulation + + + QAction::MenuRole::NoRole + + + + + + + + Stop + + + Stop simulation + + + QAction::MenuRole::NoRole + +