OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_GFX_GEOMETRY_RECT_F_H_ | 5 #ifndef UI_GFX_GEOMETRY_RECT_F_H_ |
6 #define UI_GFX_GEOMETRY_RECT_F_H_ | 6 #define UI_GFX_GEOMETRY_RECT_F_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "ui/gfx/geometry/point_f.h" | 11 #include "ui/gfx/geometry/point_f.h" |
12 #include "ui/gfx/geometry/rect_base.h" | |
13 #include "ui/gfx/geometry/size_f.h" | 12 #include "ui/gfx/geometry/size_f.h" |
14 #include "ui/gfx/geometry/vector2d_f.h" | 13 #include "ui/gfx/geometry/vector2d_f.h" |
15 | 14 |
16 namespace gfx { | 15 namespace gfx { |
17 | 16 |
18 class InsetsF; | 17 class InsetsF; |
19 | 18 |
20 // A floating version of gfx::Rect. | 19 // A floating version of gfx::Rect. |
21 class GFX_EXPORT RectF | 20 class GFX_EXPORT RectF { |
22 : public RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> { | |
23 public: | 21 public: |
24 RectF() | 22 RectF() {} |
25 : RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> | 23 RectF(float width, float height) : size_(width, height) {} |
26 (SizeF()) {} | |
27 | |
28 RectF(float width, float height) | |
29 : RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> | |
30 (SizeF(width, height)) {} | |
31 | |
32 RectF(float x, float y, float width, float height) | 24 RectF(float x, float y, float width, float height) |
33 : RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> | 25 : origin_(x, y), size_(width, height) {} |
34 (PointF(x, y), SizeF(width, height)) {} | 26 explicit RectF(const SizeF& size) : size_(size) {} |
35 | |
36 explicit RectF(const SizeF& size) | |
37 : RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> | |
38 (size) {} | |
39 | |
40 RectF(const PointF& origin, const SizeF& size) | 27 RectF(const PointF& origin, const SizeF& size) |
41 : RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> | 28 : origin_(origin), size_(size) {} |
42 (origin, size) {} | |
43 | 29 |
44 ~RectF() {} | 30 ~RectF() {} |
45 | 31 |
| 32 float x() const { return origin_.x(); } |
| 33 void set_x(float x) { origin_.set_x(x); } |
| 34 |
| 35 float y() const { return origin_.y(); } |
| 36 void set_y(float y) { origin_.set_y(y); } |
| 37 |
| 38 float width() const { return size_.width(); } |
| 39 void set_width(float width) { size_.set_width(width); } |
| 40 |
| 41 float height() const { return size_.height(); } |
| 42 void set_height(float height) { size_.set_height(height); } |
| 43 |
| 44 const PointF& origin() const { return origin_; } |
| 45 void set_origin(const PointF& origin) { origin_ = origin; } |
| 46 |
| 47 const SizeF& size() const { return size_; } |
| 48 void set_size(const SizeF& size) { size_ = size; } |
| 49 |
| 50 float right() const { return x() + width(); } |
| 51 float bottom() const { return y() + height(); } |
| 52 |
| 53 PointF top_right() const { return PointF(right(), y()); } |
| 54 PointF bottom_left() const { return PointF(x(), bottom()); } |
| 55 PointF bottom_right() const { return PointF(right(), bottom()); } |
| 56 |
| 57 Vector2dF OffsetFromOrigin() const { return Vector2dF(x(), y()); } |
| 58 |
| 59 void SetRect(float x, float y, float width, float height) { |
| 60 origin_.SetPoint(x, y); |
| 61 size_.SetSize(width, height); |
| 62 } |
| 63 |
| 64 // Shrink the rectangle by a horizontal and vertical distance on all sides. |
| 65 void Inset(float horizontal, float vertical) { |
| 66 Inset(horizontal, vertical, horizontal, vertical); |
| 67 } |
| 68 |
| 69 // Shrink the rectangle by the given insets. |
| 70 void Inset(const InsetsF& insets); |
| 71 |
| 72 // Shrink the rectangle by the specified amount on each side. |
| 73 void Inset(float left, float top, float right, float bottom); |
| 74 |
| 75 // Move the rectangle by a horizontal and vertical distance. |
| 76 void Offset(float horizontal, float vertical); |
| 77 void Offset(const Vector2dF& distance) { Offset(distance.x(), distance.y()); } |
| 78 void operator+=(const Vector2dF& offset); |
| 79 void operator-=(const Vector2dF& offset); |
| 80 |
| 81 InsetsF InsetsFrom(const RectF& inner) const; |
| 82 |
| 83 // Returns true if the area of the rectangle is zero. |
| 84 bool IsEmpty() const { return size_.IsEmpty(); } |
| 85 |
| 86 // A rect is less than another rect if its origin is less than |
| 87 // the other rect's origin. If the origins are equal, then the |
| 88 // shortest rect is less than the other. If the origin and the |
| 89 // height are equal, then the narrowest rect is less than. |
| 90 // This comparison is required to use Rects in sets, or sorted |
| 91 // vectors. |
| 92 bool operator<(const RectF& other) const; |
| 93 |
| 94 // Returns true if the point identified by point_x and point_y falls inside |
| 95 // this rectangle. The point (x, y) is inside the rectangle, but the |
| 96 // point (x + width, y + height) is not. |
| 97 bool Contains(float point_x, float point_y) const; |
| 98 |
| 99 // Returns true if the specified point is contained by this rectangle. |
| 100 bool Contains(const PointF& point) const { |
| 101 return Contains(point.x(), point.y()); |
| 102 } |
| 103 |
| 104 // Returns true if this rectangle contains the specified rectangle. |
| 105 bool Contains(const RectF& rect) const; |
| 106 |
| 107 // Returns true if this rectangle intersects the specified rectangle. |
| 108 // An empty rectangle doesn't intersect any rectangle. |
| 109 bool Intersects(const RectF& rect) const; |
| 110 |
| 111 // Computes the intersection of this rectangle with the given rectangle. |
| 112 void Intersect(const RectF& rect); |
| 113 |
| 114 // Computes the union of this rectangle with the given rectangle. The union |
| 115 // is the smallest rectangle containing both rectangles. |
| 116 void Union(const RectF& rect); |
| 117 |
| 118 // Computes the rectangle resulting from subtracting |rect| from |*this|, |
| 119 // i.e. the bounding rect of |Region(*this) - Region(rect)|. |
| 120 void Subtract(const RectF& rect); |
| 121 |
| 122 // Fits as much of the receiving rectangle into the supplied rectangle as |
| 123 // possible, becoming the result. For example, if the receiver had |
| 124 // a x-location of 2 and a width of 4, and the supplied rectangle had |
| 125 // an x-location of 0 with a width of 5, the returned rectangle would have |
| 126 // an x-location of 1 with a width of 4. |
| 127 void AdjustToFit(const RectF& rect); |
| 128 |
| 129 // Returns the center of this rectangle. |
| 130 PointF CenterPoint() const; |
| 131 |
| 132 // Becomes a rectangle that has the same center point but with a size capped |
| 133 // at given |size|. |
| 134 void ClampToCenteredSize(const SizeF& size); |
| 135 |
| 136 // Splits |this| in two halves, |left_half| and |right_half|. |
| 137 void SplitVertically(RectF* left_half, RectF* right_half) const; |
| 138 |
| 139 // Returns true if this rectangle shares an entire edge (i.e., same width or |
| 140 // same height) with the given rectangle, and the rectangles do not overlap. |
| 141 bool SharesEdgeWith(const RectF& rect) const; |
| 142 |
| 143 // Returns the manhattan distance from the rect to the point. If the point is |
| 144 // inside the rect, returns 0. |
| 145 float ManhattanDistanceToPoint(const PointF& point) const; |
| 146 |
| 147 // Returns the manhattan distance between the contents of this rect and the |
| 148 // contents of the given rect. That is, if the intersection of the two rects |
| 149 // is non-empty then the function returns 0. If the rects share a side, it |
| 150 // returns the smallest non-zero value appropriate for float. |
| 151 float ManhattanInternalDistance(const RectF& rect) const; |
| 152 |
46 // Scales the rectangle by |scale|. | 153 // Scales the rectangle by |scale|. |
47 void Scale(float scale) { | 154 void Scale(float scale) { |
48 Scale(scale, scale); | 155 Scale(scale, scale); |
49 } | 156 } |
50 | 157 |
51 void Scale(float x_scale, float y_scale) { | 158 void Scale(float x_scale, float y_scale) { |
52 set_origin(ScalePoint(origin(), x_scale, y_scale)); | 159 set_origin(ScalePoint(origin(), x_scale, y_scale)); |
53 set_size(ScaleSize(size(), x_scale, y_scale)); | 160 set_size(ScaleSize(size(), x_scale, y_scale)); |
54 } | 161 } |
55 | 162 |
56 // This method reports if the RectF can be safely converted to an integer | 163 // This method reports if the RectF can be safely converted to an integer |
57 // Rect. When it is false, some dimension of the RectF is outside the bounds | 164 // Rect. When it is false, some dimension of the RectF is outside the bounds |
58 // of what an integer can represent, and converting it to a Rect will require | 165 // of what an integer can represent, and converting it to a Rect will require |
59 // clamping. | 166 // clamping. |
60 bool IsExpressibleAsRect() const; | 167 bool IsExpressibleAsRect() const; |
61 | 168 |
62 std::string ToString() const; | 169 std::string ToString() const; |
| 170 |
| 171 private: |
| 172 PointF origin_; |
| 173 SizeF size_; |
63 }; | 174 }; |
64 | 175 |
65 inline bool operator==(const RectF& lhs, const RectF& rhs) { | 176 inline bool operator==(const RectF& lhs, const RectF& rhs) { |
66 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size(); | 177 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size(); |
67 } | 178 } |
68 | 179 |
69 inline bool operator!=(const RectF& lhs, const RectF& rhs) { | 180 inline bool operator!=(const RectF& lhs, const RectF& rhs) { |
70 return !(lhs == rhs); | 181 return !(lhs == rhs); |
71 } | 182 } |
72 | 183 |
(...skipping 25 matching lines...) Expand all Loading... |
98 } | 209 } |
99 | 210 |
100 // Constructs a rectangle with |p1| and |p2| as opposite corners. | 211 // Constructs a rectangle with |p1| and |p2| as opposite corners. |
101 // | 212 // |
102 // This could also be thought of as "the smallest rect that contains both | 213 // This could also be thought of as "the smallest rect that contains both |
103 // points", except that we consider points on the right/bottom edges of the | 214 // points", except that we consider points on the right/bottom edges of the |
104 // rect to be outside the rect. So technically one or both points will not be | 215 // rect to be outside the rect. So technically one or both points will not be |
105 // contained within the rect, because they will appear on one of these edges. | 216 // contained within the rect, because they will appear on one of these edges. |
106 GFX_EXPORT RectF BoundingRect(const PointF& p1, const PointF& p2); | 217 GFX_EXPORT RectF BoundingRect(const PointF& p1, const PointF& p2); |
107 | 218 |
108 #if !defined(COMPILER_MSVC) && !defined(__native_client__) | |
109 extern template class RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float>; | |
110 #endif | |
111 | |
112 // This is declared here for use in gtest-based unit tests but is defined in | 219 // This is declared here for use in gtest-based unit tests but is defined in |
113 // the gfx_test_support target. Depend on that to use this in your unit test. | 220 // the gfx_test_support target. Depend on that to use this in your unit test. |
114 // This should not be used in production code - call ToString() instead. | 221 // This should not be used in production code - call ToString() instead. |
115 void PrintTo(const RectF& rect, ::std::ostream* os); | 222 void PrintTo(const RectF& rect, ::std::ostream* os); |
116 | 223 |
117 } // namespace gfx | 224 } // namespace gfx |
118 | 225 |
119 #endif // UI_GFX_GEOMETRY_RECT_F_H_ | 226 #endif // UI_GFX_GEOMETRY_RECT_F_H_ |
OLD | NEW |