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 <log/log.h>
     24 
     25 #include <ui/FloatRect.h>
     26 #include <ui/Point.h>
     27 #include <ui/Size.h>
     28 
     29 #include <android/rect.h>
     30 
     31 namespace android {
     32 
     33 class Rect : public ARect, public LightFlattenablePod<Rect>
     34 {
     35 public:
     36     typedef ARect::value_type value_type;
     37 
     38     static const Rect INVALID_RECT;
     39     static const Rect EMPTY_RECT;
     40 
     41     // we don't provide copy-ctor and operator= on purpose
     42     // because we want the compiler generated versions
     43 
     44     inline Rect() : Rect(INVALID_RECT) {}
     45 
     46     template <typename T>
     47     inline Rect(T w, T h) {
     48         if (w > INT32_MAX) {
     49             w = INT32_MAX;
     50         }
     51         if (h > INT32_MAX) {
     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     inline explicit Rect(const FloatRect& floatRect) {
     74         // Ideally we would use std::round, but we don't want to add an STL
     75         // dependency here, so we use an approximation
     76         left = static_cast<int32_t>(floatRect.left + 0.5f);
     77         top = static_cast<int32_t>(floatRect.top + 0.5f);
     78         right = static_cast<int32_t>(floatRect.right + 0.5f);
     79         bottom = static_cast<int32_t>(floatRect.bottom + 0.5f);
     80     }
     81 
     82     inline explicit Rect(const ui::Size& size) {
     83         left = 0;
     84         top = 0;
     85         right = size.width;
     86         bottom = size.height;
     87     }
     88 
     89     void makeInvalid();
     90 
     91     inline void clear() {
     92         left = top = right = bottom = 0;
     93     }
     94 
     95     // a valid rectangle has a non negative width and height
     96     inline bool isValid() const {
     97         return (getWidth() >= 0) && (getHeight() >= 0);
     98     }
     99 
    100     // an empty rect has a zero width or height, or is invalid
    101     inline bool isEmpty() const {
    102         return (getWidth() <= 0) || (getHeight() <= 0);
    103     }
    104 
    105     // rectangle's width
    106     __attribute__((no_sanitize("signed-integer-overflow")))
    107     inline int32_t getWidth() const {
    108         return right - left;
    109     }
    110 
    111     // rectangle's height
    112     __attribute__((no_sanitize("signed-integer-overflow")))
    113     inline int32_t getHeight() const {
    114         return bottom - top;
    115     }
    116 
    117     ui::Size getSize() const { return ui::Size(getWidth(), getHeight()); }
    118 
    119     __attribute__((no_sanitize("signed-integer-overflow")))
    120     inline Rect getBounds() const {
    121         return Rect(right - left, bottom - top);
    122     }
    123 
    124     void setLeftTop(const Point& lt) {
    125         left = lt.x;
    126         top = lt.y;
    127     }
    128 
    129     void setRightBottom(const Point& rb) {
    130         right = rb.x;
    131         bottom = rb.y;
    132     }
    133 
    134     // the following 4 functions return the 4 corners of the rect as Point
    135     Point leftTop() const {
    136         return Point(left, top);
    137     }
    138     Point rightBottom() const {
    139         return Point(right, bottom);
    140     }
    141     Point rightTop() const {
    142         return Point(right, top);
    143     }
    144     Point leftBottom() const {
    145         return Point(left, bottom);
    146     }
    147 
    148     // comparisons
    149     inline bool operator == (const Rect& rhs) const {
    150         return (left == rhs.left) && (top == rhs.top) &&
    151                (right == rhs.right) && (bottom == rhs.bottom);
    152     }
    153 
    154     inline bool operator != (const Rect& rhs) const {
    155         return !operator == (rhs);
    156     }
    157 
    158     // operator < defines an order which allows to use rectangles in sorted
    159     // vectors.
    160     bool operator < (const Rect& rhs) const;
    161 
    162     const Rect operator + (const Point& rhs) const;
    163     const Rect operator - (const Point& rhs) const;
    164 
    165     Rect& operator += (const Point& rhs) {
    166         return offsetBy(rhs.x, rhs.y);
    167     }
    168     Rect& operator -= (const Point& rhs) {
    169         return offsetBy(-rhs.x, -rhs.y);
    170     }
    171 
    172     Rect& offsetToOrigin() {
    173         right -= left;
    174         bottom -= top;
    175         left = top = 0;
    176         return *this;
    177     }
    178     Rect& offsetTo(const Point& p) {
    179         return offsetTo(p.x, p.y);
    180     }
    181     Rect& offsetBy(const Point& dp) {
    182         return offsetBy(dp.x, dp.y);
    183     }
    184 
    185     Rect& offsetTo(int32_t x, int32_t y);
    186     Rect& offsetBy(int32_t x, int32_t y);
    187 
    188     /**
    189      * Insets the rectangle on all sides specified by the insets.
    190      */
    191     Rect& inset(int32_t _left, int32_t _top, int32_t _right, int32_t _bottom);
    192 
    193     bool intersect(const Rect& with, Rect* result) const;
    194 
    195     // Create a new Rect by transforming this one using a graphics HAL
    196     // transform.  This rectangle is defined in a coordinate space starting at
    197     // the origin and extending to (width, height).  If the transform includes
    198     // a ROT90 then the output rectangle is defined in a space extending to
    199     // (height, width).  Otherwise the output rectangle is in the same space as
    200     // the input.
    201     Rect transform(uint32_t xform, int32_t width, int32_t height) const;
    202 
    203     // this calculates (Region(*this) - exclude).bounds() efficiently
    204     Rect reduce(const Rect& exclude) const;
    205 
    206     // for backward compatibility
    207     inline int32_t width() const { return getWidth(); }
    208     inline int32_t height() const { return getHeight(); }
    209     inline void set(const Rect& rhs) { operator = (rhs); }
    210 
    211     FloatRect toFloatRect() const {
    212         return {static_cast<float>(left), static_cast<float>(top),
    213                 static_cast<float>(right), static_cast<float>(bottom)};
    214     }
    215 };
    216 
    217 ANDROID_BASIC_TYPES_TRAITS(Rect)
    218 
    219 }; // namespace android
    220 
    221 #endif // ANDROID_UI_RECT
    222