fling_tracker.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2017 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 REMOTING_CLIENT_UI_FLING_TRACKER_H_
  5. #define REMOTING_CLIENT_UI_FLING_TRACKER_H_
  6. #include "base/time/time.h"
  7. namespace remoting {
  8. // A class for tracking the positions of an object moving through a viscous
  9. // liquid.
  10. class FlingTracker {
  11. public:
  12. // time_constant: The larger the number the longer it takes to fling and the
  13. // further the object can move.
  14. explicit FlingTracker(float time_constant);
  15. ~FlingTracker();
  16. // Sets the position of the object and start fling. This will reset the
  17. // existing fling.
  18. // |velocity_x| and |velocity_y| need to be in pixel per second.
  19. void StartFling(float velocity_x, float velocity_y);
  20. void StopFling();
  21. bool IsFlingInProgress() const;
  22. // time_elapsed: The time elapsed since the animation has started.
  23. // Moves forward the object to catch up with |time_elapsed|. The change in
  24. // positions will be written to |dx| and |dy|.
  25. // Returns true if the fling is still in progress at |time_elapsed|, false
  26. // otherwise, in which case |dx| and |dy| will not be touched.
  27. bool TrackMovement(base::TimeDelta time_elapsed, float* dx, float* dy);
  28. private:
  29. float time_constant_;
  30. float initial_speed_rate_ = 0.f;
  31. float fling_duration_ = 0.f;
  32. float velocity_ratio_x_ = 0.f;
  33. float velocity_ratio_y_ = 0.f;
  34. float previous_position_x_ = 0.f;
  35. float previous_position_y_ = 0.f;
  36. // FlingTracker is neither copyable nor movable.
  37. FlingTracker(const FlingTracker&) = delete;
  38. FlingTracker& operator=(const FlingTracker&) = delete;
  39. };
  40. } // namespace remoting
  41. #endif // REMOTING_CLIENT_UI_FLING_TRACKER_H_