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 // A template for a simple rectangle class. The containment semantics | 5 // A template for a simple rectangle class. The containment semantics |
6 // are array-like; that is, the coordinate (x, y) is considered to be | 6 // are array-like; that is, the coordinate (x, y) is considered to be |
7 // contained by the rectangle, but the coordinate (x + width, y) is not. | 7 // contained by the rectangle, but the coordinate (x + width, y) is not. |
8 // The class will happily let you create malformed rectangles (that is, | 8 // The class will happily let you create malformed rectangles (that is, |
9 // rectangles with negative width and/or height), but there will be assertions | 9 // rectangles with negative width and/or height), but there will be assertions |
10 // in the operations (such as Contains()) to complain in this case. | 10 // in the operations (such as Contains()) to complain in this case. |
11 | 11 |
12 #ifndef UI_GFX_RECT_BASE_H_ | 12 #ifndef UI_GFX_RECT_BASE_H_ |
13 #define UI_GFX_RECT_BASE_H_ | 13 #define UI_GFX_RECT_BASE_H_ |
14 | 14 |
15 #include <string> | 15 #include <string> |
16 | 16 |
17 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
18 | 18 |
19 namespace gfx { | 19 namespace gfx { |
20 | 20 |
21 template<typename Class, | 21 template<typename Class, |
22 typename PointClass, | 22 typename PointClass, |
23 typename SizeClass, | 23 typename SizeClass, |
24 typename InsetsClass, | 24 typename InsetsClass, |
25 typename VectorClass, | |
25 typename Type> | 26 typename Type> |
26 class UI_EXPORT RectBase { | 27 class UI_EXPORT RectBase { |
27 public: | 28 public: |
28 Type x() const { return origin_.x(); } | 29 Type x() const { return origin_.x(); } |
29 void set_x(Type x) { origin_.set_x(x); } | 30 void set_x(Type x) { origin_.set_x(x); } |
30 | 31 |
31 Type y() const { return origin_.y(); } | 32 Type y() const { return origin_.y(); } |
32 void set_y(Type y) { origin_.set_y(y); } | 33 void set_y(Type y) { origin_.set_y(y); } |
33 | 34 |
34 Type width() const { return size_.width(); } | 35 Type width() const { return size_.width(); } |
35 void set_width(Type width) { size_.set_width(width); } | 36 void set_width(Type width) { size_.set_width(width); } |
36 | 37 |
37 Type height() const { return size_.height(); } | 38 Type height() const { return size_.height(); } |
38 void set_height(Type height) { size_.set_height(height); } | 39 void set_height(Type height) { size_.set_height(height); } |
39 | 40 |
40 const PointClass& origin() const { return origin_; } | 41 const PointClass& origin() const { return origin_; } |
41 void set_origin(const PointClass& origin) { origin_ = origin; } | 42 void set_origin(const PointClass& origin) { origin_ = origin; } |
42 | 43 |
43 const SizeClass& size() const { return size_; } | 44 const SizeClass& size() const { return size_; } |
44 void set_size(const SizeClass& size) { size_ = size; } | 45 void set_size(const SizeClass& size) { size_ = size; } |
45 | 46 |
46 Type right() const { return x() + width(); } | 47 Type right() const { return x() + width(); } |
47 Type bottom() const { return y() + height(); } | 48 Type bottom() const { return y() + height(); } |
48 | 49 |
50 VectorClass OffsetFromOrigin() const { | |
51 return VectorClass(x(), y()); | |
52 } | |
53 | |
49 void SetRect(Type x, Type y, Type width, Type height); | 54 void SetRect(Type x, Type y, Type width, Type height); |
50 | 55 |
51 // Shrink the rectangle by a horizontal and vertical distance on all sides. | 56 // Shrink the rectangle by a horizontal and vertical distance on all sides. |
52 void Inset(Type horizontal, Type vertical) { | 57 void Inset(Type horizontal, Type vertical) { |
53 Inset(horizontal, vertical, horizontal, vertical); | 58 Inset(horizontal, vertical, horizontal, vertical); |
54 } | 59 } |
55 | 60 |
56 // Shrink the rectangle by the given insets. | 61 // Shrink the rectangle by the given insets. |
57 void Inset(const InsetsClass& insets); | 62 void Inset(const InsetsClass& insets); |
58 | 63 |
59 // Shrink the rectangle by the specified amount on each side. | 64 // Shrink the rectangle by the specified amount on each side. |
60 void Inset(Type left, Type top, Type right, Type bottom); | 65 void Inset(Type left, Type top, Type right, Type bottom); |
61 | 66 |
62 // Move the rectangle by a horizontal and vertical distance. | 67 // Move the rectangle by a horizontal and vertical distance. |
63 void Offset(Type horizontal, Type vertical); | 68 void Offset(Type horizontal, Type vertical); |
64 void Offset(const PointClass& point) { | 69 void Offset(const VectorClass& distance) { |
65 Offset(point.x(), point.y()); | 70 Offset(distance.x(), distance.y()); |
66 } | 71 } |
67 | 72 |
68 InsetsClass InsetsFrom(const Class& inner) const { | 73 InsetsClass InsetsFrom(const Class& inner) const { |
69 return InsetsClass(inner.y() - y(), | 74 return InsetsClass(inner.y() - y(), |
70 inner.x() - x(), | 75 inner.x() - x(), |
71 bottom() - inner.bottom(), | 76 bottom() - inner.bottom(), |
72 right() - inner.right()); | 77 right() - inner.right()); |
73 } | 78 } |
74 | 79 |
75 // Returns true if the area of the rectangle is zero. | 80 // Returns true if the area of the rectangle is zero. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 // Fits as much of the receiving rectangle into the supplied rectangle as | 120 // Fits as much of the receiving rectangle into the supplied rectangle as |
116 // possible, becoming the result. For example, if the receiver had | 121 // possible, becoming the result. For example, if the receiver had |
117 // a x-location of 2 and a width of 4, and the supplied rectangle had | 122 // a x-location of 2 and a width of 4, and the supplied rectangle had |
118 // an x-location of 0 with a width of 5, the returned rectangle would have | 123 // an x-location of 0 with a width of 5, the returned rectangle would have |
119 // an x-location of 1 with a width of 4. | 124 // an x-location of 1 with a width of 4. |
120 void AdjustToFit(const Class& rect); | 125 void AdjustToFit(const Class& rect); |
121 | 126 |
122 // Returns the center of this rectangle. | 127 // Returns the center of this rectangle. |
123 PointClass CenterPoint() const; | 128 PointClass CenterPoint() const; |
124 | 129 |
130 // Returns the offset to the center of this rectangle from its origin. | |
131 VectorClass CenterOffset() const { | |
Peter Kasting
2012/10/30 01:14:14
I'm not convinced of the utility of this and the r
danakj
2012/10/30 19:21:21
k
| |
132 return CenterPoint().OffsetFromOrigin(); | |
133 } | |
134 | |
125 // Becomes a rectangle that has the same center point but with a size capped | 135 // Becomes a rectangle that has the same center point but with a size capped |
126 // at given |size|. | 136 // at given |size|. |
127 void ClampToCenteredSize(const SizeClass& size); | 137 void ClampToCenteredSize(const SizeClass& size); |
128 | 138 |
129 // Splits |this| in two halves, |left_half| and |right_half|. | 139 // Splits |this| in two halves, |left_half| and |right_half|. |
130 void SplitVertically(Class* left_half, Class* right_half) const; | 140 void SplitVertically(Class* left_half, Class* right_half) const; |
131 | 141 |
132 // Returns true if this rectangle shares an entire edge (i.e., same width or | 142 // Returns true if this rectangle shares an entire edge (i.e., same width or |
133 // same height) with the given rectangle, and the rectangles do not overlap. | 143 // same height) with the given rectangle, and the rectangles do not overlap. |
134 bool SharesEdgeWith(const Class& rect) const; | 144 bool SharesEdgeWith(const Class& rect) const; |
135 | 145 |
136 protected: | 146 protected: |
137 RectBase(const PointClass& origin, const SizeClass& size); | 147 RectBase(const PointClass& origin, const SizeClass& size); |
138 explicit RectBase(const SizeClass& size); | 148 explicit RectBase(const SizeClass& size); |
139 explicit RectBase(const PointClass& origin); | 149 explicit RectBase(const PointClass& origin); |
140 // Destructor is intentionally made non virtual and protected. | 150 // Destructor is intentionally made non virtual and protected. |
141 // Do not make this public. | 151 // Do not make this public. |
142 ~RectBase(); | 152 ~RectBase(); |
143 | 153 |
144 private: | 154 private: |
145 PointClass origin_; | 155 PointClass origin_; |
146 SizeClass size_; | 156 SizeClass size_; |
147 }; | 157 }; |
148 | 158 |
149 } // namespace gfx | 159 } // namespace gfx |
150 | 160 |
151 #endif // UI_GFX_RECT_BASE_H_ | 161 #endif // UI_GFX_RECT_BASE_H_ |
OLD | NEW |