blob: 71951b7a3cc82d0de8748bf78b8e4cd38dbd74eb [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 Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_RECT_H
18#define ANDROID_HWUI_RECT_H
19
20#include <cmath>
Romain Guybb9524b2010-06-22 18:56:38 -070021
Romain Guy5cbbce52010-06-27 22:59:20 -070022#include <utils/Log.h>
23
Romain Guybb9524b2010-06-22 18:56:38 -070024namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070025namespace uirenderer {
Romain Guybb9524b2010-06-22 18:56:38 -070026
27///////////////////////////////////////////////////////////////////////////////
28// Structs
29///////////////////////////////////////////////////////////////////////////////
30
31struct Rect {
Romain Guy7ae7ac42010-06-25 13:46:18 -070032 float left;
33 float top;
34 float right;
35 float bottom;
Romain Guybb9524b2010-06-22 18:56:38 -070036
Romain Guy5b3b3522010-10-27 18:57:51 -070037 // Used by Region
38 typedef float value_type;
39
40 inline Rect():
Romain Guy7ae7ac42010-06-25 13:46:18 -070041 left(0),
42 top(0),
43 right(0),
44 bottom(0) {
45 }
Romain Guy9d5316e2010-06-24 19:30:36 -070046
Romain Guy5b3b3522010-10-27 18:57:51 -070047 inline Rect(float left, float top, float right, float bottom):
Romain Guy7ae7ac42010-06-25 13:46:18 -070048 left(left),
49 top(top),
50 right(right),
51 bottom(bottom) {
52 }
Romain Guybb9524b2010-06-22 18:56:38 -070053
Romain Guy5b3b3522010-10-27 18:57:51 -070054 inline Rect(float width, float height):
55 left(0.0f),
56 top(0.0f),
57 right(width),
58 bottom(height) {
59 }
60
61 inline Rect(const Rect& r) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070062 set(r);
63 }
Romain Guybb9524b2010-06-22 18:56:38 -070064
Romain Guy5b3b3522010-10-27 18:57:51 -070065 inline Rect(Rect& r) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070066 set(r);
67 }
Romain Guybb9524b2010-06-22 18:56:38 -070068
Romain Guy7ae7ac42010-06-25 13:46:18 -070069 Rect& operator=(const Rect& r) {
70 set(r);
71 return *this;
72 }
Romain Guybb9524b2010-06-22 18:56:38 -070073
Romain Guy7ae7ac42010-06-25 13:46:18 -070074 Rect& operator=(Rect& r) {
75 set(r);
76 return *this;
77 }
Romain Guybb9524b2010-06-22 18:56:38 -070078
Romain Guy7ae7ac42010-06-25 13:46:18 -070079 friend int operator==(const Rect& a, const Rect& b) {
80 return !memcmp(&a, &b, sizeof(a));
81 }
Romain Guybb9524b2010-06-22 18:56:38 -070082
Romain Guy7ae7ac42010-06-25 13:46:18 -070083 friend int operator!=(const Rect& a, const Rect& b) {
84 return memcmp(&a, &b, sizeof(a));
85 }
Romain Guybb9524b2010-06-22 18:56:38 -070086
Romain Guy5b3b3522010-10-27 18:57:51 -070087 inline void clear() {
88 left = top = right = bottom = 0.0f;
89 }
90
91 inline bool isEmpty() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -070092 return left >= right || top >= bottom;
93 }
Romain Guybb9524b2010-06-22 18:56:38 -070094
Romain Guy5b3b3522010-10-27 18:57:51 -070095 inline void setEmpty() {
96 left = top = right = bottom = 0.0f;
Romain Guy7ae7ac42010-06-25 13:46:18 -070097 }
Romain Guybb9524b2010-06-22 18:56:38 -070098
Romain Guy5b3b3522010-10-27 18:57:51 -070099 inline void set(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700100 this->left = left;
101 this->right = right;
102 this->top = top;
103 this->bottom = bottom;
104 }
Romain Guybb9524b2010-06-22 18:56:38 -0700105
Romain Guy5b3b3522010-10-27 18:57:51 -0700106 inline void set(const Rect& r) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700107 set(r.left, r.top, r.right, r.bottom);
108 }
Romain Guybb9524b2010-06-22 18:56:38 -0700109
Romain Guy8aef54f2010-09-01 15:13:49 -0700110 inline float getWidth() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700111 return right - left;
112 }
Romain Guybb9524b2010-06-22 18:56:38 -0700113
Romain Guy8aef54f2010-09-01 15:13:49 -0700114 inline float getHeight() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700115 return bottom - top;
116 }
Romain Guybb9524b2010-06-22 18:56:38 -0700117
Romain Guy7ae7ac42010-06-25 13:46:18 -0700118 bool intersects(float left, float top, float right, float bottom) const {
119 return left < right && top < bottom &&
120 this->left < this->right && this->top < this->bottom &&
121 this->left < right && left < this->right &&
122 this->top < bottom && top < this->bottom;
123 }
Romain Guybb9524b2010-06-22 18:56:38 -0700124
Romain Guy7ae7ac42010-06-25 13:46:18 -0700125 bool intersects(const Rect& r) const {
126 return intersects(r.left, r.top, r.right, r.bottom);
127 }
Romain Guybb9524b2010-06-22 18:56:38 -0700128
Romain Guy7ae7ac42010-06-25 13:46:18 -0700129 bool intersect(float left, float top, float right, float bottom) {
130 if (left < right && top < bottom && !this->isEmpty() &&
131 this->left < right && left < this->right &&
132 this->top < bottom && top < this->bottom) {
Romain Guybb9524b2010-06-22 18:56:38 -0700133
Romain Guy7ae7ac42010-06-25 13:46:18 -0700134 if (this->left < left) this->left = left;
135 if (this->top < top) this->top = top;
136 if (this->right > right) this->right = right;
137 if (this->bottom > bottom) this->bottom = bottom;
Romain Guybb9524b2010-06-22 18:56:38 -0700138
Romain Guy7ae7ac42010-06-25 13:46:18 -0700139 return true;
140 }
141 return false;
142 }
Romain Guybb9524b2010-06-22 18:56:38 -0700143
Romain Guy7ae7ac42010-06-25 13:46:18 -0700144 bool intersect(const Rect& r) {
145 return intersect(r.left, r.top, r.right, r.bottom);
146 }
Romain Guybb9524b2010-06-22 18:56:38 -0700147
Romain Guy079ba2c2010-07-16 14:12:24 -0700148 bool unionWith(const Rect& r) {
149 if (r.left < r.right && r.top < r.bottom) {
150 if (left < right && top < bottom) {
151 if (left > r.left) left = r.left;
152 if (top > r.top) top = r.top;
153 if (right < r.right) right = r.right;
154 if (bottom < r.bottom) bottom = r.bottom;
155 return true;
156 } else {
157 left = r.left;
158 top = r.top;
159 right = r.right;
160 bottom = r.bottom;
161 return true;
162 }
163 }
164 return false;
165 }
166
Romain Guy5b3b3522010-10-27 18:57:51 -0700167 void translate(float dx, float dy) {
168 left += dx;
169 right += dx;
170 top += dy;
171 bottom += dy;
172 }
173
Romain Guybf434112010-09-16 14:40:17 -0700174 void snapToPixelBoundaries() {
Romain Guyae88e5e2010-10-22 17:49:18 -0700175 left = floorf(left + 0.5f);
176 top = floorf(top + 0.5f);
177 right = floorf(right + 0.5f);
178 bottom = floorf(bottom + 0.5f);
Romain Guybf434112010-09-16 14:40:17 -0700179 }
180
Romain Guy7ae7ac42010-06-25 13:46:18 -0700181 void dump() const {
182 LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
183 }
Romain Guybb9524b2010-06-22 18:56:38 -0700184
185}; // struct Rect
186
Romain Guy9d5316e2010-06-24 19:30:36 -0700187}; // namespace uirenderer
Romain Guybb9524b2010-06-22 18:56:38 -0700188}; // namespace android
189
Romain Guy5b3b3522010-10-27 18:57:51 -0700190#endif // ANDROID_HWUI_RECT_H