blob: 91692876673bb72ae57804c9c2f69d775e8363f2 [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
Mathias Agopian8683fca2012-08-12 19:37:16 -070020#include <utils/Flattenable.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <utils/TypeHelpers.h>
22#include <ui/Point.h>
23
Dianne Hackborn9147d112010-07-09 11:44:11 -070024#include <android/rect.h>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026namespace android {
27
Mathias Agopian8683fca2012-08-12 19:37:16 -070028class Rect : public ARect, public LightFlattenablePod<Rect>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029{
30public:
Mathias Agopian000e95e2011-09-19 16:00:46 -070031 typedef ARect::value_type value_type;
Mathias Agopian4b8160f2009-05-27 15:02:35 -070032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033 // we don't provide copy-ctor and operator= on purpose
34 // because we want the compiler generated versions
35
Mathias Agopian35801ce2009-05-26 17:44:57 -070036 inline Rect() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070038
Dianne Hackborn9147d112010-07-09 11:44:11 -070039 inline Rect(int32_t w, int32_t h) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070040 left = top = 0;
41 right = w;
42 bottom = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070044
Dianne Hackborn9147d112010-07-09 11:44:11 -070045 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070046 left = l;
47 top = t;
48 right = r;
49 bottom = b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070051
Dianne Hackborn9147d112010-07-09 11:44:11 -070052 inline Rect(const Point& lt, const Point& rb) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070053 left = lt.x;
54 top = lt.y;
55 right = rb.x;
56 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057 }
58
59 void makeInvalid();
Mathias Agopian4b8160f2009-05-27 15:02:35 -070060
61 inline void clear() {
62 left = top = right = bottom = 0;
63 }
64
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 // a valid rectangle has a non negative width and height
66 inline bool isValid() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070067 return (getWidth() >= 0) && (getHeight() >= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068 }
69
70 // an empty rect has a zero width or height, or is invalid
71 inline bool isEmpty() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070072 return (getWidth() <= 0) || (getHeight() <= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 }
74
75 // rectangle's width
Mathias Agopianb82203a2012-05-13 20:02:04 -070076 inline int32_t getWidth() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070077 return right - left;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070079
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 // rectangle's height
Mathias Agopianb82203a2012-05-13 20:02:04 -070081 inline int32_t getHeight() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070082 return bottom - top;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 }
84
Mathias Agopianb82203a2012-05-13 20:02:04 -070085 inline Rect getBounds() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070086 return Rect(right - left, bottom - top);
Mathias Agopianb82203a2012-05-13 20:02:04 -070087 }
88
Mathias Agopian35801ce2009-05-26 17:44:57 -070089 void setLeftTop(const Point& lt) {
90 left = lt.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070091 top = lt.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 }
Mathias Agopian35801ce2009-05-26 17:44:57 -070093
94 void setRightBottom(const Point& rb) {
95 right = rb.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070096 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 }
98
99 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian35801ce2009-05-26 17:44:57 -0700100 Point leftTop() const {
101 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700103 Point rightBottom() const {
104 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 }
106 Point rightTop() const {
107 return Point(right, top);
108 }
109 Point leftBottom() const {
110 return Point(left, bottom);
111 }
112
113 // comparisons
114 inline bool operator == (const Rect& rhs) const {
115 return (left == rhs.left) && (top == rhs.top) &&
116 (right == rhs.right) && (bottom == rhs.bottom);
117 }
118
119 inline bool operator != (const Rect& rhs) const {
120 return !operator == (rhs);
121 }
122
123 // operator < defines an order which allows to use rectangles in sorted
124 // vectors.
125 bool operator < (const Rect& rhs) const;
126
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700127 const Rect operator + (const Point& rhs) const;
128 const Rect operator - (const Point& rhs) const;
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 Rect& offsetToOrigin() {
138 right -= left;
139 bottom -= top;
140 left = top = 0;
141 return *this;
142 }
143 Rect& offsetTo(const Point& p) {
144 return offsetTo(p.x, p.y);
145 }
146 Rect& offsetBy(const Point& dp) {
147 return offsetBy(dp.x, dp.y);
148 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700150 Rect& offsetTo(int32_t x, int32_t y);
151 Rect& offsetBy(int32_t x, int32_t y);
Jamie Gennis59332802012-05-07 13:49:17 -0700152
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700153 bool intersect(const Rect& with, Rect* result) const;
Jamie Gennis59332802012-05-07 13:49:17 -0700154
155 // Create a new Rect by transforming this one using a graphics HAL
156 // transform. This rectangle is defined in a coordinate space starting at
157 // the origin and extending to (width, height). If the transform includes
158 // a ROT90 then the output rectangle is defined in a space extending to
159 // (height, width). Otherwise the output rectangle is in the same space as
160 // the input.
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700161 Rect transform(uint32_t xform, int32_t width, int32_t height) const;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700162
163 // for backward compatibility
164 inline int32_t width() const { return getWidth(); }
165 inline int32_t height() const { return getHeight(); }
166 inline void set(const Rect& rhs) { operator = (rhs); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167};
168
169ANDROID_BASIC_TYPES_TRAITS(Rect)
170
171}; // namespace android
172
173#endif // ANDROID_UI_RECT