From 25a2d9b5b5be2b6b7f204a747e3e0b95b3a36a0d Mon Sep 17 00:00:00 2001 From: jacob1 Date: Sun, 13 Nov 2016 19:22:21 -0500 Subject: [PATCH] some modifications to ctrl+z/ctrl+y allows infinite undoing / redoing, improvements when history limit is greater than one --- src/gui/game/GameController.cpp | 45 +++++++++++++++++---------------- src/gui/game/GameModel.cpp | 11 ++++++++ src/gui/game/GameModel.h | 3 +++ 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index f65772fad..10e966d5f 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -233,18 +233,18 @@ void GameController::HistoryRestore() { std::deque history = gameModel->GetHistory(); unsigned int historyPosition = gameModel->GetHistoryPosition(); - if(historyPosition > 0 && historyPosition <= history.size()) + unsigned int newHistoryPosition = std::max((int)historyPosition-1, 0); + // When undoing, save the current state as a final redo + // This way ctrl+y will always bring you back to the point right before your last ctrl+z + if (historyPosition == history.size()) { - if (historyPosition == history.size()) - { - Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot(); - history.push_back(newSnap); - } - Snapshot * snap = history[historyPosition - 1]; - gameModel->GetSimulation()->Restore(*snap); - gameModel->SetHistory(history); - gameModel->SetHistoryPosition(historyPosition - 1); + Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot(); + gameModel->SetRedoHistory(newSnap); } + Snapshot * snap = history[newHistoryPosition]; + gameModel->GetSimulation()->Restore(*snap); + gameModel->SetHistory(history); + gameModel->SetHistoryPosition(newHistoryPosition); } void GameController::HistorySnapshot() @@ -252,7 +252,7 @@ void GameController::HistorySnapshot() std::deque history = gameModel->GetHistory(); unsigned int historyPosition = gameModel->GetHistoryPosition(); Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot(); - if(newSnap) + if (newSnap) { while (historyPosition < history.size()) { @@ -260,20 +260,17 @@ void GameController::HistorySnapshot() history.pop_back(); delete snap; } - if(history.size() >= 1) //History limit is current 1 + if (history.size() >= 1) { Snapshot * snap = history.front(); history.pop_front(); - //snap->Particles.clear(); delete snap; if (historyPosition > history.size()) - { historyPosition--; - } } history.push_back(newSnap); gameModel->SetHistory(history); - gameModel->SetHistoryPosition(historyPosition + 1); + gameModel->SetHistoryPosition(std::min((size_t)historyPosition+1, history.size())); } } @@ -281,12 +278,16 @@ void GameController::HistoryForward() { std::deque history = gameModel->GetHistory(); unsigned int historyPosition = gameModel->GetHistoryPosition(); - if(historyPosition < history.size() - 1) - { - Snapshot * snap = history[historyPosition + 1]; - gameModel->GetSimulation()->Restore(*snap); - gameModel->SetHistoryPosition(historyPosition + 1); - } + unsigned int newHistoryPosition = std::min((size_t)historyPosition+1, history.size()); + Snapshot *snap; + if (newHistoryPosition == history.size()) + snap = gameModel->GetRedoHistory(); + else + snap = history[newHistoryPosition]; + if (!snap) + return; + gameModel->GetSimulation()->Restore(*snap); + gameModel->SetHistoryPosition(newHistoryPosition); } GameView * GameController::GetView() diff --git a/src/gui/game/GameModel.cpp b/src/gui/game/GameModel.cpp index eeb443dab..c44634a95 100644 --- a/src/gui/game/GameModel.cpp +++ b/src/gui/game/GameModel.cpp @@ -28,6 +28,7 @@ GameModel::GameModel(): currentFile(NULL), currentUser(0, ""), toolStrength(1.0f), + redoHistory(NULL), historyPosition(0), activeColourPreset(0), colourSelector(false), @@ -440,6 +441,16 @@ void GameModel::SetHistoryPosition(unsigned int newHistoryPosition) historyPosition = newHistoryPosition; } +Snapshot * GameModel::GetRedoHistory() +{ + return redoHistory; +} + +void GameModel::SetRedoHistory(Snapshot * redo) +{ + redoHistory = redo; +} + void GameModel::SetVote(int direction) { if(currentSave) diff --git a/src/gui/game/GameModel.h b/src/gui/game/GameModel.h index dd15a8766..ea0f7879d 100644 --- a/src/gui/game/GameModel.h +++ b/src/gui/game/GameModel.h @@ -65,6 +65,7 @@ private: User currentUser; float toolStrength; std::deque history; + Snapshot *redoHistory; unsigned int historyPosition; size_t activeColourPreset; @@ -133,6 +134,8 @@ public: unsigned int GetHistoryPosition(); void SetHistory(std::deque newHistory); void SetHistoryPosition(unsigned int newHistoryPosition); + Snapshot * GetRedoHistory(); + void SetRedoHistory(Snapshot * redo); void UpdateQuickOptions();