lxgui
Loading...
Searching...
No Matches
gui_bounds2.hpp
1#ifndef LXGUI_GUI_BOUNDS2_HPP
2#define LXGUI_GUI_BOUNDS2_HPP
3
4#include "lxgui/gui_vector2.hpp"
5#include "lxgui/lxgui.hpp"
6
7namespace lxgui::gui {
8
13template<typename T>
14struct bounds2 {
15 constexpr bounds2() = default;
16
17 constexpr bounds2(T l, T r, T t, T b) noexcept : left(l), right(r), top(t), bottom(b) {}
18
19 constexpr bounds2(const vector2<T>& center, const vector2<T>& size) noexcept :
20 left(center.x - size.x / 2),
21 right(left + size.x),
22 top(center.y - size.y / 2),
23 bottom(top + size.y) {}
24
25 void set(T l, T r, T t, T b) noexcept {
26 left = l;
27 right = r;
28 top = t;
29 bottom = b;
30 }
31
32 vector2<T> top_left() const noexcept {
33 return vector2<T>(left, top);
34 }
35
36 vector2<T> top_right() const noexcept {
37 return vector2<T>(right, top);
38 }
39
40 vector2<T> bottom_right() const noexcept {
41 return vector2<T>(right, bottom);
42 }
43
44 vector2<T> bottom_left() const noexcept {
45 return vector2<T>(left, bottom);
46 }
47
48 vector2<T> center() const noexcept {
49 return vector2<T>((left + right) / 2, (top + bottom) / 2);
50 }
51
52 T width() const noexcept {
53 return right - left;
54 }
55
56 T height() const noexcept {
57 return bottom - top;
58 }
59
60 vector2<T> dimensions() const noexcept {
61 return vector2<T>(width(), height());
62 }
63
64 bool contains(const vector2<T>& p) const {
65 return p.x >= left && p.x < right && p.y >= top && p.y < bottom;
66 }
67
68 bool overlaps(const bounds2<T>& quad) const {
69 auto range_overlaps = [](T min1, T max1, T min2, T max2) {
70 return (min1 >= min2 && min1 < max2) || (min2 >= min1 && min2 < max1);
71 };
72
73 return range_overlaps(left, right, quad.left, quad.right) &&
74 range_overlaps(top, bottom, quad.top, quad.bottom);
75 }
76
77 bool operator==(const bounds2<T>& quad) const {
78 return left == quad.left && right == quad.right && top == quad.top && bottom == quad.bottom;
79 }
80
81 bool operator!=(const bounds2<T>& quad) const {
82 return left != quad.left || right != quad.right || top != quad.top || bottom != quad.bottom;
83 }
84
85 static const bounds2 zero;
86
87 T left = 0, right = 0, top = 0, bottom = 0;
88};
89
90template<typename T>
91bounds2<T> operator+(const vector2<T>& offset, const bounds2<T>& quad) noexcept {
92 bounds2<T> tmp = quad;
93 tmp.left += offset.x;
94 tmp.right += offset.x;
95 tmp.top += offset.y;
96 tmp.bottom += offset.y;
97 return tmp;
98}
99
100template<typename T>
101bounds2<T> operator+(const bounds2<T>& quad, const vector2<T>& offset) noexcept {
102 bounds2<T> tmp = quad;
103 tmp.left += offset.x;
104 tmp.right += offset.x;
105 tmp.top += offset.y;
106 tmp.bottom += offset.y;
107 return tmp;
108}
109
110template<typename T>
111bounds2<T> operator-(const bounds2<T>& quad, const vector2<T>& offset) noexcept {
112 bounds2<T> tmp = quad;
113 tmp.left -= offset.x;
114 tmp.right -= offset.x;
115 tmp.top -= offset.y;
116 tmp.bottom -= offset.y;
117 return tmp;
118}
119
120template<typename T>
121bounds2<T> operator*(const bounds2<T>& quad, T scale) noexcept {
122 bounds2<T> tmp = quad;
123 tmp.left *= scale;
124 tmp.right *= scale;
125 tmp.top *= scale;
126 tmp.bottom *= scale;
127 return tmp;
128}
129
130template<typename T>
131bounds2<T> operator*(T scale, const bounds2<T>& quad) noexcept {
132 bounds2<T> tmp = quad;
133 tmp.left *= scale;
134 tmp.right *= scale;
135 tmp.top *= scale;
136 tmp.bottom *= scale;
137 return tmp;
138}
139
140template<typename T>
141bounds2<T> operator/(const bounds2<T>& quad, T scale) noexcept {
142 bounds2<T> tmp = quad;
143 tmp.left /= scale;
144 tmp.right /= scale;
145 tmp.top /= scale;
146 tmp.bottom /= scale;
147 return tmp;
148}
149
150template<typename T>
151const bounds2<T> bounds2<T>::zero(0, 0, 0, 0);
152
157
158template<typename O, typename T>
159O& operator<<(O& stream, const bounds2<T>& quad) {
160 return stream << "(" << quad.top_left() << "), (" << quad.bottom_right() << ")";
161}
162
163} // namespace lxgui::gui
164
165#endif
std::ostream & operator<<(std::ostream &stream, const color &c)
bounds2< T > operator/(const bounds2< T > &quad, T scale) noexcept
color operator+(const color &c1, const color &c2) noexcept
color operator*(const color &c1, const color &c2) noexcept
color operator-(const color &c1, const color &c2) noexcept
Holds 2D bounds of a region.
void set(T l, T r, T t, T b) noexcept
bool operator==(const bounds2< T > &quad) const
bool operator!=(const bounds2< T > &quad) const
vector2< T > bottom_left() const noexcept
static const bounds2 zero
bool contains(const vector2< T > &p) const
vector2< T > bottom_right() const noexcept
vector2< T > dimensions() const noexcept
T height() const noexcept
T width() const noexcept
constexpr bounds2(const vector2< T > &center, const vector2< T > &size) noexcept
vector2< T > center() const noexcept
vector2< T > top_right() const noexcept
constexpr bounds2(T l, T r, T t, T b) noexcept
vector2< T > top_left() const noexcept
constexpr bounds2()=default
bool overlaps(const bounds2< T > &quad) const
Simple structure holding four vertices and a material.
Definition gui_quad.hpp:18
Holds 2D coordinates.