lxgui
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 
7 namespace lxgui::gui {
8 
13 template<typename T>
14 struct 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 
90 template<typename T>
91 bounds2<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 
100 template<typename T>
101 bounds2<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 
110 template<typename T>
111 bounds2<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 
120 template<typename T>
121 bounds2<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 
130 template<typename T>
131 bounds2<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 
140 template<typename T>
141 bounds2<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 
150 template<typename T>
151 const bounds2<T> bounds2<T>::zero(0, 0, 0, 0);
152 
157 
158 template<typename O, typename T>
159 O& 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)
Definition: gui_color.cpp:212
bounds2< T > operator/(const bounds2< T > &quad, T scale) noexcept
color operator+(const color &c1, const color &c2) noexcept
Definition: gui_color.cpp:192
color operator*(const color &c1, const color &c2) noexcept
Definition: gui_color.cpp:200
color operator-(const color &c1, const color &c2) noexcept
Definition: gui_color.cpp:196
Holds 2D bounds of a region.
Definition: gui_bounds2.hpp:14
void set(T l, T r, T t, T b) noexcept
Definition: gui_bounds2.hpp:25
bool operator==(const bounds2< T > &quad) const
Definition: gui_bounds2.hpp:77
vector2< T > bottom_left() const noexcept
Definition: gui_bounds2.hpp:44
bool operator!=(const bounds2< T > &quad) const
Definition: gui_bounds2.hpp:81
static const bounds2 zero
Definition: gui_bounds2.hpp:85
bool contains(const vector2< T > &p) const
Definition: gui_bounds2.hpp:64
vector2< T > bottom_right() const noexcept
Definition: gui_bounds2.hpp:40
T height() const noexcept
Definition: gui_bounds2.hpp:56
T width() const noexcept
Definition: gui_bounds2.hpp:52
constexpr bounds2(const vector2< T > &center, const vector2< T > &size) noexcept
Definition: gui_bounds2.hpp:19
vector2< T > top_right() const noexcept
Definition: gui_bounds2.hpp:36
constexpr bounds2(T l, T r, T t, T b) noexcept
Definition: gui_bounds2.hpp:17
constexpr bounds2()=default
vector2< T > dimensions() const noexcept
Definition: gui_bounds2.hpp:60
vector2< T > center() const noexcept
Definition: gui_bounds2.hpp:48
vector2< T > top_left() const noexcept
Definition: gui_bounds2.hpp:32
bool overlaps(const bounds2< T > &quad) const
Definition: gui_bounds2.hpp:68
Simple structure holding four vertices and a material.
Definition: gui_quad.hpp:18
Holds 2D coordinates.
Definition: gui_vector2.hpp:14