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(); std::deque<Snapshot*> history = gameModel->GetHistory();
unsigned int historyPosition = gameModel->GetHistoryPosition(); 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();
{ gameModel->SetRedoHistory(newSnap);
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 * snap = history[newHistoryPosition];
gameModel->GetSimulation()->Restore(*snap);
gameModel->SetHistory(history);
gameModel->SetHistoryPosition(newHistoryPosition);
} }
void GameController::HistorySnapshot() void GameController::HistorySnapshot()
@ -252,7 +252,7 @@ void GameController::HistorySnapshot()
std::deque<Snapshot*> history = gameModel->GetHistory(); std::deque<Snapshot*> history = gameModel->GetHistory();
unsigned int historyPosition = gameModel->GetHistoryPosition(); unsigned int historyPosition = gameModel->GetHistoryPosition();
Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot(); Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot();
if(newSnap) if (newSnap)
{ {
while (historyPosition < history.size()) while (historyPosition < history.size())
{ {
@ -260,20 +260,17 @@ void GameController::HistorySnapshot()
history.pop_back(); history.pop_back();
delete snap; delete snap;
} }
if(history.size() >= 1) //History limit is current 1 if (history.size() >= 1)
{ {
Snapshot * snap = history.front(); Snapshot * snap = history.front();
history.pop_front(); history.pop_front();
//snap->Particles.clear();
delete snap; delete snap;
if (historyPosition > history.size()) if (historyPosition > history.size())
{
historyPosition--; historyPosition--;
}
} }
history.push_back(newSnap); history.push_back(newSnap);
gameModel->SetHistory(history); 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(); std::deque<Snapshot*> history = gameModel->GetHistory();
unsigned int historyPosition = gameModel->GetHistoryPosition(); unsigned int historyPosition = gameModel->GetHistoryPosition();
if(historyPosition < history.size() - 1) unsigned int newHistoryPosition = std::min((size_t)historyPosition+1, history.size());
{ Snapshot *snap;
Snapshot * snap = history[historyPosition + 1]; if (newHistoryPosition == history.size())
gameModel->GetSimulation()->Restore(*snap); snap = gameModel->GetRedoHistory();
gameModel->SetHistoryPosition(historyPosition + 1); else
} snap = history[newHistoryPosition];
if (!snap)
return;
gameModel->GetSimulation()->Restore(*snap);
gameModel->SetHistoryPosition(newHistoryPosition);
} }
GameView * GameController::GetView() GameView * GameController::GetView()

View File

@ -28,6 +28,7 @@ GameModel::GameModel():
currentFile(NULL), currentFile(NULL),
currentUser(0, ""), currentUser(0, ""),
toolStrength(1.0f), toolStrength(1.0f),
redoHistory(NULL),
historyPosition(0), historyPosition(0),
activeColourPreset(0), activeColourPreset(0),
colourSelector(false), colourSelector(false),
@ -440,6 +441,16 @@ void GameModel::SetHistoryPosition(unsigned int newHistoryPosition)
historyPosition = newHistoryPosition; historyPosition = newHistoryPosition;
} }
Snapshot * GameModel::GetRedoHistory()
{
return redoHistory;
}
void GameModel::SetRedoHistory(Snapshot * redo)
{
redoHistory = redo;
}
void GameModel::SetVote(int direction) void GameModel::SetVote(int direction)
{ {
if(currentSave) if(currentSave)

View File

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