Rect.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #pragma once
  2. #include <AK/LogStream.h>
  3. #include <AK/String.h>
  4. #include <LibDraw/Orientation.h>
  5. #include <LibDraw/Point.h>
  6. #include <LibDraw/Size.h>
  7. #include <LibDraw/TextAlignment.h>
  8. class Rect {
  9. public:
  10. Rect() {}
  11. Rect(int x, int y, int width, int height)
  12. : m_location(x, y)
  13. , m_size(width, height)
  14. {
  15. }
  16. Rect(const Point& location, const Size& size)
  17. : m_location(location)
  18. , m_size(size)
  19. {
  20. }
  21. bool is_null() const
  22. {
  23. return width() == 0 && height() == 0;
  24. }
  25. bool is_empty() const
  26. {
  27. return width() <= 0 || height() <= 0;
  28. }
  29. void move_by(int dx, int dy)
  30. {
  31. m_location.move_by(dx, dy);
  32. }
  33. void move_by(const Point& delta)
  34. {
  35. m_location.move_by(delta);
  36. }
  37. Point center() const
  38. {
  39. return { x() + width() / 2, y() + height() / 2 };
  40. }
  41. void set_location(const Point& location)
  42. {
  43. m_location = location;
  44. }
  45. void set_size(const Size& size)
  46. {
  47. m_size = size;
  48. }
  49. void set_size(int width, int height)
  50. {
  51. m_size.set_width(width);
  52. m_size.set_height(height);
  53. }
  54. void inflate(int w, int h)
  55. {
  56. set_x(x() - w / 2);
  57. set_width(width() + w);
  58. set_y(y() - h / 2);
  59. set_height(height() + h);
  60. }
  61. void shrink(int w, int h)
  62. {
  63. set_x(x() + w / 2);
  64. set_width(width() - w);
  65. set_y(y() + h / 2);
  66. set_height(height() - h);
  67. }
  68. Rect shrunken(int w, int h) const
  69. {
  70. Rect rect = *this;
  71. rect.shrink(w, h);
  72. return rect;
  73. }
  74. Rect inflated(int w, int h) const
  75. {
  76. Rect rect = *this;
  77. rect.inflate(w, h);
  78. return rect;
  79. }
  80. Rect translated(int dx, int dy) const
  81. {
  82. Rect rect = *this;
  83. rect.move_by(dx, dy);
  84. return rect;
  85. }
  86. Rect translated(const Point& delta) const
  87. {
  88. Rect rect = *this;
  89. rect.move_by(delta);
  90. return rect;
  91. }
  92. bool contains_vertically(int y) const
  93. {
  94. return y >= top() && y <= bottom();
  95. }
  96. bool contains_horizontally(int x) const
  97. {
  98. return x >= left() && x <= right();
  99. }
  100. bool contains(int x, int y) const
  101. {
  102. return x >= m_location.x() && x <= right() && y >= m_location.y() && y <= bottom();
  103. }
  104. bool contains(const Point& point) const
  105. {
  106. return contains(point.x(), point.y());
  107. }
  108. bool contains(const Rect& other) const
  109. {
  110. return left() <= other.left()
  111. && right() >= other.right()
  112. && top() <= other.top()
  113. && bottom() >= other.bottom();
  114. }
  115. int primary_offset_for_orientation(Orientation orientation) const { return m_location.primary_offset_for_orientation(orientation); }
  116. void set_primary_offset_for_orientation(Orientation orientation, int value) { m_location.set_primary_offset_for_orientation(orientation, value); }
  117. int secondary_offset_for_orientation(Orientation orientation) const { return m_location.secondary_offset_for_orientation(orientation); }
  118. void set_secondary_offset_for_orientation(Orientation orientation, int value) { m_location.set_secondary_offset_for_orientation(orientation, value); }
  119. int primary_size_for_orientation(Orientation orientation) const { return m_size.primary_size_for_orientation(orientation); }
  120. int secondary_size_for_orientation(Orientation orientation) const { return m_size.secondary_size_for_orientation(orientation); }
  121. void set_primary_size_for_orientation(Orientation orientation, int value) { m_size.set_primary_size_for_orientation(orientation, value); }
  122. void set_secondary_size_for_orientation(Orientation orientation, int value) { m_size.set_secondary_size_for_orientation(orientation, value); }
  123. int first_edge_for_orientation(Orientation orientation) const
  124. {
  125. if (orientation == Orientation::Vertical)
  126. return top();
  127. return left();
  128. }
  129. int last_edge_for_orientation(Orientation orientation) const
  130. {
  131. if (orientation == Orientation::Vertical)
  132. return bottom();
  133. return right();
  134. }
  135. int left() const { return x(); }
  136. int right() const { return x() + width() - 1; }
  137. int top() const { return y(); }
  138. int bottom() const { return y() + height() - 1; }
  139. void set_left(int left)
  140. {
  141. set_x(left);
  142. }
  143. void set_top(int top)
  144. {
  145. set_y(top);
  146. }
  147. void set_right(int right)
  148. {
  149. set_width(right - x() + 1);
  150. }
  151. void set_bottom(int bottom)
  152. {
  153. set_height(bottom - y() + 1);
  154. }
  155. void set_right_without_resize(int new_right)
  156. {
  157. int delta = new_right - right();
  158. move_by(delta, 0);
  159. }
  160. void set_bottom_without_resize(int new_bottom)
  161. {
  162. int delta = new_bottom - bottom();
  163. move_by(0, delta);
  164. }
  165. bool intersects_vertically(const Rect& other) const
  166. {
  167. return top() <= other.bottom()
  168. && other.top() <= bottom();
  169. }
  170. bool intersects_horizontally(const Rect& other) const
  171. {
  172. return left() <= other.right()
  173. && other.left() <= right();
  174. }
  175. bool intersects(const Rect& other) const
  176. {
  177. return left() <= other.right()
  178. && other.left() <= right()
  179. && top() <= other.bottom()
  180. && other.top() <= bottom();
  181. }
  182. int x() const { return location().x(); }
  183. int y() const { return location().y(); }
  184. int width() const { return m_size.width(); }
  185. int height() const { return m_size.height(); }
  186. void set_x(int x) { m_location.set_x(x); }
  187. void set_y(int y) { m_location.set_y(y); }
  188. void set_width(int width) { m_size.set_width(width); }
  189. void set_height(int height) { m_size.set_height(height); }
  190. Point location() const { return m_location; }
  191. Size size() const { return m_size; }
  192. Vector<Rect, 4> shatter(const Rect& hammer) const;
  193. bool operator==(const Rect& other) const
  194. {
  195. return m_location == other.m_location
  196. && m_size == other.m_size;
  197. }
  198. void intersect(const Rect&);
  199. static Rect from_two_points(const Point& a, const Point& b)
  200. {
  201. return { min(a.x(), b.x()), min(a.y(), b.y()), abs(a.x() - b.x()), abs(a.y() - b.y()) };
  202. }
  203. static Rect intersection(const Rect& a, const Rect& b)
  204. {
  205. Rect r(a);
  206. r.intersect(b);
  207. return r;
  208. }
  209. Rect intersected(const Rect& other) const
  210. {
  211. return intersection(*this, other);
  212. }
  213. Rect united(const Rect&) const;
  214. Point top_left() const { return { left(), top() }; }
  215. Point top_right() const { return { right(), top() }; }
  216. Point bottom_left() const { return { left(), bottom() }; }
  217. Point bottom_right() const { return { right(), bottom() }; }
  218. void align_within(const Rect&, TextAlignment);
  219. void center_within(const Rect& other)
  220. {
  221. center_horizontally_within(other);
  222. center_vertically_within(other);
  223. }
  224. void center_horizontally_within(const Rect& other)
  225. {
  226. set_x(other.center().x() - width() / 2);
  227. }
  228. void center_vertically_within(const Rect& other)
  229. {
  230. set_y(other.center().y() - height() / 2);
  231. }
  232. String to_string() const { return String::format("[%d,%d %dx%d]", x(), y(), width(), height()); }
  233. private:
  234. Point m_location;
  235. Size m_size;
  236. };
  237. inline void Point::constrain(const Rect& rect)
  238. {
  239. if (x() < rect.left())
  240. set_x(rect.left());
  241. else if (x() > rect.right())
  242. set_x(rect.right());
  243. if (y() < rect.top())
  244. set_y(rect.top());
  245. else if (y() > rect.bottom())
  246. set_y(rect.bottom());
  247. }
  248. inline const LogStream& operator<<(const LogStream& stream, const Rect& value)
  249. {
  250. return stream << value.to_string();
  251. }