OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef UI_GFX_GEOMETRY_SCROLL_OFFSET_H_ | |
6 #define UI_GFX_GEOMETRY_SCROLL_OFFSET_H_ | |
7 | |
8 #include <iosfwd> | |
9 #include <string> | |
10 | |
11 #include "ui/gfx/geometry/safe_integer_conversions.h" | |
12 #include "ui/gfx/geometry/vector2d.h" | |
13 #include "ui/gfx/gfx_export.h" | |
14 | |
15 namespace gfx { | |
16 | |
17 class GFX_EXPORT ScrollOffset { | |
18 public: | |
19 ScrollOffset() : x_(0), y_(0) {} | |
20 ScrollOffset(double x, double y) : x_(x), y_(y) {} | |
21 explicit ScrollOffset(const Vector2dF& v) : x_(v.x()), y_(v.y()) {} | |
22 explicit ScrollOffset(const Vector2d& v) : x_(v.x()), y_(v.y()) {} | |
23 | |
24 double x() const { return x_; } | |
25 void set_x(double x) { x_ = x; } | |
26 | |
27 double y() const { return y_; } | |
28 void set_y(double y) { y_ = y; } | |
29 | |
30 // True if both components are 0. | |
31 bool IsZero() const; | |
32 | |
33 // Add the components of the |other| ScrollOffset to the current ScrollOffset. | |
34 void Add(const ScrollOffset& other); | |
35 // Subtract the components of the |other| ScrollOffset from the current | |
36 // ScrollOffset. | |
37 void Subtract(const ScrollOffset& other); | |
38 | |
39 void operator+=(const ScrollOffset& other) { Add(other); } | |
40 void operator-=(const ScrollOffset& other) { Subtract(other); } | |
41 | |
42 void SetToMin(const ScrollOffset& other) { | |
43 x_ = x_ <= other.x_ ? x_ : other.x_; | |
44 y_ = y_ <= other.y_ ? y_ : other.y_; | |
45 } | |
46 | |
47 void SetToMax(const ScrollOffset& other) { | |
48 x_ = x_ >= other.x_ ? x_ : other.x_; | |
49 y_ = y_ >= other.y_ ? y_ : other.y_; | |
50 } | |
51 | |
52 void Scale(double scale) { Scale(scale, scale); } | |
53 void Scale(double x_scale, double y_scale) { | |
54 x_ *= x_scale; | |
55 y_ *= y_scale; | |
56 } | |
57 | |
58 std::string ToString() const; | |
59 | |
60 private: | |
61 double x_; | |
62 double y_; | |
63 }; | |
64 | |
65 inline bool operator==(const ScrollOffset& lhs, const ScrollOffset& rhs) { | |
66 return lhs.x() == rhs.x() && lhs.y() == rhs.y(); | |
67 } | |
68 | |
69 inline bool operator!=(const ScrollOffset& lhs, const ScrollOffset& rhs) { | |
70 return lhs.x() != rhs.x() || lhs.y() != rhs.y(); | |
71 } | |
72 | |
73 inline ScrollOffset operator-(const ScrollOffset& v) { | |
74 return ScrollOffset(-v.x(), -v.y()); | |
75 } | |
76 | |
77 inline ScrollOffset operator+(const ScrollOffset& lhs, | |
danakj
2014/09/27 00:00:44
can we remove the operator+? Seems like ScrollOffs
Yufeng Shen (Slow to review)
2014/09/29 19:27:34
LayerTreeImpl::GetDelegatedScrollOffset() has some
| |
78 const ScrollOffset& rhs) { | |
79 ScrollOffset result = lhs; | |
80 result.Add(rhs); | |
81 return result; | |
82 } | |
83 | |
84 inline ScrollOffset operator-(const ScrollOffset& lhs, | |
85 const ScrollOffset& rhs) { | |
86 ScrollOffset result = lhs; | |
87 result.Add(-rhs); | |
88 return result; | |
89 } | |
90 | |
91 inline Vector2d ScrollOffsetToFlooredVector2d(const ScrollOffset& v) { | |
92 return Vector2d(ToFlooredInt(v.x()), ToFlooredInt(v.y())); | |
93 } | |
94 | |
95 inline Vector2dF ScrollOffsetToVector2dF(const ScrollOffset& v) { | |
96 return Vector2dF(v.x(), v.y()); | |
97 } | |
98 | |
99 inline ScrollOffset ScrollOffsetWithDelta(const ScrollOffset& offset, | |
100 const Vector2dF& delta) { | |
101 return ScrollOffset(offset.x() + delta.x(), | |
102 offset.y() + delta.y()); | |
103 } | |
104 | |
105 // This is declared here for use in gtest-based unit tests but is defined in | |
106 // the gfx_test_support target. Depend on that to use this in your unit test. | |
107 // This should not be used in production code - call ToString() instead. | |
108 void PrintTo(const ScrollOffset& scroll_offset, ::std::ostream* os); | |
109 | |
110 } // namespace gfx | |
111 | |
112 #endif // UI_GFX_GEOMETRY_SCROLL_OFFSET_H_ | |
OLD | NEW |