fix(editor): reset undo history on load/new

This commit is contained in:
maelstrom 2025-06-25 01:21:03 +02:00
parent ed5aa597ad
commit d3ea7cc0cd
5 changed files with 25 additions and 10 deletions

View file

@ -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;

View file

@ -382,7 +382,6 @@ void MainGLWidget::mousePressEvent(QMouseEvent* evt) {
initialAssembly = PartAssembly::FromSelection(gDataModel->GetService<Selection>());
initialFrame = initialAssembly.assemblyOrigin();
initialTransforms = PartAssembly::FromSelection(gDataModel->GetService<Selection>()).GetCurrentTransforms();
printf("%ld\n", initialTransforms.size());
isMouseDragging = true;
draggingHandle = handle;
startLinearTransform(evt);

View file

@ -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<std::string> MainWindow::openFileDialog(QString filter, QString defaultExtension, QFileDialog::AcceptMode acceptMode, QString title) {

View file

@ -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) {

View file

@ -38,7 +38,7 @@ struct UndoStateSelectionChanged {
typedef std::variant<UndoStatePropertyChanged, UndoStateInstanceCreated, UndoStateInstanceRemoved, UndoStateInstanceReparented, UndoStateSelectionChanged> UndoStateChange;
typedef std::vector<UndoStateChange> UndoState;
typedef std::function<void(bool canUndo, bool canRedo)> UndoStateChangedListener;
typedef std::function<void()> 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);
};