blob: d232847113ab717359e09cbbf5104ca2198a1813 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_UI_RECT
18#define ANDROID_UI_RECT
19
20#include <utils/TypeHelpers.h>
21#include <ui/Point.h>
22
23namespace android {
24
25class Rect
26{
27public:
28 int left;
29 int top;
30 int right;
31 int bottom;
32
33 // we don't provide copy-ctor and operator= on purpose
34 // because we want the compiler generated versions
35
36 inline Rect()
37 {
38 }
39
40 inline Rect(int w, int h)
41 : left(0), top(0), right(w), bottom(h)
42 {
43 }
44
45 inline Rect(int l, int t, int r, int b)
46 : left(l), top(t), right(r), bottom(b)
47 {
48 }
49
50 inline Rect(const Point& lt, const Point& rb)
51 : left(lt.x), top(lt.y), right(rb.x), bottom(rb.y)
52 {
53 }
54
55 void makeInvalid();
56
57 // a valid rectangle has a non negative width and height
58 inline bool isValid() const {
59 return (width()>=0) && (height()>=0);
60 }
61
62 // an empty rect has a zero width or height, or is invalid
63 inline bool isEmpty() const {
64 return (width()<=0) || (height()<=0);
65 }
66
67 inline void set(const Rect& rhs) {
68 operator = (rhs);
69 }
70
71 // rectangle's width
72 inline int width() const {
73 return right-left;
74 }
75
76 // rectangle's height
77 inline int height() const {
78 return bottom-top;
79 }
80
81 // returns left-top Point non-const reference, can be assigned
82 inline Point& leftTop() {
83 return reinterpret_cast<Point&>(left);
84 }
85 // returns right bottom non-const reference, can be assigned
86 inline Point& rightBottom() {
87 return reinterpret_cast<Point&>(right);
88 }
89
90 // the following 4 functions return the 4 corners of the rect as Point
91 inline const Point& leftTop() const {
92 return reinterpret_cast<const Point&>(left);
93 }
94 inline const Point& rightBottom() const {
95 return reinterpret_cast<const Point&>(right);
96 }
97 Point rightTop() const {
98 return Point(right, top);
99 }
100 Point leftBottom() const {
101 return Point(left, bottom);
102 }
103
104 // comparisons
105 inline bool operator == (const Rect& rhs) const {
106 return (left == rhs.left) && (top == rhs.top) &&
107 (right == rhs.right) && (bottom == rhs.bottom);
108 }
109
110 inline bool operator != (const Rect& rhs) const {
111 return !operator == (rhs);
112 }
113
114 // operator < defines an order which allows to use rectangles in sorted
115 // vectors.
116 bool operator < (const Rect& rhs) const;
117
118 Rect& offsetToOrigin() {
119 right -= left;
120 bottom -= top;
121 left = top = 0;
122 return *this;
123 }
124 Rect& offsetTo(const Point& p) {
125 return offsetTo(p.x, p.y);
126 }
127 Rect& offsetBy(const Point& dp) {
128 return offsetBy(dp.x, dp.y);
129 }
130 Rect& operator += (const Point& rhs) {
131 return offsetBy(rhs.x, rhs.y);
132 }
133 Rect& operator -= (const Point& rhs) {
134 return offsetBy(-rhs.x, -rhs.y);
135 }
136 Rect operator + (const Point& rhs) const;
137 Rect operator - (const Point& rhs) const;
138
139 void translate(int dx, int dy) { // legacy, don't use.
140 offsetBy(dx, dy);
141 }
142
143 Rect& offsetTo(int x, int y);
144 Rect& offsetBy(int x, int y);
145 bool intersect(const Rect& with, Rect* result) const;
146};
147
148ANDROID_BASIC_TYPES_TRAITS(Rect)
149
150}; // namespace android
151
152#endif // ANDROID_UI_RECT