rect2i.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef GODOT_RECT2I_HPP
  2. #define GODOT_RECT2I_HPP
  3. #include <godot_cpp/variant/rect2.hpp>
  4. #include <godot_cpp/variant/vector2i.hpp>
  5. namespace godot {
  6. class Rect2i {
  7. public:
  8. _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
  9. Point2i position;
  10. Size2i size;
  11. const Point2i &get_position() const { return position; }
  12. void set_position(const Point2i &p_position) { position = p_position; }
  13. const Size2i &get_size() const { return size; }
  14. void set_size(const Size2i &p_size) { size = p_size; }
  15. int get_area() const { return size.width * size.height; }
  16. inline bool intersects(const Rect2i &p_rect) const {
  17. if (position.x > (p_rect.position.x + p_rect.size.width)) {
  18. return false;
  19. }
  20. if ((position.x + size.width) < p_rect.position.x) {
  21. return false;
  22. }
  23. if (position.y > (p_rect.position.y + p_rect.size.height)) {
  24. return false;
  25. }
  26. if ((position.y + size.height) < p_rect.position.y) {
  27. return false;
  28. }
  29. return true;
  30. }
  31. inline bool encloses(const Rect2i &p_rect) const {
  32. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  33. ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
  34. ((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
  35. }
  36. inline bool has_no_area() const {
  37. return (size.x <= 0 || size.y <= 0);
  38. }
  39. // Returns the instersection between two Rect2is or an empty Rect2i if there is no intersection
  40. inline Rect2i intersection(const Rect2i &p_rect) const {
  41. Rect2i new_rect = p_rect;
  42. if (!intersects(new_rect)) {
  43. return Rect2i();
  44. }
  45. new_rect.position.x = Math::max(p_rect.position.x, position.x);
  46. new_rect.position.y = Math::max(p_rect.position.y, position.y);
  47. Point2i p_rect_end = p_rect.position + p_rect.size;
  48. Point2i end = position + size;
  49. new_rect.size.x = (int)(Math::min(p_rect_end.x, end.x) - new_rect.position.x);
  50. new_rect.size.y = (int)(Math::min(p_rect_end.y, end.y) - new_rect.position.y);
  51. return new_rect;
  52. }
  53. inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
  54. Rect2i new_rect;
  55. new_rect.position.x = Math::min(p_rect.position.x, position.x);
  56. new_rect.position.y = Math::min(p_rect.position.y, position.y);
  57. new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x);
  58. new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y);
  59. new_rect.size = new_rect.size - new_rect.position; //make relative again
  60. return new_rect;
  61. }
  62. bool has_point(const Point2i &p_point) const {
  63. if (p_point.x < position.x) {
  64. return false;
  65. }
  66. if (p_point.y < position.y) {
  67. return false;
  68. }
  69. if (p_point.x >= (position.x + size.x)) {
  70. return false;
  71. }
  72. if (p_point.y >= (position.y + size.y)) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  78. bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  79. Rect2i grow(int p_amount) const {
  80. Rect2i g = *this;
  81. g.position.x -= p_amount;
  82. g.position.y -= p_amount;
  83. g.size.width += p_amount * 2;
  84. g.size.height += p_amount * 2;
  85. return g;
  86. }
  87. inline Rect2i grow_side(Side p_side, int p_amount) const {
  88. Rect2i g = *this;
  89. g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
  90. (SIDE_TOP == p_side) ? p_amount : 0,
  91. (SIDE_RIGHT == p_side) ? p_amount : 0,
  92. (SIDE_BOTTOM == p_side) ? p_amount : 0);
  93. return g;
  94. }
  95. inline Rect2i grow_side_bind(uint32_t p_side, int p_amount) const {
  96. return grow_side(Side(p_side), p_amount);
  97. }
  98. inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const {
  99. Rect2i g = *this;
  100. g.position.x -= p_left;
  101. g.position.y -= p_top;
  102. g.size.width += p_left + p_right;
  103. g.size.height += p_top + p_bottom;
  104. return g;
  105. }
  106. inline Rect2i expand(const Vector2i &p_vector) const {
  107. Rect2i r = *this;
  108. r.expand_to(p_vector);
  109. return r;
  110. }
  111. inline void expand_to(const Point2i &p_vector) {
  112. Point2i begin = position;
  113. Point2i end = position + size;
  114. if (p_vector.x < begin.x) {
  115. begin.x = p_vector.x;
  116. }
  117. if (p_vector.y < begin.y) {
  118. begin.y = p_vector.y;
  119. }
  120. if (p_vector.x > end.x) {
  121. end.x = p_vector.x;
  122. }
  123. if (p_vector.y > end.y) {
  124. end.y = p_vector.y;
  125. }
  126. position = begin;
  127. size = end - begin;
  128. }
  129. inline Rect2i abs() const {
  130. return Rect2i(Point2i(position.x + Math::min(size.x, 0), position.y + Math::min(size.y, 0)), size.abs());
  131. }
  132. inline void set_end(const Vector2i &p_end) {
  133. size = p_end - position;
  134. }
  135. inline Vector2i get_end() const {
  136. return position + size;
  137. }
  138. operator String() const { return String(position) + ", " + String(size); }
  139. operator Rect2() const { return Rect2(position, size); }
  140. Rect2i() {}
  141. Rect2i(const Rect2 &p_r2) :
  142. position(p_r2.position),
  143. size(p_r2.size) {
  144. }
  145. Rect2i(int p_x, int p_y, int p_width, int p_height) :
  146. position(Point2i(p_x, p_y)),
  147. size(Size2i(p_width, p_height)) {
  148. }
  149. Rect2i(const Point2i &p_pos, const Size2i &p_size) :
  150. position(p_pos),
  151. size(p_size) {
  152. }
  153. };
  154. } // namespace godot
  155. #endif // GODOT_RECT2I_HPP