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