blob: b50e4ec65691a9746c1468329b53f692f470c8e8 [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
Dan Stoza5a423ea2017-02-16 14:10:39 -080025#include <ui/FloatRect.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <ui/Point.h>
27
Dianne Hackborn9147d112010-07-09 11:44:11 -070028#include <android/rect.h>
29
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030namespace android {
31
Mathias Agopian8683fca2012-08-12 19:37:16 -070032class Rect : public ARect, public LightFlattenablePod<Rect>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033{
34public:
Mathias Agopian000e95e2011-09-19 16:00:46 -070035 typedef ARect::value_type value_type;
Mathias Agopian4b8160f2009-05-27 15:02:35 -070036
Dan Stoza5065a552015-03-17 16:23:42 -070037 static const Rect INVALID_RECT;
Pablo Ceballos60d69222015-08-07 14:47:20 -070038 static const Rect EMPTY_RECT;
Dan Stoza5065a552015-03-17 16:23:42 -070039
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040 // we don't provide copy-ctor and operator= on purpose
41 // because we want the compiler generated versions
42
Pablo Ceballos60d69222015-08-07 14:47:20 -070043 inline Rect() : Rect(INVALID_RECT) {}
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070044
Dan Stoza4e643af2016-01-08 13:22:49 -080045 template <typename T>
46 inline Rect(T w, T h) {
Dan Stozadd883c02014-11-18 10:24:03 -080047 if (w > INT32_MAX) {
Dan Stozadd883c02014-11-18 10:24:03 -080048 w = INT32_MAX;
49 }
50 if (h > INT32_MAX) {
Dan Stozadd883c02014-11-18 10:24:03 -080051 h = INT32_MAX;
52 }
53 left = top = 0;
Dan Stoza4e643af2016-01-08 13:22:49 -080054 right = static_cast<int32_t>(w);
55 bottom = static_cast<int32_t>(h);
Dan Stozadd883c02014-11-18 10:24:03 -080056 }
57
Dianne Hackborn9147d112010-07-09 11:44:11 -070058 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070059 left = l;
60 top = t;
61 right = r;
62 bottom = b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070064
Dianne Hackborn9147d112010-07-09 11:44:11 -070065 inline Rect(const Point& lt, const Point& rb) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070066 left = lt.x;
67 top = lt.y;
68 right = rb.x;
69 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 }
71
72 void makeInvalid();
Mathias Agopian4b8160f2009-05-27 15:02:35 -070073
74 inline void clear() {
75 left = top = right = bottom = 0;
76 }
77
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 // a valid rectangle has a non negative width and height
79 inline bool isValid() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070080 return (getWidth() >= 0) && (getHeight() >= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 }
82
83 // an empty rect has a zero width or height, or is invalid
84 inline bool isEmpty() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070085 return (getWidth() <= 0) || (getHeight() <= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 }
87
88 // rectangle's width
Mathias Agopianb82203a2012-05-13 20:02:04 -070089 inline int32_t getWidth() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070090 return right - left;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070092
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 // rectangle's height
Mathias Agopianb82203a2012-05-13 20:02:04 -070094 inline int32_t getHeight() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070095 return bottom - top;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 }
97
Mathias Agopianb82203a2012-05-13 20:02:04 -070098 inline Rect getBounds() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070099 return Rect(right - left, bottom - top);
Mathias Agopianb82203a2012-05-13 20:02:04 -0700100 }
101
Mathias Agopian35801ce2009-05-26 17:44:57 -0700102 void setLeftTop(const Point& lt) {
103 left = lt.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700104 top = lt.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700106
107 void setRightBottom(const Point& rb) {
108 right = rb.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700109 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 }
111
112 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian35801ce2009-05-26 17:44:57 -0700113 Point leftTop() const {
114 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700116 Point rightBottom() const {
117 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 }
119 Point rightTop() const {
120 return Point(right, top);
121 }
122 Point leftBottom() const {
123 return Point(left, bottom);
124 }
125
126 // comparisons
127 inline bool operator == (const Rect& rhs) const {
128 return (left == rhs.left) && (top == rhs.top) &&
129 (right == rhs.right) && (bottom == rhs.bottom);
130 }
131
132 inline bool operator != (const Rect& rhs) const {
133 return !operator == (rhs);
134 }
135
136 // operator < defines an order which allows to use rectangles in sorted
137 // vectors.
138 bool operator < (const Rect& rhs) const;
139
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700140 const Rect operator + (const Point& rhs) const;
141 const Rect operator - (const Point& rhs) const;
142
143 Rect& operator += (const Point& rhs) {
144 return offsetBy(rhs.x, rhs.y);
145 }
146 Rect& operator -= (const Point& rhs) {
147 return offsetBy(-rhs.x, -rhs.y);
148 }
149
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 Rect& offsetToOrigin() {
151 right -= left;
152 bottom -= top;
153 left = top = 0;
154 return *this;
155 }
156 Rect& offsetTo(const Point& p) {
157 return offsetTo(p.x, p.y);
158 }
159 Rect& offsetBy(const Point& dp) {
160 return offsetBy(dp.x, dp.y);
161 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700163 Rect& offsetTo(int32_t x, int32_t y);
164 Rect& offsetBy(int32_t x, int32_t y);
Jamie Gennis59332802012-05-07 13:49:17 -0700165
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700166 bool intersect(const Rect& with, Rect* result) const;
Jamie Gennis59332802012-05-07 13:49:17 -0700167
168 // Create a new Rect by transforming this one using a graphics HAL
169 // transform. This rectangle is defined in a coordinate space starting at
170 // the origin and extending to (width, height). If the transform includes
171 // a ROT90 then the output rectangle is defined in a space extending to
172 // (height, width). Otherwise the output rectangle is in the same space as
173 // the input.
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700174 Rect transform(uint32_t xform, int32_t width, int32_t height) const;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700175
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700176 // this calculates (Region(*this) - exclude).bounds() efficiently
177 Rect reduce(const Rect& exclude) const;
178
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700179 // for backward compatibility
180 inline int32_t width() const { return getWidth(); }
181 inline int32_t height() const { return getHeight(); }
182 inline void set(const Rect& rhs) { operator = (rhs); }
Dan Stoza71bded52016-10-19 11:10:33 -0700183
Dan Stoza5a423ea2017-02-16 14:10:39 -0800184 FloatRect toFloatRect() const {
Dan Stoza71bded52016-10-19 11:10:33 -0700185 return {static_cast<float>(left), static_cast<float>(top),
186 static_cast<float>(right), static_cast<float>(bottom)};
187 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188};
189
190ANDROID_BASIC_TYPES_TRAITS(Rect)
191
192}; // namespace android
193
194#endif // ANDROID_UI_RECT