Permalink
Browse files

some modifications to ctrl+z/ctrl+y

allows infinite undoing / redoing, improvements when history limit is greater than one
  • Loading branch information...
jacob1 committed Nov 14, 2016
1 parent 95d2014 commit 25a2d9b5b5be2b6b7f204a747e3e0b95b3a36a0d
Showing with 37 additions and 22 deletions.
  1. +23 −22 src/gui/game/GameController.cpp
  2. +11 −0 src/gui/game/GameModel.cpp
  3. +3 −0 src/gui/game/GameModel.h
@@ -233,60 +233,61 @@ void GameController::HistoryRestore()
{
std::deque<Snapshot*> 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()
{
std::deque<Snapshot*> history = gameModel->GetHistory();
unsigned int historyPosition = gameModel->GetHistoryPosition();
Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot();
if(newSnap)
if (newSnap)
{
while (historyPosition < history.size())
{
Snapshot * snap = history.back();
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()));
}
}
void GameController::HistoryForward()
{
std::deque<Snapshot*> 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()
@@ -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)
@@ -65,6 +65,7 @@ class GameModel
User currentUser;
float toolStrength;
std::deque<Snapshot*> history;
Snapshot *redoHistory;
unsigned int historyPosition;
size_t activeColourPreset;
@@ -133,6 +134,8 @@ class GameModel
unsigned int GetHistoryPosition();
void SetHistory(std::deque<Snapshot*> newHistory);
void SetHistoryPosition(unsigned int newHistoryPosition);
Snapshot * GetRedoHistory();
void SetRedoHistory(Snapshot * redo);
void UpdateQuickOptions();

0 comments on commit 25a2d9b

Please sign in to comment.