File: UndoManager.cpp

package info (click to toggle)
mygui 3.4.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 38,800 kB
  • sloc: cpp: 122,825; ansic: 30,231; xml: 15,792; cs: 12,601; tcl: 776; python: 397; makefile: 38
file content (119 lines) | stat: -rw-r--r-- 2,418 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "Precompiled.h"
#include "UndoManager.h"
#include "CommandManager.h"
#include "WidgetSelectorManager.h"

namespace tools
{
	MYGUI_SINGLETON_DEFINITION(UndoManager);

	const int UNDO_COUNT = 64;

	UndoManager::UndoManager() :
		mPosition(0),
		mOperations(UNDO_COUNT),
		mLastProperty(0),
		mEditorWidgets(nullptr),
		mUnsaved(false),
		mSingletonHolder(this)
	{
		CommandManager::getInstance().getEvent("Command_Undo")->connect(this, &UndoManager::commandUndo);
		CommandManager::getInstance().getEvent("Command_Redo")->connect(this, &UndoManager::commandRedo);
	}

	void UndoManager::initialise(EditorWidgets* _ew)
	{
		mPosition = 0;
		mLastProperty = PR_DEFAULT;
		mEditorWidgets = _ew;
		UndoManager::getInstance().addValue();
		setUnsaved(false);
	}

	void UndoManager::shutdown()
	{
		for (size_t i = 0; i < mOperations.GetSize(); i++)
		{
			delete mOperations[i];
		}
		mOperations.Clear();
	}

	void UndoManager::undo()
	{
		if (mPosition == mOperations.GetSize() - 1) return;

		setUnsaved(true);

		mPosition++;
		mEditorWidgets->clear();
		mEditorWidgets->loadxmlDocument(mOperations[mPosition]);
	}

	void UndoManager::redo()
	{
		if (mPosition == 0) return;

		setUnsaved(true);

		mPosition--;
		mEditorWidgets->clear();
		mEditorWidgets->loadxmlDocument(mOperations[mPosition]);
	}

	void UndoManager::addValue(int _property)
	{
		setUnsaved(true);

		if ((_property != PR_DEFAULT) && (_property == mLastProperty))
		{
			delete mOperations.Front();
			mOperations.PopFirst();
			mOperations.Push( mEditorWidgets->savexmlDocument() );
			return;
		}

		mLastProperty = _property;

		if ( mPosition != 0 )
		{
			mLastProperty = PR_DEFAULT;
			while (mPosition)
			{
				delete mOperations.Front();
				mOperations.PopFirst();
				mPosition--;
			}
		}

		if ( mOperations.IsFull() ) delete mOperations.Back();
		mOperations.Push( mEditorWidgets->savexmlDocument() );
		mPosition = 0;
	}

	void UndoManager::commandUndo(const MyGUI::UString& _commandName, bool& _result)
	{
		undo();
		WidgetSelectorManager::getInstance().setSelectedWidget(nullptr);

		_result = true;
	}

	void UndoManager::commandRedo(const MyGUI::UString& _commandName, bool& _result)
	{
		redo();
		WidgetSelectorManager::getInstance().setSelectedWidget(nullptr);

		_result = true;
	}

	void UndoManager::setUnsaved(bool _value)
	{
		if (mUnsaved != _value)
		{
			mUnsaved = _value;
			eventChanges(mUnsaved);
		}
	}

}