Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1172)

Side by Side Diff: ui/events/gesture_detection/velocity_tracker_state.cc

Issue 171773012: Port of Android platform gesture detection code to C++ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "ui/events/gesture_detection/velocity_tracker_state.h"
6
7 #include "base/logging.h"
8
9 namespace ui {
10 namespace {
11 // Special constant to request the velocity of the active pointer.
12 const int ACTIVE_POINTER_ID = -1;
13 }
14
15 VelocityTrackerState::VelocityTrackerState()
16 : velocity_tracker_(VelocityTracker::STRATEGY_DEFAULT),
17 active_pointer_id_(ACTIVE_POINTER_ID) {}
18
19 VelocityTrackerState::VelocityTrackerState(VelocityTracker::Strategy strategy)
20 : velocity_tracker_(strategy), active_pointer_id_(ACTIVE_POINTER_ID) {}
21
22 VelocityTrackerState::~VelocityTrackerState() {}
23
24 void VelocityTrackerState::Clear() {
25 velocity_tracker_.Clear();
26 active_pointer_id_ = ACTIVE_POINTER_ID;
27 calculated_id_bits_.clear();
28 }
29
30 void VelocityTrackerState::AddMovement(const MotionEvent& event) {
31 velocity_tracker_.AddMovement(event);
32 }
33
34 void VelocityTrackerState::ComputeCurrentVelocity(int32_t units,
35 float max_velocity) {
36 DCHECK_GE(max_velocity, 0);
37
38 BitSet32 id_bits(velocity_tracker_.GetCurrentPointerIdBits());
39 calculated_id_bits_ = id_bits;
40
41 for (uint32_t index = 0; !id_bits.is_empty(); index++) {
42 uint32_t id = id_bits.clear_first_marked_bit();
43
44 float vx, vy;
45 velocity_tracker_.GetVelocity(id, &vx, &vy);
46
47 vx = vx * units / 1000.f;
48 vy = vy * units / 1000.f;
49
50 if (vx > max_velocity)
51 vx = max_velocity;
52 else if (vx < -max_velocity)
53 vx = -max_velocity;
54
55 if (vy > max_velocity)
56 vy = max_velocity;
57 else if (vy < -max_velocity)
58 vy = -max_velocity;
59
60 Velocity& velocity = calculated_velocity_[index];
61 velocity.vx = vx;
62 velocity.vy = vy;
63 }
64 }
65
66 float VelocityTrackerState::GetXVelocity(int32_t id) const {
67 float vx;
68 GetVelocity(id, &vx, NULL);
69 return vx;
70 }
71
72 float VelocityTrackerState::GetYVelocity(int32_t id) const {
73 float vy;
74 GetVelocity(id, NULL, &vy);
75 return vy;
76 }
77
78 void VelocityTrackerState::GetVelocity(int32_t id,
79 float* out_vx,
80 float* out_vy) const {
81 DCHECK(out_vx || out_vy);
82 if (id == ACTIVE_POINTER_ID)
83 id = velocity_tracker_.GetActivePointerId();
84
85 float vx, vy;
86 if (id >= 0 && id <= VelocityTracker::MAX_POINTER_ID &&
87 calculated_id_bits_.has_bit(id)) {
88 uint32_t index = calculated_id_bits_.get_index_of_bit(id);
89 const Velocity& velocity = calculated_velocity_[index];
90 vx = velocity.vx;
91 vy = velocity.vy;
92 } else {
93 vx = 0;
94 vy = 0;
95 }
96
97 if (out_vx)
98 *out_vx = vx;
99
100 if (out_vy)
101 *out_vy = vy;
102 }
103
104 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698