home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / XenonSource.exe / gamesystem / includes / gs_rect.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-09  |  7.6 KB  |  348 lines

  1. //-------------------------------------------------------------
  2. //
  3. // Class:    gsCRect
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    12/03/00
  8. //
  9. // Base:    gsCObject
  10. //
  11. // Derived:    None
  12. //
  13. //-------------------------------------------------------------
  14.  
  15. #ifndef _INCLUDE_GS_RECT_H
  16. #define _INCLUDE_GS_RECT_H
  17.  
  18. #include "gs_point.h"
  19.  
  20. //-------------------------------------------------------------
  21. // gsCRect
  22.  
  23. class gsCRect : public gsCObject
  24. {
  25.     private:
  26.         RECT m_rect;
  27.  
  28.     public:
  29.         gsCRect();
  30.         gsCRect(int left,int top,int right,int bottom);
  31.         gsCRect(const gsCPoint& a,const gsCPoint& b);
  32.         gsCRect(const gsCRect& r);
  33.         gsCRect(const RECT& r);
  34.         ~gsCRect();
  35.  
  36.         int getLeft() const;
  37.         int getTop() const;
  38.         int getRight() const;
  39.         int getBottom() const;
  40.  
  41.         void setLeft(int l);
  42.         void setTop(int t);
  43.         void setRight(int r);
  44.         void setBottom(int b);
  45.  
  46.         gsCPoint getTopLeft() const;
  47.         gsCPoint getBottomRight() const;
  48.         gsCPoint getTopRight() const;
  49.         gsCPoint getBottomLeft() const;
  50.  
  51.         void setTopLeft(const gsCPoint& p);
  52.         void setBottomRight(const gsCPoint& p);
  53.  
  54.         gsCPoint getSize() const;
  55.         int getWidth() const;
  56.         int getHeight() const;
  57.  
  58.         bool contains(const gsCPoint& point);
  59.         bool contains(const gsCRect& rect);
  60.         bool overlaps(const gsCRect& rect);
  61.         void clip(gsCRect& rect);
  62.         void clip(gsCRect& source,gsCRect& dest);
  63.         bool isEmpty() const;
  64.  
  65.         void move(const gsCPoint& offset);
  66.  
  67.         const gsCRect& operator = (const gsCRect& a);
  68.  
  69.         operator RECT() const;
  70.         operator LPRECT();
  71.  
  72.         void expand(int amount);
  73. };
  74.  
  75. //-------------------------------------------------------------
  76.  
  77. inline gsCRect::gsCRect()
  78. {
  79.     m_rect.left = 0;
  80.     m_rect.top = 0;
  81.     m_rect.right = 0;
  82.     m_rect.bottom = 0;
  83. }
  84.  
  85. //-------------------------------------------------------------
  86.  
  87. inline gsCRect::gsCRect(int left,int top,int right,int bottom)
  88. {
  89.     m_rect.left = left;
  90.     m_rect.top = top;
  91.     m_rect.right = right;
  92.     m_rect.bottom = bottom;
  93. }
  94.  
  95. //-------------------------------------------------------------
  96.  
  97. inline gsCRect::gsCRect(const gsCPoint& a,const gsCPoint& b)
  98. {
  99.     m_rect.left = a.getX();
  100.     m_rect.top = a.getY();
  101.     m_rect.right = b.getX();
  102.     m_rect.bottom = b.getY();
  103. }
  104.  
  105. //-------------------------------------------------------------
  106.  
  107. inline gsCRect::gsCRect(const gsCRect& r)
  108. {
  109.     m_rect.left = r.m_rect.left;
  110.     m_rect.top = r.m_rect.top;
  111.     m_rect.right = r.m_rect.right;
  112.     m_rect.bottom = r.m_rect.bottom;
  113. }
  114.  
  115. //-------------------------------------------------------------
  116.  
  117. inline gsCRect::gsCRect(const RECT& r)
  118. {
  119.     m_rect = r;
  120. }
  121.  
  122. //-------------------------------------------------------------
  123.  
  124. inline int gsCRect::getLeft() const
  125. {
  126.     return m_rect.left;
  127. }
  128.  
  129. //-------------------------------------------------------------
  130.  
  131. inline int gsCRect::getTop() const
  132. {
  133.     return m_rect.top;
  134. }
  135.  
  136. //-------------------------------------------------------------
  137.  
  138. inline int gsCRect::getRight() const
  139. {
  140.     return m_rect.right;
  141. }
  142.  
  143. //-------------------------------------------------------------
  144.  
  145. inline int gsCRect::getBottom() const
  146. {
  147.     return m_rect.bottom;
  148. }
  149.  
  150. //-------------------------------------------------------------
  151.  
  152. inline void gsCRect::setLeft(int l)
  153. {
  154.     m_rect.left = l;
  155. }
  156.  
  157. //-------------------------------------------------------------
  158.  
  159. inline void gsCRect::setTop(int t)
  160. {
  161.     m_rect.top = t;
  162. }
  163.  
  164. //-------------------------------------------------------------
  165.  
  166. inline void gsCRect::setRight(int r)
  167. {
  168.     m_rect.right = r;
  169. }
  170.  
  171. //-------------------------------------------------------------
  172.  
  173. inline void gsCRect::setBottom(int b)
  174. {
  175.     m_rect.bottom = b;
  176. }
  177.  
  178. //-------------------------------------------------------------
  179.  
  180. inline gsCPoint gsCRect::getTopLeft() const
  181. {
  182.     return gsCPoint(m_rect.left,m_rect.top);
  183. }
  184.  
  185. //-------------------------------------------------------------
  186.  
  187. inline gsCPoint gsCRect::getBottomRight() const
  188. {
  189.     return gsCPoint(m_rect.right,m_rect.bottom);
  190. }
  191.  
  192. //-------------------------------------------------------------
  193.  
  194. inline gsCPoint gsCRect::getTopRight() const
  195. {
  196.     return gsCPoint(m_rect.right,m_rect.top);
  197. }
  198.  
  199. //-------------------------------------------------------------
  200.  
  201. inline gsCPoint gsCRect::getBottomLeft() const
  202. {
  203.     return gsCPoint(m_rect.left,m_rect.bottom);
  204. }
  205.  
  206. //-------------------------------------------------------------
  207.  
  208. inline void gsCRect::setTopLeft(const gsCPoint& p)
  209. {
  210.     m_rect.left = p.getX();
  211.     m_rect.top = p.getY();
  212. }
  213.  
  214. //-------------------------------------------------------------
  215.  
  216. inline void gsCRect::setBottomRight(const gsCPoint& p)
  217. {
  218.     m_rect.right = p.getX();
  219.     m_rect.bottom = p.getY();
  220. }
  221.  
  222. //-------------------------------------------------------------
  223.  
  224. inline gsCRect::~gsCRect()
  225. {
  226. }
  227.  
  228. //-------------------------------------------------------------
  229.  
  230. inline gsCPoint gsCRect::getSize() const
  231. {
  232.     return gsCPoint(m_rect.right - m_rect.left,
  233.                     m_rect.bottom - m_rect.top);
  234. }
  235.  
  236. //-------------------------------------------------------------
  237.  
  238. inline int gsCRect::getWidth() const
  239. {
  240.     return m_rect.right - m_rect.left;
  241. }
  242.  
  243. //-------------------------------------------------------------
  244.  
  245. inline int gsCRect::getHeight() const
  246. {
  247.     return m_rect.bottom - m_rect.top;
  248. }
  249.  
  250. //-------------------------------------------------------------
  251.  
  252. inline bool gsCRect::isEmpty() const
  253. {
  254.     return (m_rect.left >= m_rect.right) || (m_rect.top >= m_rect.bottom);
  255. }
  256.  
  257. //-------------------------------------------------------------
  258.  
  259. inline const gsCRect& gsCRect::operator = (const gsCRect& a)
  260. {
  261.     memcpy(&m_rect,&a.m_rect,sizeof(m_rect));
  262.     return *this;
  263. }
  264.  
  265. //-------------------------------------------------------------
  266.  
  267. inline gsCRect::operator RECT() const
  268. {
  269.     return m_rect;
  270. }
  271.  
  272. //-------------------------------------------------------------
  273.  
  274. inline gsCRect::operator LPRECT()
  275. {
  276.     return &m_rect;
  277. }
  278.  
  279. //-------------------------------------------------------------
  280.  
  281. inline void gsCRect::move(const gsCPoint& offset)
  282. {
  283.     m_rect.left += offset.getX();
  284.     m_rect.top += offset.getY();
  285.     m_rect.right += offset.getX();
  286.     m_rect.bottom += offset.getY();
  287. }
  288.  
  289. //-------------------------------------------------------------
  290.  
  291. inline bool gsCRect::contains(const gsCPoint& point)
  292. {
  293.     return (point.getX() >= m_rect.left &&
  294.             point.getX() < m_rect.right &&
  295.             point.getY() >= m_rect.top &&
  296.             point.getY() < m_rect.bottom);
  297. }
  298.  
  299. //-------------------------------------------------------------
  300.  
  301. inline bool gsCRect::contains(const gsCRect& rect)
  302. {
  303.     return (rect.m_rect.left >= m_rect.left &&
  304.             rect.m_rect.right <= m_rect.right &&
  305.             rect.m_rect.top >= m_rect.top &&
  306.             rect.m_rect.bottom <= m_rect.bottom);
  307. }
  308.  
  309. //-------------------------------------------------------------
  310.  
  311. inline bool gsCRect::overlaps(const gsCRect& rect)
  312. {
  313.     return (m_rect.left < rect.m_rect.right &&
  314.             m_rect.right > rect.m_rect.left &&
  315.             m_rect.top < rect.m_rect.bottom &&
  316.             m_rect.bottom > rect.m_rect.top);
  317. }
  318.  
  319. //-------------------------------------------------------------
  320. // Clip rect against this
  321.  
  322. inline void gsCRect::clip(gsCRect& rect)
  323. {
  324.     if (rect.m_rect.left < m_rect.left)
  325.         rect.m_rect.left = m_rect.left;
  326.     if (rect.m_rect.right > m_rect.right)
  327.         rect.m_rect.right = m_rect.right;
  328.     if (rect.m_rect.top < m_rect.top)
  329.         rect.m_rect.top = m_rect.top;
  330.     if (rect.m_rect.bottom > m_rect.bottom)
  331.         rect.m_rect.bottom = m_rect.bottom;
  332. }
  333.  
  334. //-------------------------------------------------------------
  335. // Expand/shrink
  336.  
  337. inline void gsCRect::expand(int amount)
  338. {
  339.     m_rect.left -= amount;
  340.     m_rect.top -= amount;
  341.     m_rect.right += amount;
  342.     m_rect.bottom += amount;
  343. }
  344.  
  345. //-------------------------------------------------------------
  346.  
  347. #endif
  348.