blob: f50ac3ccdb00f5a76d13e95a2366f6d32083688d [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
Mathias Agopian83b186a2011-09-19 16:00:46 -070031class Rect {
Mathias Agopian83b186a2011-09-19 16:00:46 -070032public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070033 float left;
34 float top;
35 float right;
36 float bottom;
Romain Guybb9524b2010-06-22 18:56:38 -070037
Romain Guy5b3b3522010-10-27 18:57:51 -070038 // Used by Region
39 typedef float value_type;
40
Mathias Agopian83b186a2011-09-19 16:00:46 -070041 // we don't provide copy-ctor and operator= on purpose
42 // because we want the compiler generated versions
43
Romain Guy5b3b3522010-10-27 18:57:51 -070044 inline Rect():
Romain Guy7ae7ac42010-06-25 13:46:18 -070045 left(0),
46 top(0),
47 right(0),
48 bottom(0) {
49 }
Romain Guy9d5316e2010-06-24 19:30:36 -070050
Romain Guy5b3b3522010-10-27 18:57:51 -070051 inline Rect(float left, float top, float right, float bottom):
Romain Guy7ae7ac42010-06-25 13:46:18 -070052 left(left),
53 top(top),
54 right(right),
55 bottom(bottom) {
56 }
Romain Guybb9524b2010-06-22 18:56:38 -070057
Romain Guy5b3b3522010-10-27 18:57:51 -070058 inline Rect(float width, float height):
59 left(0.0f),
60 top(0.0f),
61 right(width),
62 bottom(height) {
63 }
64
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 Guy5b3b3522010-10-27 18:57:51 -070073 inline void clear() {
74 left = top = right = bottom = 0.0f;
75 }
76
77 inline bool isEmpty() const {
Mathias Agopian83b186a2011-09-19 16:00:46 -070078 // this is written in such way this it'll handle NANs to return
79 // true (empty)
80 return !((left < right) && (top < bottom));
Romain Guy7ae7ac42010-06-25 13:46:18 -070081 }
Romain Guybb9524b2010-06-22 18:56:38 -070082
Romain Guy5b3b3522010-10-27 18:57:51 -070083 inline void setEmpty() {
84 left = top = right = bottom = 0.0f;
Romain Guy7ae7ac42010-06-25 13:46:18 -070085 }
Romain Guybb9524b2010-06-22 18:56:38 -070086
Romain Guy5b3b3522010-10-27 18:57:51 -070087 inline void set(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070088 this->left = left;
89 this->right = right;
90 this->top = top;
91 this->bottom = bottom;
92 }
Romain Guybb9524b2010-06-22 18:56:38 -070093
Romain Guy5b3b3522010-10-27 18:57:51 -070094 inline void set(const Rect& r) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070095 set(r.left, r.top, r.right, r.bottom);
96 }
Romain Guybb9524b2010-06-22 18:56:38 -070097
Romain Guy8aef54f2010-09-01 15:13:49 -070098 inline float getWidth() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -070099 return right - left;
100 }
Romain Guybb9524b2010-06-22 18:56:38 -0700101
Romain Guy8aef54f2010-09-01 15:13:49 -0700102 inline float getHeight() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700103 return bottom - top;
104 }
Romain Guybb9524b2010-06-22 18:56:38 -0700105
Mathias Agopian83b186a2011-09-19 16:00:46 -0700106 bool intersects(float l, float t, float r, float b) const {
Romain Guya1d3c912011-12-13 14:55:06 -0800107 return !intersectWith(l, t, r, b).isEmpty();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700108 }
Romain Guybb9524b2010-06-22 18:56:38 -0700109
Romain Guy7ae7ac42010-06-25 13:46:18 -0700110 bool intersects(const Rect& r) const {
111 return intersects(r.left, r.top, r.right, r.bottom);
112 }
Romain Guybb9524b2010-06-22 18:56:38 -0700113
Mathias Agopian83b186a2011-09-19 16:00:46 -0700114 bool intersect(float l, float t, float r, float b) {
Romain Guy8f85e802011-12-14 19:23:32 -0800115 Rect tmp(l, t, r, b);
116 intersectWith(tmp);
Mathias Agopian83b186a2011-09-19 16:00:46 -0700117 if (!tmp.isEmpty()) {
118 set(tmp);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700119 return true;
120 }
121 return false;
122 }
Romain Guybb9524b2010-06-22 18:56:38 -0700123
Romain Guy7ae7ac42010-06-25 13:46:18 -0700124 bool intersect(const Rect& r) {
125 return intersect(r.left, r.top, r.right, r.bottom);
126 }
Romain Guybb9524b2010-06-22 18:56:38 -0700127
Romain Guy586cae32012-07-13 15:28:31 -0700128 inline bool contains(float l, float t, float r, float b) {
Romain Guyec31f832011-12-13 18:39:19 -0800129 return l >= left && t >= top && r <= right && b <= bottom;
130 }
131
Romain Guy586cae32012-07-13 15:28:31 -0700132 inline bool contains(const Rect& r) {
Romain Guyec31f832011-12-13 18:39:19 -0800133 return contains(r.left, r.top, r.right, r.bottom);
134 }
135
Romain Guy079ba2c2010-07-16 14:12:24 -0700136 bool unionWith(const Rect& r) {
137 if (r.left < r.right && r.top < r.bottom) {
138 if (left < right && top < bottom) {
139 if (left > r.left) left = r.left;
140 if (top > r.top) top = r.top;
141 if (right < r.right) right = r.right;
142 if (bottom < r.bottom) bottom = r.bottom;
143 return true;
144 } else {
145 left = r.left;
146 top = r.top;
147 right = r.right;
148 bottom = r.bottom;
149 return true;
150 }
151 }
152 return false;
153 }
154
Romain Guy5b3b3522010-10-27 18:57:51 -0700155 void translate(float dx, float dy) {
156 left += dx;
157 right += dx;
158 top += dy;
159 bottom += dy;
160 }
161
Chris Craikc3566d02013-02-04 16:16:33 -0800162 void outset(float delta) {
163 left -= delta;
164 top -= delta;
165 right += delta;
166 bottom += delta;
167 }
168
Romain Guybf434112010-09-16 14:40:17 -0700169 void snapToPixelBoundaries() {
Romain Guyae88e5e2010-10-22 17:49:18 -0700170 left = floorf(left + 0.5f);
171 top = floorf(top + 0.5f);
172 right = floorf(right + 0.5f);
173 bottom = floorf(bottom + 0.5f);
Romain Guybf434112010-09-16 14:40:17 -0700174 }
175
Romain Guy7ae7ac42010-06-25 13:46:18 -0700176 void dump() const {
Steve Block5baa3a62011-12-20 16:23:08 +0000177 ALOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700178 }
Romain Guybb9524b2010-06-22 18:56:38 -0700179
Romain Guya1d3c912011-12-13 14:55:06 -0800180private:
Romain Guy8f85e802011-12-14 19:23:32 -0800181 void intersectWith(Rect& tmp) const {
Chris Craik2af46352012-11-26 18:30:17 -0800182 tmp.left = fmaxf(left, tmp.left);
183 tmp.top = fmaxf(top, tmp.top);
184 tmp.right = fminf(right, tmp.right);
185 tmp.bottom = fminf(bottom, tmp.bottom);
Romain Guy8f85e802011-12-14 19:23:32 -0800186 }
187
Romain Guya1d3c912011-12-13 14:55:06 -0800188 Rect intersectWith(float l, float t, float r, float b) const {
189 Rect tmp;
Chris Craik2af46352012-11-26 18:30:17 -0800190 tmp.left = fmaxf(left, l);
191 tmp.top = fmaxf(top, t);
192 tmp.right = fminf(right, r);
193 tmp.bottom = fminf(bottom, b);
Romain Guya1d3c912011-12-13 14:55:06 -0800194 return tmp;
195 }
196
Mathias Agopian83b186a2011-09-19 16:00:46 -0700197}; // class Rect
Romain Guybb9524b2010-06-22 18:56:38 -0700198
Romain Guy9d5316e2010-06-24 19:30:36 -0700199}; // namespace uirenderer
Romain Guybb9524b2010-06-22 18:56:38 -0700200}; // namespace android
201
Romain Guy5b3b3522010-10-27 18:57:51 -0700202#endif // ANDROID_HWUI_RECT_H