Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_RECT_H |
| 18 | #define ANDROID_HWUI_RECT_H |
| 19 | |
| 20 | #include <cmath> |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 21 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 24 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 25 | namespace uirenderer { |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | // Structs |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | |
| 31 | struct Rect { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 32 | float left; |
| 33 | float top; |
| 34 | float right; |
| 35 | float bottom; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 36 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 37 | // Used by Region |
| 38 | typedef float value_type; |
| 39 | |
| 40 | inline Rect(): |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 41 | left(0), |
| 42 | top(0), |
| 43 | right(0), |
| 44 | bottom(0) { |
| 45 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 46 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 47 | inline Rect(float left, float top, float right, float bottom): |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 48 | left(left), |
| 49 | top(top), |
| 50 | right(right), |
| 51 | bottom(bottom) { |
| 52 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 53 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 54 | 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 Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 62 | set(r); |
| 63 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 64 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 65 | inline Rect(Rect& r) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 66 | set(r); |
| 67 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 68 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 69 | Rect& operator=(const Rect& r) { |
| 70 | set(r); |
| 71 | return *this; |
| 72 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 73 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 74 | Rect& operator=(Rect& r) { |
| 75 | set(r); |
| 76 | return *this; |
| 77 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 78 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 79 | friend int operator==(const Rect& a, const Rect& b) { |
| 80 | return !memcmp(&a, &b, sizeof(a)); |
| 81 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 82 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 83 | friend int operator!=(const Rect& a, const Rect& b) { |
| 84 | return memcmp(&a, &b, sizeof(a)); |
| 85 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 86 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 87 | inline void clear() { |
| 88 | left = top = right = bottom = 0.0f; |
| 89 | } |
| 90 | |
| 91 | inline bool isEmpty() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 92 | return left >= right || top >= bottom; |
| 93 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 94 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 95 | inline void setEmpty() { |
| 96 | left = top = right = bottom = 0.0f; |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 97 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 98 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 99 | inline void set(float left, float top, float right, float bottom) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 100 | this->left = left; |
| 101 | this->right = right; |
| 102 | this->top = top; |
| 103 | this->bottom = bottom; |
| 104 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 105 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 106 | inline void set(const Rect& r) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 107 | set(r.left, r.top, r.right, r.bottom); |
| 108 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 109 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 110 | inline float getWidth() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 111 | return right - left; |
| 112 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 113 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 114 | inline float getHeight() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 115 | return bottom - top; |
| 116 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 117 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 118 | 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 Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 124 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 125 | bool intersects(const Rect& r) const { |
| 126 | return intersects(r.left, r.top, r.right, r.bottom); |
| 127 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 128 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 129 | 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 Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 133 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 134 | 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 Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 138 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 139 | return true; |
| 140 | } |
| 141 | return false; |
| 142 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 143 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 144 | bool intersect(const Rect& r) { |
| 145 | return intersect(r.left, r.top, r.right, r.bottom); |
| 146 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 147 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 148 | 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 Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 167 | void translate(float dx, float dy) { |
| 168 | left += dx; |
| 169 | right += dx; |
| 170 | top += dy; |
| 171 | bottom += dy; |
| 172 | } |
| 173 | |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 174 | void snapToPixelBoundaries() { |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 175 | left = floorf(left + 0.5f); |
| 176 | top = floorf(top + 0.5f); |
| 177 | right = floorf(right + 0.5f); |
| 178 | bottom = floorf(bottom + 0.5f); |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 181 | void dump() const { |
| 182 | LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom); |
| 183 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 184 | |
| 185 | }; // struct Rect |
| 186 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 187 | }; // namespace uirenderer |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 188 | }; // namespace android |
| 189 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 190 | #endif // ANDROID_HWUI_RECT_H |