Home | History | Annotate | Download | only in ui
      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/Flattenable.h>
     21 #include <utils/Log.h>
     22 #include <utils/TypeHelpers.h>
     23 #include <ui/Point.h>
     24 
     25 #include <android/rect.h>
     26 
     27 namespace android {
     28 
     29 class Rect : public ARect, public LightFlattenablePod<Rect>
     30 {
     31 public:
     32     typedef ARect::value_type value_type;
     33 
     34     static const Rect INVALID_RECT;
     35     static const Rect EMPTY_RECT;
     36 
     37     // we don't provide copy-ctor and operator= on purpose
     38     // because we want the compiler generated versions
     39 
     40     inline Rect() : Rect(INVALID_RECT) {}
     41 
     42     template <typename T>
     43     inline Rect(T w, T h) {
     44         if (w > INT32_MAX) {
     45             ALOG(LOG_WARN, "Rect",
     46                     "Width %u too large for Rect class, clamping", w);
     47             w = INT32_MAX;
     48         }
     49         if (h > INT32_MAX) {
     50             ALOG(LOG_WARN, "Rect",
     51                     "Height %u too large for Rect class, clamping", h);
     52             h = INT32_MAX;
     53         }
     54         left = top = 0;
     55         right = static_cast<int32_t>(w);
     56         bottom = static_cast<int32_t>(h);
     57     }
     58 
     59     inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
     60         left = l;
     61         top = t;
     62         right = r;
     63         bottom = b;
     64     }
     65 
     66     inline Rect(const Point& lt, const Point& rb) {
     67         left = lt.x;
     68         top = lt.y;
     69         right = rb.x;
     70         bottom = rb.y;
     71     }
     72 
     73     void makeInvalid();
     74 
     75     inline void clear() {
     76         left = top = right = bottom = 0;
     77     }
     78 
     79     // a valid rectangle has a non negative width and height
     80     inline bool isValid() const {
     81         return (getWidth() >= 0) && (getHeight() >= 0);
     82     }
     83 
     84     // an empty rect has a zero width or height, or is invalid
     85     inline bool isEmpty() const {
     86         return (getWidth() <= 0) || (getHeight() <= 0);
     87     }
     88 
     89     // rectangle's width
     90     inline int32_t getWidth() const {
     91         return right - left;
     92     }
     93 
     94     // rectangle's height
     95     inline int32_t getHeight() const {
     96         return bottom - top;
     97     }
     98 
     99     inline Rect getBounds() const {
    100         return Rect(right - left, bottom - top);
    101     }
    102 
    103     void setLeftTop(const Point& lt) {
    104         left = lt.x;
    105         top = lt.y;
    106     }
    107 
    108     void setRightBottom(const Point& rb) {
    109         right = rb.x;
    110         bottom = rb.y;
    111     }
    112 
    113     // the following 4 functions return the 4 corners of the rect as Point
    114     Point leftTop() const {
    115         return Point(left, top);
    116     }
    117     Point rightBottom() const {
    118         return Point(right, bottom);
    119     }
    120     Point rightTop() const {
    121         return Point(right, top);
    122     }
    123     Point leftBottom() const {
    124         return Point(left, bottom);
    125     }
    126 
    127     // comparisons
    128     inline bool operator == (const Rect& rhs) const {
    129         return (left == rhs.left) && (top == rhs.top) &&
    130                (right == rhs.right) && (bottom == rhs.bottom);
    131     }
    132 
    133     inline bool operator != (const Rect& rhs) const {
    134         return !operator == (rhs);
    135     }
    136 
    137     // operator < defines an order which allows to use rectangles in sorted
    138     // vectors.
    139     bool operator < (const Rect& rhs) const;
    140 
    141     const Rect operator + (const Point& rhs) const;
    142     const Rect operator - (const Point& rhs) const;
    143 
    144     Rect& operator += (const Point& rhs) {
    145         return offsetBy(rhs.x, rhs.y);
    146     }
    147     Rect& operator -= (const Point& rhs) {
    148         return offsetBy(-rhs.x, -rhs.y);
    149     }
    150 
    151     Rect& offsetToOrigin() {
    152         right -= left;
    153         bottom -= top;
    154         left = top = 0;
    155         return *this;
    156     }
    157     Rect& offsetTo(const Point& p) {
    158         return offsetTo(p.x, p.y);
    159     }
    160     Rect& offsetBy(const Point& dp) {
    161         return offsetBy(dp.x, dp.y);
    162     }
    163 
    164     Rect& offsetTo(int32_t x, int32_t y);
    165     Rect& offsetBy(int32_t x, int32_t y);
    166 
    167     bool intersect(const Rect& with, Rect* result) const;
    168 
    169     // Create a new Rect by transforming this one using a graphics HAL
    170     // transform.  This rectangle is defined in a coordinate space starting at
    171     // the origin and extending to (width, height).  If the transform includes
    172     // a ROT90 then the output rectangle is defined in a space extending to
    173     // (height, width).  Otherwise the output rectangle is in the same space as
    174     // the input.
    175     Rect transform(uint32_t xform, int32_t width, int32_t height) const;
    176 
    177     // this calculates (Region(*this) - exclude).bounds() efficiently
    178     Rect reduce(const Rect& exclude) const;
    179 
    180 
    181     // for backward compatibility
    182     inline int32_t width() const { return getWidth(); }
    183     inline int32_t height() const { return getHeight(); }
    184     inline void set(const Rect& rhs) { operator = (rhs); }
    185 };
    186 
    187 ANDROID_BASIC_TYPES_TRAITS(Rect)
    188 
    189 }; // namespace android
    190 
    191 #endif // ANDROID_UI_RECT
    192