2010-06-22 18:56:38 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 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.
|
|
|
|
*/
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
#ifndef ANDROID_HWUI_RECT_H
|
|
|
|
#define ANDROID_HWUI_RECT_H
|
|
|
|
|
|
|
|
#include <cmath>
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2010-06-27 22:59:20 -07:00
|
|
|
#include <utils/Log.h>
|
|
|
|
|
2010-06-22 18:56:38 -07:00
|
|
|
namespace android {
|
2010-06-24 19:30:36 -07:00
|
|
|
namespace uirenderer {
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2013-06-13 14:39:01 -07:00
|
|
|
#define RECT_STRING "%7.2f %7.2f %7.2f %7.2f"
|
2013-05-31 11:38:03 -07:00
|
|
|
#define RECT_ARGS(r) \
|
|
|
|
(r).left, (r).top, (r).right, (r).bottom
|
|
|
|
|
2010-06-22 18:56:38 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Structs
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-09-19 16:00:46 -07:00
|
|
|
class Rect {
|
|
|
|
public:
|
2010-06-25 13:46:18 -07:00
|
|
|
float left;
|
|
|
|
float top;
|
|
|
|
float right;
|
|
|
|
float bottom;
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
// Used by Region
|
|
|
|
typedef float value_type;
|
|
|
|
|
2011-09-19 16:00:46 -07:00
|
|
|
// we don't provide copy-ctor and operator= on purpose
|
|
|
|
// because we want the compiler generated versions
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
inline Rect():
|
2010-06-25 13:46:18 -07:00
|
|
|
left(0),
|
|
|
|
top(0),
|
|
|
|
right(0),
|
|
|
|
bottom(0) {
|
|
|
|
}
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
inline Rect(float left, float top, float right, float bottom):
|
2010-06-25 13:46:18 -07:00
|
|
|
left(left),
|
|
|
|
top(top),
|
|
|
|
right(right),
|
|
|
|
bottom(bottom) {
|
|
|
|
}
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
inline Rect(float width, float height):
|
|
|
|
left(0.0f),
|
|
|
|
top(0.0f),
|
|
|
|
right(width),
|
|
|
|
bottom(height) {
|
|
|
|
}
|
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
friend int operator==(const Rect& a, const Rect& b) {
|
|
|
|
return !memcmp(&a, &b, sizeof(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
friend int operator!=(const Rect& a, const Rect& b) {
|
|
|
|
return memcmp(&a, &b, sizeof(a));
|
|
|
|
}
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
inline void clear() {
|
|
|
|
left = top = right = bottom = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isEmpty() const {
|
2011-09-19 16:00:46 -07:00
|
|
|
// this is written in such way this it'll handle NANs to return
|
|
|
|
// true (empty)
|
|
|
|
return !((left < right) && (top < bottom));
|
2010-06-25 13:46:18 -07:00
|
|
|
}
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
inline void setEmpty() {
|
|
|
|
left = top = right = bottom = 0.0f;
|
2010-06-25 13:46:18 -07:00
|
|
|
}
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
inline void set(float left, float top, float right, float bottom) {
|
2010-06-25 13:46:18 -07:00
|
|
|
this->left = left;
|
|
|
|
this->right = right;
|
|
|
|
this->top = top;
|
|
|
|
this->bottom = bottom;
|
|
|
|
}
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
inline void set(const Rect& r) {
|
2010-06-25 13:46:18 -07:00
|
|
|
set(r.left, r.top, r.right, r.bottom);
|
|
|
|
}
|
|
|
|
|
2010-09-01 15:13:49 -07:00
|
|
|
inline float getWidth() const {
|
2010-06-25 13:46:18 -07:00
|
|
|
return right - left;
|
|
|
|
}
|
|
|
|
|
2010-09-01 15:13:49 -07:00
|
|
|
inline float getHeight() const {
|
2010-06-25 13:46:18 -07:00
|
|
|
return bottom - top;
|
|
|
|
}
|
|
|
|
|
2011-09-19 16:00:46 -07:00
|
|
|
bool intersects(float l, float t, float r, float b) const {
|
2011-12-13 14:55:06 -08:00
|
|
|
return !intersectWith(l, t, r, b).isEmpty();
|
2010-06-25 13:46:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool intersects(const Rect& r) const {
|
|
|
|
return intersects(r.left, r.top, r.right, r.bottom);
|
|
|
|
}
|
|
|
|
|
2011-09-19 16:00:46 -07:00
|
|
|
bool intersect(float l, float t, float r, float b) {
|
2011-12-14 19:23:32 -08:00
|
|
|
Rect tmp(l, t, r, b);
|
|
|
|
intersectWith(tmp);
|
2011-09-19 16:00:46 -07:00
|
|
|
if (!tmp.isEmpty()) {
|
|
|
|
set(tmp);
|
2010-06-25 13:46:18 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool intersect(const Rect& r) {
|
|
|
|
return intersect(r.left, r.top, r.right, r.bottom);
|
|
|
|
}
|
|
|
|
|
2013-05-21 15:29:59 -07:00
|
|
|
inline bool contains(float l, float t, float r, float b) const {
|
2011-12-13 18:39:19 -08:00
|
|
|
return l >= left && t >= top && r <= right && b <= bottom;
|
|
|
|
}
|
|
|
|
|
2013-05-21 15:29:59 -07:00
|
|
|
inline bool contains(const Rect& r) const {
|
2011-12-13 18:39:19 -08:00
|
|
|
return contains(r.left, r.top, r.right, r.bottom);
|
|
|
|
}
|
|
|
|
|
2010-07-16 14:12:24 -07:00
|
|
|
bool unionWith(const Rect& r) {
|
|
|
|
if (r.left < r.right && r.top < r.bottom) {
|
|
|
|
if (left < right && top < bottom) {
|
|
|
|
if (left > r.left) left = r.left;
|
|
|
|
if (top > r.top) top = r.top;
|
|
|
|
if (right < r.right) right = r.right;
|
|
|
|
if (bottom < r.bottom) bottom = r.bottom;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
left = r.left;
|
|
|
|
top = r.top;
|
|
|
|
right = r.right;
|
|
|
|
bottom = r.bottom;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
void translate(float dx, float dy) {
|
|
|
|
left += dx;
|
|
|
|
right += dx;
|
|
|
|
top += dy;
|
|
|
|
bottom += dy;
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:16:33 -08:00
|
|
|
void outset(float delta) {
|
|
|
|
left -= delta;
|
|
|
|
top -= delta;
|
|
|
|
right += delta;
|
|
|
|
bottom += delta;
|
|
|
|
}
|
|
|
|
|
2010-09-16 14:40:17 -07:00
|
|
|
void snapToPixelBoundaries() {
|
2010-10-22 17:49:18 -07:00
|
|
|
left = floorf(left + 0.5f);
|
|
|
|
top = floorf(top + 0.5f);
|
|
|
|
right = floorf(right + 0.5f);
|
|
|
|
bottom = floorf(bottom + 0.5f);
|
2010-09-16 14:40:17 -07:00
|
|
|
}
|
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
void dump() const {
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
|
2010-06-25 13:46:18 -07:00
|
|
|
}
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2011-12-13 14:55:06 -08:00
|
|
|
private:
|
2011-12-14 19:23:32 -08:00
|
|
|
void intersectWith(Rect& tmp) const {
|
2012-11-26 18:30:17 -08:00
|
|
|
tmp.left = fmaxf(left, tmp.left);
|
|
|
|
tmp.top = fmaxf(top, tmp.top);
|
|
|
|
tmp.right = fminf(right, tmp.right);
|
|
|
|
tmp.bottom = fminf(bottom, tmp.bottom);
|
2011-12-14 19:23:32 -08:00
|
|
|
}
|
|
|
|
|
2011-12-13 14:55:06 -08:00
|
|
|
Rect intersectWith(float l, float t, float r, float b) const {
|
|
|
|
Rect tmp;
|
2012-11-26 18:30:17 -08:00
|
|
|
tmp.left = fmaxf(left, l);
|
|
|
|
tmp.top = fmaxf(top, t);
|
|
|
|
tmp.right = fminf(right, r);
|
|
|
|
tmp.bottom = fminf(bottom, b);
|
2011-12-13 14:55:06 -08:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2011-09-19 16:00:46 -07:00
|
|
|
}; // class Rect
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2010-06-24 19:30:36 -07:00
|
|
|
}; // namespace uirenderer
|
2010-06-22 18:56:38 -07:00
|
|
|
}; // namespace android
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
#endif // ANDROID_HWUI_RECT_H
|