Geometries.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Geometries.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 <SDL2/SDL_rect.h>
  12. enum class ETextAlignment {TOPLEFT, CENTER, BOTTOMRIGHT};
  13. struct SDL_MouseMotionEvent;
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class int3;
  16. VCMI_LIB_NAMESPACE_END
  17. // A point with x/y coordinate, used mostly for graphic rendering
  18. struct Point
  19. {
  20. int x, y;
  21. //constructors
  22. Point()
  23. {
  24. x = y = 0;
  25. };
  26. Point(int X, int Y)
  27. :x(X),y(Y)
  28. {};
  29. Point(const int3 &a);
  30. explicit Point(const SDL_MouseMotionEvent &a);
  31. template<typename T>
  32. Point operator+(const T &b) const
  33. {
  34. return Point(x+b.x,y+b.y);
  35. }
  36. template<typename T>
  37. Point operator/(const T &div) const
  38. {
  39. return Point(x/div, y/div);
  40. }
  41. template<typename T>
  42. Point operator*(const T &mul) const
  43. {
  44. return Point(x*mul, y*mul);
  45. }
  46. template<typename T>
  47. Point& operator+=(const T &b)
  48. {
  49. x += b.x;
  50. y += b.y;
  51. return *this;
  52. }
  53. template<typename T>
  54. Point operator-(const T &b) const
  55. {
  56. return Point(x - b.x, y - b.y);
  57. }
  58. template<typename T>
  59. Point& operator-=(const T &b)
  60. {
  61. x -= b.x;
  62. y -= b.y;
  63. return *this;
  64. }
  65. template<typename T> Point& operator=(const T &t)
  66. {
  67. x = t.x;
  68. y = t.y;
  69. return *this;
  70. }
  71. template<typename T> bool operator==(const T &t) const
  72. {
  73. return x == t.x && y == t.y;
  74. }
  75. template<typename T> bool operator!=(const T &t) const
  76. {
  77. return !(*this == t);
  78. }
  79. };
  80. /// Rectangle class, which have a position and a size
  81. struct Rect : public SDL_Rect
  82. {
  83. Rect()
  84. {
  85. x = y = w = h = -1;
  86. }
  87. Rect(int X, int Y, int W, int H)
  88. {
  89. x = X;
  90. y = Y;
  91. w = W;
  92. h = H;
  93. }
  94. Rect(const Point & position, const Point & size)
  95. {
  96. x = position.x;
  97. y = position.y;
  98. w = size.x;
  99. h = size.y;
  100. }
  101. Rect(const SDL_Rect & r)
  102. {
  103. x = r.x;
  104. y = r.y;
  105. w = r.w;
  106. h = r.h;
  107. }
  108. Rect(const Rect& r) = default;
  109. Rect centerIn(const Rect &r);
  110. static Rect createCentered(int w, int h);
  111. static Rect around(const Rect &r, int width = 1);
  112. bool isIn(int qx, int qy) const
  113. {
  114. if (qx > x && qx<x+w && qy>y && qy<y+h)
  115. return true;
  116. return false;
  117. }
  118. bool isIn(const Point & q) const
  119. {
  120. return isIn(q.x,q.y);
  121. }
  122. Point topLeft() const
  123. {
  124. return Point(x,y);
  125. }
  126. Point topRight() const
  127. {
  128. return Point(x+w,y);
  129. }
  130. Point bottomLeft() const
  131. {
  132. return Point(x,y+h);
  133. }
  134. Point bottomRight() const
  135. {
  136. return Point(x+w,y+h);
  137. }
  138. Point center() const
  139. {
  140. return Point(x+w/2,y+h/2);
  141. }
  142. Point dimensions() const
  143. {
  144. return Point(w,h);
  145. }
  146. void moveTo(const Point & dest)
  147. {
  148. x = dest.x;
  149. y = dest.y;
  150. }
  151. Rect operator+(const Point &p) const
  152. {
  153. return Rect(x+p.x,y+p.y,w,h);
  154. }
  155. Rect& operator=(const Rect &p)
  156. {
  157. x = p.x;
  158. y = p.y;
  159. w = p.w;
  160. h = p.h;
  161. return *this;
  162. }
  163. Rect& operator+=(const Point &p)
  164. {
  165. x += p.x;
  166. y += p.y;
  167. return *this;
  168. }
  169. Rect& operator-=(const Point &p)
  170. {
  171. x -= p.x;
  172. y -= p.y;
  173. return *this;
  174. }
  175. Rect operator&(const Rect &p) const //rect intersection
  176. {
  177. bool intersect = true;
  178. if(p.topLeft().y < y && p.bottomLeft().y < y) //rect p is above *this
  179. {
  180. intersect = false;
  181. }
  182. else if(p.topLeft().y > y+h && p.bottomLeft().y > y+h) //rect p is below *this
  183. {
  184. intersect = false;
  185. }
  186. else if(p.topLeft().x > x+w && p.topRight().x > x+w) //rect p is on the right hand side of this
  187. {
  188. intersect = false;
  189. }
  190. else if(p.topLeft().x < x && p.topRight().x < x) //rect p is on the left hand side of this
  191. {
  192. intersect = false;
  193. }
  194. if(intersect)
  195. {
  196. Rect ret;
  197. ret.x = std::max(this->x, p.x);
  198. ret.y = std::max(this->y, p.y);
  199. Point bR; //bottomRight point of returned rect
  200. bR.x = std::min(this->w+this->x, p.w+p.x);
  201. bR.y = std::min(this->h+this->y, p.h+p.y);
  202. ret.w = bR.x - ret.x;
  203. ret.h = bR.y - ret.y;
  204. return ret;
  205. }
  206. else
  207. {
  208. return Rect();
  209. }
  210. }
  211. Rect operator|(const Rect &p) const //union of two rects
  212. {
  213. Rect ret;
  214. ret.x = std::min(p.x, this->x);
  215. ret.y = std::min(p.y, this->y);
  216. int x2 = std::max(p.x+p.w, this->x+this->w);
  217. int y2 = std::max(p.y+p.h, this->y+this->h);
  218. ret.w = x2 -ret.x;
  219. ret.h = y2 -ret.y;
  220. return ret;
  221. }
  222. };