blob: 3886f93142e48b41556e70516357e15ec92a0241 [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>
23#include <ui/Point.h>
24
Dianne Hackborn9147d112010-07-09 11:44:11 -070025#include <android/rect.h>
26
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027namespace android {
28
Mathias Agopian8683fca2012-08-12 19:37:16 -070029class Rect : public ARect, public LightFlattenablePod<Rect>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030{
31public:
Mathias Agopian000e95e2011-09-19 16:00:46 -070032 typedef ARect::value_type value_type;
Mathias Agopian4b8160f2009-05-27 15:02:35 -070033
Dan Stoza5065a552015-03-17 16:23:42 -070034 static const Rect INVALID_RECT;
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036 // we don't provide copy-ctor and operator= on purpose
37 // because we want the compiler generated versions
38
Mathias Agopian35801ce2009-05-26 17:44:57 -070039 inline Rect() {
Dmitriy Ivanovbccab862014-10-28 16:41:10 -070040 left = right = top = bottom = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070042
Dianne Hackborn9147d112010-07-09 11:44:11 -070043 inline Rect(int32_t w, int32_t h) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070044 left = top = 0;
45 right = w;
46 bottom = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070048
Dan Stozadd883c02014-11-18 10:24:03 -080049 inline Rect(uint32_t w, uint32_t h) {
50 if (w > INT32_MAX) {
51 ALOG(LOG_WARN, "Rect",
52 "Width %u too large for Rect class, clamping", w);
53 w = INT32_MAX;
54 }
55 if (h > INT32_MAX) {
56 ALOG(LOG_WARN, "Rect",
57 "Height %u too large for Rect class, clamping", h);
58 h = INT32_MAX;
59 }
60 left = top = 0;
61 right = w;
62 bottom = h;
63 }
64
Dianne Hackborn9147d112010-07-09 11:44:11 -070065 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070066 left = l;
67 top = t;
68 right = r;
69 bottom = b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070071
Dianne Hackborn9147d112010-07-09 11:44:11 -070072 inline Rect(const Point& lt, const Point& rb) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070073 left = lt.x;
74 top = lt.y;
75 right = rb.x;
76 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 }
78
79 void makeInvalid();
Mathias Agopian4b8160f2009-05-27 15:02:35 -070080
81 inline void clear() {
82 left = top = right = bottom = 0;
83 }
84
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 // a valid rectangle has a non negative width and height
86 inline bool isValid() 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 // an empty rect has a zero width or height, or is invalid
91 inline bool isEmpty() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070092 return (getWidth() <= 0) || (getHeight() <= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 }
94
95 // rectangle's width
Mathias Agopianb82203a2012-05-13 20:02:04 -070096 inline int32_t getWidth() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070097 return right - left;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070099
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 // rectangle's height
Mathias Agopianb82203a2012-05-13 20:02:04 -0700101 inline int32_t getHeight() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700102 return bottom - top;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 }
104
Mathias Agopianb82203a2012-05-13 20:02:04 -0700105 inline Rect getBounds() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700106 return Rect(right - left, bottom - top);
Mathias Agopianb82203a2012-05-13 20:02:04 -0700107 }
108
Mathias Agopian35801ce2009-05-26 17:44:57 -0700109 void setLeftTop(const Point& lt) {
110 left = lt.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700111 top = lt.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700113
114 void setRightBottom(const Point& rb) {
115 right = rb.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700116 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 }
118
119 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian35801ce2009-05-26 17:44:57 -0700120 Point leftTop() const {
121 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700123 Point rightBottom() const {
124 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 }
126 Point rightTop() const {
127 return Point(right, top);
128 }
129 Point leftBottom() const {
130 return Point(left, bottom);
131 }
132
133 // comparisons
134 inline bool operator == (const Rect& rhs) const {
135 return (left == rhs.left) && (top == rhs.top) &&
136 (right == rhs.right) && (bottom == rhs.bottom);
137 }
138
139 inline bool operator != (const Rect& rhs) const {
140 return !operator == (rhs);
141 }
142
143 // operator < defines an order which allows to use rectangles in sorted
144 // vectors.
145 bool operator < (const Rect& rhs) const;
146
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700147 const Rect operator + (const Point& rhs) const;
148 const Rect operator - (const Point& rhs) const;
149
150 Rect& operator += (const Point& rhs) {
151 return offsetBy(rhs.x, rhs.y);
152 }
153 Rect& operator -= (const Point& rhs) {
154 return offsetBy(-rhs.x, -rhs.y);
155 }
156
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 Rect& offsetToOrigin() {
158 right -= left;
159 bottom -= top;
160 left = top = 0;
161 return *this;
162 }
163 Rect& offsetTo(const Point& p) {
164 return offsetTo(p.x, p.y);
165 }
166 Rect& offsetBy(const Point& dp) {
167 return offsetBy(dp.x, dp.y);
168 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700170 Rect& offsetTo(int32_t x, int32_t y);
171 Rect& offsetBy(int32_t x, int32_t y);
Jamie Gennis59332802012-05-07 13:49:17 -0700172
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700173 bool intersect(const Rect& with, Rect* result) const;
Jamie Gennis59332802012-05-07 13:49:17 -0700174
175 // Create a new Rect by transforming this one using a graphics HAL
176 // transform. This rectangle is defined in a coordinate space starting at
177 // the origin and extending to (width, height). If the transform includes
178 // a ROT90 then the output rectangle is defined in a space extending to
179 // (height, width). Otherwise the output rectangle is in the same space as
180 // the input.
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700181 Rect transform(uint32_t xform, int32_t width, int32_t height) const;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700182
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700183 // this calculates (Region(*this) - exclude).bounds() efficiently
184 Rect reduce(const Rect& exclude) const;
185
186
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700187 // for backward compatibility
188 inline int32_t width() const { return getWidth(); }
189 inline int32_t height() const { return getHeight(); }
190 inline void set(const Rect& rhs) { operator = (rhs); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191};
192
193ANDROID_BASIC_TYPES_TRAITS(Rect)
194
195}; // namespace android
196
197#endif // ANDROID_UI_RECT