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

Side by Side Diff: ui/events/gestures/motion_event_aura.cc

Issue 251543003: Unified Gesture Recognizer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address jdduke comments. Created 6 years, 7 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 | Annotate | Revision Log
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/gestures/motion_event_aura.h"
6
7 #include "base/logging.h"
8 #include "ui/events/gestures/gesture_configuration.h"
9
10 namespace ui {
11
12 MotionEventAura::MotionEventAura()
13 : pointer_count_(0), cached_action_index_(-1) {
14 }
15
16 MotionEventAura::MotionEventAura(
17 size_t pointer_count,
18 const base::TimeDelta& last_touch_time,
19 Action cached_action,
20 int cached_action_index,
21 const PointData (&active_touches)[GestureSequence::kMaxGesturePoints])
22 : pointer_count_(pointer_count),
23 last_touch_time_(last_touch_time + base::TimeTicks()),
24 cached_action_(cached_action),
25 cached_action_index_(cached_action_index) {
26 DCHECK(pointer_count_);
27 for (size_t i = 0; i < pointer_count; ++i)
28 active_touches_[i] = active_touches[i];
29 }
30
31 MotionEventAura::~MotionEventAura() {}
32
33 MotionEventAura::PointData MotionEventAura::GetPointDataFromTouchEvent(
34 const TouchEvent& touch) {
35 PointData point_data;
36 point_data.x = touch.x();
37 point_data.y = touch.y();
38 point_data.touch_id = touch.touch_id();
39 point_data.pressure = touch.force();
40 point_data.source_device_id = touch.source_device_id();
41
42 // TODO(tdresser): at some point we should start using both radii if they are
43 // available, but for now we use the max.
44 point_data.major_radius = std::max(touch.radius_x(), touch.radius_y());
45 if (!point_data.major_radius)
46 point_data.major_radius = GestureConfiguration::default_radius();
47 return point_data;
48 }
49
50 void MotionEventAura::OnTouch(const TouchEvent& touch) {
51 switch (touch.type()) {
52 case ET_TOUCH_PRESSED:
53 AddTouch(touch);
54 break;
55 case ET_TOUCH_RELEASED:
56 case ET_TOUCH_CANCELLED:
57 // Removing these touch points needs to be postponed until after the
58 // MotionEvent has been dispatched. This cleanup occurs in
59 // CleanupRemovedTouchPoints.
60 UpdateTouch(touch);
61 break;
62 case ET_TOUCH_MOVED:
63 UpdateTouch(touch);
64 break;
65 default:
66 NOTREACHED();
67 break;
68 }
69
70 UpdateCachedAction(touch);
71 last_touch_time_ = touch.time_stamp() + base::TimeTicks();
72 }
73
74 int MotionEventAura::GetId() const {
75 return GetPointerId(0);
76 }
77
78 MotionEvent::Action MotionEventAura::GetAction() const {
79 return cached_action_;
80 }
81
82 int MotionEventAura::GetActionIndex() const {
83 DCHECK(cached_action_ == ACTION_POINTER_DOWN ||
84 cached_action_ == ACTION_POINTER_UP);
85 DCHECK_GE(cached_action_index_, 0);
86 DCHECK_LE(cached_action_index_, static_cast<int>(pointer_count_));
87 return cached_action_index_;
88 }
89
90 size_t MotionEventAura::GetPointerCount() const { return pointer_count_; }
91
92 int MotionEventAura::GetPointerId(size_t pointer_index) const {
93 DCHECK_LE(pointer_index, pointer_count_);
94 return active_touches_[pointer_index].touch_id;
95 }
96
97 float MotionEventAura::GetX(size_t pointer_index) const {
98 DCHECK_LE(pointer_index, pointer_count_);
99 return active_touches_[pointer_index].x;
100 }
101
102 float MotionEventAura::GetY(size_t pointer_index) const {
103 DCHECK_LE(pointer_index, pointer_count_);
104 return active_touches_[pointer_index].y;
105 }
106
107 float MotionEventAura::GetTouchMajor(size_t pointer_index) const {
108 DCHECK_LE(pointer_index, pointer_count_);
109 return active_touches_[pointer_index].major_radius * 2;
110 }
111
112 float MotionEventAura::GetPressure(size_t pointer_index) const {
113 DCHECK_LE(pointer_index, pointer_count_);
114 return active_touches_[pointer_index].pressure;
115 }
116
117 base::TimeTicks MotionEventAura::GetEventTime() const {
118 return last_touch_time_;
119 }
120
121 size_t MotionEventAura::GetHistorySize() const { return 0; }
122
123 base::TimeTicks MotionEventAura::GetHistoricalEventTime(
124 size_t historical_index) const {
125 NOTIMPLEMENTED();
126 return base::TimeTicks();
127 }
128
129 float MotionEventAura::GetHistoricalTouchMajor(size_t pointer_index,
130 size_t historical_index) const {
131 NOTIMPLEMENTED();
132 return 0;
133 }
134
135 float MotionEventAura::GetHistoricalX(size_t pointer_index,
136 size_t historical_index) const {
137 NOTIMPLEMENTED();
138 return 0;
139 }
140
141 float MotionEventAura::GetHistoricalY(size_t pointer_index,
142 size_t historical_index) const {
143 NOTIMPLEMENTED();
144 return 0;
145 }
146
147 scoped_ptr<MotionEvent> MotionEventAura::Clone() const {
148 return scoped_ptr<MotionEvent>(
149 new MotionEventAura(pointer_count_,
150 last_touch_time_ - base::TimeTicks(),
jdduke (slow) 2014/05/05 16:57:09 We should probably just take a TimeTicks in the co
jdduke (slow) 2014/05/05 16:57:09 Looks like we need a reformat here for indentation
tdresser 2014/05/05 17:45:14 Done.
tdresser 2014/05/05 17:45:14 Done.
151 cached_action_,
152 cached_action_index_,
153 active_touches_));
154 }
155 scoped_ptr<MotionEvent> MotionEventAura::Cancel() const {
156 return scoped_ptr<MotionEvent>(
157 new MotionEventAura(pointer_count_,
158 last_touch_time_ - base::TimeTicks(),
159 ACTION_CANCEL,
160 -1,
161 active_touches_));
162 }
163
164 void MotionEventAura::CleanupRemovedTouchPoints(const TouchEvent& event) {
165 if (event.type() != ET_TOUCH_RELEASED &&
166 event.type() != ET_TOUCH_CANCELLED) {
167 return;
168 }
169
170 int index_to_delete = GetIndexFromId(event.touch_id());
171 pointer_count_--;
172 active_touches_[index_to_delete] = active_touches_[pointer_count_];
173 }
174
175 int MotionEventAura::GetSourceDeviceId(size_t pointer_index) const {
176 DCHECK_LE(pointer_index, pointer_count_);
177 return active_touches_[pointer_index].source_device_id;
178 }
179
180 void MotionEventAura::AddTouch(const TouchEvent& touch) {
181 if (pointer_count_ == static_cast<size_t>(GestureSequence::kMaxGesturePoints))
182 return;
183
184 active_touches_[pointer_count_] = GetPointDataFromTouchEvent(touch);
185 pointer_count_++;
186 }
187
188
189 void MotionEventAura::UpdateTouch(const TouchEvent& touch) {
190 active_touches_[GetIndexFromId(touch.touch_id())] =
191 GetPointDataFromTouchEvent(touch);
192 }
193
194 void MotionEventAura::UpdateCachedAction(const TouchEvent& touch) {
195 DCHECK(pointer_count_);
196 switch (touch.type()) {
197 case ET_TOUCH_PRESSED:
198 if (pointer_count_ == 1) {
199 cached_action_ = ACTION_DOWN;
200 } else {
201 cached_action_ = ACTION_POINTER_DOWN;
202 cached_action_index_ =
203 static_cast<int>(GetIndexFromId(touch.touch_id()));
204 }
205 break;
206 case ET_TOUCH_RELEASED:
207 if (pointer_count_ == 1) {
208 cached_action_ = ACTION_UP;
209 } else {
210 cached_action_ = ACTION_POINTER_UP;
211 cached_action_index_ =
212 static_cast<int>(GetIndexFromId(touch.touch_id()));
213 DCHECK_LE(cached_action_index_, static_cast<int>(pointer_count_));
214 }
215 break;
216 case ET_TOUCH_CANCELLED:
217 cached_action_ = ACTION_CANCEL;
218 break;
219 case ET_TOUCH_MOVED:
220 cached_action_ = ACTION_MOVE;
221 break;
222 default:
223 NOTREACHED();
224 break;
225 }
226 }
227
228 size_t MotionEventAura::GetIndexFromId(int id) {
jdduke (slow) 2014/05/05 16:57:09 This seems pretty reasonable. It's O(N) but N is
229 for (size_t i = 0; i < pointer_count_; ++i) {
230 if (active_touches_[i].touch_id == id)
231 return i;
232 }
233 NOTREACHED();
234 return 0;
235 }
236
237 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698