rect2i.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*************************************************************************/
  2. /* rect2i.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/variant/rect2.hpp>
  33. #include <godot_cpp/variant/vector2i.hpp>
  34. namespace godot {
  35. class Rect2i {
  36. public:
  37. _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
  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. inline bool intersects(const Rect2i &p_rect) const {
  46. if (position.x > (p_rect.position.x + p_rect.size.width)) {
  47. return false;
  48. }
  49. if ((position.x + size.width) < p_rect.position.x) {
  50. return false;
  51. }
  52. if (position.y > (p_rect.position.y + p_rect.size.height)) {
  53. return false;
  54. }
  55. if ((position.y + size.height) < p_rect.position.y) {
  56. return false;
  57. }
  58. return true;
  59. }
  60. inline bool encloses(const Rect2i &p_rect) const {
  61. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  62. ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
  63. ((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
  64. }
  65. inline bool has_no_area() const {
  66. return (size.x <= 0 || size.y <= 0);
  67. }
  68. // Returns the instersection between two Rect2is or an empty Rect2i if there is no intersection
  69. inline Rect2i intersection(const Rect2i &p_rect) const {
  70. Rect2i new_rect = p_rect;
  71. if (!intersects(new_rect)) {
  72. return Rect2i();
  73. }
  74. new_rect.position.x = Math::max(p_rect.position.x, position.x);
  75. new_rect.position.y = Math::max(p_rect.position.y, position.y);
  76. Point2i p_rect_end = p_rect.position + p_rect.size;
  77. Point2i end = position + size;
  78. new_rect.size.x = (int)(Math::min(p_rect_end.x, end.x) - new_rect.position.x);
  79. new_rect.size.y = (int)(Math::min(p_rect_end.y, end.y) - new_rect.position.y);
  80. return new_rect;
  81. }
  82. inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
  83. Rect2i new_rect;
  84. new_rect.position.x = Math::min(p_rect.position.x, position.x);
  85. new_rect.position.y = Math::min(p_rect.position.y, position.y);
  86. new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x);
  87. new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y);
  88. new_rect.size = new_rect.size - new_rect.position; //make relative again
  89. return new_rect;
  90. }
  91. bool has_point(const Point2i &p_point) const {
  92. if (p_point.x < position.x) {
  93. return false;
  94. }
  95. if (p_point.y < position.y) {
  96. return false;
  97. }
  98. if (p_point.x >= (position.x + size.x)) {
  99. return false;
  100. }
  101. if (p_point.y >= (position.y + size.y)) {
  102. return false;
  103. }
  104. return true;
  105. }
  106. bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  107. bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  108. Rect2i grow(int p_amount) const {
  109. Rect2i g = *this;
  110. g.position.x -= p_amount;
  111. g.position.y -= p_amount;
  112. g.size.width += p_amount * 2;
  113. g.size.height += p_amount * 2;
  114. return g;
  115. }
  116. inline Rect2i grow_side(Side p_side, int p_amount) const {
  117. Rect2i g = *this;
  118. g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
  119. (SIDE_TOP == p_side) ? p_amount : 0,
  120. (SIDE_RIGHT == p_side) ? p_amount : 0,
  121. (SIDE_BOTTOM == p_side) ? p_amount : 0);
  122. return g;
  123. }
  124. inline Rect2i grow_side_bind(uint32_t p_side, int p_amount) const {
  125. return grow_side(Side(p_side), p_amount);
  126. }
  127. inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const {
  128. Rect2i g = *this;
  129. g.position.x -= p_left;
  130. g.position.y -= p_top;
  131. g.size.width += p_left + p_right;
  132. g.size.height += p_top + p_bottom;
  133. return g;
  134. }
  135. inline Rect2i expand(const Vector2i &p_vector) const {
  136. Rect2i r = *this;
  137. r.expand_to(p_vector);
  138. return r;
  139. }
  140. inline void expand_to(const Point2i &p_vector) {
  141. Point2i begin = position;
  142. Point2i end = position + size;
  143. if (p_vector.x < begin.x) {
  144. begin.x = p_vector.x;
  145. }
  146. if (p_vector.y < begin.y) {
  147. begin.y = p_vector.y;
  148. }
  149. if (p_vector.x > end.x) {
  150. end.x = p_vector.x;
  151. }
  152. if (p_vector.y > end.y) {
  153. end.y = p_vector.y;
  154. }
  155. position = begin;
  156. size = end - begin;
  157. }
  158. inline Rect2i abs() const {
  159. return Rect2i(Point2i(position.x + Math::min(size.x, 0), position.y + Math::min(size.y, 0)), size.abs());
  160. }
  161. inline void set_end(const Vector2i &p_end) {
  162. size = p_end - position;
  163. }
  164. inline Vector2i get_end() const {
  165. return position + size;
  166. }
  167. operator String() const { return String(position) + ", " + String(size); }
  168. operator Rect2() const { return Rect2(position, size); }
  169. Rect2i() {}
  170. Rect2i(const Rect2 &p_r2) :
  171. position(p_r2.position),
  172. size(p_r2.size) {
  173. }
  174. Rect2i(int p_x, int p_y, int p_width, int p_height) :
  175. position(Point2i(p_x, p_y)),
  176. size(Size2i(p_width, p_height)) {
  177. }
  178. Rect2i(const Point2i &p_pos, const Size2i &p_size) :
  179. position(p_pos),
  180. size(p_size) {
  181. }
  182. };
  183. } // namespace godot
  184. #endif // GODOT_RECT2I_HPP