blob: c099a026aa22219e2a7d19ad4317908a99290cc0 [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
Dan Stoza74a5e172017-12-20 15:57:52 -080072 inline explicit Rect(const FloatRect& floatRect) {
73 // Ideally we would use std::round, but we don't want to add an STL
74 // dependency here, so we use an approximation
75 left = static_cast<int32_t>(floatRect.left + 0.5f);
76 top = static_cast<int32_t>(floatRect.top + 0.5f);
77 right = static_cast<int32_t>(floatRect.right + 0.5f);
78 bottom = static_cast<int32_t>(floatRect.bottom + 0.5f);
79 }
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 void makeInvalid();
Mathias Agopian4b8160f2009-05-27 15:02:35 -070082
83 inline void clear() {
84 left = top = right = bottom = 0;
85 }
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 // a valid rectangle has a non negative width and height
88 inline bool isValid() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070089 return (getWidth() >= 0) && (getHeight() >= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 }
91
92 // an empty rect has a zero width or height, or is invalid
93 inline bool isEmpty() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070094 return (getWidth() <= 0) || (getHeight() <= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 }
96
97 // rectangle's width
Mathias Agopianb82203a2012-05-13 20:02:04 -070098 inline int32_t getWidth() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070099 return right - left;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 // rectangle's height
Mathias Agopianb82203a2012-05-13 20:02:04 -0700103 inline int32_t getHeight() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700104 return bottom - top;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 }
106
Mathias Agopianb82203a2012-05-13 20:02:04 -0700107 inline Rect getBounds() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700108 return Rect(right - left, bottom - top);
Mathias Agopianb82203a2012-05-13 20:02:04 -0700109 }
110
Mathias Agopian35801ce2009-05-26 17:44:57 -0700111 void setLeftTop(const Point& lt) {
112 left = lt.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700113 top = lt.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700115
116 void setRightBottom(const Point& rb) {
117 right = rb.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700118 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 }
120
121 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian35801ce2009-05-26 17:44:57 -0700122 Point leftTop() const {
123 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700125 Point rightBottom() const {
126 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 }
128 Point rightTop() const {
129 return Point(right, top);
130 }
131 Point leftBottom() const {
132 return Point(left, bottom);
133 }
134
135 // comparisons
136 inline bool operator == (const Rect& rhs) const {
137 return (left == rhs.left) && (top == rhs.top) &&
138 (right == rhs.right) && (bottom == rhs.bottom);
139 }
140
141 inline bool operator != (const Rect& rhs) const {
142 return !operator == (rhs);
143 }
144
145 // operator < defines an order which allows to use rectangles in sorted
146 // vectors.
147 bool operator < (const Rect& rhs) const;
148
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700149 const Rect operator + (const Point& rhs) const;
150 const Rect operator - (const Point& rhs) const;
151
152 Rect& operator += (const Point& rhs) {
153 return offsetBy(rhs.x, rhs.y);
154 }
155 Rect& operator -= (const Point& rhs) {
156 return offsetBy(-rhs.x, -rhs.y);
157 }
158
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 Rect& offsetToOrigin() {
160 right -= left;
161 bottom -= top;
162 left = top = 0;
163 return *this;
164 }
165 Rect& offsetTo(const Point& p) {
166 return offsetTo(p.x, p.y);
167 }
168 Rect& offsetBy(const Point& dp) {
169 return offsetBy(dp.x, dp.y);
170 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700172 Rect& offsetTo(int32_t x, int32_t y);
173 Rect& offsetBy(int32_t x, int32_t y);
Jamie Gennis59332802012-05-07 13:49:17 -0700174
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700175 bool intersect(const Rect& with, Rect* result) const;
Jamie Gennis59332802012-05-07 13:49:17 -0700176
177 // Create a new Rect by transforming this one using a graphics HAL
178 // transform. This rectangle is defined in a coordinate space starting at
179 // the origin and extending to (width, height). If the transform includes
180 // a ROT90 then the output rectangle is defined in a space extending to
181 // (height, width). Otherwise the output rectangle is in the same space as
182 // the input.
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700183 Rect transform(uint32_t xform, int32_t width, int32_t height) const;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700184
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700185 // this calculates (Region(*this) - exclude).bounds() efficiently
186 Rect reduce(const Rect& exclude) const;
187
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700188 // for backward compatibility
189 inline int32_t width() const { return getWidth(); }
190 inline int32_t height() const { return getHeight(); }
191 inline void set(const Rect& rhs) { operator = (rhs); }
Dan Stoza71bded52016-10-19 11:10:33 -0700192
Dan Stoza5a423ea2017-02-16 14:10:39 -0800193 FloatRect toFloatRect() const {
Dan Stoza71bded52016-10-19 11:10:33 -0700194 return {static_cast<float>(left), static_cast<float>(top),
195 static_cast<float>(right), static_cast<float>(bottom)};
196 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197};
198
199ANDROID_BASIC_TYPES_TRAITS(Rect)
200
201}; // namespace android
202
203#endif // ANDROID_UI_RECT