some modifications to ctrl+z/ctrl+y

allows infinite undoing / redoing, improvements when history limit is greater than one
This commit is contained in:
jacob1 2016-11-13 19:22:21 -05:00
parent 95d2014724
commit 25a2d9b5b5
3 changed files with 37 additions and 22 deletions

View File

@ -233,18 +233,18 @@ 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()
@ -252,7 +252,7 @@ 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())
{
@ -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<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()

View File

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

View File

@ -65,6 +65,7 @@ private:
User currentUser;
float toolStrength;
std::deque<Snapshot*> history;
Snapshot *redoHistory;
unsigned int historyPosition;
size_t activeColourPreset;
@ -133,6 +134,8 @@ public:
unsigned int GetHistoryPosition();
void SetHistory(std::deque<Snapshot*> newHistory);
void SetHistoryPosition(unsigned int newHistoryPosition);
Snapshot * GetRedoHistory();
void SetRedoHistory(Snapshot * redo);
void UpdateQuickOptions();