snap_fling_curve.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2018 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. #ifndef CC_INPUT_SNAP_FLING_CURVE_H_
  5. #define CC_INPUT_SNAP_FLING_CURVE_H_
  6. #include "base/time/time.h"
  7. #include "cc/cc_export.h"
  8. #include "ui/gfx/geometry/rect_f.h"
  9. #include "ui/gfx/geometry/vector2d_f.h"
  10. namespace cc {
  11. // The curve for the snap fling animation. The curve would generate a geometric
  12. // sequence of deltas to be scrolled at each frame.
  13. class CC_EXPORT SnapFlingCurve {
  14. public:
  15. // Creates the curve based on the start offset, target offset, and the first
  16. // inertial GSU's time_stamp.
  17. SnapFlingCurve(const gfx::PointF& start_offset,
  18. const gfx::PointF& target_offset,
  19. base::TimeTicks first_gsu_time);
  20. virtual ~SnapFlingCurve();
  21. // Estimate the total distance that will be scrolled given the first GSU's
  22. // delta
  23. static gfx::Vector2dF EstimateDisplacement(const gfx::Vector2dF& first_delta);
  24. // Returns the delta that should be scrolled at |time|.
  25. virtual gfx::Vector2dF GetScrollDelta(base::TimeTicks time);
  26. // Updates |current_displacement_|. This sync is necessary because the node
  27. // might be scrolled by other calls and the scrolls might be clamped.
  28. void UpdateCurrentOffset(const gfx::PointF& current_offset);
  29. // Returns true if the scroll has arrived at the snap destination.
  30. virtual bool IsFinished() const;
  31. base::TimeDelta duration() const { return duration_; }
  32. private:
  33. // Returns the curve's current distance at |current_time|.
  34. double GetCurrentCurveDistance(base::TimeDelta current_time);
  35. // The initial scroll offset of the scroller.
  36. const gfx::PointF start_offset_;
  37. // The total displacement to the snap position.
  38. const gfx::Vector2dF total_displacement_;
  39. // 1D representation of |total_displacement_|.
  40. const double total_distance_;
  41. // The current displacement that has been scrolled.
  42. gfx::Vector2dF current_displacement_;
  43. // The timestamp of the first GSU.
  44. const base::TimeTicks start_time_;
  45. // The number of deltas in the curve's geometric sequence.
  46. const double total_frames_;
  47. // The first delta that defines the curve's geometric sequence.
  48. const double first_delta_;
  49. // The total milliseconds needed to finish the curve.
  50. const base::TimeDelta duration_;
  51. bool is_finished_ = false;
  52. // |total_displacement_.x| / |total_distance_|
  53. double ratio_x_;
  54. // |total_displacement_.y| / |total_distance_|
  55. double ratio_y_;
  56. };
  57. } // namespace cc
  58. #endif // CC_INPUT_SNAP_FLING_CURVE_H_