File: SeparatorTextureController.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 (276 lines) | stat: -rw-r--r-- 7,964 bytes parent folder | download | duplicates (4)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*!
	@file
	@author		Albert Semenov
	@date		07/2012
*/

#include "Precompiled.h"
#include "SeparatorTextureController.h"
#include "FactoryManager.h"
#include "DataSelectorManager.h"
#include "DataManager.h"
#include "PropertyUtility.h"
#include "ScopeManager.h"
#include "DataUtility.h"

namespace tools
{

	FACTORY_ITEM_ATTRIBUTE(SeparatorTextureController)

	SeparatorTextureController::SeparatorTextureController() :
		mControl(nullptr),
		mParentData(nullptr),
		mActivated(false)
	{
	}

	SeparatorTextureController::~SeparatorTextureController()
	{
	}

	void SeparatorTextureController::setTarget(Control* _control)
	{
		mControl = _control->findControl<ScopeTextureControl>();
	}

	void SeparatorTextureController::activate()
	{
		mParentTypeName = "Skin";
		mThisType = "Separator";
		mScopeName = "Separator";

		ScopeManager::getInstance().eventChangeScope.connect(this, &SeparatorTextureController::notifyChangeScope);
		notifyChangeScope(ScopeManager::getInstance().getCurrentScope());
	}

	void SeparatorTextureController::deactivate()
	{
		ScopeManager::getInstance().eventChangeScope.disconnect(this);
	}

	void SeparatorTextureController::notifyChangeDataSelector(DataPtr _data, bool _changeOnlySelection)
	{
		mParentData = _data;
		if (mParentData != nullptr && mParentData->getType()->getName() != mParentTypeName)
			mParentData = nullptr;

		std::string texture;
		PropertyPtr property = PropertyUtility::getPropertyByName("Skin", "Texture");
		if (property != nullptr)
		{
			texture = property->getValue();

			if (!property->eventChangeProperty.exist(this, &SeparatorTextureController::notifyChangeProperty))
				property->eventChangeProperty.connect(this, &SeparatorTextureController::notifyChangeProperty);
		}

		std::string coord;
		property = PropertyUtility::getPropertyByName("Skin", "Size");
		if (property != nullptr)
		{
			coord = property->getValue();

			if (!property->eventChangeProperty.exist(this, &SeparatorTextureController::notifyChangeProperty))
				property->eventChangeProperty.connect(this, &SeparatorTextureController::notifyChangeProperty);
		}

		if (mParentData != nullptr)
		{
			for (Data::VectorData::const_iterator child = mParentData->getChilds().begin(); child != mParentData->getChilds().end(); child ++)
			{
				if ((*child)->getType()->getName() != mThisType)
					continue;

				property = (*child)->getProperty("Offset");
				if (!property->eventChangeProperty.exist(this, &SeparatorTextureController::notifyChangeProperty))
					property->eventChangeProperty.connect(this, &SeparatorTextureController::notifyChangeProperty);

				property = (*child)->getProperty("Visible");
				if (!property->eventChangeProperty.exist(this, &SeparatorTextureController::notifyChangeProperty))
					property->eventChangeProperty.connect(this, &SeparatorTextureController::notifyChangeProperty);
			}
		}

		mControl->setTextureValue(texture);
		updateCoords(coord);
	}

	void SeparatorTextureController::notifyChangeProperty(PropertyPtr _sender)
	{
		if (!mActivated)
			return;

		if (_sender->getOwner()->getType()->getName() == "Skin")
		{
			if (_sender->getType()->getName() == "Texture")
				mControl->setTextureValue(_sender->getValue());
			else if (_sender->getType()->getName() == "Size")
				updateCoords(_sender->getValue());
		}
		else if (_sender->getOwner()->getType()->getName() == "Separator")
		{
			if (_sender->getType()->getName() == "Offset")
				updateFrames();
			else if (_sender->getType()->getName() == "Visible")
				updateFrames();
		}
	}

	void SeparatorTextureController::notifyChangeValue(const std::string& _value)
	{
		if (mParentData != nullptr)
		{
			DataPtr selected = mParentData->getChildSelected();
			if (selected != nullptr && selected->getType()->getName() == "Separator")
			{
				MyGUI::IntCoord value = MyGUI::IntCoord::parse(_value);
				int offset = getOffsetByName(value, selected->getPropertyValue("Name"));
				PropertyPtr property = selected->getProperty("Offset");
				PropertyUtility::executeAction(property, MyGUI::utility::toString(offset), true);
			}
		}
	}

