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