blob: ce33d4e0313a1c266758f648cfb3d98200f34181 [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>
Mark Salyzyn7823e122016-09-29 08:08:05 -070024#include <log/log.h>
25
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) {
48 ALOG(LOG_WARN, "Rect",
49 "Width %u too large for Rect class, clamping", w);
50 w = INT32_MAX;
51 }
52 if (h > INT32_MAX) {
53 ALOG(LOG_WARN, "Rect",
54 "Height %u too large for Rect class, clamping", h);
55 h = INT32_MAX;
56 }
57 left = top = 0;
Dan Stoza4e643af2016-01-08 13:22:49 -080058 right = static_cast<int32_t>(w);
59 bottom = static_cast<int32_t>(h);
Dan Stozadd883c02014-11-18 10:24:03 -080060 }
61
Dianne Hackborn9147d112010-07-09 11:44:11 -070062 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070063 left = l;
64 top = t;
65 right = r;
66 bottom = b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070068
Dianne Hackborn9147d112010-07-09 11:44:11 -070069 inline Rect(const Point& lt, const Point& rb) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070070 left = lt.x;
71 top = lt.y;
72 right = rb.x;
73 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 }
75
76 void makeInvalid();
Mathias Agopian4b8160f2009-05-27 15:02:35 -070077
78 inline void clear() {
79 left = top = right = bottom = 0;
80 }
81
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 // a valid rectangle has a non negative width and height
83 inline bool isValid() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070084 return (getWidth() >= 0) && (getHeight() >= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 }
86
87 // an empty rect has a zero width or height, or is invalid
88 inline bool isEmpty() 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 // rectangle's width
Mathias Agopianb82203a2012-05-13 20:02:04 -070093 inline int32_t getWidth() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070094 return right - left;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070096
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 // rectangle's height
Mathias Agopianb82203a2012-05-13 20:02:04 -070098 inline int32_t getHeight() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070099 return bottom - top;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 }
101
Mathias Agopianb82203a2012-05-13 20:02:04 -0700102 inline Rect getBounds() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700103 return Rect(right - left, bottom - top);
Mathias Agopianb82203a2012-05-13 20:02:04 -0700104 }
105
Mathias Agopian35801ce2009-05-26 17:44:57 -0700106 void setLeftTop(const Point& lt) {
107 left = lt.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700108 top = lt.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700110
111 void setRightBottom(const Point& rb) {
112 right = rb.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700113 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 }
115
116 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian35801ce2009-05-26 17:44:57 -0700117 Point leftTop() const {
118 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700120 Point rightBottom() const {
121 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 }
123 Point rightTop() const {
124 return Point(right, top);
125 }
126 Point leftBottom() const {
127 return Point(left, bottom);
128 }
129
130 // comparisons
131 inline bool operator == (const Rect& rhs) const {
132 return (left == rhs.left) && (top == rhs.top) &&
133 (right == rhs.right) && (bottom == rhs.bottom);
134 }
135
136 inline bool operator != (const Rect& rhs) const {
137 return !operator == (rhs);
138 }
139
140 // operator < defines an order which allows to use rectangles in sorted
141 // vectors.
142 bool operator < (const Rect& rhs) const;
143
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700144 const Rect operator + (const Point& rhs) const;
145 const Rect operator - (const Point& rhs) const;
146
147 Rect& operator += (const Point& rhs) {
148 return offsetBy(rhs.x, rhs.y);
149 }
150 Rect& operator -= (const Point& rhs) {
151 return offsetBy(-rhs.x, -rhs.y);
152 }
153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 Rect& offsetToOrigin() {
155 right -= left;
156 bottom -= top;
157 left = top = 0;
158 return *this;
159 }
160 Rect& offsetTo(const Point& p) {
161 return offsetTo(p.x, p.y);
162 }
163 Rect& offsetBy(const Point& dp) {
164 return offsetBy(dp.x, dp.y);
165 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700167 Rect& offsetTo(int32_t x, int32_t y);
168 Rect& offsetBy(int32_t x, int32_t y);
Jamie Gennis59332802012-05-07 13:49:17 -0700169
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700170 bool intersect(const Rect& with, Rect* result) const;
Jamie Gennis59332802012-05-07 13:49:17 -0700171
172 // Create a new Rect by transforming this one using a graphics HAL
173 // transform. This rectangle is defined in a coordinate space starting at
174 // the origin and extending to (width, height). If the transform includes
175 // a ROT90 then the output rectangle is defined in a space extending to
176 // (height, width). Otherwise the output rectangle is in the same space as
177 // the input.
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700178 Rect transform(uint32_t xform, int32_t width, int32_t height) const;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700179
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700180 // this calculates (Region(*this) - exclude).bounds() efficiently
181 Rect reduce(const Rect& exclude) const;
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); }
Dan Stoza71bded52016-10-19 11:10:33 -0700187
188 gfx::FloatRect toFloatRect() const {
189 return {static_cast<float>(left), static_cast<float>(top),
190 static_cast<float>(right), static_cast<float>(bottom)};
191 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192};
193
194ANDROID_BASIC_TYPES_TRAITS(Rect)
195
196}; // namespace android
197
198#endif // ANDROID_UI_RECT