blob: 9e98bc56202664194d78abc3441ded0e1cd087f8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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
Dianne Hackborn289b9b62010-07-09 11:44:11 -070023#include <android/rect.h>
24
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025namespace android {
26
Dianne Hackborn289b9b62010-07-09 11:44:11 -070027class Rect : public ARect
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028{
29public:
Mathias Agopian83b186a2011-09-19 16:00:46 -070030 typedef ARect::value_type value_type;
Mathias Agopian8fe8a182009-05-27 15:02:35 -070031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 // we don't provide copy-ctor and operator= on purpose
33 // because we want the compiler generated versions
34
Mathias Agopianf1472a72009-05-26 17:44:57 -070035 inline Rect() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 }
Dianne Hackborn289b9b62010-07-09 11:44:11 -070037 inline Rect(int32_t w, int32_t h) {
38 left = top = 0; right = w; bottom = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 }
Dianne Hackborn289b9b62010-07-09 11:44:11 -070040 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
41 left = l; top = t; right = r; bottom = b;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 }
Dianne Hackborn289b9b62010-07-09 11:44:11 -070043 inline Rect(const Point& lt, const Point& rb) {
44 left = lt.x; top = lt.y; right = rb.x; bottom = rb.y;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 }
46
47 void makeInvalid();
Mathias Agopian8fe8a182009-05-27 15:02:35 -070048
49 inline void clear() {
50 left = top = right = bottom = 0;
51 }
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 // a valid rectangle has a non negative width and height
54 inline bool isValid() const {
55 return (width()>=0) && (height()>=0);
56 }
57
58 // an empty rect has a zero width or height, or is invalid
59 inline bool isEmpty() const {
60 return (width()<=0) || (height()<=0);
61 }
62
63 inline void set(const Rect& rhs) {
64 operator = (rhs);
65 }
66
67 // rectangle's width
Dianne Hackborn289b9b62010-07-09 11:44:11 -070068 inline int32_t width() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 return right-left;
70 }
71
72 // rectangle's height
Dianne Hackborn289b9b62010-07-09 11:44:11 -070073 inline int32_t height() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 return bottom-top;
75 }
76
Mathias Agopianf1472a72009-05-26 17:44:57 -070077 void setLeftTop(const Point& lt) {
78 left = lt.x;
79 top = lt.y;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
Mathias Agopianf1472a72009-05-26 17:44:57 -070081
82 void setRightBottom(const Point& rb) {
83 right = rb.x;
84 bottom = rb.y;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 }
86
87 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopianf1472a72009-05-26 17:44:57 -070088 Point leftTop() const {
89 return Point(left, top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 }
Mathias Agopianf1472a72009-05-26 17:44:57 -070091 Point rightBottom() const {
92 return Point(right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 }
94 Point rightTop() const {
95 return Point(right, top);
96 }
97 Point leftBottom() const {
98 return Point(left, bottom);
99 }
100
101 // comparisons
102 inline bool operator == (const Rect& rhs) const {
103 return (left == rhs.left) && (top == rhs.top) &&
104 (right == rhs.right) && (bottom == rhs.bottom);
105 }
106
107 inline bool operator != (const Rect& rhs) const {
108 return !operator == (rhs);
109 }
110
111 // operator < defines an order which allows to use rectangles in sorted
112 // vectors.
113 bool operator < (const Rect& rhs) const;
114
115 Rect& offsetToOrigin() {
116 right -= left;
117 bottom -= top;
118 left = top = 0;
119 return *this;
120 }
121 Rect& offsetTo(const Point& p) {
122 return offsetTo(p.x, p.y);
123 }
124 Rect& offsetBy(const Point& dp) {
125 return offsetBy(dp.x, dp.y);
126 }
127 Rect& operator += (const Point& rhs) {
128 return offsetBy(rhs.x, rhs.y);
129 }
130 Rect& operator -= (const Point& rhs) {
131 return offsetBy(-rhs.x, -rhs.y);
132 }
Mathias Agopianf1472a72009-05-26 17:44:57 -0700133 const Rect operator + (const Point& rhs) const;
134 const Rect operator - (const Point& rhs) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700136 void translate(int32_t dx, int32_t dy) { // legacy, don't use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 offsetBy(dx, dy);
138 }
139
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700140 Rect& offsetTo(int32_t x, int32_t y);
141 Rect& offsetBy(int32_t x, int32_t y);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 bool intersect(const Rect& with, Rect* result) const;
143};
144
145ANDROID_BASIC_TYPES_TRAITS(Rect)
146
147}; // namespace android
148
149#endif // ANDROID_UI_RECT