TGUI  0.9-dev
Vector2.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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/Global.hpp>
30 #include <TGUI/String.hpp>
31 
32 #include <SFML/System/Err.hpp>
33 #include <SFML/System/Vector2.hpp>
34 
36 
37 namespace tgui
38 {
39  template <typename T>
40  class Vector2
41  {
42  public:
43 
47  TGUI_CONSTEXPR Vector2()
48  {
49  // Constructor isn't defined as "= default" because this leads to an IntelliSense error
50  }
51 
52 
56  TGUI_CONSTEXPR Vector2(T xValue, T yValue) :
57  x{xValue},
58  y{yValue}
59  {
60  }
61 
62 
68  template <typename U>
69  explicit TGUI_CONSTEXPR Vector2(const Vector2<U>& vec) :
70  x{static_cast<T>(vec.x)},
71  y{static_cast<T>(vec.y)}
72  {
73  }
74 
75 
79  TGUI_CONSTEXPR Vector2(const sf::Vector2<T>& vec) :
80  x{vec.x},
81  y{vec.y}
82  {
83  }
84 
85 
89  Vector2(const char* str) :
90  Vector2{String(str)}
91  {
92  }
93 
98  {
99  if (str.empty())
100  {
101  TGUI_PRINT_WARNING("Failed to parse Vector2. String was empty.");
102  return;
103  }
104 
105  // Remove the brackets around the value
106  if (((str.front() == '(') && (str.back() == ')')) || ((str.front() == '{') && (str.back() == '}')))
107  str = str.substr(1, str.length() - 2);
108 
109  if (str.empty())
110  {
111  x = 0;
112  y = 0;
113  return;
114  }
115 
116  auto commaPos = str.find(',');
117  if (commaPos == String::npos)
118  {
119  TGUI_PRINT_WARNING("Failed to parse Vector2 '" + str + "'. Expected numbers separated with a comma.");
120  return;
121  }
122 
123  x = static_cast<T>(str.substr(0, commaPos).trim().toFloat());
124  y = static_cast<T>(str.substr(commaPos + 1).trim().toFloat());
125  }
126 
127 
131  operator sf::Vector2<T>() const
132  {
133  return sf::Vector2<T>{x, y};
134  }
135 
136 
138  public:
139 
140  T x = 0;
141  T y = 0;
142 
144  };
145 
146 
150  template <typename T>
151  TGUI_CONSTEXPR Vector2<T> operator-(const Vector2<T>& right)
152  {
153  return {-right.x, -right.y};
154  }
155 
159  template <typename T>
160  TGUI_CONSTEXPR Vector2<T>& operator+=(Vector2<T>& left, const Vector2<T>& right)
161  {
162  left.x += right.x;
163  left.y += right.y;
164  return left;
165  }
166 
170  template <typename T>
171  TGUI_CONSTEXPR Vector2<T>& operator-=(Vector2<T>& left, const Vector2<T>& right)
172  {
173  left.x -= right.x;
174  left.y -= right.y;
175  return left;
176  }
177 
181  template <typename T>
182  TGUI_CONSTEXPR Vector2<T> operator+(const Vector2<T>& left, const Vector2<T>& right)
183  {
184  return {left.x + right.x, left.y + right.y};
185  }
186 
190  template <typename T>
191  TGUI_CONSTEXPR Vector2<T> operator-(const Vector2<T>& left, const Vector2<T>& right)
192  {
193  return {left.x - right.x, left.y - right.y};
194  }
195 
199  template <typename T>
200  TGUI_CONSTEXPR Vector2<T> operator*(const Vector2<T>& left, float right)
201  {
202  return {left.x * right, left.y * right};
203  }
204 
208  template <typename T>
209  TGUI_CONSTEXPR Vector2<T> operator*(float left, const Vector2<T>& right)
210  {
211  return {left * right.x, left * right.y};
212  }
213 
217  template <typename T>
218  TGUI_CONSTEXPR Vector2<T>& operator*=(Vector2<T>& left, float right)
219  {
220  left.x *= right;
221  left.y *= right;
222  return left;
223  }
224 
228  template <typename T>
229  TGUI_CONSTEXPR Vector2<T> operator/(const Vector2<T>& left, float right)
230  {
231  return {left.x / right, left.y / right};
232  }
233 
237  template <typename T>
238  TGUI_CONSTEXPR Vector2<T>& operator/=(Vector2<T>& left, float right)
239  {
240  left.x /= right;
241  left.y /= right;
242  return left;
243  }
244 
248  template <typename T>
249  TGUI_CONSTEXPR bool operator==(const Vector2<T>& left, const Vector2<T>& right)
250  {
251  return (left.x == right.x) && (left.y == right.y);
252  }
253 
257  template <typename T>
258  TGUI_CONSTEXPR bool operator!=(const Vector2<T>& left, const Vector2<T>& right)
259  {
260  return !(left == right);
261  }
262 
264 
265  using Vector2f = Vector2<float>;
266  using Vector2u = Vector2<unsigned int>;
267 }
268 
270 
271 #endif // TGUI_VECTOR2_HPP
TGUI_CONSTEXPR Vector2(const Vector2< U > &vec)
Constructs the vector from an another Vector2 with a different type.
Definition: Vector2.hpp:69
TGUI_CONSTEXPR Vector2()
Default constructor.
Definition: Vector2.hpp:47
T y
Y coordinate of the vector.
Definition: Vector2.hpp:141
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
T x
X coordinate of the vector.
Definition: Vector2.hpp:140
String trim() const
Returns a string with the whitespace at the start and end of this string removed.
TGUI_CONSTEXPR Vector2(const sf::Vector2< T > &vec)
Copy constructor to create from an sf::Vector2.
Definition: Vector2.hpp:79
float toFloat(float defaultValue=0) const
Converts the string to a float.
TGUI_CONSTEXPR Vector2(T xValue, T yValue)
Constructor to create from X and Y values.
Definition: Vector2.hpp:56
Vector2(String str)
Constructor to create from a string.
Definition: Vector2.hpp:97
Wrapper class to store strings.
Definition: String.hpp:70
Definition: Vector2.hpp:40
Vector2(const char *str)
Constructor to create from a string.
Definition: Vector2.hpp:89