1 #include "Precompiled.h"
2 #include "UndoManager.h"
3 #include "CommandManager.h"
4 #include "WidgetSelectorManager.h"
5 
6 template <> tools::UndoManager* MyGUI::Singleton<tools::UndoManager>::msInstance = nullptr;
7 template <> const char* MyGUI::Singleton<tools::UndoManager>::mClassTypeName = "UndoManager";
8 
9 namespace tools
10 {
11 
12 	const int UNDO_COUNT = 64;
13 
UndoManager()14 	UndoManager::UndoManager() :
15 		mPosition(0),
16 		mOperations(UNDO_COUNT),
17 		mLastProperty(0),
18 		mEditorWidgets(nullptr),
19 		mUnsaved(false)
20 	{
21 		CommandManager::getInstance().getEvent("Command_Undo")->connect(this, &UndoManager::commandUndo);
22 		CommandManager::getInstance().getEvent("Command_Redo")->connect(this, &UndoManager::commandRedo);
23 	}
24 
initialise(EditorWidgets * _ew)25 	void UndoManager::initialise(EditorWidgets* _ew)
26 	{
27 		mPosition = 0;
28 		mLastProperty = PR_DEFAULT;
29 		mEditorWidgets = _ew;
30 		UndoManager::getInstance().addValue();
31 		setUnsaved(false);
32 	}
33 
shutdown()34 	void UndoManager::shutdown()
35 	{
36 		for (size_t i = 0; i < mOperations.GetSize(); i++)
37 		{
38 			delete mOperations[i];
39 		}
40 		mOperations.Clear();
41 	}
42 
undo()43 	void UndoManager::undo()
44 	{
45 		if (mPosition == mOperations.GetSize() - 1) return;
46 
47 		setUnsaved(true);
48 
49 		mPosition++;
50 		mEditorWidgets->clear();
51 		mEditorWidgets->loadxmlDocument(mOperations[mPosition]);
52 	}
53 
redo()54 	void UndoManager::redo()
55 	{
56 		if (mPosition == 0) return;
57 
58 		setUnsaved(true);
59 
60 		mPosition--;
61 		mEditorWidgets->clear();
62 		mEditorWidgets->loadxmlDocument(mOperations[mPosition]);
63 	}
64 
addValue(int _property)65 	void UndoManager::addValue(int _property)
66 	{
67 		setUnsaved(true);
68 
69 		if ((_property != PR_DEFAULT) && (_property == mLastProperty))
70 		{
71 			delete mOperations.Front();
72 			mOperations.PopFirst();
73 			mOperations.Push( mEditorWidgets->savexmlDocument() );
74 			return;
75 		}
76 
77 		mLastProperty = _property;
78 
79 		if ( mPosition != 0 )
80 		{
81 			mLastProperty = PR_DEFAULT;
82 			while (mPosition)
83 			{
84 				delete mOperations.Front();
85 				mOperations.PopFirst();
86 				mPosition--;
87 			}
88 		}
89 
90 		if ( mOperations.IsFull() ) delete mOperations.Back();
91 		mOperations.Push( mEditorWidgets->savexmlDocument() );
92 		mPosition = 0;
93 	}
94 
commandUndo(const MyGUI::UString & _commandName,bool & _result)95 	void UndoManager::commandUndo(const MyGUI::UString& _commandName, bool& _result)
96 	{
97 		undo();
98 		WidgetSelectorManager::getInstance().setSelectedWidget(nullptr);
99 
100 		_result = true;
101 	}
102 
commandRedo(const MyGUI::UString & _commandName,bool & _result)103 	void UndoManager::commandRedo(const MyGUI::UString& _commandName, bool& _result)
104 	{
105 		redo();
106 		WidgetSelectorManager::getInstance().setSelectedWidget(nullptr);
107 
108 		_result = true;
109 	}
110 
setUnsaved(bool _value)111 	void UndoManager::setUnsaved(bool _value)
112 	{
113 		if (mUnsaved != _value)
114 		{
115 			mUnsaved = _value;
116 			eventChanges(mUnsaved);
117 		}
118 	}
119 
120 }
121