2009-03-04 03:31:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ANDROID_UI_RECT
|
|
|
|
#define ANDROID_UI_RECT
|
|
|
|
|
2012-08-13 02:37:16 +00:00
|
|
|
#include <utils/Flattenable.h>
|
2014-11-18 18:24:03 +00:00
|
|
|
#include <utils/Log.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
#include <utils/TypeHelpers.h>
|
|
|
|
#include <ui/Point.h>
|
|
|
|
|
2010-07-09 18:44:11 +00:00
|
|
|
#include <android/rect.h>
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
namespace android {
|
|
|
|
|
2012-08-13 02:37:16 +00:00
|
|
|
class Rect : public ARect, public LightFlattenablePod<Rect>
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
public:
|
2011-09-19 23:00:46 +00:00
|
|
|
typedef ARect::value_type value_type;
|
2009-05-27 22:02:35 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// we don't provide copy-ctor and operator= on purpose
|
|
|
|
// because we want the compiler generated versions
|
|
|
|
|
2009-05-27 00:44:57 +00:00
|
|
|
inline Rect() {
|
2014-10-28 23:41:10 +00:00
|
|
|
left = right = top = bottom = 0;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2013-05-10 03:37:10 +00:00
|
|
|
|
2010-07-09 18:44:11 +00:00
|
|
|
inline Rect(int32_t w, int32_t h) {
|
2013-05-10 03:37:10 +00:00
|
|
|
left = top = 0;
|
|
|
|
right = w;
|
|
|
|
bottom = h;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2013-05-10 03:37:10 +00:00
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
inline Rect(uint32_t w, uint32_t h) {
|
|
|
|
if (w > INT32_MAX) {
|
|
|
|
ALOG(LOG_WARN, "Rect",
|
|
|
|
"Width %u too large for Rect class, clamping", w);
|
|
|
|
w = INT32_MAX;
|
|
|
|
}
|
|
|
|
if (h > INT32_MAX) {
|
|
|
|
ALOG(LOG_WARN, "Rect",
|
|
|
|
"Height %u too large for Rect class, clamping", h);
|
|
|
|
h = INT32_MAX;
|
|
|
|
}
|
|
|
|
left = top = 0;
|
|
|
|
right = w;
|
|
|
|
bottom = h;
|
|
|
|
}
|
|
|
|
|
2010-07-09 18:44:11 +00:00
|
|
|
inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
|
2013-05-10 03:37:10 +00:00
|
|
|
left = l;
|
|
|
|
top = t;
|
|
|
|
right = r;
|
|
|
|
bottom = b;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2013-05-10 03:37:10 +00:00
|
|
|
|
2010-07-09 18:44:11 +00:00
|
|
|
inline Rect(const Point& lt, const Point& rb) {
|
2013-05-10 03:37:10 +00:00
|
|
|
left = lt.x;
|
|
|
|
top = lt.y;
|
|
|
|
right = rb.x;
|
|
|
|
bottom = rb.y;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void makeInvalid();
|
2009-05-27 22:02:35 +00:00
|
|
|
|
|
|
|
inline void clear() {
|
|
|
|
left = top = right = bottom = 0;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// a valid rectangle has a non negative width and height
|
|
|
|
inline bool isValid() const {
|
2013-05-10 03:37:10 +00:00
|
|
|
return (getWidth() >= 0) && (getHeight() >= 0);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// an empty rect has a zero width or height, or is invalid
|
|
|
|
inline bool isEmpty() const {
|
2013-05-10 03:37:10 +00:00
|
|
|
return (getWidth() <= 0) || (getHeight() <= 0);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// rectangle's width
|
2012-05-14 03:02:04 +00:00
|
|
|
inline int32_t getWidth() const {
|
2013-05-10 03:37:10 +00:00
|
|
|
return right - left;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2013-05-10 03:37:10 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// rectangle's height
|
2012-05-14 03:02:04 +00:00
|
|
|
inline int32_t getHeight() const {
|
2013-05-10 03:37:10 +00:00
|
|
|
return bottom - top;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2012-05-14 03:02:04 +00:00
|
|
|
inline Rect getBounds() const {
|
2013-05-10 03:37:10 +00:00
|
|
|
return Rect(right - left, bottom - top);
|
2012-05-14 03:02:04 +00:00
|
|
|
}
|
|
|
|
|
2009-05-27 00:44:57 +00:00
|
|
|
void setLeftTop(const Point& lt) {
|
|
|
|
left = lt.x;
|
2013-05-10 03:37:10 +00:00
|
|
|
top = lt.y;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-05-27 00:44:57 +00:00
|
|
|
|
|
|
|
void setRightBottom(const Point& rb) {
|
|
|
|
right = rb.x;
|
2013-05-10 03:37:10 +00:00
|
|
|
bottom = rb.y;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the following 4 functions return the 4 corners of the rect as Point
|
2009-05-27 00:44:57 +00:00
|
|
|
Point leftTop() const {
|
|
|
|
return Point(left, top);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-05-27 00:44:57 +00:00
|
|
|
Point rightBottom() const {
|
|
|
|
return Point(right, bottom);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
Point rightTop() const {
|
|
|
|
return Point(right, top);
|
|
|
|
}
|
|
|
|
Point leftBottom() const {
|
|
|
|
return Point(left, bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
// comparisons
|
|
|
|
inline bool operator == (const Rect& rhs) const {
|
|
|
|
return (left == rhs.left) && (top == rhs.top) &&
|
|
|
|
(right == rhs.right) && (bottom == rhs.bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator != (const Rect& rhs) const {
|
|
|
|
return !operator == (rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// operator < defines an order which allows to use rectangles in sorted
|
|
|
|
// vectors.
|
|
|
|
bool operator < (const Rect& rhs) const;
|
|
|
|
|
2013-05-10 03:37:10 +00:00
|
|
|
const Rect operator + (const Point& rhs) const;
|
|
|
|
const Rect operator - (const Point& rhs) const;
|
|
|
|
|
|
|
|
Rect& operator += (const Point& rhs) {
|
|
|
|
return offsetBy(rhs.x, rhs.y);
|
|
|
|
}
|
|
|
|
Rect& operator -= (const Point& rhs) {
|
|
|
|
return offsetBy(-rhs.x, -rhs.y);
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
Rect& offsetToOrigin() {
|
|
|
|
right -= left;
|
|
|
|
bottom -= top;
|
|
|
|
left = top = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Rect& offsetTo(const Point& p) {
|
|
|
|
return offsetTo(p.x, p.y);
|
|
|
|
}
|
|
|
|
Rect& offsetBy(const Point& dp) {
|
|
|
|
return offsetBy(dp.x, dp.y);
|
|
|
|
}
|
|
|
|
|
2013-05-10 03:37:10 +00:00
|
|
|
Rect& offsetTo(int32_t x, int32_t y);
|
|
|
|
Rect& offsetBy(int32_t x, int32_t y);
|
2012-05-07 20:49:17 +00:00
|
|
|
|
2013-05-10 03:37:10 +00:00
|
|
|
bool intersect(const Rect& with, Rect* result) const;
|
2012-05-07 20:49:17 +00:00
|
|
|
|
|
|
|
// Create a new Rect by transforming this one using a graphics HAL
|
|
|
|
// transform. This rectangle is defined in a coordinate space starting at
|
|
|
|
// the origin and extending to (width, height). If the transform includes
|
|
|
|
// a ROT90 then the output rectangle is defined in a space extending to
|
|
|
|
// (height, width). Otherwise the output rectangle is in the same space as
|
|
|
|
// the input.
|
2012-05-11 03:43:55 +00:00
|
|
|
Rect transform(uint32_t xform, int32_t width, int32_t height) const;
|
2013-05-10 03:37:10 +00:00
|
|
|
|
2013-05-11 01:01:12 +00:00
|
|
|
// this calculates (Region(*this) - exclude).bounds() efficiently
|
|
|
|
Rect reduce(const Rect& exclude) const;
|
|
|
|
|
|
|
|
|
2013-05-10 03:37:10 +00:00
|
|
|
// for backward compatibility
|
|
|
|
inline int32_t width() const { return getWidth(); }
|
|
|
|
inline int32_t height() const { return getHeight(); }
|
|
|
|
inline void set(const Rect& rhs) { operator = (rhs); }
|
2009-03-04 03:31:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ANDROID_BASIC_TYPES_TRAITS(Rect)
|
|
|
|
|
|
|
|
}; // namespace android
|
|
|
|
|
|
|
|
#endif // ANDROID_UI_RECT
|