pm_rect.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * File : pm_rect.h
  3. * COPYRIGHT (C) 2012-2017, Shanghai Real-Thread Technology Co., Ltd
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2017-11-05 realthread the first version
  8. */
  9. #pragma once
  10. #include <rtgui/rtgui.h>
  11. #include <rtgui/region.h>
  12. namespace Persimmon
  13. {
  14. class Rect;
  15. class Point
  16. {
  17. public:
  18. Point()
  19. {
  20. xp = 0;
  21. yp = 0;
  22. }
  23. Point(int xpos, int ypos);
  24. void move(int x, int y);
  25. void moveTo(int x, int y);
  26. inline bool isNull() const
  27. {
  28. return (xp == 0 && yp == 0);
  29. }
  30. inline int x() const
  31. {
  32. return xp;
  33. }
  34. inline int y() const
  35. {
  36. return yp;
  37. }
  38. inline void set(int x, int y)
  39. {
  40. xp = x;
  41. yp = y;
  42. }
  43. inline void setX(int x)
  44. {
  45. xp = x;
  46. }
  47. inline void setY(int y)
  48. {
  49. yp = y;
  50. }
  51. inline Point &operator+=(const Point &p);
  52. inline Point &operator-=(const Point &p);
  53. friend inline const Point operator+(const Point &, const Point &);
  54. friend inline const Point operator-(const Point &, const Point &);
  55. struct rtgui_point getPoint(void)
  56. {
  57. struct rtgui_point p;
  58. p.x = xp;
  59. p.y = yp;
  60. return p;
  61. }
  62. private:
  63. int xp;
  64. int yp;
  65. };
  66. inline Point &Point::operator+=(const Point &p)
  67. {
  68. xp+=p.xp;
  69. yp+=p.yp;
  70. return *this;
  71. }
  72. inline Point &Point::operator-=(const Point &p)
  73. {
  74. xp-=p.xp;
  75. yp-=p.yp;
  76. return *this;
  77. }
  78. inline const Point operator+(const Point &p1, const Point &p2)
  79. {
  80. return Point(p1.xp+p2.xp, p1.yp+p2.yp);
  81. }
  82. inline const Point operator-(const Point &p1, const Point &p2)
  83. {
  84. return Point(p1.xp-p2.xp, p1.yp-p2.yp);
  85. }
  86. class Size
  87. {
  88. public:
  89. Size(int width, int height);
  90. Size(const Rect &rect);
  91. int getWidth() const
  92. {
  93. return width;
  94. }
  95. int getHeight() const
  96. {
  97. return height;
  98. }
  99. private:
  100. rt_uint16_t width;
  101. rt_uint16_t height;
  102. };
  103. class Rect
  104. {
  105. public:
  106. Rect()
  107. {
  108. rect.x1 = rect.x2 = 0;
  109. rect.y1 = rect.y2 = 0;
  110. }
  111. Rect(const struct rtgui_rect *r);
  112. Rect(int x, int y, int width, int height);
  113. Rect(const Point &point, const Size &size);
  114. bool isNull () const
  115. {
  116. return (rtgui_rect_is_empty(&rect) == RT_TRUE);
  117. }
  118. void set(int x, int y, int width, int height);
  119. int getWidth() const;
  120. Rect* setWidth(int width);
  121. int getHeight() const;
  122. Rect* setHeight(int height);
  123. Size getSize() const;
  124. void setSize(const Size& size);
  125. void moveTo(int x, int y);
  126. void moveTo(const Point &point);
  127. void move(int deltaX, int deltaY);
  128. void move(const Point &deltaPoint);
  129. int left() const;
  130. int top() const;
  131. int right() const;
  132. int bottom() const;
  133. int x() const
  134. {
  135. return rect.x1;
  136. }
  137. Rect* setX(int x)
  138. {
  139. rect.x1 = x;
  140. return this;
  141. }
  142. int y() const
  143. {
  144. return rect.y1;
  145. }
  146. Rect* setY(int y)
  147. {
  148. rect.y1 = y;
  149. return this;
  150. }
  151. bool contain(int x, int y);
  152. struct rtgui_rect* getRect(void)
  153. {
  154. return &rect;
  155. }
  156. void getRect(struct rtgui_rect* r)
  157. {
  158. if (r) *r = rect;
  159. }
  160. private:
  161. struct rtgui_rect rect;
  162. };
  163. inline const Rect operator+(const Rect& rect,
  164. const Point& pt)
  165. {
  166. Rect rt(rect);
  167. rt.move(pt);
  168. return rt;
  169. }
  170. inline const Rect operator-(const Rect& rect,
  171. const Point& pt)
  172. {
  173. Rect rt(rect);
  174. rt.move(-pt.x(), -pt.y());
  175. return rt;
  176. }
  177. inline int Rect::left() const
  178. {
  179. return rect.x1;
  180. }
  181. inline int Rect::top() const
  182. {
  183. return rect.y1;
  184. }
  185. inline int Rect::right() const
  186. {
  187. return rect.x2;
  188. }
  189. inline int Rect::bottom() const
  190. {
  191. return rect.y2;
  192. }
  193. }