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 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 31 | class Rect { |
| 32 | static inline float min(float a, float b) { return (a<b) ? a : b; } |
| 33 | static inline float max(float a, float b) { return (a>b) ? a : b; } |
| 34 | Rect intersectWith(float l, float t, float r, float b) const { |
| 35 | Rect tmp; |
| 36 | tmp.left = max(left, l); |
| 37 | tmp.top = max(top, t); |
| 38 | tmp.right = min(right, r); |
| 39 | tmp.bottom = min(bottom, b); |
| 40 | return tmp; |
| 41 | } |
| 42 | |
| 43 | public: |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 44 | float left; |
| 45 | float top; |
| 46 | float right; |
| 47 | float bottom; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 48 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 49 | // Used by Region |
| 50 | typedef float value_type; |
| 51 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 52 | // we don't provide copy-ctor and operator= on purpose |
| 53 | // because we want the compiler generated versions |
| 54 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 55 | inline Rect(): |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 56 | left(0), |
| 57 | top(0), |
| 58 | right(0), |
| 59 | bottom(0) { |
| 60 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 61 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 62 | inline Rect(float left, float top, float right, float bottom): |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 63 | left(left), |
| 64 | top(top), |
| 65 | right(right), |
| 66 | bottom(bottom) { |
| 67 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 68 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 69 | inline Rect(float width, float height): |
| 70 | left(0.0f), |
| 71 | top(0.0f), |
| 72 | right(width), |
| 73 | bottom(height) { |
| 74 | } |
| 75 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 76 | friend int operator==(const Rect& a, const Rect& b) { |
| 77 | return !memcmp(&a, &b, sizeof(a)); |
| 78 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 79 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 80 | friend int operator!=(const Rect& a, const Rect& b) { |
| 81 | return memcmp(&a, &b, sizeof(a)); |
| 82 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 83 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 84 | inline void clear() { |
| 85 | left = top = right = bottom = 0.0f; |
| 86 | } |
| 87 | |
| 88 | inline bool isEmpty() const { |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 89 | // this is written in such way this it'll handle NANs to return |
| 90 | // true (empty) |
| 91 | return !((left < right) && (top < bottom)); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 92 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 93 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 94 | inline void setEmpty() { |
| 95 | left = top = right = bottom = 0.0f; |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 96 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 97 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 98 | inline void set(float left, float top, float right, float bottom) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 99 | this->left = left; |
| 100 | this->right = right; |
| 101 | this->top = top; |
| 102 | this->bottom = bottom; |
| 103 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 104 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 105 | inline void set(const Rect& r) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 106 | set(r.left, r.top, r.right, r.bottom); |
| 107 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 108 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 109 | inline float getWidth() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 110 | return right - left; |
| 111 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 112 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 113 | inline float getHeight() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 114 | return bottom - top; |
| 115 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 116 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 117 | bool intersects(float l, float t, float r, float b) const { |
| 118 | return !intersectWith(l,t,r,b).isEmpty(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 119 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 120 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 121 | bool intersects(const Rect& r) const { |
| 122 | return intersects(r.left, r.top, r.right, r.bottom); |
| 123 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 124 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 125 | bool intersect(float l, float t, float r, float b) { |
| 126 | Rect tmp(intersectWith(l,t,r,b)); |
| 127 | if (!tmp.isEmpty()) { |
| 128 | set(tmp); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | return false; |
| 132 | } |
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 | bool intersect(const Rect& r) { |
| 135 | return intersect(r.left, r.top, r.right, r.bottom); |
| 136 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 137 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 138 | bool unionWith(const Rect& r) { |
| 139 | if (r.left < r.right && r.top < r.bottom) { |
| 140 | if (left < right && top < bottom) { |
| 141 | if (left > r.left) left = r.left; |
| 142 | if (top > r.top) top = r.top; |
| 143 | if (right < r.right) right = r.right; |
| 144 | if (bottom < r.bottom) bottom = r.bottom; |
| 145 | return true; |
| 146 | } else { |
| 147 | left = r.left; |
| 148 | top = r.top; |
| 149 | right = r.right; |
| 150 | bottom = r.bottom; |
| 151 | return true; |
| 152 | } |
| 153 | } |
| 154 | return false; |
| 155 | } |
| 156 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 157 | void translate(float dx, float dy) { |
| 158 | left += dx; |
| 159 | right += dx; |
| 160 | top += dy; |
| 161 | bottom += dy; |
| 162 | } |
| 163 | |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 164 | void snapToPixelBoundaries() { |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 165 | left = floorf(left + 0.5f); |
| 166 | top = floorf(top + 0.5f); |
| 167 | right = floorf(right + 0.5f); |
| 168 | bottom = floorf(bottom + 0.5f); |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 171 | void dump() const { |
| 172 | LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom); |
| 173 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 174 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 175 | }; // class Rect |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 176 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 177 | }; // namespace uirenderer |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 178 | }; // namespace android |
| 179 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 180 | #endif // ANDROID_HWUI_RECT_H |