blob: 6310502b34ea1a6c3f91f3becdc9ddd245f28ae8 [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>
Dan Stozadd883c02014-11-18 10:24:03 -080021#include <utils/Log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <utils/TypeHelpers.h>
23#include <ui/Point.h>
24
Dianne Hackborn9147d112010-07-09 11:44:11 -070025#include <android/rect.h>
26
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027namespace android {
28
Mathias Agopian8683fca2012-08-12 19:37:16 -070029class Rect : public ARect, public LightFlattenablePod<Rect>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030{
31public:
Mathias Agopian000e95e2011-09-19 16:00:46 -070032 typedef ARect::value_type value_type;
Mathias Agopian4b8160f2009-05-27 15:02:35 -070033
Dan Stoza5065a552015-03-17 16:23:42 -070034 static const Rect INVALID_RECT;
Pablo Ceballos60d69222015-08-07 14:47:20 -070035 static const Rect EMPTY_RECT;
Dan Stoza5065a552015-03-17 16:23:42 -070036
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037 // we don't provide copy-ctor and operator= on purpose
38 // because we want the compiler generated versions
39
Pablo Ceballos60d69222015-08-07 14:47:20 -070040 inline Rect() : Rect(INVALID_RECT) {}
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070041
Dianne Hackborn9147d112010-07-09 11:44:11 -070042 inline Rect(int32_t w, int32_t h) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070043 left = top = 0;
44 right = w;
45 bottom = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070047
Dan Stozadd883c02014-11-18 10:24:03 -080048 inline Rect(uint32_t w, uint32_t h) {
49 if (w > INT32_MAX) {
50 ALOG(LOG_WARN, "Rect",
51 "Width %u too large for Rect class, clamping", w);
52 w = INT32_MAX;
53 }
54 if (h > INT32_MAX) {
55 ALOG(LOG_WARN, "Rect",
56 "Height %u too large for Rect class, clamping", h);
57 h = INT32_MAX;
58 }
59 left = top = 0;
60 right = w;
61 bottom = h;
62 }
63
Dianne Hackborn9147d112010-07-09 11:44:11 -070064 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070065 left = l;
66 top = t;
67 right = r;
68 bottom = b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070070
Dianne Hackborn9147d112010-07-09 11:44:11 -070071 inline Rect(const Point& lt, const Point& rb) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070072 left = lt.x;
73 top = lt.y;
74 right = rb.x;
75 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 }
77
78 void makeInvalid();
Mathias Agopian4b8160f2009-05-27 15:02:35 -070079
80 inline void clear() {
81 left = top = right = bottom = 0;
82 }
83
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 // a valid rectangle has a non negative width and height
85 inline bool isValid() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070086 return (getWidth() >= 0) && (getHeight() >= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 }
88
89 // an empty rect has a zero width or height, or is invalid
90 inline bool isEmpty() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070091 return (getWidth() <= 0) || (getHeight() <= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 }
93
94 // rectangle's width
Mathias Agopianb82203a2012-05-13 20:02:04 -070095 inline int32_t getWidth() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070096 return right - left;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070098
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 // rectangle's height
Mathias Agopianb82203a2012-05-13 20:02:04 -0700100 inline int32_t getHeight() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700101 return bottom - top;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 }
103
Mathias Agopianb82203a2012-05-13 20:02:04 -0700104 inline Rect getBounds() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700105 return Rect(right - left, bottom - top);
Mathias Agopianb82203a2012-05-13 20:02:04 -0700106 }
107
Mathias Agopian35801ce2009-05-26 17:44:57 -0700108 void setLeftTop(const Point& lt) {
109 left = lt.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700110 top = lt.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700112
113 void setRightBottom(const Point& rb) {
114 right = rb.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700115 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 }
117
118 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian35801ce2009-05-26 17:44:57 -0700119 Point leftTop() const {
120 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700122 Point rightBottom() const {
123 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 }
125 Point rightTop() const {
126 return Point(right, top);
127 }
128 Point leftBottom() const {
129 return Point(left, bottom);
130 }
131
132 // comparisons
133 inline bool operator == (const Rect& rhs) const {
134 return (left == rhs.left) && (top == rhs.top) &&
135 (right == rhs.right) && (bottom == rhs.bottom);
136 }
137
138 inline bool operator != (const Rect& rhs) const {
139 return !operator == (rhs);
140 }
141
142 // operator < defines an order which allows to use rectangles in sorted
143 // vectors.
144 bool operator < (const Rect& rhs) const;
145
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700146 const Rect operator + (const Point& rhs) const;
147 const Rect operator - (const Point& rhs) const;
148
149 Rect& operator += (const Point& rhs) {
150 return offsetBy(rhs.x, rhs.y);
151 }
152 Rect& operator -= (const Point& rhs) {
153 return offsetBy(-rhs.x, -rhs.y);
154 }
155
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 Rect& offsetToOrigin() {
157 right -= left;
158 bottom -= top;
159 left = top = 0;
160 return *this;
161 }
162 Rect& offsetTo(const Point& p) {
163 return offsetTo(p.x, p.y);
164 }
165 Rect& offsetBy(const Point& dp) {
166 return offsetBy(dp.x, dp.y);
167 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700169 Rect& offsetTo(int32_t x, int32_t y);
170 Rect& offsetBy(int32_t x, int32_t y);
Jamie Gennis59332802012-05-07 13:49:17 -0700171
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700172 bool intersect(const Rect& with, Rect* result) const;
Jamie Gennis59332802012-05-07 13:49:17 -0700173
174 // Create a new Rect by transforming this one using a graphics HAL
175 // transform. This rectangle is defined in a coordinate space starting at
176 // the origin and extending to (width, height). If the transform includes
177 // a ROT90 then the output rectangle is defined in a space extending to
178 // (height, width). Otherwise the output rectangle is in the same space as
179 // the input.
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700180 Rect transform(uint32_t xform, int32_t width, int32_t height) const;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700181
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700182 // this calculates (Region(*this) - exclude).bounds() efficiently
183 Rect reduce(const Rect& exclude) const;
184
185
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700186 // for backward compatibility
187 inline int32_t width() const { return getWidth(); }
188 inline int32_t height() const { return getHeight(); }
189 inline void set(const Rect& rhs) { operator = (rhs); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190};
191
192ANDROID_BASIC_TYPES_TRAITS(Rect)
193
194}; // namespace android
195
196#endif // ANDROID_UI_RECT