	void SeparatorTextureController::notifyChangeScope(const std::string& _scope)
	{
		if (mControl == nullptr)
			return;

		if (_scope == mScopeName)
		{
			if (!mActivated)
			{
				mControl->eventChangeValue.connect(this, &SeparatorTextureController::notifyChangeValue);
				mControl->clearAll();

				DataSelectorManager::getInstance().getEvent(mParentTypeName)->connect(this, &SeparatorTextureController::notifyChangeDataSelector);
				mParentData = DataUtility::getSelectedDataByType(mParentTypeName);
				notifyChangeDataSelector(mParentData, false);

				mControl->getRoot()->setUserString("CurrentScopeController", mScopeName);

				mActivated = true;
			}
		}
		else
		{
			if (mActivated)
			{
				mControl->eventChangeValue.disconnect(this);

				DataSelectorManager::getInstance().getEvent(mParentTypeName)->disconnect(this);
				mParentData = nullptr;

				//      
				std::string value = mControl->getRoot()->getUserString("CurrentScopeController");
				if (value == mScopeName)
				{
					mControl->getRoot()->setUserString("CurrentScopeController", "");
					notifyChangeDataSelector(mParentData, false);

					mControl->clearAll();
				}

				mActivated = false;
			}
		}
	}

	void SeparatorTextureController::updateCoords(const std::string& _value)
	{
		MyGUI::IntCoord coord;
		if (MyGUI::utility::parseComplex(_value, coord.left, coord.top, coord.width, coord.height))
			mTextureCoord = coord;
		else
			mTextureCoord.clear();

		mControl->setTextureRegion(mTextureCoord);

		updateFrames();
	}

	void SeparatorTextureController::updateFrames()
	{
		mFrames.clear();

		if (mParentData != nullptr)
		{
			DataPtr selected = mParentData->getChildSelected();
			for (Data::VectorData::const_iterator child = mParentData->getChilds().begin(); child != mParentData->getChilds().end(); child ++)
			{
				if ((*child)->getType()->getName() != mThisType)
					continue;

				bool visible = (*child)->getPropertyValue<bool>("Visible");
				int offset = (*child)->getPropertyValue<int>("Offset");

				std::string name = (*child)->getPropertyValue("Name");
				MyGUI::IntCoord value = getCoordByName(name, offset);
				ScopeTextureControl::SelectorType type = getTypeByName(name);

				if (selected == *child)
				{
					if (visible)
						mControl->setCoordValue(value, type);
					else
						mControl->clearCoordValue();
				}
				else
				{
					if (visible)
					{
						mFrames.push_back(std::make_pair(value, type));
					}
				}
			}

			if (selected == nullptr)
			{
				mControl->clearCoordValue();
			}
		}

		if (mControl != nullptr)
			mControl->setViewSelectors(mFrames);
	}

	ScopeTextureControl::SelectorType SeparatorTextureController::getTypeByName(const std::string& _name)
	{
		if (_name == "Left" || _name == "Right")
			return ScopeTextureControl::SelectorOffsetV;
		else if (_name == "Top" || _name == "Bottom")
			return ScopeTextureControl::SelectorOffsetH;

		return ScopeTextureControl::SelectorPosition;
	}

	MyGUI::IntCoord SeparatorTextureController::getCoordByName(const std::string& _name, int _offset)
	{
		if (_name == "Left")
			return MyGUI::IntCoord(_offset, 0, 1, mTextureCoord.height);
		else if (_name == "Right")
			return MyGUI::IntCoord(mTextureCoord.width - _offset, 0, 1, mTextureCoord.height);
		else if (_name == "Top")
			return MyGUI::IntCoord(0, _offset, mTextureCoord.width, 1);
		else if (_name == "Bottom")
			return MyGUI::IntCoord(0, mTextureCoord.height - _offset, mTextureCoord.width, 1);

		return MyGUI::IntCoord();
	}

	int SeparatorTextureController::getOffsetByName(const MyGUI::IntCoord& _coord, const std::string& _name)
	{
		if (_name == "Left")
			return _coord.left;
		else if (_name == "Right")
			return mTextureCoord.width - _coord.left;
		else if (_name == "Top")
			return _coord.top;
		else if (_name == "Bottom")
			return mTextureCoord.height - _coord.top;

		return 0;
	}

}