1 /*!
2 	@file
3 	@author		Albert Semenov
4 	@date		12/2010
5 */
6 
7 #include "Precompiled.h"
8 #include "PropertyFieldPosition.h"
9 #include "Localise.h"
10 #include "UndoManager.h"
11 #include "Parse.h"
12 #include "CommandManager.h"
13 
14 namespace tools
15 {
16 
17 	const std::string DEFAULT_STRING = "[DEFAULT]";
18 
PropertyFieldPosition(MyGUI::Widget * _parent)19 	PropertyFieldPosition::PropertyFieldPosition(MyGUI::Widget* _parent) :
20 		BaseLayout("PropertyFieldPosition.layout", _parent),
21 		mText(nullptr),
22 		mField(nullptr),
23 		mButton(nullptr),
24 		mCurrentWidget(nullptr)
25 	{
26 		assignWidget(mText, "Text");
27 		assignWidget(mField, "Field");
28 		assignWidget(mButton, "Button");
29 
30 		mField->eventEditTextChange += newDelegate (this, &PropertyFieldPosition::notifyTryApplyProperties);
31 		mField->eventEditSelectAccept += newDelegate (this, &PropertyFieldPosition::notifyForceApplyProperties);
32 
33 		mButton->eventMouseButtonClick += newDelegate (this, &PropertyFieldPosition::notifyMouseButtonClick);
34 
35 		CommandManager::getInstance().getEvent("Command_ToggleRelativeMode")->connect(this, &PropertyFieldPosition::commandToggleRelativeMode);
36 		EditorWidgets::getInstance().eventChangeWidgetCoord += MyGUI::newDelegate(this, &PropertyFieldPosition::notifyPropertyChangeCoord);
37 	}
38 
~PropertyFieldPosition()39 	PropertyFieldPosition::~PropertyFieldPosition()
40 	{
41 		EditorWidgets::getInstance().eventChangeWidgetCoord -= MyGUI::newDelegate(this, &PropertyFieldPosition::notifyPropertyChangeCoord);
42 		CommandManager::getInstance().getEvent("Command_ToggleRelativeMode")->disconnect(this);
43 	}
44 
initialise(const std::string & _type)45 	void PropertyFieldPosition::initialise(const std::string& _type)
46 	{
47 		mType = _type;
48 	}
49 
setTarget(MyGUI::Widget * _currentWidget)50 	void PropertyFieldPosition::setTarget(MyGUI::Widget* _currentWidget)
51 	{
52 		mCurrentWidget = _currentWidget;
53 
54 		updateButton();
55 	}
56 
notifyApplyProperties(MyGUI::Widget * _sender,bool _force)57 	void PropertyFieldPosition::notifyApplyProperties(MyGUI::Widget* _sender, bool _force)
58 	{
59 		std::string DEFAULT_VALUE = replaceTags("ColourDefault") + DEFAULT_STRING;
60 
61 		std::string value = mField->getOnlyText();
62 		if (value == DEFAULT_STRING && mField->getCaption() == DEFAULT_VALUE)
63 			value = "";
64 
65 		onAction(value, _force);
66 
67 		UndoManager::getInstance().addValue(PR_PROPERTIES);
68 	}
69 
onAction(const std::string & _value,bool _force)70 	void PropertyFieldPosition::onAction(const std::string& _value, bool _force)
71 	{
72 		EditorWidgets* ew = &EditorWidgets::getInstance();
73 		WidgetContainer* widgetContainer = ew->find(mCurrentWidget);
74 
75 		bool goodData = onCheckValue();
76 
77 		if (goodData)
78 		{
79 			if (widgetContainer->getRelativeMode())
80 			{
81 				std::istringstream str(_value);
82 				MyGUI::DoubleCoord double_coord;
83 				str >> double_coord;
84 				double_coord.left /= 100;
85 				double_coord.top /= 100;
86 				double_coord.width /= 100;
87 				double_coord.height /= 100;
88 				MyGUI::IntCoord coord = MyGUI::CoordConverter::convertFromRelative(double_coord, mCurrentWidget->getParentSize());
89 
90 				mCurrentWidget->setCoord(coord);
91 				EditorWidgets::getInstance().onSetWidgetCoord(mCurrentWidget, mCurrentWidget->getAbsoluteCoord(), "PropertiesPanelView");
92 			}
93 			else
94 			{
95 				widgetContainer->getWidget()->setProperty("Coord", _value);
96 				EditorWidgets::getInstance().onSetWidgetCoord(mCurrentWidget, mCurrentWidget->getAbsoluteCoord(), "PropertiesPanelView");
97 			}
98 		}
99 	}
100 
notifyTryApplyProperties(MyGUI::EditBox * _sender)101 	void PropertyFieldPosition::notifyTryApplyProperties(MyGUI::EditBox* _sender)
102 	{
103 		notifyApplyProperties(_sender, false);
104 	}
105 
notifyForceApplyProperties(MyGUI::EditBox * _sender)106 	void PropertyFieldPosition::notifyForceApplyProperties(MyGUI::EditBox* _sender)
107 	{
108 		notifyApplyProperties(_sender, true);
109 	}
110 
onCheckValue()111 	bool PropertyFieldPosition::onCheckValue()
112 	{
113 		bool success = true;
114 
115 		if (EditorWidgets::getInstance().find(mCurrentWidget)->getRelativeMode())
116 			success = utility::checkParse<float>(mField, 4);
117 		else
118 			success = utility::checkParse<int>(mField, 4);
119 
120 		return success;
121 	}
122 
getContentSize()123 	MyGUI::IntSize PropertyFieldPosition::getContentSize()
124 	{
125 		return MyGUI::IntSize(0, mMainWidget->getHeight());
126 	}
127 
setCoord(const MyGUI::IntCoord & _coord)128 	void PropertyFieldPosition::setCoord(const MyGUI::IntCoord& _coord)
129 	{
130 		mMainWidget->setCoord(_coord);
131 	}
132 
setValue(const std::string & _value)133 	void PropertyFieldPosition::setValue(const std::string& _value)
134 	{
135 		std::string DEFAULT_VALUE = replaceTags("ColourDefault") + DEFAULT_STRING;
136 
137 		if (_value.empty())
138 		{
139 			mField->setCaption(DEFAULT_VALUE);
140 		}
141 		else
142 		{
143 			mField->setOnlyText(_value);
144 			onCheckValue();
145 		}
146 	}
147 
setName(const std::string & _value)148 	void PropertyFieldPosition::setName(const std::string& _value)
149 	{
150 		mName = _value;
151 		mText->setCaption(_value);
152 	}
153 
notifyMouseButtonClick(MyGUI::Widget * _sender)154 	void PropertyFieldPosition::notifyMouseButtonClick(MyGUI::Widget* _sender)
155 	{
156 		notifyToggleRelativeMode();
157 	}
158 
commandToggleRelativeMode(const MyGUI::UString & _commandName,bool & _result)159 	void PropertyFieldPosition::commandToggleRelativeMode(const MyGUI::UString& _commandName, bool& _result)
160 	{
161 		notifyToggleRelativeMode();
162 
163 		_result = true;
164 	}
165 
notifyToggleRelativeMode()166 	void PropertyFieldPosition::notifyToggleRelativeMode()
167 	{
168 		WidgetContainer* widgetContainer = EditorWidgets::getInstance().find(mCurrentWidget);
169 		// если нет контейнера, занчит мы не в режиме редактирования
170 		if (widgetContainer == nullptr)
171 			return;
172 
173 		widgetContainer->setRelativeMode(!widgetContainer->getRelativeMode());
174 
175 		updateButton();
176 		updatePositionCaption();
177 
178 		UndoManager::getInstance().addValue(PR_PROPERTIES);
179 	}
180 
updateButton()181 	void PropertyFieldPosition::updateButton()
182 	{
183 		WidgetContainer* widgetContainer = EditorWidgets::getInstance().find(mCurrentWidget);
184 		if (widgetContainer->getRelativeMode())
185 			mButton->setCaption(replaceTags("to_percents"));
186 		else
187 			mButton->setCaption(replaceTags("to_pixels"));
188 	}
189 
updatePositionCaption()190 	void PropertyFieldPosition::updatePositionCaption()
191 	{
192 		WidgetContainer* widgetContainer = EditorWidgets::getInstance().find(mCurrentWidget);
193 		setValue(widgetContainer->position());
194 	}
195 
notifyPropertyChangeCoord(MyGUI::Widget * _widget,const MyGUI::IntCoord & _coordValue,const std::string & _owner)196 	void PropertyFieldPosition::notifyPropertyChangeCoord(MyGUI::Widget* _widget, const MyGUI::IntCoord& _coordValue, const std::string& _owner)
197 	{
198 		if (_owner == "PropertiesPanelView" || _widget != mCurrentWidget)
199 			return;
200 
201 		updatePositionCaption();
202 	}
203 
setVisible(bool _value)204 	void PropertyFieldPosition::setVisible(bool _value)
205 	{
206 		mMainWidget->setVisible(_value);
207 	}
208 
getVisible()209 	bool PropertyFieldPosition::getVisible()
210 	{
211 		return mMainWidget->getVisible();
212 	}
213 
214 }
215