00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef CATRect_H_
00015 #define CATRect_H_
00016
00017 #pragma pack(push)
00018 #pragma pack(1)
00019
00020
00021
00022 struct CATRECT
00023 {
00024 CATInt32 left;
00025 CATInt32 top;
00026 CATInt32 right;
00027 CATInt32 bottom;
00028 };
00029
00030
00031 struct CATPOINT
00032 {
00033 CATInt32 x;
00034 CATInt32 y;
00035 };
00036
00037
00038 struct CATSIZE
00039 {
00040 CATInt32 cx;
00041 CATInt32 cy;
00042 };
00043 #pragma pack(pop)
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 class CATRect : public CATRECT
00057 {
00058 public:
00059
00060 CATRect()
00061 {
00062 left = top = right = bottom = 0;
00063 }
00064
00065
00066 CATRect(const CATRECT& rect)
00067 {
00068 this->left = rect.left;
00069 this->top = rect.top;
00070 this->right = rect.right;
00071 this->bottom = rect.bottom;
00072 }
00073
00074
00075
00076
00077
00078
00079 CATRect(CATInt32 l, CATInt32 t, CATInt32 r, CATInt32 b)
00080 {
00081 CATASSERT(l <= r, "Left should be <= right.");
00082 CATASSERT(t <= b, "Top should be <= bottom.");
00083
00084 this->left = l;
00085 this->top = t;
00086 this->right = r;
00087 this->bottom = b;
00088 }
00089
00090
00091 ~CATRect()
00092 {
00093 CATASSERT(left <= right, "Left should be <= right.");
00094 CATASSERT(top <= bottom, "Top should be <= bottom.");
00095 }
00096
00097
00098 CATRect& operator=(const CATRECT& rect)
00099 {
00100 this->left = rect.left;
00101 this->top = rect.top;
00102 this->right = rect.right;
00103 this->bottom = rect.bottom;
00104 return *this;
00105 }
00106
00107
00108 bool operator==(const CATRECT& rect)
00109 {
00110 if ((left == rect.left) &&
00111 (top == rect.top) &&
00112 (right == rect.right) &&
00113 (bottom == rect.bottom))
00114 {
00115 return true;
00116 }
00117 return false;
00118 }
00119
00120
00121 bool operator!=(const CATRECT& rect)
00122 {
00123 if ((left == rect.left) &&
00124 (top == rect.top) &&
00125 (right == rect.right) &&
00126 (bottom == rect.bottom))
00127 {
00128 return false;
00129 }
00130 return true;
00131 }
00132
00133
00134
00135 inline CATInt32 Width() const
00136 {
00137 return right - left;
00138 }
00139
00140
00141
00142 inline CATInt32 Height() const
00143 {
00144 return bottom - top;
00145 }
00146
00147
00148
00149
00150 inline CATSIZE Size() const
00151 {
00152 CATSIZE rsize = {right - left, bottom - top};
00153 return rsize;
00154 }
00155
00156
00157
00158
00159 inline CATPOINT Origin() const
00160 {
00161 CATPOINT point = {left,top};
00162 return point;
00163 }
00164
00165
00166
00167
00168 inline CATInt32 CenterX() const
00169 {
00170 return (left + right)/2;
00171 }
00172
00173
00174
00175
00176 inline CATInt32 CenterY() const
00177 {
00178 return (top + bottom)/2;
00179 }
00180
00181
00182
00183 inline CATPOINT Center() const
00184 {
00185 CATPOINT point = {CenterX(), CenterY() };
00186 return point;
00187 }
00188
00189
00190
00191 void ZeroOrigin()
00192 {
00193 right -= left;
00194 bottom -= top;
00195 left = 0;
00196 top = 0;
00197 }
00198
00199
00200
00201 inline void SetOrigin(CATPOINT& point)
00202 {
00203 SetOrigin(point.x, point.y);
00204 }
00205
00206 void SetOrigin(CATInt32 x, CATInt32 y)
00207 {
00208 ZeroOrigin();
00209 left += x;
00210 right += x;
00211 top += y;
00212 bottom += y;
00213 }
00214
00215
00216
00217
00218
00219
00220 void Offset(const CATPOINT& point)
00221 {
00222 Offset(point.x, point.y);
00223 }
00224
00225
00226
00227
00228 void Offset(CATInt32 x, CATInt32 y)
00229 {
00230 left += x;
00231 right += x;
00232 top += y;
00233 bottom += y;
00234 }
00235
00236
00237
00238
00239 void Resize(const CATSIZE& size)
00240 {
00241 right = left + size.cx;
00242 bottom = top + size.cy;
00243 }
00244
00245
00246
00247
00248
00249 void Resize(CATInt32 width, CATInt32 height)
00250 {
00251 right = left + width;
00252 bottom = top + height;
00253 }
00254
00255
00256
00257
00258 void Fix()
00259 {
00260 if (left > right)
00261 CATSwap(left, right);
00262
00263 if (top > bottom)
00264 CATSwap(top, bottom);
00265 }
00266
00267
00268
00269
00270
00271
00272 void Set(CATInt32 l, CATInt32 t, CATInt32 r, CATInt32 b)
00273 {
00274 this->left = l;
00275 this->top = t;
00276 this->right = r;
00277 this->bottom = b;
00278 }
00279
00280
00281
00282
00283 void Set(const CATPOINT& topLeft, const CATSIZE& widthHeight)
00284 {
00285 this->left = topLeft.x;
00286 this->top = topLeft.y;
00287 this->right = topLeft.x + widthHeight.cx;
00288 this->bottom = topLeft.y + widthHeight.cy;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298 bool InRect(const CATPOINT& point) const
00299 {
00300 return InRect(point.x, point.y);
00301 }
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 bool InRect(CATInt32 x, CATInt32 y) const
00313 {
00314 if ( ((x >= left) && (x < right)) &&
00315 ((y >= top) && (y < bottom)) )
00316 {
00317 return true;
00318 }
00319 return false;
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 bool Intersect( const CATRect& srcRect, CATRect* intersectRect = 0) const
00332 {
00333
00334
00335 if ( (this->right <= srcRect.left) ||
00336 (this->left >= srcRect.right) ||
00337 (this->bottom <= srcRect.top) ||
00338 (this->top >= srcRect.bottom) ||
00339 (this->Width() == 0) ||
00340 (this->Height() == 0) ||
00341 (srcRect.Width() == 0) ||
00342 (srcRect.Height() == 0) )
00343 {
00344
00345 if (intersectRect != 0)
00346 {
00347 intersectRect->left = intersectRect->right = 0;
00348 intersectRect->top = intersectRect->bottom = 0;
00349 }
00350
00351 return false;
00352 }
00353
00354
00355 if (intersectRect == 0)
00356 {
00357 return true;
00358 }
00359
00360
00361 intersectRect->left = CATMax(left, srcRect.left);
00362 intersectRect->top = CATMax(top, srcRect.top);
00363 intersectRect->right = CATMin(right, srcRect.right);
00364 intersectRect->bottom = CATMin(bottom, srcRect.bottom);
00365 return true;
00366 }
00367
00368
00369
00370
00371
00372
00373 bool Inside(const CATRect& insideRect) const
00374 {
00375 if ( ( this->left <= insideRect.left ) &&
00376 ( this->right >= insideRect.right ) &&
00377 ( this->top <= insideRect.top ) &&
00378 ( this->bottom >= insideRect.bottom ) )
00379 {
00380 return true;
00381 }
00382 return false;
00383 }
00384
00385
00386
00387 void Stretch(CATInt32 stretchSize = 1, const CATRect* bounds = 0)
00388 {
00389 left -= stretchSize;
00390 top -= stretchSize;
00391 right += stretchSize;
00392 bottom += stretchSize;
00393
00394 if (bounds)
00395 {
00396 if ( left < bounds->left)
00397 left = bounds->left;
00398 if ( top < bounds->top)
00399 top = bounds->top;
00400 if (right > bounds->right)
00401 right = bounds->right;
00402 if (bottom > bounds->bottom)
00403 right = bounds->bottom;
00404 }
00405 }
00406
00407 void Shrink(CATInt32 stretchSize = 1)
00408 {
00409 left += stretchSize;
00410 top += stretchSize;
00411 right -= stretchSize;
00412 bottom -= stretchSize;
00413 }
00414
00415 };
00416
00417 #endif // CATRect_H_
00418
00419