blob: e9859fe5c0266b8e96504d987438d49abe4889d0 [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>
Mark Salyzyn7823e122016-09-29 08:08:05 -070023#include <log/log.h>
24
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <ui/Point.h>
26
Dianne Hackborn9147d112010-07-09 11:44:11 -070027#include <android/rect.h>
28
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029namespace android {
30
Mathias Agopian8683fca2012-08-12 19:37:16 -070031class Rect : public ARect, public LightFlattenablePod<Rect>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032{
33public:
Mathias Agopian000e95e2011-09-19 16:00:46 -070034 typedef ARect::value_type value_type;
Mathias Agopian4b8160f2009-05-27 15:02:35 -070035
Dan Stoza5065a552015-03-17 16:23:42 -070036 static const Rect INVALID_RECT;
Pablo Ceballos60d69222015-08-07 14:47:20 -070037 static const Rect EMPTY_RECT;
Dan Stoza5065a552015-03-17 16:23:42 -070038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039 // we don't provide copy-ctor and operator= on purpose
40 // because we want the compiler generated versions
41
Pablo Ceballos60d69222015-08-07 14:47:20 -070042 inline Rect() : Rect(INVALID_RECT) {}
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070043
Dan Stoza4e643af2016-01-08 13:22:49 -080044 template <typename T>
45 inline Rect(T w, T h) {
Dan Stozadd883c02014-11-18 10:24:03 -080046 if (w > INT32_MAX) {
47 ALOG(LOG_WARN, "Rect",
48 "Width %u too large for Rect class, clamping", w);
49 w = INT32_MAX;
50 }
51 if (h > INT32_MAX) {
52 ALOG(LOG_WARN, "Rect",
53 "Height %u too large for Rect class, clamping", h);
54 h = INT32_MAX;
55 }
56 left = top = 0;
Dan Stoza4e643af2016-01-08 13:22:49 -080057 right = static_cast<int32_t>(w);
58 bottom = static_cast<int32_t>(h);
Dan Stozadd883c02014-11-18 10:24:03 -080059 }
60
Dianne Hackborn9147d112010-07-09 11:44:11 -070061 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070062 left = l;
63 top = t;
64 right = r;
65 bottom = b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070067
Dianne Hackborn9147d112010-07-09 11:44:11 -070068 inline Rect(const Point& lt, const Point& rb) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070069 left = lt.x;
70 top = lt.y;
71 right = rb.x;
72 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 }
74
75 void makeInvalid();
Mathias Agopian4b8160f2009-05-27 15:02:35 -070076
77 inline void clear() {
78 left = top = right = bottom = 0;
79 }
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 // a valid rectangle has a non negative width and height
82 inline bool isValid() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070083 return (getWidth() >= 0) && (getHeight() >= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 }
85
86 // an empty rect has a zero width or height, or is invalid
87 inline bool isEmpty() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070088 return (getWidth() <= 0) || (getHeight() <= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 }
90
91 // rectangle's width
Mathias Agopianb82203a2012-05-13 20:02:04 -070092 inline int32_t getWidth() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070093 return right - left;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070095
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 // rectangle's height
Mathias Agopianb82203a2012-05-13 20:02:04 -070097 inline int32_t getHeight() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070098 return bottom - top;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 }
100
Mathias Agopianb82203a2012-05-13 20:02:04 -0700101 inline Rect getBounds() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700102 return Rect(right - left, bottom - top);
Mathias Agopianb82203a2012-05-13 20:02:04 -0700103 }
104
Mathias Agopian35801ce2009-05-26 17:44:57 -0700105 void setLeftTop(const Point& lt) {
106 left = lt.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700107 top = lt.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700109
110 void setRightBottom(const Point& rb) {
111 right = rb.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700112 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 }
114
115 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian35801ce2009-05-26 17:44:57 -0700116 Point leftTop() const {
117 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700119 Point rightBottom() const {
120 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 }
122 Point rightTop() const {
123 return Point(right, top);
124 }
125 Point leftBottom() const {
126 return Point(left, bottom);
127 }
128
129 // comparisons
130 inline bool operator == (const Rect& rhs) const {
131 return (left == rhs.left) && (top == rhs.top) &&
132 (right == rhs.right) && (bottom == rhs.bottom);
133 }
134
135 inline bool operator != (const Rect& rhs) const {
136 return !operator == (rhs);
137 }
138
139 // operator < defines an order which allows to use rectangles in sorted
140 // vectors.
141 bool operator < (const Rect& rhs) const;
142
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700143 const Rect operator + (const Point& rhs) const;
144 const Rect operator - (const Point& rhs) const;
145
146 Rect& operator += (const Point& rhs) {
147 return offsetBy(rhs.x, rhs.y);
148 }
149 Rect& operator -= (const Point& rhs) {
150 return offsetBy(-rhs.x, -rhs.y);
151 }
152
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 Rect& offsetToOrigin() {
154 right -= left;
155 bottom -= top;
156 left = top = 0;
157 return *this;
158 }
159 Rect& offsetTo(const Point& p) {
160 return offsetTo(p.x, p.y);
161 }
162 Rect& offsetBy(const Point& dp) {
163 return offsetBy(dp.x, dp.y);
164 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700166 Rect& offsetTo(int32_t x, int32_t y);
167 Rect& offsetBy(int32_t x, int32_t y);
Jamie Gennis59332802012-05-07 13:49:17 -0700168
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700169 bool intersect(const Rect& with, Rect* result) const;
Jamie Gennis59332802012-05-07 13:49:17 -0700170
171 // Create a new Rect by transforming this one using a graphics HAL
172 // transform. This rectangle is defined in a coordinate space starting at
173 // the origin and extending to (width, height). If the transform includes
174 // a ROT90 then the output rectangle is defined in a space extending to
175 // (height, width). Otherwise the output rectangle is in the same space as
176 // the input.
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700177 Rect transform(uint32_t xform, int32_t width, int32_t height) const;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700178
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700179 // this calculates (Region(*this) - exclude).bounds() efficiently
180 Rect reduce(const Rect& exclude) const;
181
182
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700183 // for backward compatibility
184 inline int32_t width() const { return getWidth(); }
185 inline int32_t height() const { return getHeight(); }
186 inline void set(const Rect& rhs) { operator = (rhs); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187};
188
189ANDROID_BASIC_TYPES_TRAITS(Rect)
190
191}; // namespace android
192
193#endif // ANDROID_UI_RECT