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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 ~RectBase(); | 147 ~RectBase(); |
143 | 148 |
144 private: | 149 private: |
145 PointClass origin_; | 150 PointClass origin_; |
146 SizeClass size_; | 151 SizeClass size_; |
147 }; | 152 }; |
148 | 153 |
149 } // namespace gfx | 154 } // namespace gfx |
150 | 155 |
151 #endif // UI_GFX_RECT_BASE_H_ | 156 #endif // UI_GFX_RECT_BASE_H_ |
OLD | NEW |