blob: 7be0c340399dad6935ec5b6c96cb1a81bb32656a [file] [log] [blame]
Romain Guybb9524b2010-06-22 18:56:38 -07001/*
2 * Copyright (C) 2010 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
Romain Guy9d5316e2010-06-24 19:30:36 -070017#ifndef ANDROID_UI_RECT_H
18#define ANDROID_UI_RECT_H
Romain Guybb9524b2010-06-22 18:56:38 -070019
Romain Guy5cbbce52010-06-27 22:59:20 -070020#include <utils/Log.h>
21
Romain Guybb9524b2010-06-22 18:56:38 -070022namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070023namespace uirenderer {
Romain Guybb9524b2010-06-22 18:56:38 -070024
25///////////////////////////////////////////////////////////////////////////////
26// Structs
27///////////////////////////////////////////////////////////////////////////////
28
29struct Rect {
Romain Guy7ae7ac42010-06-25 13:46:18 -070030 float left;
31 float top;
32 float right;
33 float bottom;
Romain Guybb9524b2010-06-22 18:56:38 -070034
Romain Guy7ae7ac42010-06-25 13:46:18 -070035 Rect():
36 left(0),
37 top(0),
38 right(0),
39 bottom(0) {
40 }
Romain Guy9d5316e2010-06-24 19:30:36 -070041
Romain Guy7ae7ac42010-06-25 13:46:18 -070042 Rect(float left, float top, float right, float bottom):
43 left(left),
44 top(top),
45 right(right),
46 bottom(bottom) {
47 }
Romain Guybb9524b2010-06-22 18:56:38 -070048
Romain Guy7ae7ac42010-06-25 13:46:18 -070049 Rect(const Rect& r) {
50 set(r);
51 }
Romain Guybb9524b2010-06-22 18:56:38 -070052
Romain Guy7ae7ac42010-06-25 13:46:18 -070053 Rect(Rect& r) {
54 set(r);
55 }
Romain Guybb9524b2010-06-22 18:56:38 -070056
Romain Guy7ae7ac42010-06-25 13:46:18 -070057 Rect& operator=(const Rect& r) {
58 set(r);
59 return *this;
60 }
Romain Guybb9524b2010-06-22 18:56:38 -070061
Romain Guy7ae7ac42010-06-25 13:46:18 -070062 Rect& operator=(Rect& r) {
63 set(r);
64 return *this;
65 }
Romain Guybb9524b2010-06-22 18:56:38 -070066
Romain Guy7ae7ac42010-06-25 13:46:18 -070067 friend int operator==(const Rect& a, const Rect& b) {
68 return !memcmp(&a, &b, sizeof(a));
69 }
Romain Guybb9524b2010-06-22 18:56:38 -070070
Romain Guy7ae7ac42010-06-25 13:46:18 -070071 friend int operator!=(const Rect& a, const Rect& b) {
72 return memcmp(&a, &b, sizeof(a));
73 }
Romain Guybb9524b2010-06-22 18:56:38 -070074
Romain Guy7ae7ac42010-06-25 13:46:18 -070075 bool isEmpty() const {
76 return left >= right || top >= bottom;
77 }
Romain Guybb9524b2010-06-22 18:56:38 -070078
Romain Guy7ae7ac42010-06-25 13:46:18 -070079 void setEmpty() {
80 memset(this, 0, sizeof(*this));
81 }
Romain Guybb9524b2010-06-22 18:56:38 -070082
Romain Guy7ae7ac42010-06-25 13:46:18 -070083 void set(float left, float top, float right, float bottom) {
84 this->left = left;
85 this->right = right;
86 this->top = top;
87 this->bottom = bottom;
88 }
Romain Guybb9524b2010-06-22 18:56:38 -070089
Romain Guy7ae7ac42010-06-25 13:46:18 -070090 void set(const Rect& r) {
91 set(r.left, r.top, r.right, r.bottom);
92 }
Romain Guybb9524b2010-06-22 18:56:38 -070093
Romain Guy7ae7ac42010-06-25 13:46:18 -070094 float getWidth() const {
95 return right - left;
96 }
Romain Guybb9524b2010-06-22 18:56:38 -070097
Romain Guy7ae7ac42010-06-25 13:46:18 -070098 float getHeight() const {
99 return bottom - top;
100 }
Romain Guybb9524b2010-06-22 18:56:38 -0700101
Romain Guy7ae7ac42010-06-25 13:46:18 -0700102 bool intersects(float left, float top, float right, float bottom) const {
103 return left < right && top < bottom &&
104 this->left < this->right && this->top < this->bottom &&
105 this->left < right && left < this->right &&
106 this->top < bottom && top < this->bottom;
107 }
Romain Guybb9524b2010-06-22 18:56:38 -0700108
Romain Guy7ae7ac42010-06-25 13:46:18 -0700109 bool intersects(const Rect& r) const {
110 return intersects(r.left, r.top, r.right, r.bottom);
111 }
Romain Guybb9524b2010-06-22 18:56:38 -0700112
Romain Guy7ae7ac42010-06-25 13:46:18 -0700113 bool intersect(float left, float top, float right, float bottom) {
114 if (left < right && top < bottom && !this->isEmpty() &&
115 this->left < right && left < this->right &&
116 this->top < bottom && top < this->bottom) {
Romain Guybb9524b2010-06-22 18:56:38 -0700117
Romain Guy7ae7ac42010-06-25 13:46:18 -0700118 if (this->left < left) this->left = left;
119 if (this->top < top) this->top = top;
120 if (this->right > right) this->right = right;
121 if (this->bottom > bottom) this->bottom = bottom;
Romain Guybb9524b2010-06-22 18:56:38 -0700122
Romain Guy7ae7ac42010-06-25 13:46:18 -0700123 return true;
124 }
125 return false;
126 }
Romain Guybb9524b2010-06-22 18:56:38 -0700127
Romain Guy7ae7ac42010-06-25 13:46:18 -0700128 bool intersect(const Rect& r) {
129 return intersect(r.left, r.top, r.right, r.bottom);
130 }
Romain Guybb9524b2010-06-22 18:56:38 -0700131
Romain Guy079ba2c2010-07-16 14:12:24 -0700132 bool unionWith(const Rect& r) {
133 if (r.left < r.right && r.top < r.bottom) {
134 if (left < right && top < bottom) {
135 if (left > r.left) left = r.left;
136 if (top > r.top) top = r.top;
137 if (right < r.right) right = r.right;
138 if (bottom < r.bottom) bottom = r.bottom;
139 return true;
140 } else {
141 left = r.left;
142 top = r.top;
143 right = r.right;
144 bottom = r.bottom;
145 return true;
146 }
147 }
148 return false;
149 }
150
Romain Guy7ae7ac42010-06-25 13:46:18 -0700151 void dump() const {
152 LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
153 }
Romain Guybb9524b2010-06-22 18:56:38 -0700154
155}; // struct Rect
156
Romain Guy9d5316e2010-06-24 19:30:36 -0700157}; // namespace uirenderer
Romain Guybb9524b2010-06-22 18:56:38 -0700158}; // namespace android
159
160#endif // ANDROID_RECT_H