File: Control.cpp

package info (click to toggle)
mygui 3.4.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,792 kB
  • sloc: cpp: 133,849; ansic: 30,249; xml: 15,794; cs: 12,601; tcl: 776; python: 400; makefile: 35; sh: 4
file content (214 lines) | stat: -rw-r--r-- 5,502 bytes parent folder | download
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
207
208
209
210
211
212
213
214
/*!
	@file
	@author		Albert Semenov
	@date		07/2012
*/

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

namespace tools
{

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

		for (auto& controller : mControllers)
			delete controller;
		mControllers.clear();

		for (auto& child : mChilds)
			delete child;
		mChilds.clear();
	}

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

	void Control::Initialise(std::string_view _layoutName)
	{
		OnInitialise(nullptr, nullptr, _layoutName);
		ActivateControllers();
	}

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

	void Control::OnInitialise(Control* _parent, MyGUI::Widget* _place, std::string_view _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_view controlType = _widget->getUserString("ControlType");
		if (!controlType.empty())
		{
			std::string_view 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(MyGUI::UString(_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(
				MyGUI::UString(_sender->getItemAt(_index)->getUserString("CommandActivate")));
	}

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

	void Control::AdviceWidget(MyGUI::Widget* _widget)
	{
		std::string_view 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").empty())
				tab->eventTabChangeSelect += MyGUI::newDelegate(this, &Control::notifyTabChangeSelect);
		}

		MyGUI::Window* window = _widget->castType<MyGUI::Window>(false);
		if (window != nullptr && !window->getUserString("CommandClose").empty())
			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(std::string_view _command)
	{
		OnCommand(_command);

		for (auto& child : mChilds)
			child->SendCommand(_command);
	}

	void Control::OnCommand(std::string_view _command)
	{
	}

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

	MyGUI::Widget* Control::CreateFakeWidgetT(std::string_view _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,
			std::string_view{});
	}

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

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

		for (auto& controller : mControllers)
			controller->activate();

		for (auto& child : mChilds)
			child->ActivateControllers();
	}

	void Control::DeactivateControllers()
	{
		for (auto& controller : mControllers)
			controller->deactivate();
	}

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

		shutdown();
	}

}