| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 20 | #include <utils/Flattenable.h> | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 21 | #include <utils/Log.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/TypeHelpers.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 23 | #include <log/log.h> | 
|  | 24 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <ui/Point.h> | 
|  | 26 |  | 
| Dianne Hackborn | 9147d11 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 27 | #include <android/rect.h> | 
|  | 28 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | namespace android { | 
|  | 30 |  | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 31 | class Rect : public ARect, public LightFlattenablePod<Rect> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | { | 
|  | 33 | public: | 
| Mathias Agopian | 000e95e | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 34 | typedef ARect::value_type value_type; | 
| Mathias Agopian | 4b8160f | 2009-05-27 15:02:35 -0700 | [diff] [blame] | 35 |  | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 36 | static const Rect INVALID_RECT; | 
| Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 37 | static const Rect EMPTY_RECT; | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 38 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | // we don't provide copy-ctor and operator= on purpose | 
|  | 40 | // because we want the compiler generated versions | 
|  | 41 |  | 
| Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 42 | inline Rect() : Rect(INVALID_RECT) {} | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 43 |  | 
| Dan Stoza | 4e643af | 2016-01-08 13:22:49 -0800 | [diff] [blame] | 44 | template <typename T> | 
|  | 45 | inline Rect(T w, T h) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 46 | if (w > INT32_MAX) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 47 | w = INT32_MAX; | 
|  | 48 | } | 
|  | 49 | if (h > INT32_MAX) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 50 | h = INT32_MAX; | 
|  | 51 | } | 
|  | 52 | left = top = 0; | 
| Dan Stoza | 4e643af | 2016-01-08 13:22:49 -0800 | [diff] [blame] | 53 | right = static_cast<int32_t>(w); | 
|  | 54 | bottom = static_cast<int32_t>(h); | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 55 | } | 
|  | 56 |  | 
| Dianne Hackborn | 9147d11 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 57 | inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) { | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 58 | left = l; | 
|  | 59 | top = t; | 
|  | 60 | right = r; | 
|  | 61 | bottom = b; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 63 |  | 
| Dianne Hackborn | 9147d11 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 64 | inline Rect(const Point& lt, const Point& rb) { | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 65 | left = lt.x; | 
|  | 66 | top = lt.y; | 
|  | 67 | right = rb.x; | 
|  | 68 | bottom = rb.y; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
|  | 71 | void makeInvalid(); | 
| Mathias Agopian | 4b8160f | 2009-05-27 15:02:35 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | inline void clear() { | 
|  | 74 | left = top = right = bottom = 0; | 
|  | 75 | } | 
|  | 76 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | // a valid rectangle has a non negative width and height | 
|  | 78 | inline bool isValid() const { | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 79 | return (getWidth() >= 0) && (getHeight() >= 0); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
|  | 82 | // an empty rect has a zero width or height, or is invalid | 
|  | 83 | inline bool isEmpty() const { | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 84 | return (getWidth() <= 0) || (getHeight() <= 0); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
|  | 87 | // rectangle's width | 
| Mathias Agopian | b82203a | 2012-05-13 20:02:04 -0700 | [diff] [blame] | 88 | inline int32_t getWidth() const { | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 89 | return right - left; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 91 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | // rectangle's height | 
| Mathias Agopian | b82203a | 2012-05-13 20:02:04 -0700 | [diff] [blame] | 93 | inline int32_t getHeight() const { | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 94 | return bottom - top; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
| Mathias Agopian | b82203a | 2012-05-13 20:02:04 -0700 | [diff] [blame] | 97 | inline Rect getBounds() const { | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 98 | return Rect(right - left, bottom - top); | 
| Mathias Agopian | b82203a | 2012-05-13 20:02:04 -0700 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 101 | void setLeftTop(const Point& lt) { | 
|  | 102 | left = lt.x; | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 103 | top = lt.y; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | } | 
| Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 105 |  | 
|  | 106 | void setRightBottom(const Point& rb) { | 
|  | 107 | right = rb.x; | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 108 | bottom = rb.y; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
|  | 111 | // the following 4 functions return the 4 corners of the rect as Point | 
| Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 112 | Point leftTop() const { | 
|  | 113 | return Point(left, top); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | } | 
| Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 115 | Point rightBottom() const { | 
|  | 116 | return Point(right, bottom); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | } | 
|  | 118 | Point rightTop() const { | 
|  | 119 | return Point(right, top); | 
|  | 120 | } | 
|  | 121 | Point leftBottom() const { | 
|  | 122 | return Point(left, bottom); | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | // comparisons | 
|  | 126 | inline bool operator == (const Rect& rhs) const { | 
|  | 127 | return (left == rhs.left) && (top == rhs.top) && | 
|  | 128 | (right == rhs.right) && (bottom == rhs.bottom); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | inline bool operator != (const Rect& rhs) const { | 
|  | 132 | return !operator == (rhs); | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | // operator < defines an order which allows to use rectangles in sorted | 
|  | 136 | // vectors. | 
|  | 137 | bool operator < (const Rect& rhs) const; | 
|  | 138 |  | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 139 | const Rect operator + (const Point& rhs) const; | 
|  | 140 | const Rect operator - (const Point& rhs) const; | 
|  | 141 |  | 
|  | 142 | Rect& operator += (const Point& rhs) { | 
|  | 143 | return offsetBy(rhs.x, rhs.y); | 
|  | 144 | } | 
|  | 145 | Rect& operator -= (const Point& rhs) { | 
|  | 146 | return offsetBy(-rhs.x, -rhs.y); | 
|  | 147 | } | 
|  | 148 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | Rect& offsetToOrigin() { | 
|  | 150 | right -= left; | 
|  | 151 | bottom -= top; | 
|  | 152 | left = top = 0; | 
|  | 153 | return *this; | 
|  | 154 | } | 
|  | 155 | Rect& offsetTo(const Point& p) { | 
|  | 156 | return offsetTo(p.x, p.y); | 
|  | 157 | } | 
|  | 158 | Rect& offsetBy(const Point& dp) { | 
|  | 159 | return offsetBy(dp.x, dp.y); | 
|  | 160 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 |  | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 162 | Rect& offsetTo(int32_t x, int32_t y); | 
|  | 163 | Rect& offsetBy(int32_t x, int32_t y); | 
| Jamie Gennis | 5933280 | 2012-05-07 13:49:17 -0700 | [diff] [blame] | 164 |  | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 165 | bool intersect(const Rect& with, Rect* result) const; | 
| Jamie Gennis | 5933280 | 2012-05-07 13:49:17 -0700 | [diff] [blame] | 166 |  | 
|  | 167 | // Create a new Rect by transforming this one using a graphics HAL | 
|  | 168 | // transform.  This rectangle is defined in a coordinate space starting at | 
|  | 169 | // the origin and extending to (width, height).  If the transform includes | 
|  | 170 | // a ROT90 then the output rectangle is defined in a space extending to | 
|  | 171 | // (height, width).  Otherwise the output rectangle is in the same space as | 
|  | 172 | // the input. | 
| Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 173 | Rect transform(uint32_t xform, int32_t width, int32_t height) const; | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 174 |  | 
| Mathias Agopian | f3e85d4 | 2013-05-10 18:01:12 -0700 | [diff] [blame] | 175 | // this calculates (Region(*this) - exclude).bounds() efficiently | 
|  | 176 | Rect reduce(const Rect& exclude) const; | 
|  | 177 |  | 
|  | 178 |  | 
| Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 179 | // 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); } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | }; | 
|  | 184 |  | 
|  | 185 | ANDROID_BASIC_TYPES_TRAITS(Rect) | 
|  | 186 |  | 
|  | 187 | }; // namespace android | 
|  | 188 |  | 
|  | 189 | #endif // ANDROID_UI_RECT |