1 /*!
2 	@file
3 	@author		Albert Semenov
4 	@date		08/2010
5 */
6 
7 #include "Precompiled.h"
8 #include "WidgetCreatorManager.h"
9 #include "WidgetSelectorManager.h"
10 #include "EditorWidgets.h"
11 #include "WidgetTypes.h"
12 #include "UndoManager.h"
13 #include "GridManager.h"
14 
15 template <> tools::WidgetCreatorManager* MyGUI::Singleton<tools::WidgetCreatorManager>::msInstance = nullptr;
16 template <> const char* MyGUI::Singleton<tools::WidgetCreatorManager>::mClassTypeName = "WidgetCreatorManager";
17 
18 namespace tools
19 {
20 
WidgetCreatorManager()21 	WidgetCreatorManager::WidgetCreatorManager() :
22 		mCreateMode(false),
23 		mStartNewWidget(false),
24 		mNewWidget(nullptr),
25 		mPopupMode(false)
26 	{
27 	}
28 
initialise()29 	void WidgetCreatorManager::initialise()
30 	{
31 	}
32 
shutdown()33 	void WidgetCreatorManager::shutdown()
34 	{
35 		resetWidget();
36 	}
37 
setCreatorInfo(const std::string & _widgetType,const std::string & _widgetSkin)38 	void WidgetCreatorManager::setCreatorInfo(const std::string& _widgetType, const std::string& _widgetSkin)
39 	{
40 		mWidgetType = _widgetType;
41 		mWidgetSkin = _widgetSkin;
42 		mCreateMode = true;
43 
44 		eventChangeCreatorMode(mCreateMode);
45 	}
46 
resetCreatorInfo()47 	void WidgetCreatorManager::resetCreatorInfo()
48 	{
49 		mWidgetType = "";
50 		mWidgetSkin = "";
51 		mCreateMode = false;
52 
53 		eventChangeCreatorMode(mCreateMode);
54 	}
55 
resetAllCreatorInfo()56 	void WidgetCreatorManager::resetAllCreatorInfo()
57 	{
58 		mWidgetType = "";
59 		mWidgetSkin = "";
60 		mCreateMode = false;
61 		mPopupMode = false;
62 
63 		eventChangeCreatorMode(mCreateMode);
64 	}
65 
getCreateMode() const66 	bool WidgetCreatorManager::getCreateMode() const
67 	{
68 		return mCreateMode;
69 	}
70 
getWidgetType() const71 	const std::string& WidgetCreatorManager::getWidgetType() const
72 	{
73 		return mWidgetType;
74 	}
75 
getWidgetSkin() const76 	const std::string& WidgetCreatorManager::getWidgetSkin() const
77 	{
78 		return mWidgetSkin;
79 	}
80 
createNewWidget(const MyGUI::IntPoint & _point)81 	void WidgetCreatorManager::createNewWidget(const MyGUI::IntPoint& _point)
82 	{
83 		mStartNewWidget = true;
84 		mStartPoint = _point;
85 
86 		resetWidget();
87 
88 		eventChangeSelector(false, MyGUI::IntCoord());
89 	}
90 
moveNewWidget(const MyGUI::IntPoint & _point)91 	void WidgetCreatorManager::moveNewWidget(const MyGUI::IntPoint& _point)
92 	{
93 		if (mNewWidget == nullptr)
94 		{
95 			// тип виджета может отсутсвовать
96 			if (!MyGUI::WidgetManager::getInstance().isFactoryExist(mWidgetType))
97 				return;
98 
99 			// выделяем верний виджет
100 			if (!mPopupMode)
101 				WidgetSelectorManager::getInstance().selectWidget(mStartPoint);
102 
103 			MyGUI::Widget* parent = WidgetSelectorManager::getInstance().getSelectedWidget();
104 
105 			// пока не найдем ближайшего над нами способного быть родителем
106 			while (parent != nullptr && !WidgetTypes::getInstance().findWidgetStyle(parent->getTypeName())->parent)
107 				parent = parent->getParent();
108 
109 			if (!WidgetTypes::getInstance().findWidgetStyle(mWidgetType)->child)
110 				parent = nullptr;
111 
112 			if (parent != nullptr)
113 				mNewWidget = parent->createWidgetT(
114 					mPopupMode ? MyGUI::WidgetStyle::Popup : MyGUI::WidgetStyle::Child,
115 					mWidgetType,
116 					EditorWidgets::getInstance().getSkinReplace(mWidgetSkin),
117 					MyGUI::IntCoord(),
118 					MyGUI::Align::Default,
119 					DEFAULT_EDITOR_LAYER);
120 			else
121 				mNewWidget = MyGUI::Gui::getInstance().createWidgetT(
122 					mWidgetType,
123 					EditorWidgets::getInstance().getSkinReplace(mWidgetSkin),
124 					MyGUI::IntCoord(),
125 					MyGUI::Align::Default,
126 					DEFAULT_EDITOR_LAYER);
127 
128 			// переводим старт поинт в координаты отца
129 			if (parent != nullptr && !mNewWidget->isRootWidget())
130 			{
131 				if (parent->getClientWidget())
132 					mStartPoint -= parent->getClientWidget()->getAbsolutePosition();
133 				else
134 					mStartPoint -= parent->getAbsolutePosition();
135 			}
136 
137 			if (!MyGUI::InputManager::getInstance().isShiftPressed())
138 			{
139 				mStartPoint.left = GridManager::getInstance().toGrid(mStartPoint.left);
140 				mStartPoint.top = GridManager::getInstance().toGrid(mStartPoint.top);
141 			}
142 		}
143 
144 		MyGUI::IntCoord coord = getCoordNewWidget(_point);
145 		mNewWidget->setCoord(coord);
146 
147 		eventChangeSelector(true, mNewWidget->getAbsoluteCoord());
148 	}
149 
finishNewWidget(const MyGUI::IntPoint & _point)150 	void WidgetCreatorManager::finishNewWidget(const MyGUI::IntPoint& _point)
151 	{
152 		if (mNewWidget != nullptr)
153 		{
154 			MyGUI::IntCoord coord = getCoordNewWidget(_point);
155 
156 			if (coord.width != 0 && coord.height != 0)
157 			{
158 				mNewWidget->setCoord(coord);
159 
160 				// создали виджет, все счастливы
161 				WidgetContainer * widgetContainer = new WidgetContainer(mWidgetType, mWidgetSkin, mNewWidget);
162 				if (mPopupMode)
163 					widgetContainer->setStyle(mNewWidget->getWidgetStyle().print());
164 				mNewWidget = nullptr;
165 
166 				EditorWidgets::getInstance().add(widgetContainer);
167 				UndoManager::getInstance().addValue();
168 
169 				// чтобы выделился созданый виджет
170 				resetAllCreatorInfo();
171 
172 				WidgetSelectorManager::getInstance().setSelectedWidget(widgetContainer->getWidget());
173 			}
174 			else
175 			{
176 				// не удалось создать, т.к. размер нулевой
177 				resetWidget();
178 			}
179 		}
180 
181 		resetAllCreatorInfo();
182 
183 		eventChangeSelector(false, MyGUI::IntCoord());
184 	}
185 
resetWidget()186 	void WidgetCreatorManager::resetWidget()
187 	{
188 		// подстрахуемся
189 		if (mNewWidget != nullptr)
190 		{
191 			MyGUI::WidgetManager::getInstance().destroyWidget(mNewWidget);
192 			mNewWidget = nullptr;
193 		}
194 	}
195 
getCoordNewWidget(const MyGUI::IntPoint & _point)196 	MyGUI::IntCoord WidgetCreatorManager::getCoordNewWidget(const MyGUI::IntPoint& _point)
197 	{
198 		MyGUI::IntPoint point = _point;
199 
200 		MyGUI::Widget* parent = mNewWidget->getParent();
201 		if (parent != nullptr && !mNewWidget->isRootWidget())
202 			point -= parent->getAbsolutePosition();
203 
204 		if (!MyGUI::InputManager::getInstance().isShiftPressed())
205 		{
206 			point.left = GridManager::getInstance().toGrid(point.left);
207 			point.top = GridManager::getInstance().toGrid(point.top);
208 		}
209 
210 		MyGUI::IntCoord coord = MyGUI::IntCoord(
211 			(std::min)(mStartPoint.left, point.left),
212 			(std::min)(mStartPoint.top, point.top),
213 			abs(point.left - mStartPoint.left),
214 			abs(point.top - mStartPoint.top));
215 
216 		return coord;
217 	}
218 
setPopupMode(bool _value)219 	void WidgetCreatorManager::setPopupMode(bool _value)
220 	{
221 		mPopupMode = _value;
222 		mCreateMode = true;
223 
224 		eventChangeCreatorMode(mCreateMode);
225 	}
226 
getPopupMode() const227 	bool WidgetCreatorManager::getPopupMode() const
228 	{
229 		return mPopupMode;
230 	}
231 
232 }
233