MyGUI 3.4.3
MyGUI_Align.h
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#ifndef MYGUI_ALIGN_H_
8#define MYGUI_ALIGN_H_
9
10#include "MyGUI_Prerequest.h"
11#include "MyGUI_Macros.h"
12#include "MyGUI_Diagnostic.h"
13#include "MyGUI_StringUtility.h"
14#include <map>
15
16namespace MyGUI
17{
18
20 {
21 enum Enum
22 {
23 HCenter = MYGUI_FLAG_NONE,
24 VCenter = MYGUI_FLAG_NONE,
25 Center = HCenter | VCenter,
27 Left = MYGUI_FLAG(1),
28 Right = MYGUI_FLAG(2),
29 HStretch = Left | Right,
31 Top = MYGUI_FLAG(3),
32 Bottom = MYGUI_FLAG(4),
33 VStretch = Top | Bottom,
35 Stretch = HStretch | VStretch,
36 Default = Left | Top
37 };
38
39 Align(Enum _value = Default) :
40 mValue(_value)
41 {
42 }
43
44 bool isHCenter() const
45 {
46 return HCenter == (mValue & HStretch);
47 }
48
49 bool isVCenter() const
50 {
51 return VCenter == (mValue & VStretch);
52 }
53
54 bool isCenter() const
55 {
56 return Center == (mValue & Stretch);
57 }
58
59 bool isLeft() const
60 {
61 return Left == (mValue & HStretch);
62 }
63
64 bool isRight() const
65 {
66 return Right == (mValue & HStretch);
67 }
68
69 bool isHStretch() const
70 {
71 return HStretch == (mValue & HStretch);
72 }
73
74 bool isTop() const
75 {
76 return Top == (mValue & VStretch);
77 }
78
79 bool isBottom() const
80 {
81 return Bottom == (mValue & VStretch);
82 }
83
84 bool isVStretch() const
85 {
86 return VStretch == (mValue & VStretch);
87 }
88
89 bool isStretch() const
90 {
91 return Stretch == (mValue & Stretch);
92 }
93
94 bool isDefault() const
95 {
96 return Default == (mValue & Stretch);
97 }
98
99 Align& operator|=(Align const& _other)
100 {
101 mValue = (mValue | _other.mValue).mValue;
102 return *this;
103 }
104
105 friend Align operator|(Enum const& a, Enum const& b)
106 {
107 return {Enum((unsigned int)a | (unsigned int)b)};
108 }
109
110 friend Align operator|(Align const& a, Align const& b)
111 {
112 return a.mValue | b.mValue;
113 }
114
115 friend bool operator==(Align const& a, Align const& b)
116 {
117 return a.mValue == b.mValue;
118 }
119
120 friend bool operator!=(Align const& a, Align const& b)
121 {
122 return a.mValue != b.mValue;
123 }
124
125 using MapAlign = std::map<std::string, Align>;
126
127 static Align parse(std::string_view _value)
128 {
129 Align result(Enum(0));
130 const MapAlign& map_names = Align::getValueNames();
131 std::vector<std::string> vec = utility::split(_value);
132 for (const auto& pos : vec)
133 {
134 auto iter = map_names.find(pos);
135 if (iter != map_names.end())
136 {
137 result |= iter->second;
138 }
139 }
140 return result;
141 }
142
143 std::string print() const
144 {
145 std::string result;
146
147 if (mValue & Left)
148 {
149 if (mValue & Right)
150 result = "HStretch";
151 else
152 result = "Left";
153 }
154 else if (mValue & Right)
155 result = "Right";
156 else
157 result = "HCenter";
158
159 if (mValue & Top)
160 {
161 if (mValue & Bottom)
162 result += " VStretch";
163 else
164 result += " Top";
165 }
166 else if (mValue & Bottom)
167 result += " Bottom";
168 else
169 result += " VCenter";
170
171 return result;
172 }
173
174 friend std::ostream& operator<<(std::ostream& _stream, const Align& _value)
175 {
176 _stream << _value.print();
177 return _stream;
178 }
179
180 friend std::istream& operator>>(std::istream& _stream, Align& _value)
181 {
182 _value.mValue = Enum(0);
183 std::string value;
184 _stream >> value;
185
186 const MapAlign& map_names = Align::getValueNames();
187 auto iter = map_names.find(value);
188 if (iter != map_names.end())
189 _value |= iter->second;
190
191 if (!_stream.eof())
192 {
193 std::string value2;
194 _stream >> value2;
195 iter = map_names.find(value2);
196 if (iter != map_names.end())
197 _value |= iter->second;
198 }
199
200 return _stream;
201 }
202
203 int getValue() const
204 {
205 return mValue;
206 }
207
208 private:
209 static const MapAlign& getValueNames()
210 {
211 static MapAlign map_names;
212
213 if (map_names.empty())
214 {
215#ifndef MYGUI_DONT_USE_OBSOLETE
216 // OBSOLETE
217 map_names["ALIGN_HCENTER"] = HCenter;
218 map_names["ALIGN_VCENTER"] = VCenter;
219 map_names["ALIGN_CENTER"] = Center;
220 map_names["ALIGN_LEFT"] = Left;
221 map_names["ALIGN_RIGHT"] = Right;
222 map_names["ALIGN_HSTRETCH"] = HStretch;
223 map_names["ALIGN_TOP"] = Top;
224 map_names["ALIGN_BOTTOM"] = Bottom;
225 map_names["ALIGN_VSTRETCH"] = VStretch;
226 map_names["ALIGN_STRETCH"] = Stretch;
227 map_names["ALIGN_DEFAULT"] = Default;
228#endif
229
230 MYGUI_REGISTER_VALUE(map_names, HCenter);
231 MYGUI_REGISTER_VALUE(map_names, VCenter);
232 MYGUI_REGISTER_VALUE(map_names, Center);
233 MYGUI_REGISTER_VALUE(map_names, Left);
234 MYGUI_REGISTER_VALUE(map_names, Right);
235 MYGUI_REGISTER_VALUE(map_names, HStretch);
236 MYGUI_REGISTER_VALUE(map_names, Top);
237 MYGUI_REGISTER_VALUE(map_names, Bottom);
238 MYGUI_REGISTER_VALUE(map_names, VStretch);
239 MYGUI_REGISTER_VALUE(map_names, Stretch);
240 MYGUI_REGISTER_VALUE(map_names, Default);
241 }
242
243 return map_names;
244 }
245
246 private:
247 Enum mValue;
248 };
249
250} // namespace MyGUI
251
252#endif // MYGUI_ALIGN_H_
#define MYGUI_REGISTER_VALUE(map, value)
#define MYGUI_EXPORT
constexpr unsigned int MYGUI_FLAG(uint8_t num)
constexpr unsigned int MYGUI_FLAG_NONE
static Align parse(std::string_view _value)
bool isHStretch() const
Definition MyGUI_Align.h:69
Align(Enum _value=Default)
Definition MyGUI_Align.h:39
bool isVCenter() const
Definition MyGUI_Align.h:49
int getValue() const
friend std::ostream & operator<<(std::ostream &_stream, const Align &_value)
bool isTop() const
Definition MyGUI_Align.h:74
std::string print() const
friend std::istream & operator>>(std::istream &_stream, Align &_value)
std::map< std::string, Align > MapAlign
friend Align operator|(Enum const &a, Enum const &b)
bool isStretch() const
Definition MyGUI_Align.h:89
bool isVStretch() const
Definition MyGUI_Align.h:84
bool isRight() const
Definition MyGUI_Align.h:64
bool isCenter() const
Definition MyGUI_Align.h:54
bool isHCenter() const
Definition MyGUI_Align.h:44
bool isLeft() const
Definition MyGUI_Align.h:59
bool isBottom() const
Definition MyGUI_Align.h:79
friend bool operator!=(Align const &a, Align const &b)
bool isDefault() const
Definition MyGUI_Align.h:94
friend bool operator==(Align const &a, Align const &b)
friend Align operator|(Align const &a, Align const &b)
Align & operator|=(Align const &_other)
Definition MyGUI_Align.h:99