TGUI 1.x-dev
Loading...
Searching...
No Matches
Vector2.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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
30#include <TGUI/String.hpp>
31
32#if TGUI_HAS_WINDOW_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:
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 const 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 [[nodiscard]] operator sf::Vector2<T>() const
127 {
128 return sf::Vector2<T>{x, y};
129 }
130#endif
135 [[nodiscard]] constexpr Vector2 multiplyComponents(Vector2 right) const
136 {
137 return {x * right.x, y * right.y};
138 }
139
141
142 public:
143 T x = 0;
144 T y = 0;
145
147 };
148
152 template <typename T>
153 constexpr Vector2<T> operator-(const Vector2<T>& right)
154 {
155 return {-right.x, -right.y};
156 }
157
161 template <typename T>
162 constexpr Vector2<T>& operator+=(Vector2<T>& left, const Vector2<T>& right)
163 {
164 left.x += right.x;
165 left.y += right.y;
166 return left;
167 }
168
172 template <typename T>
173 constexpr Vector2<T>& operator-=(Vector2<T>& left, const Vector2<T>& right)
174 {
175 left.x -= right.x;
176 left.y -= right.y;
177 return left;
178 }
179
183 template <typename T>
184 constexpr Vector2<T> operator+(const Vector2<T>& left, const Vector2<T>& right)
185 {
186 return {left.x + right.x, left.y + right.y};
187 }
188
192 template <typename T>
193 constexpr Vector2<T> operator-(const Vector2<T>& left, const Vector2<T>& right)
194 {
195 return {left.x - right.x, left.y - right.y};
196 }
197
201 template <typename T>
202 constexpr Vector2<T> operator*(const Vector2<T>& left, float right)
203 {
204 return {static_cast<T>(static_cast<float>(left.x) * right), static_cast<T>(static_cast<float>(left.y) * right)};
205 }
206
210 template <typename T>
211 constexpr Vector2<T> operator*(float left, const Vector2<T>& right)
212 {
213 return {static_cast<T>(left * static_cast<float>(right.x)), static_cast<T>(left * static_cast<float>(right.y))};
214 }
215
219 template <typename T>
220 constexpr Vector2<T>& operator*=(Vector2<T>& left, float right)
221 {
222 return left = left * right;
223 }
224
228 template <typename T>
229 constexpr Vector2<T> operator/(const Vector2<T>& left, float right)
230 {
231 return {static_cast<T>(static_cast<float>(left.x) / right), static_cast<T>(static_cast<float>(left.y) / right)};
232 }
233
237 template <typename T>
238 constexpr Vector2<T>& operator/=(Vector2<T>& left, float right)
239 {
240 return left = left / right;
241 }
242
246 template <typename T>
247 constexpr bool operator==(const Vector2<T>& left, const Vector2<T>& right)
248 {
249 return (left.x == right.x) && (left.y == right.y);
250 }
251
255 template <typename T>
256 constexpr bool operator!=(const Vector2<T>& left, const Vector2<T>& right)
257 {
258 return !(left == right);
259 }
260
262
263 using Vector2f = Vector2<float>;
264 using Vector2u = Vector2<unsigned int>;
265 using Vector2i = Vector2<int>;
266} // namespace tgui
267
268#endif // TGUI_VECTOR2_HPP
Wrapper class to store strings.
Definition String.hpp:94
Definition Vector2.hpp:42
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
constexpr Vector2 multiplyComponents(Vector2 right) const
Returns the component-wise multiplication of the x and y components between *this and right.
Definition Vector2.hpp:135
float y
Definition Vector2.hpp:144
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
float x
Definition Vector2.hpp:143
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37