diff options
Diffstat (limited to 'tools/mapedit/src/undo.cpp')
| -rw-r--r-- | tools/mapedit/src/undo.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
| diff --git a/tools/mapedit/src/undo.cpp b/tools/mapedit/src/undo.cpp new file mode 100644 index 0000000..bcf7b92 --- /dev/null +++ b/tools/mapedit/src/undo.cpp | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | #include "undo.h" | ||
| 2 | #include "frame.h" | ||
| 3 | |||
| 4 | Undoable::Undoable(std::string title, std::function<void ()> forward, std::function<void ()> backward) | ||
| 5 | { | ||
| 6 | this->title = title; | ||
| 7 | this->forward = forward; | ||
| 8 | this->backward = backward; | ||
| 9 | } | ||
| 10 | |||
| 11 | std::string Undoable::getTitle() const | ||
| 12 | { | ||
| 13 | return title; | ||
| 14 | } | ||
| 15 | |||
| 16 | void Undoable::apply() | ||
| 17 | { | ||
| 18 | forward(); | ||
| 19 | } | ||
| 20 | |||
| 21 | void Undoable::undo() | ||
| 22 | { | ||
| 23 | backward(); | ||
| 24 | } | ||
| 25 | |||
| 26 | wxBEGIN_EVENT_TABLE(UndoableTextBox, wxTextCtrl) | ||
| 27 | EVT_SET_FOCUS(UndoableTextBox::OnFocus) | ||
| 28 | EVT_KILL_FOCUS(UndoableTextBox::OnKillFocus) | ||
| 29 | wxEND_EVENT_TABLE() | ||
| 30 | |||
| 31 | UndoableTextBox::UndoableTextBox(wxWindow* parent, wxWindowID id, wxString startText, std::string undoType, MapeditFrame* realParent, wxPoint pos, wxSize size, long style) : wxTextCtrl(parent, id, startText, pos, size, style) | ||
| 32 | { | ||
| 33 | this->undoText = undoType; | ||
| 34 | this->realParent = realParent; | ||
| 35 | |||
| 36 | Init(); | ||
| 37 | } | ||
| 38 | |||
| 39 | void UndoableTextBox::Init() | ||
| 40 | { | ||
| 41 | Bind(wxEVT_TEXT, &UndoableTextBox::OnTextChange, this); | ||
| 42 | |||
| 43 | curText = GetValue(); | ||
| 44 | } | ||
| 45 | |||
| 46 | void UndoableTextBox::OnFocus(wxFocusEvent& event) | ||
| 47 | { | ||
| 48 | curText = GetValue(); | ||
| 49 | |||
| 50 | event.Skip(); | ||
| 51 | } | ||
| 52 | |||
| 53 | void UndoableTextBox::OnKillFocus(wxFocusEvent& event) | ||
| 54 | { | ||
| 55 | undo.reset(); | ||
| 56 | |||
| 57 | event.Skip(); | ||
| 58 | } | ||
| 59 | |||
| 60 | void UndoableTextBox::OnTextChange(wxCommandEvent& event) | ||
| 61 | { | ||
| 62 | if (!undo) | ||
| 63 | { | ||
| 64 | auto tempUndo = std::make_shared<Undo>(undoText, curText, *this); | ||
| 65 | |||
| 66 | realParent->commitAfter(tempUndo); | ||
| 67 | |||
| 68 | undo = tempUndo; | ||
| 69 | } | ||
| 70 | |||
| 71 | undo->newText = GetValue(); | ||
| 72 | curText = GetValue(); | ||
| 73 | |||
| 74 | event.Skip(); | ||
| 75 | } | ||
| 76 | |||
| 77 | UndoableTextBox::Undo::Undo(std::string title, wxString origText, UndoableTextBox& parent) : origText(origText), parent(parent) | ||
| 78 | { | ||
| 79 | this->title = title; | ||
| 80 | } | ||
| 81 | |||
| 82 | void UndoableTextBox::Undo::apply() | ||
| 83 | { | ||
| 84 | parent.ChangeValue(newText); | ||
| 85 | } | ||
| 86 | |||
| 87 | void UndoableTextBox::Undo::undo() | ||
| 88 | { | ||
| 89 | parent.ChangeValue(origText); | ||
| 90 | } | ||
| 91 | |||
| 92 | void UndoableTextBox::Undo::endChanges() | ||
| 93 | { | ||
| 94 | parent.undo.reset(); | ||
| 95 | } | ||
