#include "HistoryManager.h" #include "../AppLayers/NotifyLayer.h" void HistoryManager::Clear() { for (int i = 0; i < (int)history.size(); i++) { delete history[i].release(); } history.clear(); cursor = -1; } json HistoryManager::GetEvents() { json events = json::array(); for (int i = 0; i <= cursor; i++) { json event = history[i]->Serialize(); events.push_back(event); } return events; } void HistoryManager::Execute(std::unique_ptr command) { if (history.size() && cursor < (int)history.size() - 1) { for (int i = cursor + 1; i < (int)history.size(); i++) { delete history[i].release(); } history.erase(history.begin() + cursor + 1, history.end()); } command->Execute(); history.push_back(std::move(command)); cursor++; } void HistoryManager::Undo() { if (CanUndo()) { NotifyAppLayer::SetMessage("Undo: " + history[cursor]->GetTitle()); history[cursor]->Undo(); cursor--; } } void HistoryManager::Redo() { if (CanRedo()) { cursor++; NotifyAppLayer::SetMessage("Redo: " + history[cursor]->GetTitle()); history[cursor]->Execute(); } } bool HistoryManager::CanUndo() { return cursor >= 0; } bool HistoryManager::CanRedo() { return cursor < (int)history.size() - 1; }