summaryrefslogtreecommitdiffstats
path: root/ui/gfx/geometry/scroll_offset.h
blob: 496b5f4299e3e0de978b9ffc119124367c68b29a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_GEOMETRY_SCROLL_OFFSET_H_
#define UI_GFX_GEOMETRY_SCROLL_OFFSET_H_

#include <iosfwd>
#include <string>

#include "ui/gfx/geometry/safe_integer_conversions.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/gfx_export.h"

namespace gfx {

class GFX_EXPORT ScrollOffset {
 public:
  ScrollOffset() : x_(0), y_(0) {}
  ScrollOffset(double x, double y) : x_(x), y_(y) {}
  explicit ScrollOffset(const Vector2dF& v) : x_(v.x()), y_(v.y()) {}
  explicit ScrollOffset(const Vector2d& v) : x_(v.x()), y_(v.y()) {}

  double x() const { return x_; }
  void set_x(double x) { x_ = x; }

  double y() const { return y_; }
  void set_y(double y) { y_ = y; }

  // True if both components are 0.
  bool IsZero() const {
    return x_ == 0 && y_ == 0;
  }

  // Add the components of the |other| ScrollOffset to the current ScrollOffset.
  void Add(const ScrollOffset& other) {
    x_ += other.x_;
    y_ += other.y_;
  }

  // Subtract the components of the |other| ScrollOffset from the current
  // ScrollOffset.
  void Subtract(const ScrollOffset& other) {
    x_ -= other.x_;
    y_ -= other.y_;
  }

  Vector2dF DeltaFrom(const ScrollOffset& v) const {
    return Vector2dF(x_ - v.x(), y_ - v.y());
  }

  void operator+=(const ScrollOffset& other) { Add(other); }
  void operator-=(const ScrollOffset& other) { Subtract(other); }

  void SetToMin(const ScrollOffset& other) {
    x_ = x_ <= other.x_ ? x_ : other.x_;
    y_ = y_ <= other.y_ ? y_ : other.y_;
  }

  void SetToMax(const ScrollOffset& other) {
    x_ = x_ >= other.x_ ? x_ : other.x_;
    y_ = y_ >= other.y_ ? y_ : other.y_;
  }

  void Scale(double scale) { Scale(scale, scale); }
  void Scale(double x_scale, double y_scale) {
    x_ *= x_scale;
    y_ *= y_scale;
  }

  std::string ToString() const;

 private:
  double x_;
  double y_;
};

inline bool operator==(const ScrollOffset& lhs, const ScrollOffset& rhs) {
  return lhs.x() == rhs.x() && lhs.y() == rhs.y();
}

inline bool operator!=(const ScrollOffset& lhs, const ScrollOffset& rhs) {
  return lhs.x() != rhs.x() || lhs.y() != rhs.y();
}

inline ScrollOffset operator-(const ScrollOffset& v) {
  return ScrollOffset(-v.x(), -v.y());
}

inline ScrollOffset operator+(const ScrollOffset& lhs,
                              const ScrollOffset& rhs) {
  ScrollOffset result = lhs;
  result.Add(rhs);
  return result;
}

inline ScrollOffset operator-(const ScrollOffset& lhs,
                              const ScrollOffset& rhs) {
  ScrollOffset result = lhs;
  result.Add(-rhs);
  return result;
}

inline Vector2d ScrollOffsetToFlooredVector2d(const ScrollOffset& v) {
  return Vector2d(ToFlooredInt(v.x()), ToFlooredInt(v.y()));
}

inline Vector2dF ScrollOffsetToVector2dF(const ScrollOffset& v) {
  return Vector2dF(v.x(), v.y());
}

inline ScrollOffset ScrollOffsetWithDelta(const ScrollOffset& offset,
                                          const Vector2dF& delta) {
  return ScrollOffset(offset.x() + delta.x(),
                      offset.y() + delta.y());
}

// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const ScrollOffset& scroll_offset, ::std::ostream* os);

}  // namespace gfx

#endif  // UI_GFX_GEOMETRY_SCROLL_OFFSET_H_