blob: 31e28d27f7074712454c93bf4c986f59a1a1ab89 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <utils/TypeHelpers.h>
22#include <ui/Point.h>
23
Dianne Hackborn9147d112010-07-09 11:44:11 -070024#include <android/rect.h>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026namespace android {
27
Mathias Agopian8683fca2012-08-12 19:37:16 -070028class Rect : public ARect, public LightFlattenablePod<Rect>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029{
30public:
Mathias Agopian000e95e2011-09-19 16:00:46 -070031 typedef ARect::value_type value_type;
Mathias Agopian4b8160f2009-05-27 15:02:35 -070032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033 // we don't provide copy-ctor and operator= on purpose
34 // because we want the compiler generated versions
35
Mathias Agopian35801ce2009-05-26 17:44:57 -070036 inline Rect() {
Dmitriy Ivanovb2e19202014-10-28 16:41:10 -070037 left = right = top = bottom = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070039
Dianne Hackborn9147d112010-07-09 11:44:11 -070040 inline Rect(int32_t w, int32_t h) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070041 left = top = 0;
42 right = w;
43 bottom = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070045
Dianne Hackborn9147d112010-07-09 11:44:11 -070046 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070047 left = l;
48 top = t;
49 right = r;
50 bottom = b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070052
Dianne Hackborn9147d112010-07-09 11:44:11 -070053 inline Rect(const Point& lt, const Point& rb) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070054 left = lt.x;
55 top = lt.y;
56 right = rb.x;
57 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058 }
59
60 void makeInvalid();
Mathias Agopian4b8160f2009-05-27 15:02:35 -070061
62 inline void clear() {
63 left = top = right = bottom = 0;
64 }
65
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 // a valid rectangle has a non negative width and height
67 inline bool isValid() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070068 return (getWidth() >= 0) && (getHeight() >= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 }
70
71 // an empty rect has a zero width or height, or is invalid
72 inline bool isEmpty() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070073 return (getWidth() <= 0) || (getHeight() <= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 }
75
76 // rectangle's width
Mathias Agopianb82203a2012-05-13 20:02:04 -070077 inline int32_t getWidth() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070078 return right - left;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 // rectangle's height
Mathias Agopianb82203a2012-05-13 20:02:04 -070082 inline int32_t getHeight() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070083 return bottom - top;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 }
85
Mathias Agopianb82203a2012-05-13 20:02:04 -070086 inline Rect getBounds() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070087 return Rect(right - left, bottom - top);
Mathias Agopianb82203a2012-05-13 20:02:04 -070088 }
89
Mathias Agopian35801ce2009-05-26 17:44:57 -070090 void setLeftTop(const Point& lt) {
91 left = lt.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070092 top = lt.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 }
Mathias Agopian35801ce2009-05-26 17:44:57 -070094
95 void setRightBottom(const Point& rb) {
96 right = rb.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070097 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 }
99
100 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian35801ce2009-05-26 17:44:57 -0700101 Point leftTop() const {
102 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700104 Point rightBottom() const {
105 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 }
107 Point rightTop() const {
108 return Point(right, top);
109 }
110 Point leftBottom() const {
111 return Point(left, bottom);
112 }
113
114 // comparisons
115 inline bool operator == (const Rect& rhs) const {
116 return (left == rhs.left) && (top == rhs.top) &&
117 (right == rhs.right) && (bottom == rhs.bottom);
118 }
119
120 inline bool operator != (const Rect& rhs) const {
121 return !operator == (rhs);
122 }
123
124 // operator < defines an order which allows to use rectangles in sorted
125 // vectors.
126 bool operator < (const Rect& rhs) const;
127
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700128 const Rect operator + (const Point& rhs) const;
129 const Rect operator - (const Point& rhs) const;
130
131 Rect& operator += (const Point& rhs) {
132 return offsetBy(rhs.x, rhs.y);
133 }
134 Rect& operator -= (const Point& rhs) {
135 return offsetBy(-rhs.x, -rhs.y);
136 }
137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 Rect& offsetToOrigin() {
139 right -= left;
140 bottom -= top;
141 left = top = 0;
142 return *this;
143 }
144 Rect& offsetTo(const Point& p) {
145 return offsetTo(p.x, p.y);
146 }
147 Rect& offsetBy(const Point& dp) {
148 return offsetBy(dp.x, dp.y);
149 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700151 Rect& offsetTo(int32_t x, int32_t y);
152 Rect& offsetBy(int32_t x, int32_t y);
Jamie Gennis59332802012-05-07 13:49:17 -0700153
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700154 bool intersect(const Rect& with, Rect* result) const;
Jamie Gennis59332802012-05-07 13:49:17 -0700155
156 // Create a new Rect by transforming this one using a graphics HAL
157 // transform. This rectangle is defined in a coordinate space starting at
158 // the origin and extending to (width, height). If the transform includes
159 // a ROT90 then the output rectangle is defined in a space extending to
160 // (height, width). Otherwise the output rectangle is in the same space as
161 // the input.
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700162 Rect transform(uint32_t xform, int32_t width, int32_t height) const;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700163
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700164 // this calculates (Region(*this) - exclude).bounds() efficiently
165 Rect reduce(const Rect& exclude) const;
166
167
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700168 // for backward compatibility
169 inline int32_t width() const { return getWidth(); }
170 inline int32_t height() const { return getHeight(); }
171 inline void set(const Rect& rhs) { operator = (rhs); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172};
173
174ANDROID_BASIC_TYPES_TRAITS(Rect)
175
176}; // namespace android
177
178#endif // ANDROID_UI_RECT