Rect.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Rect.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "Point.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. /// Rectangle class, which have a position and a size
  14. class Rect
  15. {
  16. public:
  17. int x;
  18. int y;
  19. int w;
  20. int h;
  21. Rect()
  22. {
  23. x = y = w = h = -1;
  24. }
  25. Rect(int X, int Y, int W, int H)
  26. {
  27. x = X;
  28. y = Y;
  29. w = W;
  30. h = H;
  31. }
  32. Rect(const Point & position, const Point & size)
  33. {
  34. x = position.x;
  35. y = position.y;
  36. w = size.x;
  37. h = size.y;
  38. }
  39. Rect(const Rect& r) = default;
  40. DLL_LINKAGE static Rect createCentered( const Point & around, const Point & size );
  41. DLL_LINKAGE static Rect createCentered( const Rect & target, const Point & size );
  42. DLL_LINKAGE static Rect createAround(const Rect &r, int borderWidth);
  43. bool isInside(int qx, int qy) const
  44. {
  45. if (qx > x && qx<x+w && qy>y && qy<y+h)
  46. return true;
  47. return false;
  48. }
  49. bool isInside(const Point & q) const
  50. {
  51. return isInside(q.x,q.y);
  52. }
  53. int top() const
  54. {
  55. return y;
  56. }
  57. int bottom() const
  58. {
  59. return y+h;
  60. }
  61. int left() const
  62. {
  63. return x;
  64. }
  65. int right() const
  66. {
  67. return x+w;
  68. }
  69. Point topLeft() const
  70. {
  71. return Point(x,y);
  72. }
  73. Point topRight() const
  74. {
  75. return Point(x+w,y);
  76. }
  77. Point bottomLeft() const
  78. {
  79. return Point(x,y+h);
  80. }
  81. Point bottomRight() const
  82. {
  83. return Point(x+w,y+h);
  84. }
  85. Point center() const
  86. {
  87. return Point(x+w/2,y+h/2);
  88. }
  89. Point dimensions() const
  90. {
  91. return Point(w,h);
  92. }
  93. void moveTo(const Point & dest)
  94. {
  95. x = dest.x;
  96. y = dest.y;
  97. }
  98. Rect operator+(const Point &p) const
  99. {
  100. return Rect(x+p.x,y+p.y,w,h);
  101. }
  102. Rect& operator=(const Rect &p)
  103. {
  104. x = p.x;
  105. y = p.y;
  106. w = p.w;
  107. h = p.h;
  108. return *this;
  109. }
  110. Rect& operator+=(const Point &p)
  111. {
  112. x += p.x;
  113. y += p.y;
  114. return *this;
  115. }
  116. Rect& operator-=(const Point &p)
  117. {
  118. x -= p.x;
  119. y -= p.y;
  120. return *this;
  121. }
  122. /// returns true if this rect intersects with another rect
  123. DLL_LINKAGE bool intersectionTest(const Rect & other) const;
  124. /// returns true if this rect intersects with line specified by two points
  125. DLL_LINKAGE bool intersectionTest(const Point & line1, const Point & line2) const;
  126. /// Returns rect that represents intersection of two rects
  127. DLL_LINKAGE Rect intersect(const Rect & other) const;
  128. /// Returns rect union - rect that covers both this rect and provided rect
  129. DLL_LINKAGE Rect include(const Rect & other) const;
  130. template <typename Handler>
  131. void serialize(Handler &h, const int version)
  132. {
  133. h & x;
  134. h & y;
  135. h & w;
  136. h & h;
  137. }
  138. };
  139. VCMI_LIB_NAMESPACE_END