1 /*!
2 	@file
3 	@author		Albert Semenov
4 	@date		07/2012
5 */
6 
7 #include "Precompiled.h"
8 #include "IndexTextureController.h"
9 #include "FactoryManager.h"
10 #include "DataSelectorManager.h"
11 #include "DataManager.h"
12 #include "PropertyUtility.h"
13 #include "ScopeManager.h"
14 #include "DataUtility.h"
15 
16 namespace tools
17 {
18 
FACTORY_ITEM_ATTRIBUTE(IndexTextureController)19 	FACTORY_ITEM_ATTRIBUTE(IndexTextureController)
20 
21 	IndexTextureController::IndexTextureController() :
22 		mControl(nullptr),
23 		mParentData(nullptr),
24 		mActivated(false)
25 	{
26 	}
27 
setTarget(Control * _control)28 	void IndexTextureController::setTarget(Control* _control)
29 	{
30 		mControl = _control->findControl<ScopeTextureControl>();
31 	}
32 
activate()33 	void IndexTextureController::activate()
34 	{
35 		mParentTypeName = "Index";
36 		mScopeName = "Index";
37 
38 		ScopeManager::getInstance().eventChangeScope.connect(this, &IndexTextureController::notifyChangeScope);
39 		notifyChangeScope(ScopeManager::getInstance().getCurrentScope());
40 	}
41 
deactivate()42 	void IndexTextureController::deactivate()
43 	{
44 		ScopeManager::getInstance().eventChangeScope.disconnect(this);
45 	}
46 
notifyChangeDataSelector(DataPtr _data,bool _changeOnlySelection)47 	void IndexTextureController::notifyChangeDataSelector(DataPtr _data, bool _changeOnlySelection)
48 	{
49 		mParentData = _data;
50 		if (mParentData != nullptr && mParentData->getType()->getName() != mParentTypeName)
51 			mParentData = nullptr;
52 
53 		std::string texture;
54 		PropertyPtr property = PropertyUtility::getPropertyByName("Group", "Texture");
55 		if (property != nullptr)
56 		{
57 			texture = property->getValue();
58 
59 			if (!property->eventChangeProperty.exist(this, &IndexTextureController::notifyChangeProperty))
60 				property->eventChangeProperty.connect(this, &IndexTextureController::notifyChangeProperty);
61 		}
62 
63 		std::string coord;
64 		property = PropertyUtility::getPropertyByName("Group", "Size");
65 		if (property != nullptr)
66 		{
67 			coord = property->getValue();
68 
69 			if (!property->eventChangeProperty.exist(this, &IndexTextureController::notifyChangeProperty))
70 				property->eventChangeProperty.connect(this, &IndexTextureController::notifyChangeProperty);
71 		}
72 
73 		if (mParentData != nullptr)
74 		{
75 			for (Data::VectorData::const_iterator child = mParentData->getChilds().begin(); child != mParentData->getChilds().end(); child ++)
76 			{
77 				property = (*child)->getProperty("Point");
78 				if (!property->eventChangeProperty.exist(this, &IndexTextureController::notifyChangeProperty))
79 					property->eventChangeProperty.connect(this, &IndexTextureController::notifyChangeProperty);
80 			}
81 		}
82 
83 		mControl->setTextureValue(texture);
84 		updateCoords(coord);
85 	}
86 
notifyChangeProperty(PropertyPtr _sender)87 	void IndexTextureController::notifyChangeProperty(PropertyPtr _sender)
88 	{
89 		if (!mActivated || !PropertyUtility::isDataSelected(_sender->getOwner()))
90 			return;
91 
92 		if (_sender->getOwner()->getType()->getName() == "Group")
93 		{
94 			if (_sender->getType()->getName() == "Texture")
95 				mControl->setTextureValue(_sender->getValue());
96 			else if (_sender->getType()->getName() == "Size")
97 				updateCoords(_sender->getValue());
98 		}
99 		else if (_sender->getOwner()->getType()->getName() == "Frame")
100 		{
101 			if (_sender->getType()->getName() == "Point")
102 				updateFrames();
103 		}
104 	}
105 
notifyChangeScope(const std::string & _scope)106 	void IndexTextureController::notifyChangeScope(const std::string& _scope)
107 	{
108 		if (mControl == nullptr)
109 			return;
110 
111 		if (_scope == mScopeName)
112 		{
113 			if (!mActivated)
114 			{
115 				mControl->clearAll();
116 
117 				DataSelectorManager::getInstance().getEvent(mParentTypeName)->connect(this, &IndexTextureController::notifyChangeDataSelector);
118 				mParentData = DataUtility::getSelectedDataByType(mParentTypeName);
119 				notifyChangeDataSelector(mParentData, false);
120 
121 				mControl->getRoot()->setUserString("CurrentScopeController", mScopeName);
122 
123 				mActivated = true;
124 			}
125 		}
126 		else
127 		{
128 			if (mActivated)
129 			{
130 				DataSelectorManager::getInstance().getEvent(mParentTypeName)->disconnect(this);
131 				mParentData = nullptr;
132 
133 				// мы еще владельцы контрола сбрасываем его
134 				std::string value = mControl->getRoot()->getUserString("CurrentScopeController");
135 				if (value == mScopeName)
136 				{
137 					mControl->getRoot()->setUserString("CurrentScopeController", "");
138 					notifyChangeDataSelector(mParentData, false);
139 
140 					mControl->clearAll();
141 				}
142 
143 				mActivated = false;
144 			}
145 		}
146 	}
147 
updateCoords(const std::string & _value)148 	void IndexTextureController::updateCoords(const std::string& _value)
149 	{
150 		MyGUI::IntCoord coord;
151 		if (MyGUI::utility::parseComplex(_value, coord.left, coord.top, coord.width, coord.height))
152 			mSize = coord.size();
153 		else
154 			mSize.clear();
155 
156 		updateFrames();
157 	}
158 
updateFrames()159 	void IndexTextureController::updateFrames()
160 	{
161 		mFrames.clear();
162 
163 		if (mParentData != nullptr)
164 		{
165 			for (Data::VectorData::const_iterator child = mParentData->getChilds().begin(); child != mParentData->getChilds().end(); child ++)
166 			{
167 				MyGUI::IntPoint value = (*child)->getPropertyValue<MyGUI::IntPoint>("Point");
168 				mFrames.push_back(std::make_pair(MyGUI::IntCoord(value, mSize), ScopeTextureControl::SelectorPosition));
169 			}
170 		}
171 
172 		if (mControl != nullptr)
173 			mControl->setViewSelectors(mFrames);
174 	}
175 
176 }
177