ScummVM API documentation
rect.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef ULTIMA8_MISC_RECT_H
23 #define ULTIMA8_MISC_RECT_H
24 
25 namespace Ultima {
26 namespace Ultima8 {
27 
28 // TODO: Replace Ultima8::Rect with Common::Rect
29 // The key difference between Ultima8::Rect and Common::Rect is the use of int32 for variables.
30 // Attempts to change this may cause the game to be unstable.
31 
32 struct Rect {
33  int32 left, top;
34  int32 right, bottom;
35 
36  Rect() : top(0), left(0), bottom(0), right(0) {}
37  Rect(int x1, int y1, int x2, int y2) : top(y1), left(x1), bottom(y2), right(x2) {}
38 
39  bool operator==(const Rect &rhs) const { return equals(rhs); }
40  bool operator!=(const Rect &rhs) const { return !equals(rhs); }
41 
42  int16 width() const { return right - left; }
43  int16 height() const { return bottom - top; }
44 
45  void setWidth(int16 aWidth) {
46  right = left + aWidth;
47  }
48 
49  void setHeight(int16 aHeight) {
50  bottom = top + aHeight;
51  }
52 
53  void grow(int16 offset) {
54  left -= offset;
55  top -= offset;
56  right += offset;
57  bottom += offset;
58  }
59 
60  // Check if the rectangle is empty (its width or length is 0) or invalid (its width or length are negative).
61  bool isEmpty() const {
62  return (left >= right || top >= bottom);
63  }
64 
65  // Check to see if a Rectangle is 'valid'
66  bool isValidRect() const {
67  return (left <= right && top <= bottom);
68  }
69 
70  // Check to see if a point is within the Rectangle
71  bool contains(int16 x, int16 y) const {
72  return (left <= x) && (x < right) && (top <= y) && (y < bottom);
73  }
74 
75  // Check if the given Rect is contained inside this rectangle.
76  bool contains(const Rect &r) const {
77  return (left <= r.left) && (r.right <= right) && (top <= r.top) && (r.bottom <= bottom);
78  }
79 
80  // Move the Rect (Relative)
81  void translate(int32 dx, int32 dy) {
82  left += dx;
83  right += dx;
84  top += dy;
85  bottom += dy;
86  }
87 
88  // Move the Rect (Absolute)
89  void moveTo(int32 x, int32 y) {
90  bottom += y - top;
91  right += x - left;
92  top = y;
93  left = x;
94  }
95 
96  void clip(const Rect &r) {
97  if (top < r.top) top = r.top;
98  else if (top > r.bottom) top = r.bottom;
99 
100  if (left < r.left) left = r.left;
101  else if (left > r.right) left = r.right;
102 
103  if (bottom < r.top) bottom = r.top;
104  else if (bottom > r.bottom) bottom = r.bottom;
105 
106  if (right < r.left) right = r.left;
107  else if (right > r.right) right = r.right;
108  }
109 
110  bool intersects(const Rect &r) const {
111  return (left < r.right) && (r.left < right) && (top < r.bottom) && (r.top < bottom);
112  }
113 
114  bool equals(const Rect &o) const {
115  return left == o.left && top == o.top && right == o.right && bottom == o.bottom;
116  }
117 
118 };
119 
120 } // End of namespace Ultima8
121 } // End of namespace Ultima
122 
123 #endif
Definition: rect.h:32
Definition: detection.h:27