summary refs log tree commit diff stats
path: root/tools/mapedit/src/undo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/mapedit/src/undo.cpp')
-rw-r--r--tools/mapedit/src/undo.cpp95
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
4Undoable::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
11std::string Undoable::getTitle() const
12{
13 return title;
14}
15
16void Undoable::apply()
17{
18 forward();
19}
20
21void Undoable::undo()
22{
23 backward();
24}
25
26wxBEGIN_EVENT_TABLE(UndoableTextBox, wxTextCtrl)
27 EVT_SET_FOCUS(UndoableTextBox::OnFocus)
28 EVT_KILL_FOCUS(UndoableTextBox::OnKillFocus)
29wxEND_EVENT_TABLE()
30
31UndoableTextBox::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
39void UndoableTextBox::Init()
40{
41 Bind(wxEVT_TEXT, &UndoableTextBox::OnTextChange, this);
42
43 curText = GetValue();
44}
45
46void UndoableTextBox::OnFocus(wxFocusEvent& event)
47{
48 curText = GetValue();
49
50 event.Skip();
51}
52
53void UndoableTextBox::OnKillFocus(wxFocusEvent& event)
54{
55 undo.reset();
56
57 event.Skip();
58}
59
60void 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
77UndoableTextBox::Undo::Undo(std::string title, wxString origText, UndoableTextBox& parent) : origText(origText), parent(parent)
78{
79 this->title = title;
80}
81
82void UndoableTextBox::Undo::apply()
83{
84 parent.ChangeValue(newText);
85}
86
87void UndoableTextBox::Undo::undo()
88{
89 parent.ChangeValue(origText);
90}
91
92void UndoableTextBox::Undo::endChanges()
93{
94 parent.undo.reset();
95}