| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2017 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include "undostack.hpp" |
| 17 | |
| 18 | UndoStack::UndoStack(unsigned int maxHistorySize) |
| 19 | : m_maxHistorySize(maxHistorySize) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | void UndoStack::pushUndoPoint(TrackDataPtr trackData) |
| 24 | { |
| 25 | auto copyData = new TrackData(*trackData.get()); |
| 26 | m_undoStack.push_back(TrackDataPtr(copyData)); |
| 27 | |
| 28 | if (m_undoStack.size() > m_maxHistorySize) |
| 29 | { |
| 30 | m_undoStack.pop_front(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void UndoStack::pushRedoPoint(TrackDataPtr trackData) |
| 35 | { |
| 36 | auto copyData = new TrackData(*trackData.get()); |
| 37 | m_redoStack.push_back(TrackDataPtr(copyData)); |
| 38 | |
| 39 | if (m_redoStack.size() > m_maxHistorySize) |
| 40 | { |
| 41 | m_redoStack.pop_front(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void UndoStack::clear() |
| 46 | { |
| 47 | m_undoStack.clear(); |
| 48 | m_redoStack.clear(); |
| 49 | } |
| 50 | |
| 51 | bool UndoStack::isUndoable() const |
| 52 | { |
| 53 | return m_undoStack.size() > 0; |
| 54 | } |
| 55 | |
| 56 | TrackDataPtr UndoStack::undo() |
| 57 | { |
| 58 | if (isUndoable()) |
| 59 | { |
| 60 | auto head = m_undoStack.back(); |
| 61 | m_undoStack.pop_back(); |
| 62 | return head; |
| 63 | } |
| 64 | |
| 65 | return TrackDataPtr(); |
| 66 | } |
| 67 | |
| 68 | bool UndoStack::isRedoable() const |
| 69 | { |
| 70 | return m_redoStack.size() > 0; |
| 71 | } |
| 72 | |
| 73 | TrackDataPtr UndoStack::redo() |
| 74 | { |
| 75 | if (isRedoable()) |
| 76 | { |
| 77 | auto head = m_redoStack.back(); |
| 78 | m_redoStack.pop_back(); |
| 79 | return head; |
| 80 | } |
| 81 | |
| 82 | return TrackDataPtr(); |
| 83 | } |