GHOST_Rect.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software Foundation,
  14. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  17. * All rights reserved.
  18. */
  19. /** \file
  20. * \ingroup GHOST
  21. * Macro's used in GHOST debug target.
  22. */
  23. #ifndef __GHOST_RECT_H__
  24. #define __GHOST_RECT_H__
  25. #include "GHOST_Types.h"
  26. /**
  27. * Implements rectangle functionality.
  28. * The four extreme coordinates are stored as left, top, right and bottom.
  29. * To be valid, a rectangle should have a left coordinate smaller than or equal to right.
  30. * To be valid, a rectangle should have a top coordinate smaller than or equal to bottom.
  31. */
  32. class GHOST_Rect {
  33. public:
  34. /**
  35. * Constructs a rectangle with the given values.
  36. * \param l requested left coordinate of the rectangle
  37. * \param t requested top coordinate of the rectangle
  38. * \param r requested right coordinate of the rectangle
  39. * \param b requested bottom coordinate of the rectangle
  40. */
  41. GHOST_Rect(GHOST_TInt32 l = 0, GHOST_TInt32 t = 0, GHOST_TInt32 r = 0, GHOST_TInt32 b = 0)
  42. : m_l(l), m_t(t), m_r(r), m_b(b)
  43. {
  44. }
  45. /**
  46. * Copy constructor.
  47. * \param r rectangle to copy
  48. */
  49. GHOST_Rect(const GHOST_Rect &r) : m_l(r.m_l), m_t(r.m_t), m_r(r.m_r), m_b(r.m_b)
  50. {
  51. }
  52. /**
  53. * Destructor.
  54. */
  55. virtual ~GHOST_Rect()
  56. {
  57. }
  58. /**
  59. * Access to rectangle width.
  60. * \return width of the rectangle
  61. */
  62. virtual inline GHOST_TInt32 getWidth() const;
  63. /**
  64. * Access to rectangle height.
  65. * \return height of the rectangle
  66. */
  67. virtual inline GHOST_TInt32 getHeight() const;
  68. /**
  69. * Sets all members of the rectangle.
  70. * \param l: requested left coordinate of the rectangle.
  71. * \param t: requested top coordinate of the rectangle.
  72. * \param r: requested right coordinate of the rectangle.
  73. * \param b: requested bottom coordinate of the rectangle.
  74. */
  75. virtual inline void set(GHOST_TInt32 l, GHOST_TInt32 t, GHOST_TInt32 r, GHOST_TInt32 b);
  76. /**
  77. * Returns whether this rectangle is empty.
  78. * Empty rectangles are rectangles that have width==0 and/or height==0.
  79. * \return boolean value (true==empty rectangle)
  80. */
  81. virtual inline bool isEmpty() const;
  82. /**
  83. * Returns whether this rectangle is valid.
  84. * Valid rectangles are rectangles that have m_l <= m_r and m_t <= m_b.
  85. * Thus, empty rectangles are valid.
  86. * \return boolean value (true==valid rectangle)
  87. */
  88. virtual inline bool isValid() const;
  89. /**
  90. * Grows (or shrinks the rectangle).
  91. * The method avoids negative insets making the rectangle invalid
  92. * \param i: The amount of offset given to each extreme (negative values shrink the rectangle).
  93. */
  94. virtual void inset(GHOST_TInt32 i);
  95. /**
  96. * Does a union of the rectangle given and this rectangle.
  97. * The result is stored in this rectangle.
  98. * \param r: The rectangle that is input for the union operation.
  99. */
  100. virtual inline void unionRect(const GHOST_Rect &r);
  101. /**
  102. * Grows the rectangle to included a point.
  103. * \param x: The x-coordinate of the point.
  104. * \param y: The y-coordinate of the point.
  105. */
  106. virtual inline void unionPoint(GHOST_TInt32 x, GHOST_TInt32 y);
  107. /**
  108. * Grows the rectangle to included a point.
  109. * \param x The x-coordinate of the point.
  110. * \param y The y-coordinate of the point.
  111. */
  112. virtual inline void wrapPoint(GHOST_TInt32 &x,
  113. GHOST_TInt32 &y,
  114. GHOST_TInt32 ofs,
  115. GHOST_TAxisFlag axis);
  116. /**
  117. * Returns whether the point is inside this rectangle.
  118. * Point on the boundary is considered inside.
  119. * \param x x-coordinate of point to test.
  120. * \param y y-coordinate of point to test.
  121. * \return boolean value (true if point is inside).
  122. */
  123. virtual inline bool isInside(GHOST_TInt32 x, GHOST_TInt32 y) const;
  124. /**
  125. * Returns whether the rectangle is inside this rectangle.
  126. * \param r rectangle to test.
  127. * \return visibility (not, partially or fully visible).
  128. */
  129. virtual GHOST_TVisibility getVisibility(GHOST_Rect &r) const;
  130. /**
  131. * Sets rectangle members.
  132. * Sets rectangle members such that it is centered at the given location.
  133. * \param cx requested center x-coordinate of the rectangle
  134. * \param cy requested center y-coordinate of the rectangle
  135. */
  136. virtual void setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy);
  137. /**
  138. * Sets rectangle members.
  139. * Sets rectangle members such that it is centered at the given location,
  140. * with the width requested.
  141. * \param cx requested center x-coordinate of the rectangle
  142. * \param cy requested center y-coordinate of the rectangle
  143. * \param w requested width of the rectangle
  144. * \param h requested height of the rectangle
  145. */
  146. virtual void setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy, GHOST_TInt32 w, GHOST_TInt32 h);
  147. /**
  148. * Clips a rectangle.
  149. * Updates the rectangle given such that it will fit within this one.
  150. * This can result in an empty rectangle.
  151. * \param r the rectangle to clip
  152. * \return whether clipping has occurred
  153. */
  154. virtual bool clip(GHOST_Rect &r) const;
  155. /** Left coordinate of the rectangle */
  156. GHOST_TInt32 m_l;
  157. /** Top coordinate of the rectangle */
  158. GHOST_TInt32 m_t;
  159. /** Right coordinate of the rectangle */
  160. GHOST_TInt32 m_r;
  161. /** Bottom coordinate of the rectangle */
  162. GHOST_TInt32 m_b;
  163. #ifdef WITH_CXX_GUARDEDALLOC
  164. MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_Rect")
  165. #endif
  166. };
  167. inline GHOST_TInt32 GHOST_Rect::getWidth() const
  168. {
  169. return m_r - m_l;
  170. }
  171. inline GHOST_TInt32 GHOST_Rect::getHeight() const
  172. {
  173. return m_b - m_t;
  174. }
  175. inline void GHOST_Rect::set(GHOST_TInt32 l, GHOST_TInt32 t, GHOST_TInt32 r, GHOST_TInt32 b)
  176. {
  177. m_l = l;
  178. m_t = t;
  179. m_r = r;
  180. m_b = b;
  181. }
  182. inline bool GHOST_Rect::isEmpty() const
  183. {
  184. return (getWidth() == 0) || (getHeight() == 0);
  185. }
  186. inline bool GHOST_Rect::isValid() const
  187. {
  188. return (m_l <= m_r) && (m_t <= m_b);
  189. }
  190. inline void GHOST_Rect::unionRect(const GHOST_Rect &r)
  191. {
  192. if (r.m_l < m_l)
  193. m_l = r.m_l;
  194. if (r.m_r > m_r)
  195. m_r = r.m_r;
  196. if (r.m_t < m_t)
  197. m_t = r.m_t;
  198. if (r.m_b > m_b)
  199. m_b = r.m_b;
  200. }
  201. inline void GHOST_Rect::unionPoint(GHOST_TInt32 x, GHOST_TInt32 y)
  202. {
  203. if (x < m_l)
  204. m_l = x;
  205. if (x > m_r)
  206. m_r = x;
  207. if (y < m_t)
  208. m_t = y;
  209. if (y > m_b)
  210. m_b = y;
  211. }
  212. inline void GHOST_Rect::wrapPoint(GHOST_TInt32 &x,
  213. GHOST_TInt32 &y,
  214. GHOST_TInt32 ofs,
  215. GHOST_TAxisFlag axis)
  216. {
  217. GHOST_TInt32 w = getWidth();
  218. GHOST_TInt32 h = getHeight();
  219. /* highly unlikely but avoid eternal loop */
  220. if (w - ofs * 2 <= 0 || h - ofs * 2 <= 0) {
  221. return;
  222. }
  223. if (axis & GHOST_kAxisX) {
  224. while (x - ofs < m_l)
  225. x += w - (ofs * 2);
  226. while (x + ofs > m_r)
  227. x -= w - (ofs * 2);
  228. }
  229. if (axis & GHOST_kGrabAxisY) {
  230. while (y - ofs < m_t)
  231. y += h - (ofs * 2);
  232. while (y + ofs > m_b)
  233. y -= h - (ofs * 2);
  234. }
  235. }
  236. inline bool GHOST_Rect::isInside(GHOST_TInt32 x, GHOST_TInt32 y) const
  237. {
  238. return (x >= m_l) && (x <= m_r) && (y >= m_t) && (y <= m_b);
  239. }
  240. #endif // __GHOST_RECT_H__