1 /*!
2 	@file
3 	@author		Albert Semenov
4 	@date		08/2010
5 */
6 
7 #include "Precompiled.h"
8 #include "ScopeTextureControl.h"
9 #include "CommandManager.h"
10 #include "Localise.h"
11 #include "GridManager.h"
12 #include "DataSelectorManager.h"
13 #include "DataManager.h"
14 #include "PositionSelectorBlackControl.h"
15 #include "HorizontalSelectorBlackControl.h"
16 #include "VerticalSelectorBlackControl.h"
17 #include "AreaSelectorControl.h"
18 #include "PositionSelectorControl.h"
19 #include "HorizontalSelectorControl.h"
20 #include "VerticalSelectorControl.h"
21 
22 namespace tools
23 {
24 
ScopeTextureControl()25 	ScopeTextureControl::ScopeTextureControl() :
26 		mCurrentSelectorControl(nullptr),
27 		mCurrentSelectorType(SelectorNone)
28 	{
29 	}
30 
~ScopeTextureControl()31 	ScopeTextureControl::~ScopeTextureControl()
32 	{
33 		for (VectorSelector::iterator selector = mSelectors.begin(); selector != mSelectors.end(); selector ++)
34 			(*selector).first->eventChangePosition.disconnect(this);
35 	}
36 
OnInitialise(Control * _parent,MyGUI::Widget * _place,const std::string & _layoutName)37 	void ScopeTextureControl::OnInitialise(Control* _parent, MyGUI::Widget* _place, const std::string& _layoutName)
38 	{
39 		TextureToolControl::OnInitialise(_parent, _place, _layoutName);
40 
41 		CommandManager::getInstance().getEvent("Command_MoveLeft")->connect(this, &ScopeTextureControl::CommandMoveLeft);
42 		CommandManager::getInstance().getEvent("Command_MoveRight")->connect(this, &ScopeTextureControl::CommandMoveRight);
43 		CommandManager::getInstance().getEvent("Command_MoveTop")->connect(this, &ScopeTextureControl::CommandMoveTop);
44 		CommandManager::getInstance().getEvent("Command_MoveBottom")->connect(this, &ScopeTextureControl::CommandMoveBottom);
45 		CommandManager::getInstance().getEvent("Command_SizeLeft")->connect(this, &ScopeTextureControl::CommandSizeLeft);
46 		CommandManager::getInstance().getEvent("Command_SizeRight")->connect(this, &ScopeTextureControl::CommandSizeRight);
47 		CommandManager::getInstance().getEvent("Command_SizeTop")->connect(this, &ScopeTextureControl::CommandSizeTop);
48 		CommandManager::getInstance().getEvent("Command_SizeBottom")->connect(this, &ScopeTextureControl::CommandSizeBottom);
49 		CommandManager::getInstance().getEvent("Command_GridMoveLeft")->connect(this, &ScopeTextureControl::CommandGridMoveLeft);
50 		CommandManager::getInstance().getEvent("Command_GridMoveRight")->connect(this, &ScopeTextureControl::CommandGridMoveRight);
51 		CommandManager::getInstance().getEvent("Command_GridMoveTop")->connect(this, &ScopeTextureControl::CommandGridMoveTop);
52 		CommandManager::getInstance().getEvent("Command_GridMoveBottom")->connect(this, &ScopeTextureControl::CommandGridMoveBottom);
53 		CommandManager::getInstance().getEvent("Command_GridSizeLeft")->connect(this, &ScopeTextureControl::CommandGridSizeLeft);
54 		CommandManager::getInstance().getEvent("Command_GridSizeRight")->connect(this, &ScopeTextureControl::CommandGridSizeRight);
55 		CommandManager::getInstance().getEvent("Command_GridSizeTop")->connect(this, &ScopeTextureControl::CommandGridSizeTop);
56 		CommandManager::getInstance().getEvent("Command_GridSizeBottom")->connect(this, &ScopeTextureControl::CommandGridSizeBottom);
57 
58 		updateCaption();
59 
60 		setTextureValue("");
61 		clearCoordValue();
62 	}
63 
onMouseButtonClick(const MyGUI::IntPoint & _point)64 	void ScopeTextureControl::onMouseButtonClick(const MyGUI::IntPoint& _point)
65 	{
66 		mCoordValue.left = _point.left - (mCoordValue.width / 2);
67 		mCoordValue.top = _point.top - (mCoordValue.height / 2);
68 
69 		updateFromCoordValue();
70 	}
71 
updateFromCoordValue()72 	void ScopeTextureControl::updateFromCoordValue()
73 	{
74 		if (mCurrentSelectorControl != nullptr)
75 			mCurrentSelectorControl->setCoord(mCoordValue);
76 
77 		setValue(mCoordValue.print());
78 	}
79 
CommandMoveLeft(const MyGUI::UString & _commandName,bool & _result)80 	void ScopeTextureControl::CommandMoveLeft(const MyGUI::UString& _commandName, bool& _result)
81 	{
82 		if (!checkCommand())
83 			return;
84 
85 		mCoordValue.left --;
86 		updateFromCoordValue();
87 
88 		_result = true;
89 	}
90 
CommandMoveRight(const MyGUI::UString & _commandName,bool & _result)91 	void ScopeTextureControl::CommandMoveRight(const MyGUI::UString& _commandName, bool& _result)
92 	{
93 		if (!checkCommand())
94 			return;
95 
96 		mCoordValue.left ++;
97 		updateFromCoordValue();
98 
99 		_result = true;
100 	}
101 
CommandMoveTop(const MyGUI::UString & _commandName,bool & _result)102 	void ScopeTextureControl::CommandMoveTop(const MyGUI::UString& _commandName, bool& _result)
103 	{
104 		if (!checkCommand())
105 			return;
106 
107 		mCoordValue.top --;
108 		updateFromCoordValue();
109 
110 		_result = true;
111 	}
112 
CommandMoveBottom(const MyGUI::UString & _commandName,bool & _result)113 	void ScopeTextureControl::CommandMoveBottom(const MyGUI::UString& _commandName, bool& _result)
114 	{
115 		if (!checkCommand())
116 			return;
117 
118 		mCoordValue.top ++;
119 		updateFromCoordValue();
120 
121 		_result = true;
122 	}
123 
CommandGridMoveLeft(const MyGUI::UString & _commandName,bool & _result)124 	void ScopeTextureControl::CommandGridMoveLeft(const MyGUI::UString& _commandName, bool& _result)
125 	{
126 		if (!checkCommand())
127 			return;
128 
129 		mCoordValue.left = GridManager::getInstance().toGrid(mCoordValue.left, GridManager::Previous);
130 		updateFromCoordValue();
131 
132 		_result = true;
133 	}
134 
CommandGridMoveRight(const MyGUI::UString & _commandName,bool & _result)135 	void ScopeTextureControl::CommandGridMoveRight(const MyGUI::UString& _commandName, bool& _result)
136 	{
137 		if (!checkCommand())
138 			return;
139 
140 		mCoordValue.left = GridManager::getInstance().toGrid(mCoordValue.left, GridManager::Next);
141 		updateFromCoordValue();
142 
143 		_result = true;
144 	}
145 
CommandGridMoveTop(const MyGUI::UString & _commandName,bool & _result)146 	void ScopeTextureControl::CommandGridMoveTop(const MyGUI::UString& _commandName, bool& _result)
147 	{
148 		if (!checkCommand())
149 			return;
150 
151 		mCoordValue.top = GridManager::getInstance().toGrid(mCoordValue.top, GridManager::Previous);
152 		updateFromCoordValue();
153 
154 		_result = true;
155 	}
156 
CommandGridMoveBottom(const MyGUI::UString & _commandName,bool & _result)157 	void ScopeTextureControl::CommandGridMoveBottom(const MyGUI::UString& _commandName, bool& _result)
158 	{
159 		if (!checkCommand())
160 			return;
161 
162 		mCoordValue.top = GridManager::getInstance().toGrid(mCoordValue.top, GridManager::Next);
163 		updateFromCoordValue();
164 
165 		_result = true;
166 	}
167 
CommandGridSizeLeft(const MyGUI::UString & _commandName,bool & _result)168 	void ScopeTextureControl::CommandGridSizeLeft(const MyGUI::UString& _commandName, bool& _result)
169 	{
170 		if (!checkCommand())
171 			return;
172 
173 		mCoordValue.width = GridManager::getInstance().toGrid(mCoordValue.right(), GridManager::Previous) - mCoordValue.left;
174 		updateFromCoordValue();
175 
176 		_result = true;
177 	}
178 
CommandGridSizeRight(const MyGUI::UString & _commandName,bool & _result)179 	void ScopeTextureControl::CommandGridSizeRight(const MyGUI::UString& _commandName, bool& _result)
180 	{
181 		if (!checkCommand())
182 			return;
183 
184 		mCoordValue.width = GridManager::getInstance().toGrid(mCoordValue.right(), GridManager::Next) - mCoordValue.left;
185 		updateFromCoordValue();
186 
187 		_result = true;
188 	}
189 
CommandGridSizeTop(const MyGUI::UString & _commandName,bool & _result)190 	void ScopeTextureControl::CommandGridSizeTop(const MyGUI::UString& _commandName, bool& _result)
191 	{
192 		if (!checkCommand())
193 			return;
194 
195 		mCoordValue.height = GridManager::getInstance().toGrid(mCoordValue.bottom(), GridManager::Previous) - mCoordValue.top;
196 		updateFromCoordValue();
197 
198 		_result = true;
199 	}
200 
CommandGridSizeBottom(const MyGUI::UString & _commandName,bool & _result)201 	void ScopeTextureControl::CommandGridSizeBottom(const MyGUI::UString& _commandName, bool& _result)
202 	{
203 		if (!checkCommand())
204 			return;
205 
206 		mCoordValue.height = GridManager::getInstance().toGrid(mCoordValue.bottom(), GridManager::Next) - mCoordValue.top;
207 		updateFromCoordValue();
208 
209 		_result = true;
210 	}
211 
CommandSizeLeft(const MyGUI::UString & _commandName,bool & _result)212 	void ScopeTextureControl::CommandSizeLeft(const MyGUI::UString& _commandName, bool& _result)
213 	{
214 		if (!checkCommand())
215 			return;
216 
217 		mCoordValue.width --;
218 		updateFromCoordValue();
219 
220 		_result = true;
221 	}
222 
CommandSizeRight(const MyGUI::UString & _commandName,bool & _result)223 	void ScopeTextureControl::CommandSizeRight(const MyGUI::UString& _commandName, bool& _result)
224 	{
225 		if (!checkCommand())
226 			return;
227 
228 		mCoordValue.width ++;
229 		updateFromCoordValue();
230 
231 		_result = true;
232 	}
233 
CommandSizeTop(const MyGUI::UString & _commandName,bool & _result)234 	void ScopeTextureControl::CommandSizeTop(const MyGUI::UString& _commandName, bool& _result)
235 	{
236 		if (!checkCommand())
237 			return;
238 
239 		mCoordValue.height --;
240 		updateFromCoordValue();
241 
242 		_result = true;
243 	}
244 
CommandSizeBottom(const MyGUI::UString & _commandName,bool & _result)245 	void ScopeTextureControl::CommandSizeBottom(const MyGUI::UString& _commandName, bool& _result)
246 	{
247 		if (!checkCommand())
248 			return;
249 
250 		mCoordValue.height ++;
251 		updateFromCoordValue();
252 
253 		_result = true;
254 	}
255 
notifyChangePosition(SelectorControl * _sender)256 	void ScopeTextureControl::notifyChangePosition(SelectorControl* _sender)
257 	{
258 		mCoordValue = mCurrentSelectorControl->getCoord();
259 
260 		// снапим к гриду
261 		if (!MyGUI::InputManager::getInstance().isShiftPressed())
262 		{
263 			MyGUI::IntCoord coord = mCoordValue;
264 			MyGUI::IntCoord actionScale = mCurrentSelectorControl->getActionScale();
265 
266 			if (actionScale.left != 0 && actionScale.width != 0)
267 			{
268 				int right = coord.right();
269 				coord.width = GridManager::getInstance().toGrid(coord.width);
270 				coord.left = right - coord.width;
271 			}
272 			else if (actionScale.width != 0)
273 			{
274 				int right = GridManager::getInstance().toGrid(coord.right());
275 				coord.width = right - coord.left;
276 			}
277 			else if (actionScale.left != 0)
278 			{
279 				coord.left = GridManager::getInstance().toGrid(coord.left);
280 			}
281 
282 			if (actionScale.top != 0 && actionScale.height != 0)
283 			{
284 				int bottom = coord.bottom();
285 				coord.height = GridManager::getInstance().toGrid(coord.height);
286 				coord.top = bottom - coord.height;
287 			}
288 			else if (actionScale.height != 0)
289 			{
290 				int bottom = GridManager::getInstance().toGrid(coord.bottom());
291 				coord.height = bottom - coord.top;
292 			}
293 			else if (actionScale.top != 0)
294 			{
295 				coord.top = GridManager::getInstance().toGrid(coord.top);
296 			}
297 
298 			if (coord != mCoordValue)
299 			{
300 				mCoordValue = coord;
301 				mCurrentSelectorControl->setCoord(mCoordValue);
302 			}
303 		}
304 
305 		setValue(mCoordValue.print());
306 	}
307 
onChangeScale()308 	void ScopeTextureControl::onChangeScale()
309 	{
310 		updateCaption();
311 	}
312 
updateCaption()313 	void ScopeTextureControl::updateCaption()
314 	{
315 		int scale = (int)(getScale() * (double)100);
316 		addUserTag("CurrentScale", MyGUI::utility::toString(scale));
317 
318 		CommandManager::getInstance().executeCommand("Command_UpdateAppCaption");
319 	}
320 
setValue(const std::string & _value)321 	void ScopeTextureControl::setValue(const std::string& _value)
322 	{
323 		eventChangeValue(_value);
324 	}
325 
setCoordValue(const MyGUI::IntCoord & _value,SelectorType _type)326 	void ScopeTextureControl::setCoordValue(const MyGUI::IntCoord& _value, SelectorType _type)
327 	{
328 		if (mCurrentSelectorType != _type)
329 		{
330 			clearCoordValue();
331 
332 			mCurrentSelectorType = _type;
333 			bool changes = false;
334 			mCurrentSelectorControl = getFreeSelector(mSelectors, false, mCurrentSelectorType, changes);
335 			mCurrentSelectorControl->setCoord(mCoordValue);
336 		}
337 
338 		mCurrentSelectorControl->setVisible(true);
339 
340 		if (mCoordValue != _value)
341 		{
342 			mCoordValue = _value;
343 			mCurrentSelectorControl->setCoord(mCoordValue);
344 		}
345 	}
346 
clearCoordValue()347 	void ScopeTextureControl::clearCoordValue()
348 	{
349 		for (VectorSelector::iterator selector = mSelectors.begin(); selector != mSelectors.end(); selector ++)
350 			(*selector).first->setVisible(false);
351 	}
352 
clearAll()353 	void ScopeTextureControl::clearAll()
354 	{
355 		setTextureValue("");
356 		clearCoordValue();
357 		clearViewSelectors();
358 	}
359 
setViewSelectors(const VectorCoord & _selectors)360 	void ScopeTextureControl::setViewSelectors(const VectorCoord& _selectors)
361 	{
362 		clearViewSelectors();
363 
364 		bool changes = false;
365 		for (VectorCoord::const_iterator selectorCoord = _selectors.begin(); selectorCoord != _selectors.end(); selectorCoord ++)
366 		{
367 			SelectorControl* selector = getFreeSelector(mBlackSelectors, true, (*selectorCoord).second, changes);
368 			selector->setCoord((*selectorCoord).first);
369 		}
370 
371 		// FIXME
372 		/*if (changes)
373 		{
374 			bool visible = mCurrentSelectorControl->getVisible();
375 			MyGUI::IntCoord coord = mCoordValue;
376 
377 			InitialiseSelectors();
378 
379 			if (visible)
380 				setCoordValue(coord, ScopeTextureControl::SelectorPosition); //FIXME
381 		}*/
382 	}
383 
clearViewSelectors()384 	void ScopeTextureControl::clearViewSelectors()
385 	{
386 		for (VectorSelector::iterator selector = mBlackSelectors.begin(); selector != mBlackSelectors.end(); selector ++)
387 			(*selector).first->setVisible(false);
388 	}
389 
getFreeSelector(VectorSelector & _selectors,bool _backType,SelectorType _type,bool & _changes)390 	SelectorControl* ScopeTextureControl::getFreeSelector(VectorSelector& _selectors, bool _backType, SelectorType _type, bool& _changes)
391 	{
392 		for (VectorSelector::iterator selector = _selectors.begin(); selector != _selectors.end(); selector ++)
393 		{
394 			if (!(*selector).first->getVisible())
395 			{
396 				if ((*selector).second == _type)
397 				{
398 					(*selector).first->setVisible(true);
399 					return (*selector).first;
400 				}
401 			}
402 		}
403 
404 		_changes = true;
405 
406 		SelectorControl* control = nullptr;
407 
408 		if (_backType)
409 		{
410 			if (_type == SelectorPosition)
411 				control = new PositionSelectorBlackControl();
412 			else if (_type == SelectorOffsetH)
413 				control = new HorizontalSelectorBlackControl();
414 			else if (_type == SelectorOffsetV)
415 				control = new VerticalSelectorBlackControl();
416 		}
417 		else
418 		{
419 			if (_type == SelectorPosition)
420 				control = new PositionSelectorControl();
421 			else if (_type == SelectorPositionReadOnly)
422 				control = new PositionSelectorControl();
423 			else if (_type == SelectorCoord)
424 				control = new AreaSelectorControl();
425 			else if (_type == SelectorOffsetH)
426 				control = new HorizontalSelectorControl();
427 			else if (_type == SelectorOffsetV)
428 				control = new VerticalSelectorControl();
429 
430 			control->eventChangePosition.connect(this, &ScopeTextureControl::notifyChangePosition);
431 		}
432 
433 		MYGUI_ASSERT(control != nullptr, "Selector type not found");
434 
435 		addSelectorControl(control);
436 
437 		if (_type == SelectorPositionReadOnly)
438 			control->setEnabled(false);
439 
440 		_selectors.push_back(std::make_pair(control, _type));
441 
442 		return control;
443 	}
444 
445 }
446