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

Side by Side Diff: content/browser/renderer_host/input/gestures/velocity_tracker_state.cc

Issue 128613003: [Tracking Patch] Unified gesture detection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 11 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 "content/browser/renderer_host/input/gestures/velocity_tracker_state.h"
6
7 #include "base/logging.h"
8
9 namespace content {
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 : active_pointer_id_(ACTIVE_POINTER_ID) {}
17
18 void VelocityTrackerState::Clear() {
19 velocity_tracker_.Clear();
20 active_pointer_id_ = ACTIVE_POINTER_ID;
21 calculated_id_bits_.clear();
22 }
23
24 void VelocityTrackerState::AddMovement(const MotionEvent& event) {
25 velocity_tracker_.AddMovement(event);
26 }
27
28 void VelocityTrackerState::ComputeCurrentVelocity(int32_t units,
29 float max_velocity) {
30 BitSet32 id_bits(velocity_tracker_.GetCurrentPointerIdBits());
31 calculated_id_bits_ = id_bits;
32
33 for (uint32_t index = 0; !id_bits.is_empty(); index++) {
34 uint32_t id = id_bits.clear_first_marked_bit();
35
36 float vx, vy;
37 velocity_tracker_.GetVelocity(id, &vx, &vy);
38
39 vx = vx * units / 1000.f;
40 vy = vy * units / 1000.f;
41
42 if (vx > max_velocity)
43 vx = max_velocity;
44 else if (vx < -max_velocity)
45 vx = -max_velocity;
46
47 if (vy > max_velocity)
48 vy = max_velocity;
49 else if (vy < -max_velocity)
50 vy = -max_velocity;
51
52 Velocity& velocity = calculated_velocity_[index];
53 velocity.vx = vx;
54 velocity.vy = vy;
55 }
56 }
57
58 float VelocityTrackerState::GetXVelocity(int32_t id) const {
59 float vx;
60 GetVelocity(id, &vx, NULL);
61 return vx;
62 }
63
64 float VelocityTrackerState::GetYVelocity(int32_t id) const {
65 float vy;
66 GetVelocity(id, &vy, NULL);
67 return vy;
68 }
69
70 void VelocityTrackerState::GetVelocity(int32_t id,
71 float* out_vx,
72 float* out_vy) const {
73 DCHECK(out_vx || out_vy);
74 if (id == ACTIVE_POINTER_ID)
75 id = velocity_tracker_.GetActivePointerId();
76
77 float vx, vy;
78 if (id >= 0 && id <= VelocityTracker::MAX_POINTER_ID &&
79 calculated_id_bits_.has_bit(id)) {
80 uint32_t index = calculated_id_bits_.get_index_of_bit(id);
81 const Velocity& velocity = calculated_velocity_[index];
82 vx = velocity.vx;
83 vy = velocity.vy;
84 } else {
85 vx = 0;
86 vy = 0;
87 }
88
89 if (out_vx)
90 *out_vx = vx;
91
92 if (out_vy)
93 *out_vy = vy;
94 }
95
96 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698