velocity_tracker_state.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include "ui/events/gesture_detection/velocity_tracker_state.h"
  5. #include "base/check_op.h"
  6. #include "ui/events/gesture_detection/motion_event.h"
  7. namespace ui {
  8. namespace {
  9. // Special constant to request the velocity of the active pointer.
  10. const int ACTIVE_POINTER_ID = -1;
  11. }
  12. VelocityTrackerState::VelocityTrackerState(VelocityTracker::Strategy strategy)
  13. : velocity_tracker_(strategy), active_pointer_id_(ACTIVE_POINTER_ID) {}
  14. VelocityTrackerState::~VelocityTrackerState() {}
  15. void VelocityTrackerState::Clear() {
  16. velocity_tracker_.Clear();
  17. active_pointer_id_ = ACTIVE_POINTER_ID;
  18. calculated_id_bits_.clear();
  19. }
  20. void VelocityTrackerState::AddMovement(const MotionEvent& event) {
  21. velocity_tracker_.AddMovement(event);
  22. }
  23. void VelocityTrackerState::ComputeCurrentVelocity(int32_t units,
  24. float max_velocity) {
  25. DCHECK_GE(max_velocity, 0);
  26. BitSet32 id_bits(velocity_tracker_.GetCurrentPointerIdBits());
  27. calculated_id_bits_ = id_bits;
  28. for (uint32_t index = 0; !id_bits.is_empty(); index++) {
  29. uint32_t id = id_bits.clear_first_marked_bit();
  30. float vx, vy;
  31. velocity_tracker_.GetVelocity(id, &vx, &vy);
  32. vx = vx * units / 1000.f;
  33. vy = vy * units / 1000.f;
  34. if (vx > max_velocity)
  35. vx = max_velocity;
  36. else if (vx < -max_velocity)
  37. vx = -max_velocity;
  38. if (vy > max_velocity)
  39. vy = max_velocity;
  40. else if (vy < -max_velocity)
  41. vy = -max_velocity;
  42. Velocity& velocity = calculated_velocity_[index];
  43. velocity.vx = vx;
  44. velocity.vy = vy;
  45. }
  46. }
  47. float VelocityTrackerState::GetXVelocity(int32_t id) const {
  48. float vx;
  49. GetVelocity(id, &vx, NULL);
  50. return vx;
  51. }
  52. float VelocityTrackerState::GetYVelocity(int32_t id) const {
  53. float vy;
  54. GetVelocity(id, NULL, &vy);
  55. return vy;
  56. }
  57. void VelocityTrackerState::GetVelocity(int32_t id,
  58. float* out_vx,
  59. float* out_vy) const {
  60. DCHECK(out_vx || out_vy);
  61. if (id == ACTIVE_POINTER_ID)
  62. id = velocity_tracker_.GetActivePointerId();
  63. float vx, vy;
  64. if (id >= 0 && id <= MotionEvent::MAX_POINTER_ID &&
  65. calculated_id_bits_.has_bit(id)) {
  66. uint32_t index = calculated_id_bits_.get_index_of_bit(id);
  67. const Velocity& velocity = calculated_velocity_[index];
  68. vx = velocity.vx;
  69. vy = velocity.vy;
  70. } else {
  71. vx = 0;
  72. vy = 0;
  73. }
  74. if (out_vx)
  75. *out_vx = vx;
  76. if (out_vy)
  77. *out_vy = vy;
  78. }
  79. } // namespace ui