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 | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_UI_RECT_H |
| 18 | #define ANDROID_UI_RECT_H |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 19 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 20 | #include <utils/Log.h> |
| 21 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 22 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 23 | namespace uirenderer { |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 24 | |
| 25 | /////////////////////////////////////////////////////////////////////////////// |
| 26 | // Structs |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | |
| 29 | struct Rect { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 30 | float left; |
| 31 | float top; |
| 32 | float right; |
| 33 | float bottom; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 34 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 35 | Rect(): |
| 36 | left(0), |
| 37 | top(0), |
| 38 | right(0), |
| 39 | bottom(0) { |
| 40 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 41 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 42 | Rect(float left, float top, float right, float bottom): |
| 43 | left(left), |
| 44 | top(top), |
| 45 | right(right), |
| 46 | bottom(bottom) { |
| 47 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 48 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 49 | Rect(const Rect& r) { |
| 50 | set(r); |
| 51 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 52 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 53 | Rect(Rect& r) { |
| 54 | set(r); |
| 55 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 56 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 57 | Rect& operator=(const Rect& r) { |
| 58 | set(r); |
| 59 | return *this; |
| 60 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 61 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 62 | Rect& operator=(Rect& r) { |
| 63 | set(r); |
| 64 | return *this; |
| 65 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 66 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 67 | friend int operator==(const Rect& a, const Rect& b) { |
| 68 | return !memcmp(&a, &b, sizeof(a)); |
| 69 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 70 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 71 | friend int operator!=(const Rect& a, const Rect& b) { |
| 72 | return memcmp(&a, &b, sizeof(a)); |
| 73 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 74 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 75 | bool isEmpty() const { |
| 76 | return left >= right || top >= bottom; |
| 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 | void setEmpty() { |
| 80 | memset(this, 0, sizeof(*this)); |
| 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 | 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 Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 89 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 90 | void set(const Rect& r) { |
| 91 | set(r.left, r.top, r.right, r.bottom); |
| 92 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 93 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 94 | float getWidth() const { |
| 95 | return right - left; |
| 96 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 97 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 98 | float getHeight() const { |
| 99 | return bottom - top; |
| 100 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 101 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 102 | 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 Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 108 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 109 | bool intersects(const Rect& r) const { |
| 110 | return intersects(r.left, r.top, r.right, r.bottom); |
| 111 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 112 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 113 | 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 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 | 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 Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 122 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | return false; |
| 126 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 127 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 128 | bool intersect(const Rect& r) { |
| 129 | return intersect(r.left, r.top, r.right, r.bottom); |
| 130 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 131 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 132 | 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 Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 151 | void dump() const { |
| 152 | LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom); |
| 153 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 154 | |
| 155 | }; // struct Rect |
| 156 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 157 | }; // namespace uirenderer |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 158 | }; // namespace android |
| 159 | |
| 160 | #endif // ANDROID_RECT_H |