From e882367d80a0bcdd09b5412d908b0fdb6b6bfe34 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 17 Mar 2015 13:58:32 -0400 Subject: Implemented undo/redo framework in map editor --- tools/mapedit/src/undo.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 tools/mapedit/src/undo.cpp (limited to 'tools/mapedit/src/undo.cpp') 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 @@ +#include "undo.h" +#include "frame.h" + +Undoable::Undoable(std::string title, std::function forward, std::function backward) +{ + this->title = title; + this->forward = forward; + this->backward = backward; +} + +std::string Undoable::getTitle() const +{ + return title; +} + +void Undoable::apply() +{ + forward(); +} + +void Undoable::undo() +{ + backward(); +} + +wxBEGIN_EVENT_TABLE(UndoableTextBox, wxTextCtrl) + EVT_SET_FOCUS(UndoableTextBox::OnFocus) + EVT_KILL_FOCUS(UndoableTextBox::OnKillFocus) +wxEND_EVENT_TABLE() + +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) +{ + this->undoText = undoType; + this->realParent = realParent; + + Init(); +} + +void UndoableTextBox::Init() +{ + Bind(wxEVT_TEXT, &UndoableTextBox::OnTextChange, this); + + curText = GetValue(); +} + +void UndoableTextBox::OnFocus(wxFocusEvent& event) +{ + curText = GetValue(); + + event.Skip(); +} + +void UndoableTextBox::OnKillFocus(wxFocusEvent& event) +{ + undo.reset(); + + event.Skip(); +} + +void UndoableTextBox::OnTextChange(wxCommandEvent& event) +{ + if (!undo) + { + auto tempUndo = std::make_shared(undoText, curText, *this); + + realParent->commitAfter(tempUndo); + + undo = tempUndo; + } + + undo->newText = GetValue(); + curText = GetValue(); + + event.Skip(); +} + +UndoableTextBox::Undo::Undo(std::string title, wxString origText, UndoableTextBox& parent) : origText(origText), parent(parent) +{ + this->title = title; +} + +void UndoableTextBox::Undo::apply() +{ + parent.ChangeValue(newText); +} + +void UndoableTextBox::Undo::undo() +{ + parent.ChangeValue(origText); +} + +void UndoableTextBox::Undo::endChanges() +{ + parent.undo.reset(); +} -- cgit 1.4.1