TGUI  0.8.9
Vector2f.hpp
1
2//
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_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
36namespace tgui
37{
38 class TGUI_API Vector2f
39 {
40 public:
41
45 TGUI_CONSTEXPR Vector2f()
46 {
47 // Constructor isn't defined as "= default" because this leads to an IntelliSense error
48 }
49
53 TGUI_CONSTEXPR Vector2f(float xValue, float yValue) :
54 x{xValue},
55 y{yValue}
56 {
57 }
58
62 Vector2f(const sf::Vector2f& vec) :
63 x{vec.x},
64 y{vec.y}
65 {
66 }
67
71 Vector2f(const char* str) :
72 Vector2f{std::string(str)}
73 {
74 }
75
79 Vector2f(std::string str)
80 {
81 if (str.empty())
82 {
83 TGUI_PRINT_WARNING("Failed to parse Vector2f. String was empty.");
84 return;
85 }
86
87 // Remove the brackets around the value
88 if (((str.front() == '(') && (str.back() == ')')) || ((str.front() == '{') && (str.back() == '}')))
89 str = str.substr(1, str.length() - 2);
90
91 if (str.empty())
92 {
93 x = 0;
94 y = 0;
95 return;
96 }
97
98 auto commaPos = str.find(',');
99 if (commaPos == std::string::npos)
100 {
101 TGUI_PRINT_WARNING("Failed to parse Vector2f '" + str + "'. Expected numbers separated with a comma.");
102 return;
103 }
104
105 x = strToFloat(trim(str.substr(0, commaPos)));
106 y = strToFloat(trim(str.substr(commaPos + 1)));
107 }
108
109
113 operator sf::Vector2f() const
114 {
115 return sf::Vector2f{x, y};
116 }
117
118
120 public:
121
122 float x = 0;
123 float y = 0;
124
126 };
127
128
132 inline TGUI_CONSTEXPR Vector2f operator-(const Vector2f& right)
133 {
134 return {-right.x, -right.y};
135 }
136
140 inline TGUI_CONSTEXPR Vector2f& operator+=(Vector2f& left, const Vector2f& right)
141 {
142 left.x += right.x;
143 left.y += right.y;
144 return left;
145 }
146
150 inline TGUI_CONSTEXPR Vector2f& operator-=(Vector2f& left, const Vector2f& right)
151 {
152 left.x -= right.x;
153 left.y -= right.y;
154 return left;
155 }
156
160 inline TGUI_CONSTEXPR Vector2f operator+(const Vector2f& left, const Vector2f& right)
161 {
162 return {left.x + right.x, left.y + right.y};
163 }
164
168 inline TGUI_CONSTEXPR Vector2f operator-(const Vector2f& left, const Vector2f& right)
169 {
170 return {left.x - right.x, left.y - right.y};
171 }
172
176 inline TGUI_CONSTEXPR Vector2f operator*(const Vector2f& left, float right)
177 {
178 return {left.x * right, left.y * right};
179 }
180
184 inline TGUI_CONSTEXPR Vector2f operator*(float left, const Vector2f& right)
185 {
186 return {left * right.x, left * right.y};
187 }
188
192 inline TGUI_CONSTEXPR Vector2f& operator*=(Vector2f& left, float right)
193 {
194 left.x *= right;
195 left.y *= right;
196 return left;
197 }
198
202 inline TGUI_CONSTEXPR Vector2f operator/(const Vector2f& left, float right)
203 {
204 return {left.x / right, left.y / right};
205 }
206
210 inline TGUI_CONSTEXPR Vector2f& operator/=(Vector2f& left, float right)
211 {
212 left.x /= right;
213 left.y /= right;
214 return left;
215 }
216
220 inline TGUI_CONSTEXPR bool operator==(const Vector2f& left, const Vector2f& right)
221 {
222 return (left.x == right.x) && (left.y == right.y);
223 }
224
228 inline TGUI_CONSTEXPR bool operator!=(const Vector2f& left, const Vector2f& right)
229 {
230 return !(left == right);
231 }
232
234}
235
237
238#endif // TGUI_VECTOR2F_HPP
Definition: Vector2f.hpp:39
Vector2f(const char *str)
Constructor to create from a string.
Definition: Vector2f.hpp:71
Vector2f(const sf::Vector2f &vec)
Copy constructor to create from an sf::Vector2f.
Definition: Vector2f.hpp:62
TGUI_CONSTEXPR Vector2f(float xValue, float yValue)
Constructor to create from X and Y values.
Definition: Vector2f.hpp:53
Vector2f(std::string str)
Constructor to create from a string.
Definition: Vector2f.hpp:79
float x
X coordinate of the vector.
Definition: Vector2f.hpp:122
TGUI_CONSTEXPR Vector2f()
Default constructor.
Definition: Vector2f.hpp:45
float y
Y coordinate of the vector.
Definition: Vector2f.hpp:123
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
TGUI_CONSTEXPR Vector2f & operator*=(Vector2f &left, float right)
Overload of binary operator *=.
Definition: Vector2f.hpp:192
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_API std::string trim(const std::string &str)
Trims the whitespace from a string.
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
TGUI_CONSTEXPR Vector2f & operator-=(Vector2f &left, const Vector2f &right)
Overload of binary operator -=.
Definition: Vector2f.hpp:150
TGUI_API Layout operator-(Layout right)
Unary minus operator for the Layout class.
TGUI_CONSTEXPR Vector2f & operator/=(Vector2f &left, float right)
Overload of binary operator /=.
Definition: Vector2f.hpp:210
TGUI_CONSTEXPR Vector2f & operator+=(Vector2f &left, const Vector2f &right)
Overload of binary operator +=.
Definition: Vector2f.hpp:140