michael@0: /* michael@0: * Copyright (C) 2006 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef ANDROID_UI_RECT michael@0: #define ANDROID_UI_RECT michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: michael@0: namespace android { michael@0: michael@0: class Rect : public ARect michael@0: { michael@0: public: michael@0: typedef ARect::value_type value_type; michael@0: michael@0: // we don't provide copy-ctor and operator= on purpose michael@0: // because we want the compiler generated versions michael@0: michael@0: inline Rect() { michael@0: } michael@0: inline Rect(int32_t w, int32_t h) { michael@0: left = top = 0; right = w; bottom = h; michael@0: } michael@0: inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) { michael@0: left = l; top = t; right = r; bottom = b; michael@0: } michael@0: inline Rect(const Point& lt, const Point& rb) { michael@0: left = lt.x; top = lt.y; right = rb.x; bottom = rb.y; michael@0: } michael@0: michael@0: void makeInvalid(); michael@0: michael@0: inline void clear() { michael@0: left = top = right = bottom = 0; michael@0: } michael@0: michael@0: // a valid rectangle has a non negative width and height michael@0: inline bool isValid() const { michael@0: return (width()>=0) && (height()>=0); michael@0: } michael@0: michael@0: // an empty rect has a zero width or height, or is invalid michael@0: inline bool isEmpty() const { michael@0: return (width()<=0) || (height()<=0); michael@0: } michael@0: michael@0: inline void set(const Rect& rhs) { michael@0: operator = (rhs); michael@0: } michael@0: michael@0: // rectangle's width michael@0: inline int32_t width() const { michael@0: return right-left; michael@0: } michael@0: michael@0: // rectangle's height michael@0: inline int32_t height() const { michael@0: return bottom-top; michael@0: } michael@0: michael@0: void setLeftTop(const Point& lt) { michael@0: left = lt.x; michael@0: top = lt.y; michael@0: } michael@0: michael@0: void setRightBottom(const Point& rb) { michael@0: right = rb.x; michael@0: bottom = rb.y; michael@0: } michael@0: michael@0: // the following 4 functions return the 4 corners of the rect as Point michael@0: Point leftTop() const { michael@0: return Point(left, top); michael@0: } michael@0: Point rightBottom() const { michael@0: return Point(right, bottom); michael@0: } michael@0: Point rightTop() const { michael@0: return Point(right, top); michael@0: } michael@0: Point leftBottom() const { michael@0: return Point(left, bottom); michael@0: } michael@0: michael@0: // comparisons michael@0: inline bool operator == (const Rect& rhs) const { michael@0: return (left == rhs.left) && (top == rhs.top) && michael@0: (right == rhs.right) && (bottom == rhs.bottom); michael@0: } michael@0: michael@0: inline bool operator != (const Rect& rhs) const { michael@0: return !operator == (rhs); michael@0: } michael@0: michael@0: // operator < defines an order which allows to use rectangles in sorted michael@0: // vectors. michael@0: bool operator < (const Rect& rhs) const; michael@0: michael@0: Rect& offsetToOrigin() { michael@0: right -= left; michael@0: bottom -= top; michael@0: left = top = 0; michael@0: return *this; michael@0: } michael@0: Rect& offsetTo(const Point& p) { michael@0: return offsetTo(p.x, p.y); michael@0: } michael@0: Rect& offsetBy(const Point& dp) { michael@0: return offsetBy(dp.x, dp.y); michael@0: } michael@0: Rect& operator += (const Point& rhs) { michael@0: return offsetBy(rhs.x, rhs.y); michael@0: } michael@0: Rect& operator -= (const Point& rhs) { michael@0: return offsetBy(-rhs.x, -rhs.y); michael@0: } michael@0: const Rect operator + (const Point& rhs) const; michael@0: const Rect operator - (const Point& rhs) const; michael@0: michael@0: void translate(int32_t dx, int32_t dy) { // legacy, don't use. michael@0: offsetBy(dx, dy); michael@0: } michael@0: michael@0: Rect& offsetTo(int32_t x, int32_t y); michael@0: Rect& offsetBy(int32_t x, int32_t y); michael@0: bool intersect(const Rect& with, Rect* result) const; michael@0: }; michael@0: michael@0: ANDROID_BASIC_TYPES_TRAITS(Rect) michael@0: michael@0: }; // namespace android michael@0: michael@0: #endif // ANDROID_UI_RECT