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/afxwin.h"
28 #include "bagel/boflib/stdinc.h"
29 #include "bagel/boflib/object.h"
30 #include "bagel/boflib/point.h"
31 #include "bagel/boflib/size.h"
32 
33 namespace Bagel {
34 
35 class CBofRect : public CBofObject {
36 public:
37  // Constructors
38  CBofRect();
39  CBofRect(int l, int t, int r, int b);
40  CBofRect(const CBofRect &srcRect);
41  CBofRect(const CBofPoint &point, const CBofSize &size);
42  CBofRect(const CBofPoint &pttopLeft, const CBofPoint &ptBottomRight);
43  virtual ~CBofRect() {
44  }
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  void operator=(const Common::Rect &srcRect) {
81  const RECT r = { srcRect.left, srcRect.top, srcRect.right, srcRect.bottom };
82  setWinRect(&r);
83  }
84 
85  operator const RECT() {
86  return getWinRect();
87  }
88  operator const Common::Rect() {
89  return Common::Rect(left, top, right, bottom);
90  }
91 
92  void offsetRect(int x, int y);
93  void offsetRect(const CBofSize &size);
94  void offsetRect(const CBofPoint &point);
95 
96  // Operations that fill '*this' with result
97  bool intersectRect(const CBofRect *pRect1, const CBofRect *pRect2);
98  bool intersectRect(const CBofRect &cRect1, const CBofRect &cRect2);
99  bool unionRect(const CBofRect *pRect1, const CBofRect *pRect2);
100 
101  // Additional Operations
102  void operator=(const CBofRect &srcRect);
103  bool operator==(const CBofRect &rect);
104  bool operator!=(const CBofRect &rect);
105  void operator+=(const CBofPoint &point);
106  void operator-=(const CBofPoint &point);
107  void operator&=(const CBofRect &rect);
108  void operator|=(const CBofRect &rect);
109 
110  // Operators returning CBofRect values
111  CBofRect operator+(const CBofPoint &point);
112  CBofRect operator-(const CBofPoint &point);
113  CBofRect operator&(const CBofRect &rect2);
114  CBofRect operator|(const CBofRect &rect2);
115 
116  int left;
117  int top;
118  int right;
119  int bottom;
120 };
121 
122 inline CBofRect::CBofRect() {
123  top = left = 0;
124  right = bottom = -1;
125 }
126 
127 inline CBofRect::CBofRect(int l, int t, int r, int b) {
128  left = l;
129  top = t;
130  right = r;
131  bottom = b;
132 }
133 
134 inline CBofRect::CBofRect(const CBofRect &srcRect) {
135  left = srcRect.left;
136  top = srcRect.top;
137  right = srcRect.right;
138  bottom = srcRect.bottom;
139 }
140 
141 inline CBofRect::CBofRect(const CBofPoint &point, const CBofSize &size) {
142  right = (left = point.x) + size.cx - 1;
143  bottom = (top = point.y) + size.cy - 1;
144 }
145 
146 inline CBofRect::CBofRect(const CBofPoint &pttopLeft, const CBofPoint &ptBottomRight) {
147  setRect(pttopLeft.x, pttopLeft.y, ptBottomRight.x, ptBottomRight.y);
148 }
149 
150 inline int CBofRect::width() const {
151  // Make sure object is not used after it is destructed
152  assert(isValidObject(this));
153 
154  return right - left + 1;
155 }
156 
157 inline int CBofRect::height() const {
158  // Make sure object is not used after it is destructed
159  assert(isValidObject(this));
160 
161  return bottom - top + 1;
162 }
163 
164 inline CBofSize CBofRect::size() const {
165  // Make sure object is not used after it is destructed
166  assert(isValidObject(this));
167 
168  return CBofSize(width(), height());
169 }
170 
171 inline CBofPoint CBofRect::topLeft() const {
172  // Make sure object is not used after it is destructed
173  assert(isValidObject(this));
174 
175  return CBofPoint(left, top);
176 }
177 
178 inline CBofPoint CBofRect::bottomRight() const {
179  // Make sure object is not used after it is destructed
180  assert(isValidObject(this));
181 
182  return CBofPoint(right, bottom);
183 }
184 
185 inline CBofPoint CBofRect::topRight() const {
186  // Make sure object is not used after it is destructed
187  assert(isValidObject(this));
188 
189  return CBofPoint(right, top);
190 }
191 
192 inline CBofPoint CBofRect::bottomLeft() const {
193  // Make sure object is not used after it is destructed
194  assert(isValidObject(this));
195 
196  return CBofPoint(left, bottom);
197 }
198 
199 inline bool CBofRect::isRectEmpty() const {
200  // Make sure object is not used after it is destructed
201  assert(isValidObject(this));
202 
203  return (width() <= 0 || height() <= 0);
204 }
205 
206 inline bool CBofRect::isRectNull() const {
207  // Make sure object is not used after it is destructed
208  assert(isValidObject(this));
209 
210  return (left == 0 && right == 0 && top == 0 && bottom == 0);
211 }
212 
213 inline bool CBofRect::ptInRect(const CBofPoint &point) const {
214  // Make sure object is not used after it is destructed
215  assert(isValidObject(this));
216 
217  return (point.x >= left && point.x <= right && point.y >= top && point.y <= bottom);
218 }
219 
220 inline void CBofRect::setRect(int x1, int y1, int x2, int y2) {
221  // Make sure object is not used after it is destructed
222  assert(isValidObject(this));
223 
224  left = x1;
225  top = y1;
226  right = x2;
227  bottom = y2;
228 }
229 
230 inline void CBofRect::setRectEmpty() {
231  // Make sure object is not used after it is destructed
232  assert(isValidObject(this));
233 
234  left = top = 0;
235  right = bottom = -1;
236 
237  // Verify that the rectangle is now empty
238  assert(isRectEmpty());
239 }
240 
241 inline void CBofRect::copyRect(const CBofRect &cSrcRect) {
242  // Make sure object is not used after it is destructed
243  assert(isValidObject(this));
244 
245  assert(isValidObject(&cSrcRect));
246 
247  left = cSrcRect.left;
248  right = cSrcRect.right;
249  top = cSrcRect.top;
250  bottom = cSrcRect.bottom;
251 }
252 
253 inline bool CBofRect::equalRect(const CBofRect &cRect) {
254  // Make sure object is not used after it is destructed
255  assert(isValidObject(this));
256 
257  assert(isValidObject(&cRect));
258 
259  return (left == cRect.left && right == cRect.right && top == cRect.top && bottom == cRect.bottom);
260 }
261 
262 inline RECT CBofRect::getWinRect() {
263  assert(isValidObject(this));
264 
265  RECT stRect;
266 
267  stRect.left = left;
268  stRect.top = top;
269  stRect.right = right + 1;
270  stRect.bottom = bottom + 1;
271 
272  return stRect;
273 }
274 
275 inline void CBofRect::setWinRect(const RECT *pRect) {
276  assert(isValidObject(this));
277 
278  assert(pRect != nullptr);
279 
280  left = pRect->left;
281  top = pRect->top;
282  right = pRect->right - 1;
283  bottom = pRect->bottom - 1;
284 }
285 
286 inline void CBofRect::offsetRect(int x, int y) {
287  // Make sure object is not used after it is destructed
288  assert(isValidObject(this));
289 
290  left += x;
291  right += x;
292  top += y;
293  bottom += y;
294 }
295 
296 inline void CBofRect::offsetRect(const CBofPoint &point) {
297  // Make sure object is not used after it is destructed
298  assert(isValidObject(this));
299 
300  left += point.x;
301  right += point.x;
302  top += point.y;
303  bottom += point.y;
304 }
305 
306 inline void CBofRect::offsetRect(const CBofSize &size) {
307  // Make sure object is not used after it is destructed
308  assert(isValidObject(this));
309 
310  left += size.cx;
311  right += size.cx;
312  top += size.cy;
313  bottom += size.cy;
314 }
315 
316 inline bool CBofRect::intersectRect(const CBofRect *pRect1, const CBofRect *pRect2) {
317  // Make sure object is not used after it is destructed
318  assert(isValidObject(this));
319 
320  // Can't access null pointers
321  assert(pRect1 != nullptr);
322  assert(pRect2 != nullptr);
323 
324  left = MAX(pRect1->left, pRect2->left);
325  top = MAX(pRect1->top, pRect2->top);
326  right = MIN(pRect1->right, pRect2->right);
327  bottom = MIN(pRect1->bottom, pRect2->bottom);
328 
329  return ((width() > 0) && (height() > 0));
330 }
331 
332 inline bool CBofRect::intersectRect(const CBofRect &cRect1, const CBofRect &cRect2) {
333  // Make sure object is not used after it is destructed
334  assert(isValidObject(this));
335 
336  return intersectRect(&cRect1, &cRect2);
337 }
338 
339 inline bool CBofRect::unionRect(const CBofRect *pRect1, const CBofRect *pRect2) {
340  // Make sure object is not used after it is destructed
341  assert(isValidObject(this));
342 
343  // Can't access null pointers
344  assert(pRect1 != nullptr);
345  assert(pRect2 != nullptr);
346 
347  left = MIN(pRect1->left, pRect2->left);
348  top = MIN(pRect1->top, pRect2->top);
349  right = MAX(pRect1->right, pRect2->right);
350  bottom = MAX(pRect1->bottom, pRect2->bottom);
351 
352  return isRectEmpty();
353 }
354 
355 inline void CBofRect::operator=(const CBofRect &srcRect) {
356  // Make sure object is not used after it is destructed
357  assert(isValidObject(this));
358 
359  left = srcRect.left;
360  right = srcRect.right;
361  top = srcRect.top;
362  bottom = srcRect.bottom;
363 }
364 
365 inline bool CBofRect::operator==(const CBofRect &rect) {
366  // Make sure object is not used after it is destructed
367  assert(isValidObject(this));
368 
369  return (left == rect.left && right == rect.right &&
370  top == rect.top && bottom == rect.bottom);
371 }
372 
373 inline bool CBofRect::operator!=(const CBofRect &rect) {
374  // Make sure object is not used after it is destructed
375  assert(isValidObject(this));
376 
377  return (left != rect.left || right != rect.right ||
378  top != rect.top || bottom != rect.bottom);
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);
386 }
387 
388 inline void CBofRect::operator-=(const CBofPoint &point) {
389  // Make sure object is not used after it is destructed
390  assert(isValidObject(this));
391 
392  offsetRect(-point.x, -point.y);
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  intersectRect(this, &rect);
400 }
401 
402 inline void CBofRect::operator|=(const CBofRect &rect) {
403  // Make sure object is not used after it is destructed
404  assert(isValidObject(this));
405 
406  unionRect(this, &rect);
407 }
408 
409 inline CBofRect CBofRect::operator+(const CBofPoint &pt) {
410  // Make sure object is not used after it is destructed
411  assert(isValidObject(this));
412 
413  CBofRect rect(*this);
414 
415  rect.offsetRect(pt.x, pt.y);
416 
417  return rect;
418 }
419 
420 inline CBofRect CBofRect::operator-(const CBofPoint &pt) {
421  // Make sure object is not used after it is destructed
422  assert(isValidObject(this));
423 
424  CBofRect rect(*this);
425 
426  rect.offsetRect(-pt.x, -pt.y);
427 
428  return rect;
429 }
430 
431 inline CBofRect CBofRect::operator&(const CBofRect &rect2) {
432  // Make sure object is not used after it is destructed
433  assert(isValidObject(this));
434 
435  CBofRect rect;
436 
437  rect.intersectRect(this, &rect2);
438 
439  return rect;
440 }
441 
442 inline CBofRect CBofRect::operator|(const CBofRect &rect2) {
443  // Make sure object is not used after it is destructed
444  assert(isValidObject(this));
445 
446  CBofRect rect;
447 
448  rect.unionRect(this, &rect2);
449 
450  return rect;
451 }
452 
453 } // namespace Bagel
454 
455 #endif
T left
Definition: rect.h:170
Definition: object.h:28
Definition: size.h:32
Definition: rect.h:524
Definition: rect.h:35
T right
Definition: rect.h:171
Definition: minwindef.h:179
Definition: afxwin.h:27
Definition: point.h:32
T MIN(T a, T b)
Definition: util.h:61
T MAX(T a, T b)
Definition: util.h:64