ScummVM API documentation
rect.h
1 
2 /* ScummVM - Graphic Adventure Engine
3  *
4  * ScummVM is the legal property of its developers, whose names
5  * are too numerous to list here. Please refer to the COPYRIGHT
6  * file distributed with this source distribution.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef BAGEL_BOFLIB_RECT_H
24 #define BAGEL_BOFLIB_RECT_H
25 
26 #include "common/rect.h"
27 #include "bagel/boflib/stdinc.h"
28 #include "bagel/boflib/object.h"
29 #include "bagel/boflib/point.h"
30 #include "bagel/boflib/size.h"
31 
32 namespace Bagel {
33 
34 typedef Common::Rect RECT;
35 
36 class CBofRect : public CBofObject {
37 
38 public:
39  // Constructors
40  CBofRect();
41  CBofRect(int l, int t, int r, int b);
42  CBofRect(const CBofRect &srcRect);
43  CBofRect(const CBofPoint &point, const CBofSize &size);
44  CBofRect(const CBofPoint &pttopLeft, const CBofPoint &ptBottomRight);
45 
46  // Attributes
47  int width() const;
48  int height() const;
49  CBofSize size() const;
50  CBofPoint topLeft() const;
51  CBofPoint bottomRight() const;
52 
53  CBofPoint topRight() const;
54  CBofPoint bottomLeft() const;
55 
56  bool isRectEmpty() const;
57  bool isRectNull() const;
58  bool ptInRect(const CBofPoint &point) const;
59 
60  // Operations
61 
62  inline void setRect(int x1, int y1, int x2, int y2);
63  void setRectEmpty();
64  void copyRect(const CBofRect &cRect);
65  bool equalRect(const CBofRect &cRect);
66 
67  CBofRect(const RECT &cRect) {
68  setWinRect(&cRect);
69  }
70  CBofRect(const RECT *pRect) {
71  setWinRect(pRect);
72  }
73 
74  void setWinRect(const RECT *pRect);
75  RECT getWinRect();
76 
77  void operator=(const RECT &srcRect) {
78  setWinRect(&srcRect);
79  }
80 
81  operator const RECT() {
82  return getWinRect();
83  }
84 
85  void offsetRect(int x, int y);
86  void offsetRect(const CBofSize &size);
87  void offsetRect(const CBofPoint &point);
88 
89  // Operations that fill '*this' with result
90  bool intersectRect(const CBofRect *pRect1, const CBofRect *pRect2);
91  bool intersectRect(const CBofRect &cRect1, const CBofRect &cRect2);
92  bool unionRect(const CBofRect *pRect1, const CBofRect *pRect2);
93 
94  // Additional Operations
95  void operator=(const CBofRect &srcRect);
96  bool operator==(const CBofRect &rect);
97  bool operator!=(const CBofRect &rect);
98  void operator+=(const CBofPoint &point);
99  void operator-=(const CBofPoint &point);
100  void operator&=(const CBofRect &rect);
101  void operator|=(const CBofRect &rect);
102 
103  // Operators returning CBofRect values
104  CBofRect operator+(const CBofPoint &point);
105  CBofRect operator-(const CBofPoint &point);
106  CBofRect operator&(const CBofRect &rect2);
107  CBofRect operator|(const CBofRect &rect2);
108 
109  int left;
110  int top;
111  int right;
112  int bottom;
113 };
114 
115 inline CBofRect::CBofRect() {
116  top = left = 0;
117  right = bottom = -1;
118 }
119 
120 inline CBofRect::CBofRect(int l, int t, int r, int b) {
121  left = l;
122  top = t;
123  right = r;
124  bottom = b;
125 }
126 
127 inline CBofRect::CBofRect(const CBofRect &srcRect) {
128  left = srcRect.left;
129  top = srcRect.top;
130  right = srcRect.right;
131  bottom = srcRect.bottom;
132 }
133 
134 inline CBofRect::CBofRect(const CBofPoint &point, const CBofSize &size) {
135  right = (left = point.x) + size.cx - 1;
136  bottom = (top = point.y) + size.cy - 1;
137 }
138 
139 inline CBofRect::CBofRect(const CBofPoint &pttopLeft, const CBofPoint &ptBottomRight) {
140  setRect(pttopLeft.x, pttopLeft.y, ptBottomRight.x, ptBottomRight.y);
141 }
142 
143 inline int CBofRect::width() const {
144  // Make sure object is not used after it is destructed
145  assert(isValidObject(this));
146 
147  return right - left + 1;
148 }
149 
150 inline int CBofRect::height() const {
151  // Make sure object is not used after it is destructed
152  assert(isValidObject(this));
153 
154  return bottom - top + 1;
155 }
156 
157 inline CBofSize CBofRect::size() const {
158  // Make sure object is not used after it is destructed
159  assert(isValidObject(this));
160 
161  return CBofSize(width(), height());
162 }
163 
164 inline CBofPoint CBofRect::topLeft() const {
165  // Make sure object is not used after it is destructed
166  assert(isValidObject(this));
167 
168  return CBofPoint(left, top);
169 }
170 
171 inline CBofPoint CBofRect::bottomRight() const {
172  // Make sure object is not used after it is destructed
173  assert(isValidObject(this));
174 
175  return CBofPoint(right, bottom);
176 }
177 
178 inline CBofPoint CBofRect::topRight() const {
179  // Make sure object is not used after it is destructed
180  assert(isValidObject(this));
181 
182  return CBofPoint(right, top);
183 }
184 
185 inline CBofPoint CBofRect::bottomLeft() const {
186  // Make sure object is not used after it is destructed
187  assert(isValidObject(this));
188 
189  return CBofPoint(left, bottom);
190 }
191 
192 inline bool CBofRect::isRectEmpty() const {
193  // Make sure object is not used after it is destructed
194  assert(isValidObject(this));
195 
196  return (width() <= 0 || height() <= 0);
197 }
198 
199 inline bool CBofRect::isRectNull() const {
200  // Make sure object is not used after it is destructed
201  assert(isValidObject(this));
202 
203  return (left == 0 && right == 0 && top == 0 && bottom == 0);
204 }
205 
206 inline bool CBofRect::ptInRect(const CBofPoint &point) const {
207  // Make sure object is not used after it is destructed
208  assert(isValidObject(this));
209 
210  return (point.x >= left && point.x <= right && point.y >= top && point.y <= bottom);
211 }
212 
213 inline void CBofRect::setRect(int x1, int y1, int x2, int y2) {
214  // Make sure object is not used after it is destructed
215  assert(isValidObject(this));
216 
217  left = x1;
218  top = y1;
219  right = x2;
220  bottom = y2;
221 }
222 
223 inline void CBofRect::setRectEmpty() {
224  // Make sure object is not used after it is destructed
225  assert(isValidObject(this));
226 
227  left = top = 0;
228  right = bottom = -1;
229 
230  // Verify that the rectangle is now empty
231  assert(isRectEmpty());
232 }
233 
234 inline void CBofRect::copyRect(const CBofRect &cSrcRect) {
235  // Make sure object is not used after it is destructed
236  assert(isValidObject(this));
237 
238  assert(isValidObject(&cSrcRect));
239 
240  left = cSrcRect.left;
241  right = cSrcRect.right;
242  top = cSrcRect.top;
243  bottom = cSrcRect.bottom;
244 }
245 
246 inline bool CBofRect::equalRect(const CBofRect &cRect) {
247  // Make sure object is not used after it is destructed
248  assert(isValidObject(this));
249 
250  assert(isValidObject(&cRect));
251 
252  return (left == cRect.left && right == cRect.right && top == cRect.top && bottom == cRect.bottom);
253 }
254 
255 inline RECT CBofRect::getWinRect() {
256  assert(isValidObject(this));
257 
258  RECT stRect;
259 
260  stRect.left = left;
261  stRect.top = top;
262  stRect.right = right + 1;
263  stRect.bottom = bottom + 1;
264 
265  return stRect;
266 }
267 
268 inline void CBofRect::setWinRect(const RECT *pRect) {
269  assert(isValidObject(this));
270 
271  assert(pRect != nullptr);
272 
273  left = pRect->left;
274  top = pRect->top;
275  right = pRect->right - 1;
276  bottom = pRect->bottom - 1;
277 }
278 
279 inline void CBofRect::offsetRect(int x, int y) {
280  // Make sure object is not used after it is destructed
281  assert(isValidObject(this));
282 
283  left += x;
284  right += x;
285  top += y;
286  bottom += y;
287 }
288 
289 inline void CBofRect::offsetRect(const CBofPoint &point) {
290  // Make sure object is not used after it is destructed
291  assert(isValidObject(this));
292 
293  left += point.x;
294  right += point.x;
295  top += point.y;
296  bottom += point.y;
297 }
298 
299 inline void CBofRect::offsetRect(const CBofSize &size) {
300  // Make sure object is not used after it is destructed
301  assert(isValidObject(this));
302 
303  left += size.cx;
304  right += size.cx;
305  top += size.cy;
306  bottom += size.cy;
307 }
308 
309 inline bool CBofRect::intersectRect(const CBofRect *pRect1, const CBofRect *pRect2) {
310  // Make sure object is not used after it is destructed
311  assert(isValidObject(this));
312 
313  // Can't access null pointers
314  assert(pRect1 != nullptr);
315  assert(pRect2 != nullptr);
316 
317  left = MAX(pRect1->left, pRect2->left);
318  top = MAX(pRect1->top, pRect2->top);
319  right = MIN(pRect1->right, pRect2->right);
320  bottom = MIN(pRect1->bottom, pRect2->bottom);
321 
322  return ((width() > 0) && (height() > 0));
323 }
324 
325 inline bool CBofRect::intersectRect(const CBofRect &cRect1, const CBofRect &cRect2) {
326  // Make sure object is not used after it is destructed
327  assert(isValidObject(this));
328 
329  return intersectRect(&cRect1, &cRect2);
330 }
331 
332 inline bool CBofRect::unionRect(const CBofRect *pRect1, const CBofRect *pRect2) {
333  // Make sure object is not used after it is destructed
334  assert(isValidObject(this));
335 
336  // Can't access null pointers
337  assert(pRect1 != nullptr);
338  assert(pRect2 != nullptr);
339 
340  left = MIN(pRect1->left, pRect2->left);
341  top = MIN(pRect1->top, pRect2->top);
342  right = MAX(pRect1->right, pRect2->right);
343  bottom = MAX(pRect1->bottom, pRect2->bottom);
344 
345  return isRectEmpty();
346 }
347 
348 inline void CBofRect::operator=(const CBofRect &srcRect) {
349  // Make sure object is not used after it is destructed
350  assert(isValidObject(this));
351 
352  left = srcRect.left;
353  right = srcRect.right;
354  top = srcRect.top;
355  bottom = srcRect.bottom;
356 }
357 
358 inline bool CBofRect::operator==(const CBofRect &rect) {
359  // Make sure object is not used after it is destructed
360  assert(isValidObject(this));
361 
362  return (left == rect.left && right == rect.right &&
363  top == rect.top && bottom == rect.bottom);
364 }
365 
366 inline bool CBofRect::operator!=(const CBofRect &rect) {
367  // Make sure object is not used after it is destructed
368  assert(isValidObject(this));
369 
370  return (left != rect.left || right != rect.right ||
371  top != rect.top || bottom != rect.bottom);
372 }
373 
374 inline void CBofRect::operator+=(const CBofPoint &point) {
375  // Make sure object is not used after it is destructed
376  assert(isValidObject(this));
377 
378  offsetRect(point);
379 }
380 
381 inline void CBofRect::operator-=(const CBofPoint &point) {
382  // Make sure object is not used after it is destructed
383  assert(isValidObject(this));
384 
385  offsetRect(-point.x, -point.y);
386 }
387 
388 inline void CBofRect::operator&=(const CBofRect &rect) {
389  // Make sure object is not used after it is destructed
390  assert(isValidObject(this));
391 
392  intersectRect(this, &rect);
393 }
394 
395 inline void CBofRect::operator|=(const CBofRect &rect) {
396  // Make sure object is not used after it is destructed
397  assert(isValidObject(this));
398 
399  unionRect(this, &rect);
400 }
401 
402 inline CBofRect CBofRect::operator+(const CBofPoint &pt) {
403  // Make sure object is not used after it is destructed
404  assert(isValidObject(this));
405 
406  CBofRect rect(*this);
407 
408  rect.offsetRect(pt.x, pt.y);
409 
410  return rect;
411 }
412 
413 inline CBofRect CBofRect::operator-(const CBofPoint &pt) {
414  // Make sure object is not used after it is destructed
415  assert(isValidObject(this));
416 
417  CBofRect rect(*this);
418 
419  rect.offsetRect(-pt.x, -pt.y);
420 
421  return rect;
422 }
423 
424 inline CBofRect CBofRect::operator&(const CBofRect &rect2) {
425  // Make sure object is not used after it is destructed
426  assert(isValidObject(this));
427 
428  CBofRect rect;
429 
430  rect.intersectRect(this, &rect2);
431 
432  return rect;
433 }
434 
435 inline CBofRect CBofRect::operator|(const CBofRect &rect2) {
436  // Make sure object is not used after it is destructed
437  assert(isValidObject(this));
438 
439  CBofRect rect;
440 
441  rect.unionRect(this, &rect2);
442 
443  return rect;
444 }
445 
446 #define CRect CBofRect
447 
448 } // namespace Bagel
449 
450 #endif
Definition: object.h:28
Definition: size.h:31
int16 right
Definition: rect.h:146
Definition: rect.h:144
Definition: rect.h:36
int16 left
Definition: rect.h:145
Definition: bagel.h:31
Definition: point.h:34
T MIN(T a, T b)
Definition: util.h:59
T MAX(T a, T b)
Definition: util.h:62