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_POINT_H_ | 5 #ifndef UI_GFX_GEOMETRY_POINT_H_ |
6 #define UI_GFX_GEOMETRY_POINT_H_ | 6 #define UI_GFX_GEOMETRY_POINT_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "ui/gfx/geometry/point_base.h" | |
12 #include "ui/gfx/geometry/point_f.h" | 11 #include "ui/gfx/geometry/point_f.h" |
13 #include "ui/gfx/geometry/vector2d.h" | 12 #include "ui/gfx/geometry/vector2d.h" |
14 #include "ui/gfx/gfx_export.h" | 13 #include "ui/gfx/gfx_export.h" |
15 | 14 |
16 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
17 typedef unsigned long DWORD; | 16 typedef unsigned long DWORD; |
18 typedef struct tagPOINT POINT; | 17 typedef struct tagPOINT POINT; |
19 #elif defined(OS_IOS) | 18 #elif defined(OS_IOS) |
20 #include <CoreGraphics/CoreGraphics.h> | 19 #include <CoreGraphics/CoreGraphics.h> |
21 #elif defined(OS_MACOSX) | 20 #elif defined(OS_MACOSX) |
22 #include <ApplicationServices/ApplicationServices.h> | 21 #include <ApplicationServices/ApplicationServices.h> |
23 #endif | 22 #endif |
24 | 23 |
25 namespace gfx { | 24 namespace gfx { |
26 | 25 |
27 // A point has an x and y coordinate. | 26 // A point has an x and y coordinate. |
28 class GFX_EXPORT Point : public PointBase<Point, int, Vector2d> { | 27 class GFX_EXPORT Point { |
29 public: | 28 public: |
30 Point() : PointBase<Point, int, Vector2d>(0, 0) {} | 29 Point() : x_(0), y_(0) {} |
31 Point(int x, int y) : PointBase<Point, int, Vector2d>(x, y) {} | 30 Point(int x, int y) : x_(x), y_(y) {} |
32 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
33 // |point| is a DWORD value that contains a coordinate. The x-coordinate is | 32 // |point| is a DWORD value that contains a coordinate. The x-coordinate is |
34 // the low-order short and the y-coordinate is the high-order short. This | 33 // the low-order short and the y-coordinate is the high-order short. This |
35 // value is commonly acquired from GetMessagePos/GetCursorPos. | 34 // value is commonly acquired from GetMessagePos/GetCursorPos. |
36 explicit Point(DWORD point); | 35 explicit Point(DWORD point); |
37 explicit Point(const POINT& point); | 36 explicit Point(const POINT& point); |
38 Point& operator=(const POINT& point); | 37 Point& operator=(const POINT& point); |
39 #elif defined(OS_MACOSX) | 38 #elif defined(OS_MACOSX) |
40 explicit Point(const CGPoint& point); | 39 explicit Point(const CGPoint& point) : x_(point.x), y_(point.y) {} |
41 #endif | 40 #endif |
42 | 41 |
43 ~Point() {} | 42 ~Point() {} |
44 | 43 |
45 #if defined(OS_WIN) | 44 #if defined(OS_WIN) |
46 POINT ToPOINT() const; | 45 POINT ToPOINT() const; |
47 #elif defined(OS_MACOSX) | 46 #elif defined(OS_MACOSX) |
48 CGPoint ToCGPoint() const; | 47 CGPoint ToCGPoint() const { return CGPointMake(x(), y()); } |
49 #endif | 48 #endif |
50 | 49 |
| 50 int x() const { return x_; } |
| 51 int y() const { return y_; } |
| 52 void set_x(int x) { x_ = x; } |
| 53 void set_y(int y) { y_ = y; } |
| 54 |
| 55 void SetPoint(int x, int y) { |
| 56 x_ = x; |
| 57 y_ = y; |
| 58 } |
| 59 |
| 60 void Offset(int delta_x, int delta_y) { |
| 61 x_ += delta_x; |
| 62 y_ += delta_y; |
| 63 } |
| 64 |
| 65 void operator+=(const Vector2d& vector) { |
| 66 x_ += vector.x(); |
| 67 y_ += vector.y(); |
| 68 } |
| 69 |
| 70 void operator-=(const Vector2d& vector) { |
| 71 x_ -= vector.x(); |
| 72 y_ -= vector.y(); |
| 73 } |
| 74 |
| 75 void SetToMin(const Point& other); |
| 76 void SetToMax(const Point& other); |
| 77 |
| 78 bool IsOrigin() const { return x_ == 0 && y_ == 0; } |
| 79 |
| 80 Vector2d OffsetFromOrigin() const { return Vector2d(x_, y_); } |
| 81 |
| 82 // A point is less than another point if its y-value is closer |
| 83 // to the origin. If the y-values are the same, then point with |
| 84 // the x-value closer to the origin is considered less than the |
| 85 // other. |
| 86 // This comparison is required to use Point in sets, or sorted |
| 87 // vectors. |
| 88 bool operator<(const Point& rhs) const { |
| 89 return (y_ == rhs.y_) ? (x_ < rhs.x_) : (y_ < rhs.y_); |
| 90 } |
| 91 |
51 operator PointF() const { | 92 operator PointF() const { |
52 return PointF(x(), y()); | 93 return PointF(x(), y()); |
53 } | 94 } |
54 | 95 |
55 // Returns a string representation of point. | 96 // Returns a string representation of point. |
56 std::string ToString() const; | 97 std::string ToString() const; |
| 98 |
| 99 private: |
| 100 int x_; |
| 101 int y_; |
57 }; | 102 }; |
58 | 103 |
59 inline bool operator==(const Point& lhs, const Point& rhs) { | 104 inline bool operator==(const Point& lhs, const Point& rhs) { |
60 return lhs.x() == rhs.x() && lhs.y() == rhs.y(); | 105 return lhs.x() == rhs.x() && lhs.y() == rhs.y(); |
61 } | 106 } |
62 | 107 |
63 inline bool operator!=(const Point& lhs, const Point& rhs) { | 108 inline bool operator!=(const Point& lhs, const Point& rhs) { |
64 return !(lhs == rhs); | 109 return !(lhs == rhs); |
65 } | 110 } |
66 | 111 |
(...skipping 10 matching lines...) Expand all Loading... |
77 } | 122 } |
78 | 123 |
79 inline Vector2d operator-(const Point& lhs, const Point& rhs) { | 124 inline Vector2d operator-(const Point& lhs, const Point& rhs) { |
80 return Vector2d(lhs.x() - rhs.x(), lhs.y() - rhs.y()); | 125 return Vector2d(lhs.x() - rhs.x(), lhs.y() - rhs.y()); |
81 } | 126 } |
82 | 127 |
83 inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) { | 128 inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) { |
84 return Point(offset_from_origin.x(), offset_from_origin.y()); | 129 return Point(offset_from_origin.x(), offset_from_origin.y()); |
85 } | 130 } |
86 | 131 |
87 #if !defined(COMPILER_MSVC) && !defined(__native_client__) | |
88 extern template class PointBase<Point, int, Vector2d>; | |
89 #endif | |
90 | |
91 // This is declared here for use in gtest-based unit tests but is defined in | 132 // This is declared here for use in gtest-based unit tests but is defined in |
92 // the gfx_test_support target. Depend on that to use this in your unit test. | 133 // the gfx_test_support target. Depend on that to use this in your unit test. |
93 // This should not be used in production code - call ToString() instead. | 134 // This should not be used in production code - call ToString() instead. |
94 void PrintTo(const Point& point, ::std::ostream* os); | 135 void PrintTo(const Point& point, ::std::ostream* os); |
95 | 136 |
96 } // namespace gfx | 137 } // namespace gfx |
97 | 138 |
98 #endif // UI_GFX_GEOMETRY_POINT_H_ | 139 #endif // UI_GFX_GEOMETRY_POINT_H_ |
OLD | NEW |