File: PropertyFieldPosition.cpp

package info (click to toggle)
mygui 3.4.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,792 kB
  • sloc: cpp: 133,849; ansic: 30,249; xml: 15,794; cs: 12,601; tcl: 776; python: 400; makefile: 35; sh: 4
file content (225 lines) | stat: -rw-r--r-- 5,911 bytes parent folder | download
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
/*!
	@file
	@author		Albert Semenov
	@date		12/2010
*/

#include "Precompiled.h"
#include "PropertyFieldPosition.h"
#include "Localise.h"
#include "UndoManager.h"
#include "Parse.h"
#include "CommandManager.h"

namespace tools
{

	const std::string DEFAULT_STRING = "[DEFAULT]";

	PropertyFieldPosition::PropertyFieldPosition(MyGUI::Widget* _parent) :
		BaseLayout("PropertyFieldPosition.layout", _parent)
	{
		assignWidget(mText, "Text");
		assignWidget(mField, "Field");
		assignWidget(mButton, "Button");

		mField->eventEditTextChange += newDelegate(this, &PropertyFieldPosition::notifyTryApplyProperties);
		mField->eventEditSelectAccept += newDelegate(this, &PropertyFieldPosition::notifyForceApplyProperties);

		mButton->eventMouseButtonClick += newDelegate(this, &PropertyFieldPosition::notifyMouseButtonClick);

		CommandManager::getInstance()
			.getEvent("Command_ToggleRelativeMode")
			->connect(this, &PropertyFieldPosition::commandToggleRelativeMode);
		EditorWidgets::getInstance().eventChangeWidgetCoord +=
			MyGUI::newDelegate(this, &PropertyFieldPosition::notifyPropertyChangeCoord);
	}

	PropertyFieldPosition::~PropertyFieldPosition()
	{
		EditorWidgets::getInstance().eventChangeWidgetCoord -=
			MyGUI::newDelegate(this, &PropertyFieldPosition::notifyPropertyChangeCoord);
		CommandManager::getInstance().getEvent("Command_ToggleRelativeMode")->disconnect(this);
	}

	void PropertyFieldPosition::initialise(std::string_view _type)
	{
		mType = _type;
	}

	void PropertyFieldPosition::setTarget(MyGUI::Widget* _currentWidget)
	{
		mCurrentWidget = _currentWidget;

		updateButton();
	}

	void PropertyFieldPosition::notifyApplyProperties(MyGUI::Widget* _sender, bool _force)
	{
		std::string DEFAULT_VALUE = replaceTags("ColourDefault") + DEFAULT_STRING;

		std::string value = mField->getOnlyText();
		if (value == DEFAULT_STRING && mField->getCaption() == DEFAULT_VALUE)
			value.clear();

		onAction(value, _force);

		UndoManager::getInstance().addValue(PR_PROPERTIES);
	}

	void PropertyFieldPosition::onAction(std::string_view _value, bool _force)
	{
		EditorWidgets* ew = &EditorWidgets::getInstance();
		WidgetContainer* widgetContainer = ew->find(mCurrentWidget);

		bool goodData = onCheckValue();

		if (goodData)
		{
			if (widgetContainer->getRelativeMode())
			{
				std::stringstream str;
				str << _value;
				MyGUI::DoubleCoord double_coord;
				str >> double_coord;
				double_coord.left /= 100;
				double_coord.top /= 100;
				double_coord.width /= 100;
				double_coord.height /= 100;
				MyGUI::IntCoord coord =
					MyGUI::CoordConverter::convertFromRelative(double_coord, mCurrentWidget->getParentSize());

				mCurrentWidget->setCoord(coord);
				EditorWidgets::getInstance().onSetWidgetCoord(
					mCurrentWidget,
					mCurrentWidget->getAbsoluteCoord(),
					"PropertiesPanelView");
			}
			else
			{
				widgetContainer->getWidget()->setProperty("Coord", _value);
				EditorWidgets::getInstance().onSetWidgetCoord(
					mCurrentWidget,
					mCurrentWidget->getAbsoluteCoord(),
					"PropertiesPanelView");
			}
		}
	}

	void PropertyFieldPosition::notifyTryApplyProperties(MyGUI::EditBox* _sender)
	{
		notifyApplyProperties(_sender, false);
	}

	void PropertyFieldPosition::notifyForceApplyProperties(MyGUI::EditBox* _sender)
	{
		notifyApplyProperties(_sender, true);
	}

	bool PropertyFieldPosition::onCheckValue()
	{
		bool success = true;

		if (EditorWidgets::getInstance().find(mCurrentWidget)->getRelativeMode())
			success = utility::checkParse<float>(mField, 4);
		else
			success = utility::checkParse<int>(mField, 4);

		return success;
	}

	MyGUI::IntSize PropertyFieldPosition::getContentSize()
	{
		return {0, mMainWidget->getHeight()};
	}

	void PropertyFieldPosition::setCoord(const MyGUI::IntCoord& _coord)
	{
		mMainWidget->setCoord(_coord);
	}

	void PropertyFieldPosition::setValue(std::string_view _value)
	{
		if (_value.empty())
		{
			std::string DEFAULT_VALUE = replaceTags("ColourDefault") + DEFAULT_STRING;

			mField->setCaption(DEFAULT_VALUE);
		}
		else
		{
			mField->setOnlyText(MyGUI::UString(_value));
			onCheckValue();
		}
	}

	void PropertyFieldPosition::setName(std::string_view _value)
	{
		mName = _value;
		mText->setCaption(mName);
	}

	void PropertyFieldPosition::notifyMouseButtonClick(MyGUI::Widget* _sender)
	{
		notifyToggleRelativeMode();
	}

	void PropertyFieldPosition::commandToggleRelativeMode(const MyGUI::UString& _commandName, bool& _result)
	{
		notifyToggleRelativeMode();

		_result = true;
	}

	void PropertyFieldPosition::notifyToggleRelativeMode()
	{
		WidgetContainer* widgetContainer = EditorWidgets::getInstance().find(mCurrentWidget);
		// если нет контейнера, занчит мы не в режиме редактирования
		if (widgetContainer == nullptr)
			return;

		widgetContainer->setRelativeMode(!widgetContainer->getRelativeMode());

		updateButton();
		updatePositionCaption();

		UndoManager::getInstance().addValue(PR_PROPERTIES);
	}

	void PropertyFieldPosition::updateButton()
	{
		WidgetContainer* widgetContainer = EditorWidgets::getInstance().find(mCurrentWidget);
		if (widgetContainer->getRelativeMode())
			mButton->setCaption(replaceTags("to_percents"));
		else
			mButton->setCaption(replaceTags("to_pixels"));
	}

	void PropertyFieldPosition::updatePositionCaption()
	{
		WidgetContainer* widgetContainer = EditorWidgets::getInstance().find(mCurrentWidget);
		setValue(widgetContainer->position());
	}

	void PropertyFieldPosition::notifyPropertyChangeCoord(
		MyGUI::Widget* _widget,
		const MyGUI::IntCoord& _coordValue,
		std::string_view _owner)
	{
		if (_owner == "PropertiesPanelView" || _widget != mCurrentWidget)
			return;

		updatePositionCaption();
	}

	void PropertyFieldPosition::setVisible(bool _value)
	{
		mMainWidget->setVisible(_value);
	}

	bool PropertyFieldPosition::getVisible()
	{
		return mMainWidget->getVisible();
	}

}