The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
| 17 | #ifndef ANDROID_UI_RECT |
| 18 | #define ANDROID_UI_RECT |
| 19 | |
| 20 | #include <utils/TypeHelpers.h> |
| 21 | #include <ui/Point.h> |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | class Rect |
| 26 | { |
| 27 | public: |
| 28 | int left; |
| 29 | int top; |
| 30 | int right; |
| 31 | int bottom; |
| 32 | |
| 33 | // we don't provide copy-ctor and operator= on purpose |
| 34 | // because we want the compiler generated versions |
| 35 | |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 36 | inline Rect() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | inline Rect(int w, int h) |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 39 | : left(0), top(0), right(w), bottom(h) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | inline Rect(int l, int t, int r, int b) |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 42 | : left(l), top(t), right(r), bottom(b) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | inline Rect(const Point& lt, const Point& rb) |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 45 | : left(lt.x), top(lt.y), right(rb.x), bottom(rb.y) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void makeInvalid(); |
| 49 | |
| 50 | // a valid rectangle has a non negative width and height |
| 51 | inline bool isValid() const { |
| 52 | return (width()>=0) && (height()>=0); |
| 53 | } |
| 54 | |
| 55 | // an empty rect has a zero width or height, or is invalid |
| 56 | inline bool isEmpty() const { |
| 57 | return (width()<=0) || (height()<=0); |
| 58 | } |
| 59 | |
| 60 | inline void set(const Rect& rhs) { |
| 61 | operator = (rhs); |
| 62 | } |
| 63 | |
| 64 | // rectangle's width |
| 65 | inline int width() const { |
| 66 | return right-left; |
| 67 | } |
| 68 | |
| 69 | // rectangle's height |
| 70 | inline int height() const { |
| 71 | return bottom-top; |
| 72 | } |
| 73 | |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 74 | void setLeftTop(const Point& lt) { |
| 75 | left = lt.x; |
| 76 | top = lt.y; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 78 | |
| 79 | void setRightBottom(const Point& rb) { |
| 80 | right = rb.x; |
| 81 | bottom = rb.y; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // the following 4 functions return the 4 corners of the rect as Point |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 85 | Point leftTop() const { |
| 86 | return Point(left, top); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | } |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 88 | Point rightBottom() const { |
| 89 | return Point(right, bottom); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
| 91 | Point rightTop() const { |
| 92 | return Point(right, top); |
| 93 | } |
| 94 | Point leftBottom() const { |
| 95 | return Point(left, bottom); |
| 96 | } |
| 97 | |
| 98 | // comparisons |
| 99 | inline bool operator == (const Rect& rhs) const { |
| 100 | return (left == rhs.left) && (top == rhs.top) && |
| 101 | (right == rhs.right) && (bottom == rhs.bottom); |
| 102 | } |
| 103 | |
| 104 | inline bool operator != (const Rect& rhs) const { |
| 105 | return !operator == (rhs); |
| 106 | } |
| 107 | |
| 108 | // operator < defines an order which allows to use rectangles in sorted |
| 109 | // vectors. |
| 110 | bool operator < (const Rect& rhs) const; |
| 111 | |
| 112 | Rect& offsetToOrigin() { |
| 113 | right -= left; |
| 114 | bottom -= top; |
| 115 | left = top = 0; |
| 116 | return *this; |
| 117 | } |
| 118 | Rect& offsetTo(const Point& p) { |
| 119 | return offsetTo(p.x, p.y); |
| 120 | } |
| 121 | Rect& offsetBy(const Point& dp) { |
| 122 | return offsetBy(dp.x, dp.y); |
| 123 | } |
| 124 | Rect& operator += (const Point& rhs) { |
| 125 | return offsetBy(rhs.x, rhs.y); |
| 126 | } |
| 127 | Rect& operator -= (const Point& rhs) { |
| 128 | return offsetBy(-rhs.x, -rhs.y); |
| 129 | } |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 130 | const Rect operator + (const Point& rhs) const; |
| 131 | const Rect operator - (const Point& rhs) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | |
| 133 | void translate(int dx, int dy) { // legacy, don't use. |
| 134 | offsetBy(dx, dy); |
| 135 | } |
| 136 | |
| 137 | Rect& offsetTo(int x, int y); |
| 138 | Rect& offsetBy(int x, int y); |
| 139 | bool intersect(const Rect& with, Rect* result) const; |
| 140 | }; |
| 141 | |
| 142 | ANDROID_BASIC_TYPES_TRAITS(Rect) |
| 143 | |
| 144 | }; // namespace android |
| 145 | |
| 146 | #endif // ANDROID_UI_RECT |