blob: d4eea025b4706478c1fd77c4ef3d2fa4257677b7 [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
Dan Stoza71bded52016-10-19 11:10:33 -070020#include <gfx/FloatRect.h>
Mathias Agopian8683fca2012-08-12 19:37:16 -070021#include <utils/Flattenable.h>
Dan Stozadd883c02014-11-18 10:24:03 -080022#include <utils/Log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/TypeHelpers.h>
24#include <ui/Point.h>
25
Dianne Hackborn9147d112010-07-09 11:44:11 -070026#include <android/rect.h>
27
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028namespace android {
29
Mathias Agopian8683fca2012-08-12 19:37:16 -070030class Rect : public ARect, public LightFlattenablePod<Rect>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031{
32public:
Mathias Agopian000e95e2011-09-19 16:00:46 -070033 typedef ARect::value_type value_type;
Mathias Agopian4b8160f2009-05-27 15:02:35 -070034
Dan Stoza5065a552015-03-17 16:23:42 -070035 static const Rect INVALID_RECT;
Pablo Ceballos60d69222015-08-07 14:47:20 -070036 static const Rect EMPTY_RECT;
Dan Stoza5065a552015-03-17 16:23:42 -070037
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038 // we don't provide copy-ctor and operator= on purpose
39 // because we want the compiler generated versions
40
Pablo Ceballos60d69222015-08-07 14:47:20 -070041 inline Rect() : Rect(INVALID_RECT) {}
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070042
Dan Stoza4e643af2016-01-08 13:22:49 -080043 template <typename T>
44 inline Rect(T w, T h) {
Dan Stozadd883c02014-11-18 10:24:03 -080045 if (w > INT32_MAX) {
46 ALOG(LOG_WARN, "Rect",
47 "Width %u too large for Rect class, clamping", w);
48 w = INT32_MAX;
49 }
50 if (h > INT32_MAX) {
51 ALOG(LOG_WARN, "Rect",
52 "Height %u too large for Rect class, clamping", h);
53 h = INT32_MAX;
54 }
55 left = top = 0;
Dan Stoza4e643af2016-01-08 13:22:49 -080056 right = static_cast<int32_t>(w);
57 bottom = static_cast<int32_t>(h);
Dan Stozadd883c02014-11-18 10:24:03 -080058 }
59
Dianne Hackborn9147d112010-07-09 11:44:11 -070060 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070061 left = l;
62 top = t;
63 right = r;
64 bottom = b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070066
Dianne Hackborn9147d112010-07-09 11:44:11 -070067 inline Rect(const Point& lt, const Point& rb) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070068 left = lt.x;
69 top = lt.y;
70 right = rb.x;
71 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 }
73
74 void makeInvalid();
Mathias Agopian4b8160f2009-05-27 15:02:35 -070075
76 inline void clear() {
77 left = top = right = bottom = 0;
78 }
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 // a valid rectangle has a non negative width and height
81 inline bool isValid() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070082 return (getWidth() >= 0) && (getHeight() >= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 }
84
85 // an empty rect has a zero width or height, or is invalid
86 inline bool isEmpty() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070087 return (getWidth() <= 0) || (getHeight() <= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 }
89
90 // rectangle's width
Mathias Agopianb82203a2012-05-13 20:02:04 -070091 inline int32_t getWidth() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070092 return right - left;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070094
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 // rectangle's height
Mathias Agopianb82203a2012-05-13 20:02:04 -070096 inline int32_t getHeight() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070097 return bottom - top;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 }
99
Mathias Agopianb82203a2012-05-13 20:02:04 -0700100 inline Rect getBounds() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700101 return Rect(right - left, bottom - top);
Mathias Agopianb82203a2012-05-13 20:02:04 -0700102 }
103
Mathias Agopian35801ce2009-05-26 17:44:57 -0700104 void setLeftTop(const Point& lt) {
105 left = lt.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700106 top = lt.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700108
109 void setRightBottom(const Point& rb) {
110 right = rb.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700111 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 }
113
114 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian35801ce2009-05-26 17:44:57 -0700115 Point leftTop() const {
116 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700118 Point rightBottom() const {
119 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 }
121 Point rightTop() const {
122 return Point(right, top);
123 }
124 Point leftBottom() const {
125 return Point(left, bottom);
126 }
127
128 // comparisons
129 inline bool operator == (const Rect& rhs) const {
130 return (left == rhs.left) && (top == rhs.top) &&
131 (right == rhs.right) && (bottom == rhs.bottom);
132 }
133
134 inline bool operator != (const Rect& rhs) const {
135 return !operator == (rhs);
136 }
137
138 // operator < defines an order which allows to use rectangles in sorted
139 // vectors.
140 bool operator < (const Rect& rhs) const;
141
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700142 const Rect operator + (const Point& rhs) const;
143 const Rect operator - (const Point& rhs) const;
144
145 Rect& operator += (const Point& rhs) {
146 return offsetBy(rhs.x, rhs.y);
147 }
148 Rect& operator -= (const Point& rhs) {
149 return offsetBy(-rhs.x, -rhs.y);
150 }
151
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 Rect& offsetToOrigin() {
153 right -= left;
154 bottom -= top;
155 left = top = 0;
156 return *this;
157 }
158 Rect& offsetTo(const Point& p) {
159 return offsetTo(p.x, p.y);
160 }
161 Rect& offsetBy(const Point& dp) {
162 return offsetBy(dp.x, dp.y);
163 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700165 Rect& offsetTo(int32_t x, int32_t y);
166 Rect& offsetBy(int32_t x, int32_t y);
Jamie Gennis59332802012-05-07 13:49:17 -0700167
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700168 bool intersect(const Rect& with, Rect* result) const;
Jamie Gennis59332802012-05-07 13:49:17 -0700169
170 // Create a new Rect by transforming this one using a graphics HAL
171 // transform. This rectangle is defined in a coordinate space starting at
172 // the origin and extending to (width, height). If the transform includes
173 // a ROT90 then the output rectangle is defined in a space extending to
174 // (height, width). Otherwise the output rectangle is in the same space as
175 // the input.
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700176 Rect transform(uint32_t xform, int32_t width, int32_t height) const;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700177
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700178 // this calculates (Region(*this) - exclude).bounds() efficiently
179 Rect reduce(const Rect& exclude) const;
180
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700181 // for backward compatibility
182 inline int32_t width() const { return getWidth(); }
183 inline int32_t height() const { return getHeight(); }
184 inline void set(const Rect& rhs) { operator = (rhs); }
Dan Stoza71bded52016-10-19 11:10:33 -0700185
186 gfx::FloatRect toFloatRect() const {
187 return {static_cast<float>(left), static_cast<float>(top),
188 static_cast<float>(right), static_cast<float>(bottom)};
189 }
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