From d3ea7cc0cd5579c413bfb6aa8d9a8a5f499ddedc Mon Sep 17 00:00:00 2001 From: maelstrom Date: Wed, 25 Jun 2025 01:21:03 +0200 Subject: [PATCH] fix(editor): reset undo history on load/new --- core/src/objects/part.cpp | 2 -- editor/mainglwidget.cpp | 1 - editor/mainwindow.cpp | 14 +++++++++++--- editor/undohistory.cpp | 13 ++++++++++--- editor/undohistory.h | 5 ++++- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/core/src/objects/part.cpp b/core/src/objects/part.cpp index eb88c57..bb10cec 100644 --- a/core/src/objects/part.cpp +++ b/core/src/objects/part.cpp @@ -124,8 +124,6 @@ SurfaceType Part::surfaceFromFace(NormalId face) { } float Part::GetSurfaceParamA(Vector3 face) { - // printVec(face); - // printf("Enum: %d\n", faceFromNormal(face)); switch (faceFromNormal(face)) { case Top: return topParamA; case Bottom: return bottomParamA; diff --git a/editor/mainglwidget.cpp b/editor/mainglwidget.cpp index b82bd22..d40cef3 100755 --- a/editor/mainglwidget.cpp +++ b/editor/mainglwidget.cpp @@ -382,7 +382,6 @@ void MainGLWidget::mousePressEvent(QMouseEvent* evt) { initialAssembly = PartAssembly::FromSelection(gDataModel->GetService()); initialFrame = initialAssembly.assemblyOrigin(); initialTransforms = PartAssembly::FromSelection(gDataModel->GetService()).GetCurrentTransforms(); - printf("%ld\n", initialTransforms.size()); isMouseDragging = true; draggingHandle = handle; startLinearTransform(evt); diff --git a/editor/mainwindow.cpp b/editor/mainwindow.cpp index 179b188..752cebc 100644 --- a/editor/mainwindow.cpp +++ b/editor/mainwindow.cpp @@ -108,9 +108,8 @@ MainWindow::MainWindow(QWidget *parent) ui->actionUndo->setEnabled(false); ui->actionRedo->setEnabled(false); - undoManager.SetUndoStateListener([&](bool canUndo, bool canRedo) { - ui->actionUndo->setEnabled(canUndo); - ui->actionRedo->setEnabled(canRedo); + undoManager.SetUndoStateListener([&]() { + updateToolbars(); }); } @@ -234,6 +233,11 @@ void MainWindow::connectActionHandlers() { gDataModel->Init(); ui->explorerView->updateRoot(gDataModel); + // Reset running state + placeDocument->setRunState(RUN_STOPPED); + undoManager.Reset(); + updateToolbars(); + // Baseplate gWorkspace()->AddChild(placeDocument->placeWidget->lastPart = Part::New({ .position = glm::vec3(0, -5, 0), @@ -279,6 +283,7 @@ void MainWindow::connectActionHandlers() { // Reset running state placeDocument->setRunState(RUN_STOPPED); + undoManager.Reset(); updateToolbars(); }); @@ -546,6 +551,9 @@ void MainWindow::updateToolbars() { ui->actionRunSimulation->setEnabled(placeDocument->runState() != RUN_RUNNING); ui->actionPauseSimulation->setEnabled(placeDocument->runState() == RUN_RUNNING); ui->actionStopSimulation->setEnabled(placeDocument->runState() != RUN_STOPPED); + + ui->actionUndo->setEnabled(undoManager.CanUndo() && placeDocument->runState() == RUN_STOPPED); + ui->actionRedo->setEnabled(undoManager.CanRedo() && placeDocument->runState() == RUN_STOPPED); } std::optional MainWindow::openFileDialog(QString filter, QString defaultExtension, QFileDialog::AcceptMode acceptMode, QString title) { diff --git a/editor/undohistory.cpp b/editor/undohistory.cpp index f49b721..d57e43b 100644 --- a/editor/undohistory.cpp +++ b/editor/undohistory.cpp @@ -10,7 +10,7 @@ void UndoHistory::PushState(UndoState state) { undoHistory.erase(undoHistory.begin(), undoHistory.begin()+maxBufferSize-(int)undoHistory.size()-1); undoHistory.push_back(state); - undoStateListener(!undoHistory.empty(), !redoHistory.empty()); + undoStateListener(); } void UndoHistory::Undo() { @@ -38,7 +38,7 @@ void UndoHistory::Undo() { } processingUndo = false; - undoStateListener(!undoHistory.empty(), !redoHistory.empty()); + undoStateListener(); } void UndoHistory::Redo() { @@ -66,7 +66,14 @@ void UndoHistory::Redo() { } processingUndo = false; - undoStateListener(!undoHistory.empty(), !redoHistory.empty()); + undoStateListener(); +} + +void UndoHistory::Reset() { + processingUndo = false; + undoHistory.clear(); + redoHistory = {}; + undoStateListener(); } void UndoHistory::SetUndoStateListener(UndoStateChangedListener listener) { diff --git a/editor/undohistory.h b/editor/undohistory.h index b4cc5fc..a35c284 100644 --- a/editor/undohistory.h +++ b/editor/undohistory.h @@ -38,7 +38,7 @@ struct UndoStateSelectionChanged { typedef std::variant UndoStateChange; typedef std::vector UndoState; -typedef std::function UndoStateChangedListener; +typedef std::function UndoStateChangedListener; class UndoHistory { // Ignore PushState requests @@ -53,6 +53,9 @@ public: void PushState(UndoState); void Undo(); void Redo(); + void Reset(); + inline bool CanUndo() { return !undoHistory.empty(); } + inline bool CanRedo() { return !redoHistory.empty(); } void SetUndoStateListener(UndoStateChangedListener listener); }; \ No newline at end of file