TGUI  0.8.6
Vector2f.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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 <TGUI/String.hpp>
31 #include <SFML/System/Vector2.hpp>
32 #include <SFML/System/Err.hpp>
33 
35 
36 namespace tgui
37 {
38  class TGUI_API Vector2f
39  {
40  public:
41 
45  TGUI_CONSTEXPR Vector2f() = default;
46 
50  TGUI_CONSTEXPR Vector2f(float xValue, float yValue) :
51  x{xValue},
52  y{yValue}
53  {
54  }
55 
59  Vector2f(const sf::Vector2f& vec) :
60  x{vec.x},
61  y{vec.y}
62  {
63  }
64 
68  Vector2f(const char* str) :
69  Vector2f{std::string(str)}
70  {
71  }
72 
76  Vector2f(std::string str)
77  {
78  if (str.empty())
79  {
80  TGUI_PRINT_WARNING("Failed to parse Vector2f. String was empty.");
81  return;
82  }
83 
84  // Remove the brackets around the value
85  if (((str.front() == '(') && (str.back() == ')')) || ((str.front() == '{') && (str.back() == '}')))
86  str = str.substr(1, str.length() - 2);
87 
88  if (str.empty())
89  {
90  x = 0;
91  y = 0;
92  return;
93  }
94 
95  auto commaPos = str.find(',');
96  if (commaPos == std::string::npos)
97  {
98  TGUI_PRINT_WARNING("Failed to parse Vector2f '" + str + "'. Expected numbers separated with a comma.");
99  return;
100  }
101 
102  x = strToFloat(trim(str.substr(0, commaPos)));
103  y = strToFloat(trim(str.substr(commaPos + 1)));
104  }
105 
106 
110  operator sf::Vector2f() const
111  {
112  return sf::Vector2f{x, y};
113  }
114 
115 
117  public:
118 
119  float x = 0;
120  float y = 0;
121 
123  };
124 
125 
129  inline TGUI_CONSTEXPR Vector2f operator-(const Vector2f& right)
130  {
131  return {-right.x, -right.y};
132  }
133 
137  inline TGUI_CONSTEXPR Vector2f& operator+=(Vector2f& left, const Vector2f& right)
138  {
139  left.x += right.x;
140  left.y += right.y;
141  return left;
142  }
143 
147  inline TGUI_CONSTEXPR Vector2f& operator-=(Vector2f& left, const Vector2f& right)
148  {
149  left.x -= right.x;
150  left.y -= right.y;
151  return left;
152  }
153 
157  inline TGUI_CONSTEXPR Vector2f operator+(const Vector2f& left, const Vector2f& right)
158  {
159  return {left.x + right.x, left.y + right.y};
160  }
161 
165  inline TGUI_CONSTEXPR Vector2f operator-(const Vector2f& left, const Vector2f& right)
166  {
167  return {left.x - right.x, left.y - right.y};
168  }
169 
173  inline TGUI_CONSTEXPR Vector2f operator*(const Vector2f& left, float right)
174  {
175  return {left.x * right, left.y * right};
176  }
177 
181  inline TGUI_CONSTEXPR Vector2f operator*(float left, const Vector2f& right)
182  {
183  return {left * right.x, left * right.y};
184  }
185 
189  inline TGUI_CONSTEXPR Vector2f& operator*=(Vector2f& left, float right)
190  {
191  left.x *= right;
192  left.y *= right;
193  return left;
194  }
195 
199  inline TGUI_CONSTEXPR Vector2f operator/(const Vector2f& left, float right)
200  {
201  return {left.x / right, left.y / right};
202  }
203 
207  inline TGUI_CONSTEXPR Vector2f& operator/=(Vector2f& left, float right)
208  {
209  left.x /= right;
210  left.y /= right;
211  return left;
212  }
213 
217  inline TGUI_CONSTEXPR bool operator==(const Vector2f& left, const Vector2f& right)
218  {
219  return (left.x == right.x) && (left.y == right.y);
220  }
221 
225  inline TGUI_CONSTEXPR bool operator!=(const Vector2f& left, const Vector2f& right)
226  {
227  return !(left == right);
228  }
229 
231 }
232 
234 
235 #endif // TGUI_VECTOR2F_HPP
TGUI_API std::string trim(const std::string &str)
Trims the whitespace from a string.
Vector2f(const sf::Vector2f &vec)
Copy constructor to create from an sf::Vector2f.
Definition: Vector2f.hpp:59
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Vector2f(std::string str)
Constructor to create from a string.
Definition: Vector2f.hpp:76
float y
Y coordinate of the vector.
Definition: Vector2f.hpp:120
TGUI_CONSTEXPR Vector2f & operator*=(Vector2f &left, float right)
Overload of binary operator *=.
Definition: Vector2f.hpp:189
TGUI_API Layout operator*(Layout left, Layout right)
operator for the Layout class
Vector2f(const char *str)
Constructor to create from a string.
Definition: Vector2f.hpp:68
float x
X coordinate of the vector.
Definition: Vector2f.hpp:119
TGUI_CONSTEXPR Vector2f & operator-=(Vector2f &left, const Vector2f &right)
Overload of binary operator -=.
Definition: Vector2f.hpp:147
Definition: Vector2f.hpp:38
TGUI_CONSTEXPR Vector2f & operator/=(Vector2f &left, float right)
Overload of binary operator /=.
Definition: Vector2f.hpp:207
TGUI_API Layout operator+(Layout left, Layout right)
operator for the Layout class
TGUI_API float strToFloat(const std::string &str, float defaultValue=0)
Converts a string to a float.
TGUI_CONSTEXPR Vector2f & operator+=(Vector2f &left, const Vector2f &right)
Overload of binary operator +=.
Definition: Vector2f.hpp:137
TGUI_API Layout operator/(Layout left, Layout right)
/ operator for the Layout class
TGUI_API Layout operator-(Layout right)
Unary minus operator for the Layout class.
TGUI_CONSTEXPR Vector2f(float xValue, float yValue)
Constructor to create from X and Y values.
Definition: Vector2f.hpp:50