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