File: Control.cpp

package info (click to toggle)
mygui 3.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,224 kB
  • sloc: cpp: 118,031; ansic: 30,202; xml: 15,544; cs: 12,602; tcl: 776; python: 417; makefile: 34
file content (206 lines) | stat: -rw-r--r-- 5,911 bytes parent folder | download | duplicates (6)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*!
	@file
	@author		Albert Semenov
	@date		07/2012
*/

#include "Precompiled.h"
#include "Control.h"
#include "FactoryManager.h"
#include "CommandManager.h"

namespace tools
{

	Control::Control() :
		mParent(nullptr)
	{
	}

	Control::~Control()
	{
		DeactivateControllers();

		for (VectorController::iterator controller = mControllers.begin(); controller != mControllers.end(); controller ++)
			delete (*controller);
		mControllers.clear();

		for (VectorControl::iterator child = mChilds.begin(); child != mChilds.end(); child ++)
			delete *child;
		mChilds.clear();
	}

	MyGUI::Widget* Control::getRoot()
	{
		return mMainWidget;
	}

	void Control::Initialise(const std::string& _layoutName)
	{
		OnInitialise(nullptr, nullptr, _layoutName);
		ActivateControllers();
	}

	void Control::Initialise(Control* _parent, MyGUI::Widget* _place, const std::string& _layoutName)
	{
		OnInitialise(_parent, _place, _layoutName);
		ActivateControllers();
	}

	void Control::OnInitialise(Control* _parent, MyGUI::Widget* _place, const std::string& _layoutName)
	{
		mParent = _parent;
		if (_parent != nullptr)
		{
			initialise(_layoutName, _place);
			_parent->mChilds.push_back(this);
		}
		else
		{
			initialise(_layoutName, nullptr);
		}

		AdviceWidget(mMainWidget);

		for (size_t index = 0; index < getRoot()->getChildCount(); index ++)
			CreateChilds(this, getRoot()->getChildAt(index));
	}

	void Control::CreateChilds(Control* _parent, MyGUI::Widget* _widget)
	{
		AdviceWidget(_widget);

		std::string controlType = _widget->getUserString("ControlType");
		if (!controlType.empty())
		{
			std::string controlLayout = _widget->getUserString("ControlLayout");

			Control* control = components::FactoryManager::GetInstance().CreateItem<Control>(controlType);
			if (control != nullptr)
			{
				control->OnInitialise(_parent, _widget, controlLayout);
				return;
			}
		}

		for (size_t index = 0; index < _widget->getChildCount(); index ++)
			CreateChilds(_parent, _widget->getChildAt(index));
	}

	void Control::notifyMouseButtonClick(MyGUI::Widget* _sender)
	{
		CommandManager::getInstance().executeCommand(_sender->getUserString("CommandClick"));
	}

	const Control::VectorControl& Control::getChilds() const
	{
		return mChilds;
	}

	void Control::notifyTabChangeSelect(MyGUI::TabControl* _sender, size_t _index)
	{
		if (_index != MyGUI::ITEM_NONE)
			CommandManager::getInstance().executeCommand(_sender->getItemAt(_index)->getUserString("CommandActivate"));
	}

	void Control::notifyWindowButtonPressed(MyGUI::Window* _sender, const std::string& _name)
	{
		if (_name == "close")
			CommandManager::getInstance().executeCommand(_sender->getUserString("CommandClose"));
	}

	void Control::AdviceWidget(MyGUI::Widget* _widget)
	{
		std::string command = _widget->getUserString("CommandClick");
		if (!command.empty())
			_widget->eventMouseButtonClick += MyGUI::newDelegate(this, &Control::notifyMouseButtonClick);

		MyGUI::TabControl* tab = _widget->castType<MyGUI::TabControl>(false);
		if (tab != nullptr)
		{
			if (tab->getItemCount() != 0 && tab->getItemAt(0)->getUserString("CommandActivate") != "")
				tab->eventTabChangeSelect += MyGUI::newDelegate(this, &Control::notifyTabChangeSelect);
		}

		MyGUI::Window* window = _widget->castType<MyGUI::Window>(false);
		if (window != nullptr && window->getUserString("CommandClose") != "")
			window->eventWindowButtonPressed += MyGUI::newDelegate(this, &Control::notifyWindowButtonPressed);

		command = _widget->getUserString("CommandAccept");
		if (!command.empty())
		{
			MyGUI::EditBox* edit = _widget->castType<MyGUI::EditBox>(false);
			if (edit != nullptr)
				edit->eventEditSelectAccept += MyGUI::newDelegate(this, &Control::notifyEditSelectAccept);
		}
	}

	void Control::SendCommand(const std::string& _command)
	{
		OnCommand(_command);

		for (VectorControl::iterator child = mChilds.begin(); child != mChilds.end(); child ++)
			(*child)->SendCommand(_command);
	}

	void Control::OnCommand(const std::string& _command)
	{
	}

	void Control::notifyEditSelectAccept(MyGUI::EditBox* _sender)
	{
		CommandManager::getInstance().executeCommand(_sender->getUserString("CommandAccept"));
	}

	MyGUI::Widget* Control::CreateFakeWidgetT(const std::string& _typeName, MyGUI::Widget* _parent)
	{
		if (_parent)
			return _parent->createWidgetT(_typeName, MyGUI::SkinManager::getInstance().getDefaultSkin(), MyGUI::IntCoord(), MyGUI::Align::Default);

		return MyGUI::Gui::getInstance().createWidgetT(_typeName, MyGUI::SkinManager::getInstance().getDefaultSkin(), MyGUI::IntCoord(), MyGUI::Align::Default, "");
	}

	void Control::CreateControllers()
	{
		std::string controllers = mMainWidget->getUserString("ControlControllers");
		if (!controllers.empty())
		{
			std::vector<std::string> values = MyGUI::utility::split(controllers, "\t\n ,");
			for (std::vector<std::string>::const_iterator value = values.begin(); value != values.end(); value ++)
			{
				IControlController* controller = components::FactoryManager::GetInstance().CreateItem<IControlController>(*value);
				if (controller != nullptr)
				{
					controller->setTarget(this);
					mControllers.push_back(controller);
				}
			}
		}
	}

	void Control::ActivateControllers()
	{
		CreateControllers();

		for (VectorController::iterator controller = mControllers.begin(); controller != mControllers.end(); controller ++)
			(*controller)->activate();

		for (VectorControl::iterator child = mChilds.begin(); child != mChilds.end(); child ++)
			(*child)->ActivateControllers();
	}

	void Control::DeactivateControllers()
	{
		for (VectorController::iterator controller = mControllers.begin(); controller != mControllers.end(); controller ++)
			(*controller)->deactivate();
	}

	void Control::Shutdown()
	{
		mParent->mChilds.erase(std::remove(mParent->mChilds.begin(), mParent->mChilds.end(), this), mParent->mChilds.end());
		mParent = nullptr;

		shutdown();
	}

}