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