TGUI  1.6.1
Loading...
Searching...
No Matches
Vector2.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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#ifndef TGUI_VECTOR2_HPP
26#define TGUI_VECTOR2_HPP
27
28#include <TGUI/Config.hpp>
29#include <TGUI/String.hpp>
30
31#if TGUI_HAS_WINDOW_BACKEND_SFML
32 #include <SFML/System/Vector2.hpp>
33#endif
34
36
37TGUI_MODULE_EXPORT namespace tgui
38{
39 template <typename T>
40 class Vector2
41 {
42 public:
43
47 constexpr Vector2() = default;
48
52 constexpr Vector2(T xValue, T yValue) :
53 x{xValue},
54 y{yValue}
55 {
56 }
57
63 template <typename U>
64 explicit constexpr Vector2(const Vector2<U>& vec) :
65 x{static_cast<T>(vec.x)},
66 y{static_cast<T>(vec.y)}
67 {
68 }
69
70#if TGUI_HAS_WINDOW_BACKEND_SFML
74 constexpr Vector2(const sf::Vector2<T>& vec) :
75 x{vec.x},
76 y{vec.y}
77 {
78 }
79#endif
80
84 Vector2(const char* str) :
85 Vector2{String(str)}
86 {
87 }
88
93 {
94 if (str.empty())
95 {
96 TGUI_PRINT_WARNING("Failed to parse Vector2. String was empty.");
97 return;
98 }
99
100 // Remove the brackets around the value
101 if (((str.front() == '(') && (str.back() == ')')) || ((str.front() == '{') && (str.back() == '}')))
102 str = str.substr(1, str.length() - 2);
103
104 if (str.empty())
105 {
106 x = 0;
107 y = 0;
108 return;
109 }
110
111 auto commaPos = str.find(',');
112 if (commaPos == String::npos)
113 {
114 TGUI_PRINT_WARNING("Failed to parse Vector2 '" + str + "'. Expected numbers separated with a comma.");
115 return;
116 }
117
118 x = static_cast<T>(str.substr(0, commaPos).trim().toFloat());
119 y = static_cast<T>(str.substr(commaPos + 1).trim().toFloat());
120 }
121
122#if TGUI_HAS_WINDOW_BACKEND_SFML
126 TGUI_NODISCARD operator sf::Vector2<T>() const
127 {
128 return sf::Vector2<T>{x, y};
129 }
130#endif
131
133 public:
134
135 T x = 0;
136 T y = 0;
137
139 };
140
144 template <typename T>
145 constexpr Vector2<T> operator-(const Vector2<T>& right)
146 {
147 return {-right.x, -right.y};
148 }
149
153 template <typename T>
154 constexpr Vector2<T>& operator+=(Vector2<T>& left, const Vector2<T>& right)
155 {
156 left.x += right.x;
157 left.y += right.y;
158 return left;
159 }
160
164 template <typename T>
165 constexpr Vector2<T>& operator-=(Vector2<T>& left, const Vector2<T>& right)
166 {
167 left.x -= right.x;
168 left.y -= right.y;
169 return left;
170 }
171
175 template <typename T>
176 constexpr Vector2<T> operator+(const Vector2<T>& left, const Vector2<T>& right)
177 {
178 return {left.x + right.x, left.y + right.y};
179 }
180
184 template <typename T>
185 constexpr Vector2<T> operator-(const Vector2<T>& left, const Vector2<T>& right)
186 {
187 return {left.x - right.x, left.y - right.y};
188 }
189
193 template <typename T>
194 constexpr Vector2<T> operator*(const Vector2<T>& left, float right)
195 {
196 return {static_cast<T>(static_cast<float>(left.x) * right), static_cast<T>(static_cast<float>(left.y) * right)};
197 }
198
202 template <typename T>
203 constexpr Vector2<T> operator*(float left, const Vector2<T>& right)
204 {
205 return {static_cast<T>(left * static_cast<float>(right.x)), static_cast<T>(left * static_cast<float>(right.y))};
206 }
207
211 template <typename T>
212 constexpr Vector2<T>& operator*=(Vector2<T>& left, float right)
213 {
214 return left = left * right;
215 }
216
220 template <typename T>
221 constexpr Vector2<T> operator/(const Vector2<T>& left, float right)
222 {
223 return {static_cast<T>(static_cast<float>(left.x) / right), static_cast<T>(static_cast<float>(left.y) / right)};
224 }
225
229 template <typename T>
230 constexpr Vector2<T>& operator/=(Vector2<T>& left, float right)
231 {
232 return left = left / right;
233 }
234
238 template <typename T>
239 constexpr bool operator==(const Vector2<T>& left, const Vector2<T>& right)
240 {
241 return (left.x == right.x) && (left.y == right.y);
242 }
243
247 template <typename T>
248 constexpr bool operator!=(const Vector2<T>& left, const Vector2<T>& right)
249 {
250 return !(left == right);
251 }
252
254
255 using Vector2f = Vector2<float>;
256 using Vector2u = Vector2<unsigned int>;
257 using Vector2i = Vector2<int>;
258}
259
261
262#endif // TGUI_VECTOR2_HPP
Wrapper class to store strings.
Definition String.hpp:96
Definition Vector2.hpp:41
constexpr Vector2(T xValue, T yValue)
Constructor to create from X and Y values.
Definition Vector2.hpp:52
constexpr Vector2()=default
Default constructor.
constexpr Vector2(const sf::Vector2< T > &vec)
Copy constructor to create from an sf::Vector2.
Definition Vector2.hpp:74
T y
Y coordinate of the vector.
Definition Vector2.hpp:136
Vector2(String str)
Constructor to create from a string.
Definition Vector2.hpp:92
constexpr Vector2(const Vector2< U > &vec)
Constructs the vector from an another Vector2 with a different type.
Definition Vector2.hpp:64
Vector2(const char *str)
Constructor to create from a string.
Definition Vector2.hpp:84
T x
X coordinate of the vector.
Definition Vector2.hpp:135
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38