TGUI  0.9.5
Loading...
Searching...
No Matches
Vector2.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25
26#ifndef TGUI_VECTOR2_HPP
27#define TGUI_VECTOR2_HPP
28
29#include <TGUI/Config.hpp>
30#include <TGUI/String.hpp>
31
32#if TGUI_HAS_BACKEND_SFML
33 #include <SFML/System/Vector2.hpp>
34#endif
35
37
38namespace tgui
39{
40 template <typename T>
41 class Vector2
42 {
43 public:
44
48 TGUI_CONSTEXPR Vector2()
49 {
50 // Constructor isn't defined as "= default" because this leads to an IntelliSense error
51 }
52
53
57 TGUI_CONSTEXPR Vector2(T xValue, T yValue) :
58 x{xValue},
59 y{yValue}
60 {
61 }
62
63
69 template <typename U>
70 explicit TGUI_CONSTEXPR Vector2(const Vector2<U>& vec) :
71 x{static_cast<T>(vec.x)},
72 y{static_cast<T>(vec.y)}
73 {
74 }
75
76#if TGUI_HAS_BACKEND_SFML
80 TGUI_CONSTEXPR Vector2(const sf::Vector2<T>& vec) :
81 x{vec.x},
82 y{vec.y}
83 {
84 }
85#endif
86
90 Vector2(const char* str) :
91 Vector2{String(str)}
92 {
93 }
94
99 {
100 if (str.empty())
101 {
102 TGUI_PRINT_WARNING("Failed to parse Vector2. String was empty.");
103 return;
104 }
105
106 // Remove the brackets around the value
107 if (((str.front() == '(') && (str.back() == ')')) || ((str.front() == '{') && (str.back() == '}')))
108 str = str.substr(1, str.length() - 2);
109
110 if (str.empty())
111 {
112 x = 0;
113 y = 0;
114 return;
115 }
116
117 auto commaPos = str.find(',');
118 if (commaPos == String::npos)
119 {
120 TGUI_PRINT_WARNING("Failed to parse Vector2 '" + str + "'. Expected numbers separated with a comma.");
121 return;
122 }
123
124 x = static_cast<T>(str.substr(0, commaPos).trim().toFloat());
125 y = static_cast<T>(str.substr(commaPos + 1).trim().toFloat());
126 }
127
128#if TGUI_HAS_BACKEND_SFML
132 operator sf::Vector2<T>() const
133 {
134 return sf::Vector2<T>{x, y};
135 }
136#endif
137
139 public:
140
141 T x = 0;
142 T y = 0;
143
145 };
146
147
151 template <typename T>
152 TGUI_CONSTEXPR Vector2<T> operator-(const Vector2<T>& right)
153 {
154 return {-right.x, -right.y};
155 }
156
160 template <typename T>
161 TGUI_CONSTEXPR Vector2<T>& operator+=(Vector2<T>& left, const Vector2<T>& right)
162 {
163 left.x += right.x;
164 left.y += right.y;
165 return left;
166 }
167
171 template <typename T>
172 TGUI_CONSTEXPR Vector2<T>& operator-=(Vector2<T>& left, const Vector2<T>& right)
173 {
174 left.x -= right.x;
175 left.y -= right.y;
176 return left;
177 }
178
182 template <typename T>
183 TGUI_CONSTEXPR Vector2<T> operator+(const Vector2<T>& left, const Vector2<T>& right)
184 {
185 return {left.x + right.x, left.y + right.y};
186 }
187
191 template <typename T>
192 TGUI_CONSTEXPR Vector2<T> operator-(const Vector2<T>& left, const Vector2<T>& right)
193 {
194 return {left.x - right.x, left.y - right.y};
195 }
196
200 template <typename T>
201 TGUI_CONSTEXPR Vector2<T> operator*(const Vector2<T>& left, float right)
202 {
203 return {left.x * right, left.y * right};
204 }
205
209 template <typename T>
210 TGUI_CONSTEXPR Vector2<T> operator*(float left, const Vector2<T>& right)
211 {
212 return {left * right.x, left * right.y};
213 }
214
218 template <typename T>
219 TGUI_CONSTEXPR Vector2<T>& operator*=(Vector2<T>& left, float right)
220 {
221 left.x *= right;
222 left.y *= right;
223 return left;
224 }
225
229 template <typename T>
230 TGUI_CONSTEXPR Vector2<T> operator/(const Vector2<T>& left, float right)
231 {
232 return {left.x / right, left.y / right};
233 }
234
238 template <typename T>
239 TGUI_CONSTEXPR Vector2<T>& operator/=(Vector2<T>& left, float right)
240 {
241 left.x /= right;
242 left.y /= right;
243 return left;
244 }
245
249 template <typename T>
250 TGUI_CONSTEXPR bool operator==(const Vector2<T>& left, const Vector2<T>& right)
251 {
252 return (left.x == right.x) && (left.y == right.y);
253 }
254
258 template <typename T>
259 TGUI_CONSTEXPR bool operator!=(const Vector2<T>& left, const Vector2<T>& right)
260 {
261 return !(left == right);
262 }
263
265
266 using Vector2f = Vector2<float>;
267 using Vector2u = Vector2<unsigned int>;
268 using Vector2i = Vector2<int>;
269}
270
272
273#endif // TGUI_VECTOR2_HPP
Wrapper class to store strings.
Definition String.hpp:79
float toFloat(float defaultValue=0) const
Converts the string to a float.
String trim() const
Returns a string with the whitespace at the start and end of this string removed.
Definition Vector2.hpp:42
T y
Y coordinate of the vector.
Definition Vector2.hpp:142
TGUI_CONSTEXPR Vector2(T xValue, T yValue)
Constructor to create from X and Y values.
Definition Vector2.hpp:57
Vector2(String str)
Constructor to create from a string.
Definition Vector2.hpp:98
TGUI_CONSTEXPR Vector2(const sf::Vector2< T > &vec)
Copy constructor to create from an sf::Vector2.
Definition Vector2.hpp:80
TGUI_CONSTEXPR Vector2()
Default constructor.
Definition Vector2.hpp:48
Vector2(const char *str)
Constructor to create from a string.
Definition Vector2.hpp:90
T x
X coordinate of the vector.
Definition Vector2.hpp:141
TGUI_CONSTEXPR Vector2(const Vector2< U > &vec)
Constructs the vector from an another Vector2 with a different type.
Definition Vector2.hpp:70
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36