allow redo with Ctrl-Y

This commit is contained in:
krawthekrow 2016-09-12 15:22:23 +08:00 committed by jacob1
parent 63b2227802
commit 95d2014724
5 changed files with 57 additions and 8 deletions

View File

@ -232,34 +232,60 @@ GameController::~GameController()
void GameController::HistoryRestore() void GameController::HistoryRestore()
{ {
std::deque<Snapshot*> history = gameModel->GetHistory(); std::deque<Snapshot*> history = gameModel->GetHistory();
if(history.size()) unsigned int historyPosition = gameModel->GetHistoryPosition();
if(historyPosition > 0 && historyPosition <= history.size())
{ {
Snapshot * snap = history.back(); if (historyPosition == history.size())
gameModel->GetSimulation()->Restore(*snap);
if(history.size()>1)
{ {
history.pop_back(); Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot();
delete snap; history.push_back(newSnap);
gameModel->SetHistory(history);
} }
Snapshot * snap = history[historyPosition - 1];
gameModel->GetSimulation()->Restore(*snap);
gameModel->SetHistory(history);
gameModel->SetHistoryPosition(historyPosition - 1);
} }
} }
void GameController::HistorySnapshot() void GameController::HistorySnapshot()
{ {
std::deque<Snapshot*> history = gameModel->GetHistory(); std::deque<Snapshot*> history = gameModel->GetHistory();
unsigned int historyPosition = gameModel->GetHistoryPosition();
Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot(); 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) //History limit is current 1
{ {
Snapshot * snap = history.front(); Snapshot * snap = history.front();
history.pop_front(); history.pop_front();
//snap->Particles.clear(); //snap->Particles.clear();
delete snap; delete snap;
if (historyPosition > history.size())
{
historyPosition--;
}
} }
history.push_back(newSnap); history.push_back(newSnap);
gameModel->SetHistory(history); gameModel->SetHistory(history);
gameModel->SetHistoryPosition(historyPosition + 1);
}
}
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);
} }
} }

View File

@ -73,6 +73,7 @@ public:
void HistoryRestore(); void HistoryRestore();
void HistorySnapshot(); void HistorySnapshot();
void HistoryForward();
void AdjustGridSize(int direction); void AdjustGridSize(int direction);
void InvertAirSim(); void InvertAirSim();

View File

@ -28,6 +28,7 @@ GameModel::GameModel():
currentFile(NULL), currentFile(NULL),
currentUser(0, ""), currentUser(0, ""),
toolStrength(1.0f), toolStrength(1.0f),
historyPosition(0),
activeColourPreset(0), activeColourPreset(0),
colourSelector(false), colourSelector(false),
colour(255, 0, 0, 255), colour(255, 0, 0, 255),
@ -423,11 +424,22 @@ std::deque<Snapshot*> GameModel::GetHistory()
{ {
return history; return history;
} }
unsigned int GameModel::GetHistoryPosition()
{
return historyPosition;
}
void GameModel::SetHistory(std::deque<Snapshot*> newHistory) void GameModel::SetHistory(std::deque<Snapshot*> newHistory)
{ {
history = newHistory; history = newHistory;
} }
void GameModel::SetHistoryPosition(unsigned int newHistoryPosition)
{
historyPosition = newHistoryPosition;
}
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;
unsigned int historyPosition;
size_t activeColourPreset; size_t activeColourPreset;
std::vector<ui::Colour> colourPresets; std::vector<ui::Colour> colourPresets;
@ -129,7 +130,9 @@ public:
void BuildQuickOptionMenu(GameController * controller); void BuildQuickOptionMenu(GameController * controller);
std::deque<Snapshot*> GetHistory(); std::deque<Snapshot*> GetHistory();
unsigned int GetHistoryPosition();
void SetHistory(std::deque<Snapshot*> newHistory); void SetHistory(std::deque<Snapshot*> newHistory);
void SetHistoryPosition(unsigned int newHistoryPosition);
void UpdateQuickOptions(); void UpdateQuickOptions();

View File

@ -1545,7 +1545,14 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
} }
break; break;
case 'y': case 'y':
c->SwitchAir(); if (ctrl)
{
c->HistoryForward();
}
else
{
c->SwitchAir();
}
break; break;
case SDLK_ESCAPE: case SDLK_ESCAPE:
case 'q': case 'q':