Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cRectI.h
Go to the documentation of this file.
1 //
5 
6 #ifndef _INC_cRectI_H
7 #define _INC_cRectI_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cRectT.h"
13 #include "cPoint2.h"
14 #include "../WinAPI/WinTypes.h"
16 
17 namespace GrayLib
18 {
19  class cVariant;
20 
21  class GRAYLIB_LINK cRectI : public cRectNT<int>
22  {
26 
27  typedef cRectNT<int> SUPER_t;
28 
29  public:
30  cRectI() noexcept
31  {
32  // ASSUME DVALUE_t = int
33  }
34  cRectI(DVALUE_t nleft, DVALUE_t ntop, DVALUE_t nright = 0, DVALUE_t nbottom = 0)
35  : cRectNT<DVALUE_t>(nleft, ntop, nright, nbottom)
36  {
37  // NormalizeRect(); //?
38  }
39 
40 #ifdef _WIN32
41  // Cast directly to the windows type.
42  cRectI(const RECT& rect)
43  : cRectNT<DVALUE_t>(*((const SUPER_t*)&rect))
44  {}
45  inline operator RECT&() noexcept
46  {
47  return *((RECT*)this);
48  }
49  inline operator const RECT&() const noexcept
50  {
51  return *((const RECT*)this);
52  }
53  inline operator RECT*() noexcept
54  {
55  return (RECT*) this;
56  }
57  inline operator const RECT*() const noexcept
58  {
59  return (const RECT*) this;
60  }
61 #endif
62 
63  bool IsInsideX(DVALUE_t x) const noexcept
64  {
66  return(x >= left && x < right);
67  }
68  bool IsInsideY(DVALUE_t y) const noexcept
69  {
71  return(y >= top && y < bottom);
72  }
73  bool PtInRect(DVALUE_t x, DVALUE_t y) const noexcept
74  {
76  return(IsInsideX(x) && IsInsideY(y));
77  }
78  bool PtInRect(const cPoint2i& pt) const noexcept
79  {
84  return PtInRect(pt.x, pt.y);
85  }
86  bool ClampPtInRectX(cPoint2i& pt) const noexcept
87  {
88  bool bChange = false;
89  // x
90  if (pt.x < left)
91  {
92  pt.x = left;
93  bChange = true;
94  }
95  if (pt.x >= right)
96  {
97  pt.x = right - 1;
98  bChange = true;
99  }
100  // y
101  if (pt.y < top)
102  {
103  pt.y = top;
104  bChange = true;
105  }
106  if (pt.y >= bottom)
107  {
108  pt.y = bottom - 1;
109  bChange = true;
110  }
111  return bChange;
112  }
113  bool IsInsideMe(const cRectI& rect) const noexcept
114  {
118  if (rect.left < left)
119  return false;
120  if (rect.top < top)
121  return false;
122  if (rect.right > right)
123  return false;
124  if (rect.bottom > bottom)
125  return false;
126  return true;
127  }
128 
129  DVALUE_t GetOverlapX(const cRectI& rect) const noexcept
130  {
131  DVALUE_t iLeft2 = MAX(this->left, rect.left);
132  DVALUE_t iRight2 = MIN(this->right, rect.right);
133  // if (iLeft2 > iRight2) then the two rectangles do not overlap.
134  return iRight2 - iLeft2;
135  }
136  DVALUE_t GetOverlapY(const cRectI& rect) const noexcept
137  {
138  DVALUE_t iTop2 = MAX(this->top, rect.top);
139  DVALUE_t iBottom2 = MIN(this->bottom, rect.bottom);
140  // if (iTop2 > iBottom2) then the two rectangles do not overlap.
141  return iBottom2 - iTop2;
142  }
143  bool IsOverlapped(const cRectI& r, DVALUE_t minx, DVALUE_t miny) const noexcept
144  {
146  return(GetOverlapX(r) >= minx && GetOverlapY(r) >= miny);
147  }
148  bool IsOverlapped(const cRectI& rect) const noexcept
149  {
153  if (rect.left >= right)
154  return false;
155  if (rect.top >= bottom)
156  return false;
157  if (rect.right <= left)
158  return false;
159  if (rect.bottom <= top)
160  return false;
161  return true;
162  }
163 
164  bool ClampPtInRectI(cPoint2f& pt) const
165  {
167  bool bChange = false;
168  // x
169  if (pt.x < left)
170  {
171  pt.x = (float)left;
172  bChange = true;
173  }
174  if (pt.x > right)
175  {
176  pt.x = (float)right;
177  bChange = true;
178  }
179  // y
180  if (pt.y < top)
181  {
182  pt.y = (float)top;
183  bChange = true;
184  }
185  if (pt.y > bottom)
186  {
187  pt.y = (float)bottom;
188  bChange = true;
189  }
190  return bChange;
191  }
192 
194  {
198  if (x < left)
199  left = x;
200  if (y < top)
201  top = y;
202  if (x >= right)
203  right = x + 1;
204  if (y >= bottom)
205  bottom = y + 1;
206  }
207 
209  {
210  NormalizeRect();
211  if (left < 0)
212  OffsetRectX(-left);
213  if (top < 0)
214  OffsetRectY(-top);
215  }
216 
217  void SnapToGrid(DVALUE_t iSizeXY) noexcept
218  {
220  iSizeXY--;
221  left &= ~iSizeXY;
222  right = (right | iSizeXY) + 1;
223  top &= ~iSizeXY;
224  bottom = (bottom | iSizeXY) + 1;
225  }
226 
227  cPoint2i GetRectCorner(DIR_TYPE eDir) const;
228  cWinSize get_RectSize() const noexcept
229  {
230  return cWinSize(Width(), Height());
231  }
232 
233  ITERATE_t SetRectStr(const GChar_t* pszVal, bool bSizes = false);
234  cString get_RectStr(void) const;
235 
236  ITERATE_t v_SetRect(const cVariant& vVal, ITERATE_t i = 0);
237  void v_GetRect(cVariant& vVal) const;
238  };
239 
240  //**************************************************************
241 
242 #if ! defined(_MFC_VER) // && !defined(__ATLTYPES_H__)
243  typedef cRectI CRect;
244 #endif // ! _MFC_VER
245 
246 #ifdef _WIN32
247  // convert windows native types to Gray types.
248  inline cRectI& Cvt(RECT& pt)
249  {
250  return *((cRectI*)&pt);
251  }
252  inline const cRectI& Cvt(const RECT& pt)
253  {
254  return *((const cRectI*)&pt);
255  }
256 #endif
257 }
258 
259 #endif // _INC_cRectI_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define MIN(a, b)
Definition: SysTypes.h:457
#define MAX(a, b)
Definition: SysTypes.h:456
Definition: cPoint2.h:20
Definition: cRectI.h:22
bool ClampPtInRectI(cPoint2f &pt) const
Definition: cRectI.h:164
cWinSize get_RectSize() const noexcept
Definition: cRectI.h:228
bool IsInsideMe(const cRectI &rect) const noexcept
Definition: cRectI.h:113
bool IsOverlapped(const cRectI &rect) const noexcept
Definition: cRectI.h:148
bool IsOverlapped(const cRectI &r, DVALUE_t minx, DVALUE_t miny) const noexcept
Definition: cRectI.h:143
void OffsetPositive()
Definition: cRectI.h:208
void SnapToGrid(DVALUE_t iSizeXY) noexcept
Definition: cRectI.h:217
void UnionPoint(DVALUE_t x, DVALUE_t y)
Definition: cRectI.h:193
DVALUE_t GetOverlapY(const cRectI &rect) const noexcept
Definition: cRectI.h:136
bool ClampPtInRectX(cPoint2i &pt) const noexcept
Definition: cRectI.h:86
bool IsInsideX(DVALUE_t x) const noexcept
Definition: cRectI.h:63
DVALUE_t GetOverlapX(const cRectI &rect) const noexcept
Definition: cRectI.h:129
cRectI() noexcept
Definition: cRectI.h:30
cRectI(DVALUE_t nleft, DVALUE_t ntop, DVALUE_t nright=0, DVALUE_t nbottom=0)
Definition: cRectI.h:34
bool PtInRect(DVALUE_t x, DVALUE_t y) const noexcept
Definition: cRectI.h:73
bool IsInsideY(DVALUE_t y) const noexcept
Definition: cRectI.h:68
bool PtInRect(const cPoint2i &pt) const noexcept
Definition: cRectI.h:78
Definition: cRectT.h:359
TYPE DVALUE_t
Dimension value type.
Definition: cRectT.h:32
Definition: cVariant.h:26
Definition: cVecT.h:473
TYPE x
Definition: cVecT.h:490
TYPE y
Definition: cVecT.h:490
Definition: WinTypes.h:128
Definition: cMesh.h:22
DIR_TYPE
Definition: cDirectionDef.h:25
cRectI CRect
Compatible with MFC 2d CRect.
Definition: cRectI.h:243
interface const RECTQ_t & rect
Definition: cQuadtree.h:44
class __DECL_IMPORT cVariant
Definition: cJSONWriter.h:19
int ITERATE_t
like size_t but signed
Definition: Index.h:28
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26