TGUI  1.3-dev
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
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_WINDOW_BACKEND_SFML
33 #include <SFML/System/Vector2.hpp>
34#endif
35
37
38TGUI_MODULE_EXPORT namespace tgui
39{
40 template <typename T>
41 class Vector2
42 {
43 public:
44
48 constexpr Vector2() = default;
49
50
54 constexpr Vector2(T xValue, T yValue) :
55 x{xValue},
56 y{yValue}
57 {
58 }
59
60
66 template <typename U>
67 explicit constexpr Vector2(const Vector2<U>& vec) :
68 x{static_cast<T>(vec.x)},
69 y{static_cast<T>(vec.y)}
70 {
71 }
72
73#if TGUI_HAS_WINDOW_BACKEND_SFML
77 constexpr Vector2(const sf::Vector2<T>& vec) :
78 x{vec.x},
79 y{vec.y}
80 {
81 }
82#endif
83
87 Vector2(const char* str) :
88 Vector2{String(str)}
89 {
90 }
91
96 {
97 if (str.empty())
98 {
99 TGUI_PRINT_WARNING("Failed to parse Vector2. String was empty.");
100 return;
101 }
102
103 // Remove the brackets around the value
104 if (((str.front() == '(') && (str.back() == ')')) || ((str.front() == '{') && (str.back() == '}')))
105 str = str.substr(1, str.length() - 2);
106
107 if (str.empty())
108 {
109 x = 0;
110 y = 0;
111 return;
112 }
113
114 auto commaPos = str.find(',');
115 if (commaPos == String::npos)
116 {
117 TGUI_PRINT_WARNING("Failed to parse Vector2 '" + str + "'. Expected numbers separated with a comma.");
118 return;
119 }
120
121 x = static_cast<T>(str.substr(0, commaPos).trim().toFloat());
122 y = static_cast<T>(str.substr(commaPos + 1).trim().toFloat());
123 }
124
125#if TGUI_HAS_WINDOW_BACKEND_SFML
129 TGUI_NODISCARD operator sf::Vector2<T>() const
130 {
131 return sf::Vector2<T>{x, y};
132 }
133#endif
134
136 public:
137
138 T x = 0;
139 T y = 0;
140
142 };
143
144
148 template <typename T>
149 constexpr Vector2<T> operator-(const Vector2<T>& right)
150 {
151 return {-right.x, -right.y};
152 }
153
157 template <typename T>
158 constexpr Vector2<T>& operator+=(Vector2<T>& left, const Vector2<T>& right)
159 {
160 left.x += right.x;
161 left.y += right.y;
162 return left;
163 }
164
168 template <typename T>
169 constexpr Vector2<T>& operator-=(Vector2<T>& left, const Vector2<T>& right)
170 {
171 left.x -= right.x;
172 left.y -= right.y;
173 return left;
174 }
175
179 template <typename T>
180 constexpr Vector2<T> operator+(const Vector2<T>& left, const Vector2<T>& right)
181 {
182 return {left.x + right.x, left.y + right.y};
183 }
184
188 template <typename T>
189 constexpr Vector2<T> operator-(const Vector2<T>& left, const Vector2<T>& right)
190 {
191 return {left.x - right.x, left.y - right.y};
192 }
193
197 template <typename T>
198 constexpr Vector2<T> operator*(const Vector2<T>& left, float right)
199 {
200 return {static_cast<T>(static_cast<float>(left.x) * right), static_cast<T>(static_cast<float>(left.y) * right)};
201 }
202
206 template <typename T>
207 constexpr Vector2<T> operator*(float left, const Vector2<T>& right)
208 {
209 return {static_cast<T>(left * static_cast<float>(right.x)), static_cast<T>(left * static_cast<float>(right.y))};
210 }
211
215 template <typename T>
216 constexpr Vector2<T>& operator*=(Vector2<T>& left, float right)
217 {
218 return left = left * right;
219 }
220
224 template <typename T>
225 constexpr Vector2<T> operator/(const Vector2<T>& left, float right)
226 {
227 return {static_cast<T>(static_cast<float>(left.x) / right), static_cast<T>(static_cast<float>(left.y) / right)};
228 }
229
233 template <typename T>
234 constexpr Vector2<T>& operator/=(Vector2<T>& left, float right)
235 {
236 return left = left / right;
237 }
238
242 template <typename T>
243 constexpr bool operator==(const Vector2<T>& left, const Vector2<T>& right)
244 {
245 return (left.x == right.x) && (left.y == right.y);
246 }
247
251 template <typename T>
252 constexpr bool operator!=(const Vector2<T>& left, const Vector2<T>& right)
253 {
254 return !(left == right);
255 }
256
258
259 using Vector2f = Vector2<float>;
260 using Vector2u = Vector2<unsigned int>;
261 using Vector2i = Vector2<int>;
262}
263
265
266#endif // TGUI_VECTOR2_HPP
Wrapper class to store strings.
Definition String.hpp:101
TGUI_NODISCARD String trim() const
Returns a string with the whitespace at the start and end of this string removed.
TGUI_NODISCARD float toFloat(float defaultValue=0) const
Converts the string to a float.
Definition Vector2.hpp:42
constexpr Vector2(T xValue, T yValue)
Constructor to create from X and Y values.
Definition Vector2.hpp:54
constexpr Vector2()=default
Default constructor.
constexpr Vector2(const sf::Vector2< T > &vec)
Copy constructor to create from an sf::Vector2.
Definition Vector2.hpp:77
T y
Y coordinate of the vector.
Definition Vector2.hpp:139
Vector2(String str)
Constructor to create from a string.
Definition Vector2.hpp:95
constexpr Vector2(const Vector2< U > &vec)
Constructs the vector from an another Vector2 with a different type.
Definition Vector2.hpp:67
Vector2(const char *str)
Constructor to create from a string.
Definition Vector2.hpp:87
T x
X coordinate of the vector.
Definition Vector2.hpp:138
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39