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

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

Issue 251543003: Unified Gesture Recognizer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix windows linking 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/gesture_detection/motion_event_ui.h"
6
7 #include "base/logging.h"
8 #include "ui/events/gestures/gesture_configuration.h"
9
10 namespace ui {
11
12 MotionEventUI::MotionEventUI() : pointer_count_(0), cached_action_index_(-1) {}
13
14 MotionEventUI::MotionEventUI(
15 size_t pointer_count,
16 const base::TimeDelta& last_touch_time,
17 Action cached_action,
18 int cached_action_index,
19 const PointData (&active_touches)[GestureSequence::kMaxGesturePoints],
20 const std::map<int, int>& id_to_index)
21 : pointer_count_(pointer_count),
22 last_touch_time_(last_touch_time),
23 cached_action_(cached_action),
24 cached_action_index_(cached_action_index),
25 id_to_index_(id_to_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 MotionEventUI::~MotionEventUI() {}
32
33 void MotionEventUI::OnTouch(const TouchEvent& touch) {
34 switch (touch.type()) {
35 case ET_TOUCH_PRESSED:
36 AddTouch(touch);
37 break;
38 case ET_TOUCH_RELEASED:
39 case ET_TOUCH_CANCELLED:
40 UpdateTouch(touch);
41 // Removing these touch points needs to be postponed until after the
42 // MotionEvent has been dispatched. This cleanup occurs in
43 // CleanupRemovedTouchPoints.
44 break;
45 case ET_TOUCH_MOVED:
46 UpdateTouch(touch);
47 break;
48 default:
49 NOTREACHED();
50 break;
51 }
52
53 SetCachedAction(touch);
jdduke (slow) 2014/05/02 17:56:43 UpdateCachedAction?
tdresser 2014/05/05 15:42:35 Done.
54 last_touch_time_ = touch.time_stamp();
jdduke (slow) 2014/05/02 17:56:43 We should probably make this member a |TimeTicks|,
tdresser 2014/05/05 15:42:35 Done.
55 }
56
57 int MotionEventUI::GetId() const {
58 return GetPointerId(0);
59 }
60
61 MotionEvent::Action MotionEventUI::GetAction() const { return cached_action_; }
62
63 int MotionEventUI::GetActionIndex() const {
64 DCHECK(cached_action_ == ACTION_POINTER_DOWN ||
jdduke (slow) 2014/05/02 17:56:43 Hmm, we should probably make a note in the MotionE
tdresser 2014/05/05 15:42:35 Done.
65 cached_action_ == ACTION_POINTER_UP);
66 DCHECK(0 <= cached_action_index_ &&
jdduke (slow) 2014/05/02 17:56:43 DCHECK_GE + DCHECK_LT
tdresser 2014/05/05 15:42:35 Done.
67 cached_action_index_ < static_cast<int>(pointer_count_));
68 return cached_action_index_;
69 }
70
71 size_t MotionEventUI::GetPointerCount() const { return pointer_count_; }
72
73 int MotionEventUI::GetPointerId(size_t pointer_index) const {
74 DCHECK(pointer_index < pointer_count_);
jdduke (slow) 2014/05/02 17:56:43 DCHECK_LT for all |pointer_index| checks.
tdresser 2014/05/05 15:42:35 Done.
75 return active_touches_[pointer_index].touch_id;
76 }
77
78 float MotionEventUI::GetX(size_t pointer_index) const {
79 DCHECK(pointer_index < pointer_count_);
80 return active_touches_[pointer_index].x;
81 }
82
83 float MotionEventUI::GetY(size_t pointer_index) const {
84 DCHECK(pointer_index < pointer_count_);
85 return active_touches_[pointer_index].y;
86 }
87
88 float MotionEventUI::GetTouchMajor(size_t pointer_index) const {
89 DCHECK(pointer_index < pointer_count_);
90 return active_touches_[pointer_index].major_radius * 2;
91 }
92
93 float MotionEventUI::GetPressure(size_t pointer_index) const {
94 DCHECK(pointer_index < pointer_count_);
95 return active_touches_[pointer_index].pressure;
96 }
97
98 base::TimeTicks MotionEventUI::GetEventTime() const {
99 return base::TimeTicks() + last_touch_time_;
100 }
101
102 size_t MotionEventUI::GetHistorySize() const { return 0; }
103
104 base::TimeTicks MotionEventUI::GetHistoricalEventTime(
105 size_t historical_index) const {
106 return base::TimeTicks();
107 NOTIMPLEMENTED();
jdduke (slow) 2014/05/02 17:56:43 Hmm, I think you want |NOTIMPLEMENTED()| before th
tdresser 2014/05/05 15:42:35 Pffft, done. Thanks.
108 }
109
110 float MotionEventUI::GetHistoricalTouchMajor(size_t pointer_index,
111 size_t historical_index) const {
112 return 0;
113 NOTIMPLEMENTED();
114 }
115
116 float MotionEventUI::GetHistoricalX(size_t pointer_index,
117 size_t historical_index) const {
118 return 0;
119 NOTIMPLEMENTED();
120 }
121
122 float MotionEventUI::GetHistoricalY(size_t pointer_index,
123 size_t historical_index) const {
124 return 0;
125 NOTIMPLEMENTED();
126 }
127
128 scoped_ptr<MotionEvent> MotionEventUI::Clone() const {
129 MotionEventUI* event = new MotionEventUI(pointer_count_,
130 last_touch_time_,
jdduke (slow) 2014/05/02 17:56:43 Might as well return scoped_ptr<MotionEvent>(new .
tdresser 2014/05/05 15:42:35 Done.
131 cached_action_,
132 cached_action_index_,
133 active_touches_,
134 id_to_index_);
135 return scoped_ptr<MotionEvent>(event);
136 }
137 scoped_ptr<MotionEvent> MotionEventUI::Cancel() const {
138 scoped_ptr<MotionEvent> clone = Clone();
139 static_cast<MotionEventUI*>(clone.get())->cached_action_ = ACTION_CANCEL;
jdduke (slow) 2014/05/02 17:56:43 I'm not sure re-using clone buys us much here. Le
tdresser 2014/05/05 15:42:35 Done.
140 return clone.Pass();
141 }
142
143 void MotionEventUI::CleanupRemovedTouchPoints(const TouchEvent& touch) {
144 if (touch.type() != ET_TOUCH_RELEASED && touch.type() != ET_TOUCH_CANCELLED)
145 return;
146
147 DCHECK(id_to_index_.count(touch.touch_id()));
148 int index_to_delete = id_to_index_[touch.touch_id()];
149
150 pointer_count_--;
151 active_touches_[index_to_delete] = active_touches_[pointer_count_];
152 id_to_index_[active_touches_[pointer_count_].touch_id] = index_to_delete;
153 }
154
155 int MotionEventUI::GetSourceDeviceId(size_t pointer_index) const {
156 DCHECK(pointer_index < pointer_count_);
157 return active_touches_[pointer_index].source_device_id;
158 }
159
160 void MotionEventUI::UpdatePointData(const TouchEvent& touch,
161 PointData* point_data) {
162 point_data->x = touch.x();
163 point_data->y = touch.y();
164 point_data->touch_id = touch.touch_id();
165 point_data->pressure = touch.force();
166 point_data->source_device_id = touch.source_device_id();
167
168 // TODO(tdresser): at some point we should start using both radii if they are
169 // available, but for now we use the max.
170 point_data->major_radius = std::max(touch.radius_x(), touch.radius_y());
171 if (!point_data->major_radius)
172 point_data->major_radius = GestureConfiguration::default_radius();
173 }
174
175 void MotionEventUI::AddTouch(const TouchEvent& touch) {
jdduke (slow) 2014/05/02 17:56:43 Would it ever make sense for |AddTouch()| to be th
tdresser 2014/05/05 15:42:35 In GestureProviderAura::OnTouchEvent, where we ign
176 if (pointer_count_ == static_cast<size_t>(GestureSequence::kMaxGesturePoints))
177 return;
178
179 PointData* point_data = &active_touches_[pointer_count_];
jdduke (slow) 2014/05/02 17:56:43 What if we had a free function |GetPointDataFromTo
tdresser 2014/05/05 15:42:35 Can't quite be free, since PointData is (and I bel
180 UpdatePointData(touch, point_data);
181 id_to_index_[touch.touch_id()] = static_cast<int>(pointer_count_);
182 pointer_count_++;
183 }
184
185
186 void MotionEventUI::UpdateTouch(const TouchEvent& touch) {
187 PointData* point_data = &active_touches_[id_to_index_[touch.touch_id()]];
jdduke (slow) 2014/05/02 17:56:43 What if touch_id() is not contained in id_to_index
tdresser 2014/05/05 15:42:35 Done.
188 UpdatePointData(touch, point_data);
189 }
190
191 void MotionEventUI::SetCachedAction(const TouchEvent& touch) {
192 switch (touch.type()) {
jdduke (slow) 2014/05/02 17:56:43 Might be worth another DCHECK(pointer_count_) here
tdresser 2014/05/05 15:42:35 Done.
193 case ET_TOUCH_PRESSED:
194 if (pointer_count_ == 1) {
195 cached_action_ = ACTION_DOWN;
196 } else {
197 cached_action_ = ACTION_POINTER_DOWN;
198 cached_action_index_ = static_cast<int>(id_to_index_[touch.touch_id()]);
199 }
200 break;
201 case ET_TOUCH_RELEASED:
202 if (pointer_count_ == 1) {
203 cached_action_ = ACTION_UP;
204 } else {
205 cached_action_ = ACTION_POINTER_UP;
206 DCHECK(id_to_index_.count(touch.touch_id()));
207 cached_action_index_ = static_cast<int>(id_to_index_[touch.touch_id()]);
208 DCHECK(cached_action_index_ < static_cast<int>(pointer_count_));
209 }
210 break;
211 case ET_TOUCH_CANCELLED:
212 cached_action_ = ACTION_CANCEL;
213 break;
214 case ET_TOUCH_MOVED:
215 cached_action_ = ACTION_MOVE;
216 break;
217 default:
218 NOTREACHED();
219 break;
220 }
221 }
222
223 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698