TGUI  0.8.0
Vector2f.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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_VECTOR2F_HPP
27 #define TGUI_VECTOR2F_HPP
28 
29 #include <TGUI/Global.hpp>
30 #include <SFML/System/Vector2.hpp>
31 #include <SFML/System/Err.hpp>
32 
34 
35 namespace tgui
36 {
37  class TGUI_API Vector2f
38  {
39  public:
40 
44  TGUI_CONSTEXPR Vector2f() = default;
45 
49  TGUI_CONSTEXPR Vector2f(float xValue, float yValue) :
50  x{xValue},
51  y{yValue}
52  {
53  }
54 
58  Vector2f(const sf::Vector2f& vec) :
59  x{vec.x},
60  y{vec.y}
61  {
62  }
63 
67  Vector2f(const char* str) :
68  Vector2f{std::string(str)}
69  {
70  }
71 
75  Vector2f(std::string str)
76  {
77  if (str.empty())
78  {
79  TGUI_PRINT_WARNING("Failed to parse Vector2f. String was empty.");
80  return;
81  }
82 
83  // Remove the brackets around the value
84  if (((str.front() == '(') && (str.back() == ')')) || ((str.front() == '{') && (str.back() == '}')))
85  str = str.substr(1, str.length() - 2);
86 
87  if (str.empty())
88  {
89  x = 0;
90  y = 0;
91  return;
92  }
93 
94  auto commaPos = str.find(',');
95  if (commaPos == std::string::npos)
96  {
97  TGUI_PRINT_WARNING("Failed to parse Vector2f '" + str + "'. Expected numbers separated with a comma.");
98  return;
99  }
100 
101  x = tgui::stof(trim(str.substr(0, commaPos)));
102  y = tgui::stof(trim(str.substr(commaPos + 1)));
103  }
104 
105 
109  operator sf::Vector2f() const
110  {
111  return sf::Vector2f{x, y};
112  }
113 
114 
116  public:
117 
118  float x = 0;
119  float y = 0;
120 
122  };
123 
124 
128  inline TGUI_CONSTEXPR Vector2f operator-(const Vector2f& right)
129  {
130  return {-right.x, -right.y};
131  }
132 
136  inline TGUI_CONSTEXPR Vector2f& operator+=(Vector2f& left, const Vector2f& right)
137  {
138  left.x += right.x;
139  left.y += right.y;
140  return left;
141  }
142 
146  inline TGUI_CONSTEXPR Vector2f& operator-=(Vector2f& left, const Vector2f& right)
147  {
148  left.x -= right.x;
149  left.y -= right.y;
150  return left;
151  }
152 
156  inline TGUI_CONSTEXPR Vector2f operator+(const Vector2f& left, const Vector2f& right)
157  {
158  return {left.x + right.x, left.y + right.y};
159  }
160 
164  inline TGUI_CONSTEXPR Vector2f operator-(const Vector2f& left, const Vector2f& right)
165  {
166  return {left.x - right.x, left.y - right.y};
167  }
168 
172  inline TGUI_CONSTEXPR Vector2f operator*(const Vector2f& left, float right)
173  {
174  return {left.x * right, left.y * right};
175  }
176 
180  inline TGUI_CONSTEXPR Vector2f operator*(float left, const Vector2f& right)
181  {
182  return {left * right.x, left * right.y};
183  }
184 
188  inline TGUI_CONSTEXPR Vector2f& operator*=(Vector2f& left, float right)
189  {
190  left.x *= right;
191  left.y *= right;
192  return left;
193  }
194 
198  inline TGUI_CONSTEXPR Vector2f operator/(const Vector2f& left, float right)
199  {
200  return {left.x / right, left.y / right};
201  }
202 
206  inline TGUI_CONSTEXPR Vector2f& operator/=(Vector2f& left, float right)
207  {
208  left.x /= right;
209  left.y /= right;
210  return left;
211  }
212 
216  inline TGUI_CONSTEXPR bool operator==(const Vector2f& left, const Vector2f& right)
217  {
218  return (left.x == right.x) && (left.y == right.y);
219  }
220 
224  inline TGUI_CONSTEXPR bool operator!=(const Vector2f& left, const Vector2f& right)
225  {
226  return !(left == right);
227  }
228 
230 }
231 
233 
234 #endif // TGUI_VECTOR2F_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
TGUI_CONSTEXPR Vector2f & operator+=(Vector2f &left, const Vector2f &right)
Overload of binary operator +=.
Definition: Vector2f.hpp:136
TGUI_CONSTEXPR Vector2f & operator*=(Vector2f &left, float right)
Overload of binary operator *=.
Definition: Vector2f.hpp:188
Definition: Vector2f.hpp:37
TGUI_CONSTEXPR Vector2f(float xValue, float yValue)
Constructor to create from X and Y values.
Definition: Vector2f.hpp:49
Vector2f(const sf::Vector2f &vec)
Copy constructor to create from an sf::Vector2f.
Definition: Vector2f.hpp:58
TGUI_CONSTEXPR bool operator!=(const Vector2f &left, const Vector2f &right)
Overload of binary operator ==.
Definition: Vector2f.hpp:224
TGUI_CONSTEXPR bool operator==(const Vector2f &left, const Vector2f &right)
Overload of binary operator ==.
Definition: Vector2f.hpp:216
TGUI_API Layout operator*(Layout left, Layout right)
operator for the Layout class
TGUI_CONSTEXPR Vector2f & operator-=(Vector2f &left, const Vector2f &right)
Overload of binary operator -=.
Definition: Vector2f.hpp:146
TGUI_API Layout operator+(Layout left, Layout right)
operator for the Layout class
TGUI_API Layout operator/(Layout left, Layout right)
/ operator for the Layout class
float x
X coordinate of the vector.
Definition: Vector2f.hpp:118
TGUI_API Layout operator-(Layout right)
Unary minus operator for the Layout class.
Vector2f(const char *str)
Constructor to create from a string.
Definition: Vector2f.hpp:67
Vector2f(std::string str)
Constructor to create from a string.
Definition: Vector2f.hpp:75
TGUI_CONSTEXPR Vector2f & operator/=(Vector2f &left, float right)
Overload of binary operator /=.
Definition: Vector2f.hpp:206
float y
Y coordinate of the vector.
Definition: Vector2f.hpp:119