blob: b382dc3f58129bc2f02073fda1921496f06dc724 [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
20namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070021namespace uirenderer {
Romain Guybb9524b2010-06-22 18:56:38 -070022
23///////////////////////////////////////////////////////////////////////////////
24// Structs
25///////////////////////////////////////////////////////////////////////////////
26
27struct Rect {
Romain Guy7ae7ac42010-06-25 13:46:18 -070028 float left;
29 float top;
30 float right;
31 float bottom;
Romain Guybb9524b2010-06-22 18:56:38 -070032
Romain Guy7ae7ac42010-06-25 13:46:18 -070033 Rect():
34 left(0),
35 top(0),
36 right(0),
37 bottom(0) {
38 }
Romain Guy9d5316e2010-06-24 19:30:36 -070039
Romain Guy7ae7ac42010-06-25 13:46:18 -070040 Rect(float left, float top, float right, float bottom):
41 left(left),
42 top(top),
43 right(right),
44 bottom(bottom) {
45 }
Romain Guybb9524b2010-06-22 18:56:38 -070046
Romain Guy7ae7ac42010-06-25 13:46:18 -070047 Rect(const Rect& r) {
48 set(r);
49 }
Romain Guybb9524b2010-06-22 18:56:38 -070050
Romain Guy7ae7ac42010-06-25 13:46:18 -070051 Rect(Rect& r) {
52 set(r);
53 }
Romain Guybb9524b2010-06-22 18:56:38 -070054
Romain Guy7ae7ac42010-06-25 13:46:18 -070055 Rect& operator=(const Rect& r) {
56 set(r);
57 return *this;
58 }
Romain Guybb9524b2010-06-22 18:56:38 -070059
Romain Guy7ae7ac42010-06-25 13:46:18 -070060 Rect& operator=(Rect& r) {
61 set(r);
62 return *this;
63 }
Romain Guybb9524b2010-06-22 18:56:38 -070064
Romain Guy7ae7ac42010-06-25 13:46:18 -070065 friend int operator==(const Rect& a, const Rect& b) {
66 return !memcmp(&a, &b, sizeof(a));
67 }
Romain Guybb9524b2010-06-22 18:56:38 -070068
Romain Guy7ae7ac42010-06-25 13:46:18 -070069 friend int operator!=(const Rect& a, const Rect& b) {
70 return memcmp(&a, &b, sizeof(a));
71 }
Romain Guybb9524b2010-06-22 18:56:38 -070072
Romain Guy7ae7ac42010-06-25 13:46:18 -070073 bool isEmpty() const {
74 return left >= right || top >= bottom;
75 }
Romain Guybb9524b2010-06-22 18:56:38 -070076
Romain Guy7ae7ac42010-06-25 13:46:18 -070077 void setEmpty() {
78 memset(this, 0, sizeof(*this));
79 }
Romain Guybb9524b2010-06-22 18:56:38 -070080
Romain Guy7ae7ac42010-06-25 13:46:18 -070081 void set(float left, float top, float right, float bottom) {
82 this->left = left;
83 this->right = right;
84 this->top = top;
85 this->bottom = bottom;
86 }
Romain Guybb9524b2010-06-22 18:56:38 -070087
Romain Guy7ae7ac42010-06-25 13:46:18 -070088 void set(const Rect& r) {
89 set(r.left, r.top, r.right, r.bottom);
90 }
Romain Guybb9524b2010-06-22 18:56:38 -070091
Romain Guy7ae7ac42010-06-25 13:46:18 -070092 float getWidth() const {
93 return right - left;
94 }
Romain Guybb9524b2010-06-22 18:56:38 -070095
Romain Guy7ae7ac42010-06-25 13:46:18 -070096 float getHeight() const {
97 return bottom - top;
98 }
Romain Guybb9524b2010-06-22 18:56:38 -070099
Romain Guy7ae7ac42010-06-25 13:46:18 -0700100 bool intersects(float left, float top, float right, float bottom) const {
101 return left < right && top < bottom &&
102 this->left < this->right && this->top < this->bottom &&
103 this->left < right && left < this->right &&
104 this->top < bottom && top < this->bottom;
105 }
Romain Guybb9524b2010-06-22 18:56:38 -0700106
Romain Guy7ae7ac42010-06-25 13:46:18 -0700107 bool intersects(const Rect& r) const {
108 return intersects(r.left, r.top, r.right, r.bottom);
109 }
Romain Guybb9524b2010-06-22 18:56:38 -0700110
Romain Guy7ae7ac42010-06-25 13:46:18 -0700111 bool intersect(float left, float top, float right, float bottom) {
112 if (left < right && top < bottom && !this->isEmpty() &&
113 this->left < right && left < this->right &&
114 this->top < bottom && top < this->bottom) {
Romain Guybb9524b2010-06-22 18:56:38 -0700115
Romain Guy7ae7ac42010-06-25 13:46:18 -0700116 if (this->left < left) this->left = left;
117 if (this->top < top) this->top = top;
118 if (this->right > right) this->right = right;
119 if (this->bottom > bottom) this->bottom = bottom;
Romain Guybb9524b2010-06-22 18:56:38 -0700120
Romain Guy7ae7ac42010-06-25 13:46:18 -0700121 return true;
122 }
123 return false;
124 }
Romain Guybb9524b2010-06-22 18:56:38 -0700125
Romain Guy7ae7ac42010-06-25 13:46:18 -0700126 bool intersect(const Rect& r) {
127 return intersect(r.left, r.top, r.right, r.bottom);
128 }
Romain Guybb9524b2010-06-22 18:56:38 -0700129
Romain Guy7ae7ac42010-06-25 13:46:18 -0700130 void dump() const {
131 LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
132 }
Romain Guybb9524b2010-06-22 18:56:38 -0700133
134}; // struct Rect
135
Romain Guy9d5316e2010-06-24 19:30:36 -0700136}; // namespace uirenderer
Romain Guybb9524b2010-06-22 18:56:38 -0700137}; // namespace android
138
139#endif // ANDROID_RECT_H