1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/aura/gestures/gesture_sequence.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "ui/aura/event.h"
#include "ui/base/events.h"
// TODO(sad): Pinch gestures currently always assume that the first two
// touch-points (i.e. at indices 0 and 1) are involved. This may not
// always be the case. This needs to be fixed eventually.
// http://crbug.com/113144
namespace {
// TODO(girard): Make these configurable in sync with this CL
// http://crbug.com/100773
const float kMinimumPinchUpdateDistance = 5; // in pixels
const float kMinimumDistanceForPinchScroll = 20;
} // namespace
namespace aura {
namespace {
// ui::EventType is mapped to TouchState so it can fit into 3 bits of
// Signature.
enum TouchState {
TS_RELEASED,
TS_PRESSED,
TS_MOVED,
TS_STATIONARY,
TS_CANCELLED,
TS_UNKNOWN,
};
// Get equivalent TouchState from EventType |type|.
TouchState TouchEventTypeToTouchState(ui::EventType type) {
switch (type) {
case ui::ET_TOUCH_RELEASED:
return TS_RELEASED;
case ui::ET_TOUCH_PRESSED:
return TS_PRESSED;
case ui::ET_TOUCH_MOVED:
return TS_MOVED;
case ui::ET_TOUCH_STATIONARY:
return TS_STATIONARY;
case ui::ET_TOUCH_CANCELLED:
return TS_CANCELLED;
default:
VLOG(1) << "Unknown Touch Event type";
}
return TS_UNKNOWN;
}
// Gesture signature types for different values of combination (GestureState,
// touch_id, ui::EventType, touch_handled), see Signature for more info.
//
// Note: New addition of types should be placed as per their Signature value.
#define G(gesture_state, id, touch_state, handled) 1 + ( \
(((touch_state) & 0x7) << 1) | \
((handled) ? (1 << 4) : 0) | \
(((id) & 0xfff) << 5) | \
((gesture_state) << 17))
enum EdgeStateSignatureType {
GST_NO_GESTURE_FIRST_PRESSED =
G(GS_NO_GESTURE, 0, TS_PRESSED, false),
GST_PENDING_SYNTHETIC_CLICK_FIRST_RELEASED =
G(GS_PENDING_SYNTHETIC_CLICK, 0, TS_RELEASED, false),
GST_PENDING_SYNTHETIC_CLICK_FIRST_MOVED =
G(GS_PENDING_SYNTHETIC_CLICK, 0, TS_MOVED, false),
GST_PENDING_SYNTHETIC_CLICK_FIRST_STATIONARY =
G(GS_PENDING_SYNTHETIC_CLICK, 0, TS_STATIONARY, false),
GST_PENDING_SYNTHETIC_CLICK_FIRST_CANCELLED =
G(GS_PENDING_SYNTHETIC_CLICK, 0, TS_CANCELLED, false),
GST_SCROLL_FIRST_RELEASED =
G(GS_SCROLL, 0, TS_RELEASED, false),
GST_SCROLL_FIRST_MOVED =
G(GS_SCROLL, 0, TS_MOVED, false),
GST_SCROLL_FIRST_CANCELLED =
G(GS_SCROLL, 0, TS_CANCELLED, false),
GST_SCROLL_SECOND_RELEASED =
G(GS_SCROLL, 1, TS_RELEASED, false),
GST_SCROLL_SECOND_MOVED =
G(GS_SCROLL, 1, TS_MOVED, false),
GST_SCROLL_SECOND_CANCELLED =
G(GS_SCROLL, 1, TS_CANCELLED, false),
GST_PENDING_SYNTHETIC_CLICK_SECOND_PRESSED =
G(GS_PENDING_SYNTHETIC_CLICK, 1, TS_PRESSED, false),
GST_SCROLL_FIRST_PRESSED =
G(GS_SCROLL, 0, TS_PRESSED, false),
GST_SCROLL_SECOND_PRESSED =
G(GS_SCROLL, 1, TS_PRESSED, false),
GST_PINCH_FIRST_MOVED =
G(GS_PINCH, 0, TS_MOVED, false),
GST_PINCH_SECOND_MOVED =
G(GS_PINCH, 1, TS_MOVED, false),
GST_PINCH_FIRST_RELEASED =
G(GS_PINCH, 0, TS_RELEASED, false),
GST_PINCH_SECOND_RELEASED =
G(GS_PINCH, 1, TS_RELEASED, false),
GST_PINCH_FIRST_CANCELLED =
G(GS_PINCH, 0, TS_CANCELLED, false),
GST_PINCH_SECOND_CANCELLED =
G(GS_PINCH, 1, TS_CANCELLED, false),
};
// Builds a signature. Signatures are assembled by joining together
// multiple bits.
// 1 LSB bit so that the computed signature is always greater than 0
// 3 bits for the |type|.
// 1 bit for |touch_handled|
// 12 bits for |touch_id|
// 15 bits for the |gesture_state|.
EdgeStateSignatureType Signature(GestureState gesture_state,
unsigned int touch_id,
ui::EventType type,
bool touch_handled) {
CHECK((touch_id & 0xfff) == touch_id);
TouchState touch_state = TouchEventTypeToTouchState(type);
return static_cast<EdgeStateSignatureType>
(G(gesture_state, touch_id, touch_state, touch_handled));
}
#undef G
} // namespace
////////////////////////////////////////////////////////////////////////////////
// GestureSequence Public:
GestureSequence::GestureSequence()
: state_(GS_NO_GESTURE),
flags_(0),
pinch_distance_start_(0.f),
pinch_distance_current_(0.f),
point_count_(0) {
}
GestureSequence::~GestureSequence() {
}
GestureSequence::Gestures* GestureSequence::ProcessTouchEventForGesture(
const TouchEvent& event,
ui::TouchStatus status) {
// TODO(sad): All the state transitions should happen in this function.
// http://crbug.com/113146
if (status != ui::TOUCH_STATUS_UNKNOWN)
return NULL; // The event was consumed by a touch sequence.
// Set a limit on the number of simultaneous touches in a gesture.
if (event.touch_id() >= kMaxGesturePoints)
return NULL;
if (event.type() == ui::ET_TOUCH_PRESSED) {
if (point_count_ == kMaxGesturePoints)
return NULL;
++point_count_;
}
scoped_ptr<Gestures> gestures(new Gestures());
GesturePoint& point = GesturePointForEvent(event);
point.UpdateValues(event);
flags_ = event.flags();
switch (Signature(state_, event.touch_id(), event.type(), false)) {
case GST_NO_GESTURE_FIRST_PRESSED:
TouchDown(event, point, gestures.get());
break;
case GST_PENDING_SYNTHETIC_CLICK_FIRST_RELEASED:
if (Click(event, point, gestures.get()))
point.UpdateForTap();
break;
case GST_PENDING_SYNTHETIC_CLICK_FIRST_MOVED:
case GST_PENDING_SYNTHETIC_CLICK_FIRST_STATIONARY:
if (InClickOrScroll(event, point, gestures.get()))
point.UpdateForScroll();
break;
case GST_PENDING_SYNTHETIC_CLICK_FIRST_CANCELLED:
NoGesture(event, point, gestures.get());
break;
case GST_SCROLL_FIRST_MOVED:
case GST_SCROLL_SECOND_MOVED:
if (InScroll(event, point, gestures.get()))
point.UpdateForScroll();
break;
case GST_SCROLL_FIRST_RELEASED:
case GST_SCROLL_FIRST_CANCELLED:
case GST_SCROLL_SECOND_RELEASED:
case GST_SCROLL_SECOND_CANCELLED:
ScrollEnd(event, point, gestures.get());
break;
case GST_PENDING_SYNTHETIC_CLICK_SECOND_PRESSED:
case GST_SCROLL_FIRST_PRESSED:
case GST_SCROLL_SECOND_PRESSED:
PinchStart(event, point, gestures.get());
break;
case GST_PINCH_FIRST_MOVED:
case GST_PINCH_SECOND_MOVED:
if (PinchUpdate(event, point, gestures.get())) {
points_[0].UpdateForScroll();
points_[1].UpdateForScroll();
}
break;
case GST_PINCH_FIRST_RELEASED:
case GST_PINCH_SECOND_RELEASED:
case GST_PINCH_FIRST_CANCELLED:
case GST_PINCH_SECOND_CANCELLED:
PinchEnd(event, point, gestures.get());
break;
}
if (event.type() == ui::ET_TOUCH_RELEASED)
--point_count_;
return gestures.release();
}
void GestureSequence::Reset() {
set_state(GS_NO_GESTURE);
for (int i = 0; i < point_count_; ++i)
points_[i].Reset();
}
////////////////////////////////////////////////////////////////////////////////
// GestureSequence Private:
GesturePoint& GestureSequence::GesturePointForEvent(
const TouchEvent& event) {
return points_[event.touch_id()];
}
void GestureSequence::AppendTapDownGestureEvent(const GesturePoint& point,
Gestures* gestures) {
gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent(
ui::ET_GESTURE_TAP_DOWN,
point.first_touch_position().x(),
point.first_touch_position().y(),
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
0.f, 0.f)));
}
void GestureSequence::AppendClickGestureEvent(const GesturePoint& point,
Gestures* gestures) {
gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent(
ui::ET_GESTURE_TAP,
point.first_touch_position().x(),
point.first_touch_position().y(),
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
0.f, 0.f)));
}
void GestureSequence::AppendDoubleClickGestureEvent(const GesturePoint& point,
Gestures* gestures) {
gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent(
ui::ET_GESTURE_DOUBLE_TAP,
point.first_touch_position().x(),
point.first_touch_position().y(),
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
0.f, 0.f)));
}
void GestureSequence::AppendScrollGestureBegin(const GesturePoint& point,
const gfx::Point& location,
Gestures* gestures) {
gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent(
ui::ET_GESTURE_SCROLL_BEGIN,
location.x(),
location.y(),
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
0.f, 0.f)));
}
void GestureSequence::AppendScrollGestureEnd(const GesturePoint& point,
const gfx::Point& location,
Gestures* gestures,
float x_velocity,
float y_velocity) {
gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent(
ui::ET_GESTURE_SCROLL_END,
location.x(),
location.y(),
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
x_velocity, y_velocity)));
}
void GestureSequence::AppendScrollGestureUpdate(const GesturePoint& point,
const gfx::Point& location,
Gestures* gestures) {
gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent(
ui::ET_GESTURE_SCROLL_UPDATE,
location.x(),
location.y(),
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
point.x_delta(), point.y_delta())));
}
void GestureSequence::AppendPinchGestureBegin(const GesturePoint& p1,
const GesturePoint& p2,
Gestures* gestures) {
gfx::Point center = p1.last_touch_position().Middle(p2.last_touch_position());
gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent(
ui::ET_GESTURE_PINCH_BEGIN,
center.x(),
center.y(),
flags_,
base::Time::FromDoubleT(p1.last_touch_time()),
0.f, 0.f)));
}
void GestureSequence::AppendPinchGestureEnd(const GesturePoint& p1,
const GesturePoint& p2,
float scale,
Gestures* gestures) {
gfx::Point center = p1.last_touch_position().Middle(p2.last_touch_position());
gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent(
ui::ET_GESTURE_PINCH_END,
center.x(),
center.y(),
flags_,
base::Time::FromDoubleT(p1.last_touch_time()),
scale, 0.f)));
}
void GestureSequence::AppendPinchGestureUpdate(const GesturePoint& p1,
const GesturePoint& p2,
float scale,
Gestures* gestures) {
// TODO(sad): Compute rotation and include it in delta_y.
// http://crbug.com/113145
gfx::Point center = p1.last_touch_position().Middle(p2.last_touch_position());
gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent(
ui::ET_GESTURE_PINCH_UPDATE,
center.x(),
center.y(),
flags_,
base::Time::FromDoubleT(p1.last_touch_time()),
scale, 0.f)));
}
bool GestureSequence::Click(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
if (point.IsInClickWindow(event)) {
AppendClickGestureEvent(point, gestures);
if (point.IsInDoubleClickWindow(event))
AppendDoubleClickGestureEvent(point, gestures);
set_state(GS_NO_GESTURE);
return true;
}
set_state(GS_NO_GESTURE);
return false;
}
bool GestureSequence::InClickOrScroll(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
if (point.IsInClickWindow(event)) {
set_state(GS_PENDING_SYNTHETIC_CLICK);
return false;
}
if (point.IsInScrollWindow(event)) {
AppendScrollGestureBegin(point, point.last_touch_position(), gestures);
AppendScrollGestureUpdate(point, point.last_touch_position(), gestures);
set_state(GS_SCROLL);
return true;
}
return false;
}
bool GestureSequence::InScroll(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
if (!point.DidScroll(event, 0))
return false;
AppendScrollGestureUpdate(point, point.last_touch_position(), gestures);
return true;
}
bool GestureSequence::NoGesture(const TouchEvent&,
const GesturePoint& point, Gestures*) {
Reset();
return false;
}
bool GestureSequence::TouchDown(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
AppendTapDownGestureEvent(point, gestures);
set_state(GS_PENDING_SYNTHETIC_CLICK);
return false;
}
bool GestureSequence::ScrollEnd(const TouchEvent& event,
GesturePoint& point, Gestures* gestures) {
if (point.IsInFlickWindow(event)) {
AppendScrollGestureEnd(point, point.last_touch_position(), gestures,
point.XVelocity(), point.YVelocity());
} else {
AppendScrollGestureEnd(point, point.last_touch_position(), gestures,
0.f, 0.f);
}
Reset();
return false;
}
bool GestureSequence::PinchStart(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
AppendTapDownGestureEvent(point, gestures);
pinch_distance_current_ = points_[0].Distance(points_[1]);
pinch_distance_start_ = pinch_distance_current_;
AppendPinchGestureBegin(points_[0], points_[1], gestures);
if (state_ == GS_PENDING_SYNTHETIC_CLICK) {
gfx::Point center = points_[0].last_touch_position().Middle(
points_[1].last_touch_position());
AppendScrollGestureBegin(point, center, gestures);
}
set_state(GS_PINCH);
return true;
}
bool GestureSequence::PinchUpdate(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
float distance = points_[0].Distance(points_[1]);
if (abs(distance - pinch_distance_current_) < kMinimumPinchUpdateDistance) {
// The fingers didn't move towards each other, or away from each other,
// enough to constitute a pinch. But perhaps they moved enough in the same
// direction to do a two-finger scroll.
if (!points_[0].DidScroll(event, kMinimumDistanceForPinchScroll) ||
!points_[1].DidScroll(event, kMinimumDistanceForPinchScroll))
return false;
gfx::Point center = points_[0].last_touch_position().Middle(
points_[1].last_touch_position());
AppendScrollGestureUpdate(point, center, gestures);
} else {
AppendPinchGestureUpdate(points_[0], points_[1],
distance / pinch_distance_current_, gestures);
pinch_distance_current_ = distance;
}
return true;
}
bool GestureSequence::PinchEnd(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
float distance = points_[0].Distance(points_[1]);
AppendPinchGestureEnd(points_[0], points_[1],
distance / pinch_distance_start_, gestures);
// Once pinch ends, it should still be possible to scroll with the remaining
// finger on the screen.
set_state(GS_SCROLL);
pinch_distance_start_ = 0;
pinch_distance_current_ = 0;
return false;
}
} // namespace aura
|