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 return x_ == 0 && y_ == 0; |
| 33 } |
| 34 |
| 35 // Add the components of the |other| ScrollOffset to the current ScrollOffset. |
| 36 void Add(const ScrollOffset& other) { |
| 37 x_ += other.x_; |
| 38 y_ += other.y_; |
| 39 } |
| 40 |
| 41 // Subtract the components of the |other| ScrollOffset from the current |
| 42 // ScrollOffset. |
| 43 void Subtract(const ScrollOffset& other) { |
| 44 x_ -= other.x_; |
| 45 y_ -= other.y_; |
| 46 } |
| 47 |
| 48 Vector2dF DeltaFrom(const ScrollOffset& v) const { |
| 49 return Vector2dF(x_ - v.x(), y_ - v.y()); |
| 50 } |
| 51 |
| 52 void operator+=(const ScrollOffset& other) { Add(other); } |
| 53 void operator-=(const ScrollOffset& other) { Subtract(other); } |
| 54 |
| 55 void SetToMin(const ScrollOffset& other) { |
| 56 x_ = x_ <= other.x_ ? x_ : other.x_; |
| 57 y_ = y_ <= other.y_ ? y_ : other.y_; |
| 58 } |
| 59 |
| 60 void SetToMax(const ScrollOffset& other) { |
| 61 x_ = x_ >= other.x_ ? x_ : other.x_; |
| 62 y_ = y_ >= other.y_ ? y_ : other.y_; |
| 63 } |
| 64 |
| 65 void Scale(double scale) { Scale(scale, scale); } |
| 66 void Scale(double x_scale, double y_scale) { |
| 67 x_ *= x_scale; |
| 68 y_ *= y_scale; |
| 69 } |
| 70 |
| 71 std::string ToString() const; |
| 72 |
| 73 private: |
| 74 double x_; |
| 75 double y_; |
| 76 }; |
| 77 |
| 78 inline bool operator==(const ScrollOffset& lhs, const ScrollOffset& rhs) { |
| 79 return lhs.x() == rhs.x() && lhs.y() == rhs.y(); |
| 80 } |
| 81 |
| 82 inline bool operator!=(const ScrollOffset& lhs, const ScrollOffset& rhs) { |
| 83 return lhs.x() != rhs.x() || lhs.y() != rhs.y(); |
| 84 } |
| 85 |
| 86 inline ScrollOffset operator-(const ScrollOffset& v) { |
| 87 return ScrollOffset(-v.x(), -v.y()); |
| 88 } |
| 89 |
| 90 inline ScrollOffset operator+(const ScrollOffset& lhs, |
| 91 const ScrollOffset& rhs) { |
| 92 ScrollOffset result = lhs; |
| 93 result.Add(rhs); |
| 94 return result; |
| 95 } |
| 96 |
| 97 inline ScrollOffset operator-(const ScrollOffset& lhs, |
| 98 const ScrollOffset& rhs) { |
| 99 ScrollOffset result = lhs; |
| 100 result.Add(-rhs); |
| 101 return result; |
| 102 } |
| 103 |
| 104 inline Vector2d ScrollOffsetToFlooredVector2d(const ScrollOffset& v) { |
| 105 return Vector2d(ToFlooredInt(v.x()), ToFlooredInt(v.y())); |
| 106 } |
| 107 |
| 108 inline Vector2dF ScrollOffsetToVector2dF(const ScrollOffset& v) { |
| 109 return Vector2dF(v.x(), v.y()); |
| 110 } |
| 111 |
| 112 inline ScrollOffset ScrollOffsetWithDelta(const ScrollOffset& offset, |
| 113 const Vector2dF& delta) { |
| 114 return ScrollOffset(offset.x() + delta.x(), |
| 115 offset.y() + delta.y()); |
| 116 } |
| 117 |
| 118 // This is declared here for use in gtest-based unit tests but is defined in |
| 119 // the gfx_test_support target. Depend on that to use this in your unit test. |
| 120 // This should not be used in production code - call ToString() instead. |
| 121 void PrintTo(const ScrollOffset& scroll_offset, ::std::ostream* os); |
| 122 |
| 123 } // namespace gfx |
| 124 |
| 125 #endif // UI_GFX_GEOMETRY_SCROLL_OFFSET_H_ |
OLD | NEW |