rect2i.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*************************************************************************/
  2. /* rect2i.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GODOT_RECT2I_HPP
  31. #define GODOT_RECT2I_HPP
  32. #include <godot_cpp/classes/global_constants.hpp>
  33. #include <godot_cpp/variant/vector2i.hpp>
  34. namespace godot {
  35. class String;
  36. struct Rect2;
  37. struct _NO_DISCARD_ Rect2i {
  38. Point2i position;
  39. Size2i size;
  40. const Point2i &get_position() const { return position; }
  41. void set_position(const Point2i &p_position) { position = p_position; }
  42. const Size2i &get_size() const { return size; }
  43. void set_size(const Size2i &p_size) { size = p_size; }
  44. int get_area() const { return size.width * size.height; }
  45. _FORCE_INLINE_ Vector2i get_center() const { return position + (size / 2); }
  46. inline bool intersects(const Rect2i &p_rect) const {
  47. #ifdef MATH_CHECKS
  48. if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
  49. ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
  50. }
  51. #endif
  52. if (position.x >= (p_rect.position.x + p_rect.size.width)) {
  53. return false;
  54. }
  55. if ((position.x + size.width) <= p_rect.position.x) {
  56. return false;
  57. }
  58. if (position.y >= (p_rect.position.y + p_rect.size.height)) {
  59. return false;
  60. }
  61. if ((position.y + size.height) <= p_rect.position.y) {
  62. return false;
  63. }
  64. return true;
  65. }
  66. inline bool encloses(const Rect2i &p_rect) const {
  67. #ifdef MATH_CHECKS
  68. if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
  69. ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
  70. }
  71. #endif
  72. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  73. ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) &&
  74. ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y));
  75. }
  76. _FORCE_INLINE_ bool has_area() const {
  77. return size.x > 0 && size.y > 0;
  78. }
  79. // Returns the instersection between two Rect2is or an empty Rect2i if there is no intersection
  80. inline Rect2i intersection(const Rect2i &p_rect) const {
  81. Rect2i new_rect = p_rect;
  82. if (!intersects(new_rect)) {
  83. return Rect2i();
  84. }
  85. new_rect.position.x = Math::max(p_rect.position.x, position.x);
  86. new_rect.position.y = Math::max(p_rect.position.y, position.y);
  87. Point2i p_rect_end = p_rect.position + p_rect.size;
  88. Point2i end = position + size;
  89. new_rect.size.x = Math::min(p_rect_end.x, end.x) - new_rect.position.x;
  90. new_rect.size.y = Math::min(p_rect_end.y, end.y) - new_rect.position.y;
  91. return new_rect;
  92. }
  93. inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
  94. #ifdef MATH_CHECKS
  95. if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
  96. ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
  97. }
  98. #endif
  99. Rect2i new_rect;
  100. new_rect.position.x = Math::min(p_rect.position.x, position.x);
  101. new_rect.position.y = Math::min(p_rect.position.y, position.y);
  102. new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x);
  103. new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y);
  104. new_rect.size = new_rect.size - new_rect.position; // Make relative again.
  105. return new_rect;
  106. }
  107. bool has_point(const Point2i &p_point) const {
  108. #ifdef MATH_CHECKS
  109. if (unlikely(size.x < 0 || size.y < 0)) {
  110. ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
  111. }
  112. #endif
  113. if (p_point.x < position.x) {
  114. return false;
  115. }
  116. if (p_point.y < position.y) {
  117. return false;
  118. }
  119. if (p_point.x >= (position.x + size.x)) {
  120. return false;
  121. }
  122. if (p_point.y >= (position.y + size.y)) {
  123. return false;
  124. }
  125. return true;
  126. }
  127. bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  128. bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  129. Rect2i grow(int p_amount) const {
  130. Rect2i g = *this;
  131. g.position.x -= p_amount;
  132. g.position.y -= p_amount;
  133. g.size.width += p_amount * 2;
  134. g.size.height += p_amount * 2;
  135. return g;
  136. }
  137. inline Rect2i grow_side(Side p_side, int p_amount) const {
  138. Rect2i g = *this;
  139. g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
  140. (SIDE_TOP == p_side) ? p_amount : 0,
  141. (SIDE_RIGHT == p_side) ? p_amount : 0,
  142. (SIDE_BOTTOM == p_side) ? p_amount : 0);
  143. return g;
  144. }
  145. inline Rect2i grow_side_bind(uint32_t p_side, int p_amount) const {
  146. return grow_side(Side(p_side), p_amount);
  147. }
  148. inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const {
  149. Rect2i g = *this;
  150. g.position.x -= p_left;
  151. g.position.y -= p_top;
  152. g.size.width += p_left + p_right;
  153. g.size.height += p_top + p_bottom;
  154. return g;
  155. }
  156. _FORCE_INLINE_ Rect2i expand(const Vector2i &p_vector) const {
  157. Rect2i r = *this;
  158. r.expand_to(p_vector);
  159. return r;
  160. }
  161. inline void expand_to(const Point2i &p_vector) {
  162. #ifdef MATH_CHECKS
  163. if (unlikely(size.x < 0 || size.y < 0)) {
  164. ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
  165. }
  166. #endif
  167. Point2i begin = position;
  168. Point2i end = position + size;
  169. if (p_vector.x < begin.x) {
  170. begin.x = p_vector.x;
  171. }
  172. if (p_vector.y < begin.y) {
  173. begin.y = p_vector.y;
  174. }
  175. if (p_vector.x > end.x) {
  176. end.x = p_vector.x;
  177. }
  178. if (p_vector.y > end.y) {
  179. end.y = p_vector.y;
  180. }
  181. position = begin;
  182. size = end - begin;
  183. }
  184. _FORCE_INLINE_ Rect2i abs() const {
  185. return Rect2i(Point2i(position.x + Math::min(size.x, 0), position.y + Math::min(size.y, 0)), size.abs());
  186. }
  187. _FORCE_INLINE_ void set_end(const Vector2i &p_end) {
  188. size = p_end - position;
  189. }
  190. _FORCE_INLINE_ Vector2i get_end() const {
  191. return position + size;
  192. }
  193. operator String() const;
  194. operator Rect2() const;
  195. Rect2i() {}
  196. Rect2i(int p_x, int p_y, int p_width, int p_height) :
  197. position(Point2i(p_x, p_y)),
  198. size(Size2i(p_width, p_height)) {
  199. }
  200. Rect2i(const Point2i &p_pos, const Size2i &p_size) :
  201. position(p_pos),
  202. size(p_size) {
  203. }
  204. };
  205. } // namespace godot
  206. #endif // GODOT_RECT2I_HPP