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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
|
// 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/base/gestures/gesture_sequence.h"
#include <cmath>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/time.h"
#include "ui/base/events.h"
#include "ui/base/gestures/gesture_configuration.h"
#include "ui/gfx/rect.h"
namespace ui {
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:
DVLOG(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_PENDING_SYNTHETIC_CLICK_SECOND_PRESSED =
G(GS_PENDING_SYNTHETIC_CLICK, 1, TS_PRESSED, 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_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),
GST_PINCH_THIRD_PRESSED =
G(GS_PINCH, 2, TS_PRESSED, false),
GST_PINCH_THIRD_MOVED =
G(GS_PINCH, 2, TS_MOVED, false),
GST_PINCH_THIRD_RELEASED =
G(GS_PINCH, 2, TS_RELEASED, false),
GST_PINCH_THIRD_CANCELLED =
G(GS_PINCH, 2, TS_CANCELLED, false),
GST_PINCH_FOURTH_PRESSED =
G(GS_PINCH, 3, TS_PRESSED, false),
GST_PINCH_FOURTH_MOVED =
G(GS_PINCH, 3, TS_MOVED, false),
GST_PINCH_FOURTH_RELEASED =
G(GS_PINCH, 3, TS_RELEASED, false),
GST_PINCH_FOURTH_CANCELLED =
G(GS_PINCH, 3, TS_CANCELLED, false),
GST_PINCH_FIFTH_PRESSED =
G(GS_PINCH, 4, TS_PRESSED, false),
GST_PINCH_FIFTH_MOVED =
G(GS_PINCH, 4, TS_MOVED, false),
GST_PINCH_FIFTH_RELEASED =
G(GS_PINCH, 4, TS_RELEASED, false),
GST_PINCH_FIFTH_CANCELLED =
G(GS_PINCH, 4, 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
float BoundingBoxDiagonal(const gfx::Rect& rect) {
float width = rect.width() * rect.width();
float height = rect.height() * rect.height();
return sqrt(width + height);
}
unsigned int ComputeTouchBitmask(const GesturePoint* points) {
unsigned int touch_bitmask = 0;
for (int i = 0; i < GestureSequence::kMaxGesturePoints; ++i) {
if (points[i].in_use())
touch_bitmask |= 1 << points[i].touch_id();
}
return touch_bitmask;
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// GestureSequence Public:
GestureSequence::GestureSequence(GestureEventHelper* helper)
: state_(GS_NO_GESTURE),
flags_(0),
pinch_distance_start_(0.f),
pinch_distance_current_(0.f),
scroll_type_(ST_FREE),
long_press_timer_(CreateTimer()),
point_count_(0),
helper_(helper) {
}
GestureSequence::~GestureSequence() {
}
GestureSequence::Gestures* GestureSequence::ProcessTouchEventForGesture(
const TouchEvent& event,
ui::TouchStatus status) {
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.GetTouchId() >= kMaxGesturePoints)
return NULL;
if (event.GetEventType() == ui::ET_TOUCH_PRESSED) {
if (point_count_ == kMaxGesturePoints)
return NULL;
GesturePoint* new_point = &points_[event.GetTouchId()];
// We shouldn't be able to get two PRESSED events from the same
// finger without either a RELEASE or CANCEL in between.
DCHECK(!points_[event.GetTouchId()].in_use());
new_point->set_point_id(point_count_++);
new_point->set_touch_id(event.GetTouchId());
}
GestureState last_state = state_;
// NOTE: when modifying these state transitions, also update gestures.dot
scoped_ptr<Gestures> gestures(new Gestures());
GesturePoint& point = GesturePointForEvent(event);
point.UpdateValues(event);
RecreateBoundingBox();
flags_ = event.GetEventFlags();
const int point_id = points_[event.GetTouchId()].point_id();
if (point_id < 0)
return NULL;
switch (Signature(state_, point_id, event.GetEventType(), false)) {
case GST_NO_GESTURE_FIRST_PRESSED:
TouchDown(event, point, gestures.get());
set_state(GS_PENDING_SYNTHETIC_CLICK);
break;
case GST_PENDING_SYNTHETIC_CLICK_FIRST_RELEASED:
if (Click(event, point, gestures.get()))
point.UpdateForTap();
set_state(GS_NO_GESTURE);
break;
case GST_PENDING_SYNTHETIC_CLICK_FIRST_MOVED:
case GST_PENDING_SYNTHETIC_CLICK_FIRST_STATIONARY:
if (ScrollStart(event, point, gestures.get())) {
set_state(GS_SCROLL);
if (ScrollUpdate(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:
if (scroll_type_ == ST_VERTICAL ||
scroll_type_ == ST_HORIZONTAL)
BreakRailScroll(event, point, gestures.get());
if (ScrollUpdate(event, point, gestures.get()))
point.UpdateForScroll();
break;
case GST_SCROLL_FIRST_RELEASED:
case GST_SCROLL_FIRST_CANCELLED:
ScrollEnd(event, point, gestures.get());
set_state(GS_NO_GESTURE);
break;
case GST_SCROLL_SECOND_PRESSED:
case GST_PENDING_SYNTHETIC_CLICK_SECOND_PRESSED:
// Once pinch starts, we immediately break rail scroll.
scroll_type_ = ST_FREE;
PinchStart(event, point, gestures.get());
set_state(GS_PINCH);
break;
case GST_PINCH_FIRST_MOVED:
case GST_PINCH_SECOND_MOVED:
case GST_PINCH_THIRD_MOVED:
case GST_PINCH_FOURTH_MOVED:
case GST_PINCH_FIFTH_MOVED:
if (PinchUpdate(event, point, gestures.get())) {
for (int i = 0; i < point_count_; ++i)
GetPointByPointId(i)->UpdateForScroll();
}
break;
case GST_PINCH_FIRST_RELEASED:
case GST_PINCH_SECOND_RELEASED:
case GST_PINCH_THIRD_RELEASED:
case GST_PINCH_FOURTH_RELEASED:
case GST_PINCH_FIFTH_RELEASED:
case GST_PINCH_FIRST_CANCELLED:
case GST_PINCH_SECOND_CANCELLED:
case GST_PINCH_THIRD_CANCELLED:
case GST_PINCH_FOURTH_CANCELLED:
case GST_PINCH_FIFTH_CANCELLED:
if (point_count_ == 2) {
PinchEnd(event, point, gestures.get());
// Once pinch ends, it should still be possible to scroll with the
// remaining finger on the screen.
set_state(GS_SCROLL);
} else {
// Was it a swipe? i.e. were all the fingers moving in the same
// direction?
MaybeSwipe(event, point, gestures.get());
// Nothing else to do if we have more than 2 fingers active, since after
// the release/cancel, there are still enough fingers to do pinch.
// pinch_distance_current_ and pinch_distance_start_ will be updated
// when the bounding-box is updated.
}
ResetVelocities();
break;
case GST_PINCH_THIRD_PRESSED:
case GST_PINCH_FOURTH_PRESSED:
case GST_PINCH_FIFTH_PRESSED:
AppendTapDownGestureEvent(point, gestures.get());
pinch_distance_current_ = BoundingBoxDiagonal(bounding_box_);
pinch_distance_start_ = pinch_distance_current_;
break;
}
if (event.GetEventType() == ui::ET_TOUCH_RELEASED ||
event.GetEventType() == ui::ET_TOUCH_CANCELLED)
AppendTapUpGestureEvent(point, gestures.get());
if (state_ != last_state)
DVLOG(4) << "Gesture Sequence"
<< " State: " << state_
<< " touch id: " << event.GetTouchId();
if (last_state == GS_PENDING_SYNTHETIC_CLICK && state_ != last_state)
long_press_timer_->Stop();
// The set of point_ids must be contiguous and include 0.
// When a touch point is released, all points with ids greater than the
// released point must have their ids decremented, or the set of point_ids
// could end up with gaps.
if (event.GetEventType() == ui::ET_TOUCH_RELEASED ||
event.GetEventType() == ui::ET_TOUCH_CANCELLED) {
GesturePoint& old_point = points_[event.GetTouchId()];
for (int i = 0; i < kMaxGesturePoints; ++i) {
GesturePoint& point = points_[i];
if (point.point_id() > old_point.point_id())
point.set_point_id(point.point_id() - 1);
}
if (old_point.in_use()) {
old_point.Reset();
--point_count_;
DCHECK_GE(point_count_, 0);
RecreateBoundingBox();
if (state_ == GS_PINCH) {
pinch_distance_current_ = BoundingBoxDiagonal(bounding_box_);
pinch_distance_start_ = pinch_distance_current_;
}
}
}
return gestures.release();
}
void GestureSequence::Reset() {
set_state(GS_NO_GESTURE);
for (int i = 0; i < kMaxGesturePoints; ++i)
points_[i].Reset();
point_count_ = 0;
}
void GestureSequence::RecreateBoundingBox() {
// TODO(sad): Recreating the bounding box at every touch-event is not very
// efficient. This should be made better.
int left = INT_MAX, top = INT_MAX, right = INT_MIN, bottom = INT_MIN;
for (int i = 0; i < kMaxGesturePoints; ++i) {
if (!points_[i].in_use())
continue;
if (left > points_[i].x())
left = points_[i].x();
if (right < points_[i].x())
right = points_[i].x();
if (top > points_[i].y())
top = points_[i].y();
if (bottom < points_[i].y())
bottom = points_[i].y();
}
bounding_box_last_center_ = bounding_box_.CenterPoint();
bounding_box_.SetRect(left, top, right - left, bottom - top);
}
void GestureSequence::ResetVelocities() {
for (int i = 0; i < kMaxGesturePoints; ++i) {
if (points_[i].in_use())
points_[i].ResetVelocity();
}
}
////////////////////////////////////////////////////////////////////////////////
// GestureSequence Protected:
base::OneShotTimer<GestureSequence>* GestureSequence::CreateTimer() {
return new base::OneShotTimer<GestureSequence>();
}
////////////////////////////////////////////////////////////////////////////////
// GestureSequence Private:
GesturePoint& GestureSequence::GesturePointForEvent(
const TouchEvent& event) {
return points_[event.GetTouchId()];
}
GesturePoint* GestureSequence::GetPointByPointId(int point_id) {
DCHECK(0 <= point_id && point_id < kMaxGesturePoints);
for (int i = 0; i < kMaxGesturePoints; ++i) {
GesturePoint& point = points_[i];
if (point.in_use() && point.point_id() == point_id)
return &point;
}
NOTREACHED();
return NULL;
}
void GestureSequence::AppendTapDownGestureEvent(const GesturePoint& point,
Gestures* gestures) {
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_GESTURE_TAP_DOWN,
point.first_touch_position(),
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
point_count_, 0.f, 1 << point.touch_id()));
}
void GestureSequence::AppendTapUpGestureEvent(const GesturePoint& point,
Gestures* gestures) {
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_GESTURE_TAP_UP,
point.first_touch_position(),
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
point_count_, 0.f, 1 << point.touch_id()));
}
void GestureSequence::AppendClickGestureEvent(const GesturePoint& point,
Gestures* gestures) {
gfx::Rect er = point.enclosing_rectangle();
gfx::Point center = er.CenterPoint();
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_GESTURE_TAP,
center,
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
er.width() / 2,
er.height() / 2,
1 << point.touch_id()));
}
void GestureSequence::AppendDoubleClickGestureEvent(const GesturePoint& point,
Gestures* gestures) {
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_GESTURE_DOUBLE_TAP,
point.first_touch_position(),
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
0.f, 0.f, 1 << point.touch_id()));
}
void GestureSequence::AppendScrollGestureBegin(const GesturePoint& point,
const gfx::Point& location,
Gestures* gestures) {
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_GESTURE_SCROLL_BEGIN,
location,
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
0.f, 0.f, 1 << point.touch_id()));
}
void GestureSequence::AppendScrollGestureEnd(const GesturePoint& point,
const gfx::Point& location,
Gestures* gestures,
float x_velocity,
float y_velocity) {
float railed_x_velocity = x_velocity;
float railed_y_velocity = y_velocity;
if (scroll_type_ == ST_HORIZONTAL)
railed_y_velocity = 0;
else if (scroll_type_ == ST_VERTICAL)
railed_x_velocity = 0;
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_GESTURE_SCROLL_END,
location,
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
railed_x_velocity, railed_y_velocity, 1 << point.touch_id()));
if (railed_x_velocity != 0 || railed_y_velocity != 0) {
// TODO(sad|rjkroege): fling-curve is currently configured to work well with
// touchpad scroll-events. This curve needs to be adjusted to work correctly
// with both touchpad and touchscreen. Until then, multiply the velocity for
// touchscreen with a constant.
// http://crbug.com/120154
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_SCROLL_FLING_START,
location,
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
railed_x_velocity * 2.5, railed_y_velocity * 2.5,
1 << point.touch_id()));
}
}
void GestureSequence::AppendScrollGestureUpdate(const GesturePoint& point,
const gfx::Point& location,
Gestures* gestures) {
int dx = location.x() - bounding_box_last_center_.x();
int dy = location.y() - bounding_box_last_center_.y();
if (dx == 0 && dy == 0)
return;
if (scroll_type_ == ST_HORIZONTAL)
dy = 0;
else if (scroll_type_ == ST_VERTICAL)
dx = 0;
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_GESTURE_SCROLL_UPDATE,
location,
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
dx, dy, ComputeTouchBitmask(points_)));
}
void GestureSequence::AppendPinchGestureBegin(const GesturePoint& p1,
const GesturePoint& p2,
Gestures* gestures) {
gfx::Point center = bounding_box_.CenterPoint();
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_GESTURE_PINCH_BEGIN,
center,
flags_,
base::Time::FromDoubleT(p1.last_touch_time()),
0.f, 0.f, 1 << p1.touch_id() | 1 << p2.touch_id()));
}
void GestureSequence::AppendPinchGestureEnd(const GesturePoint& p1,
const GesturePoint& p2,
float scale,
Gestures* gestures) {
gfx::Point center = bounding_box_.CenterPoint();
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_GESTURE_PINCH_END,
center,
flags_,
base::Time::FromDoubleT(p1.last_touch_time()),
0.f, 0.f,
1 << p1.touch_id() | 1 << p2.touch_id()));
}
void GestureSequence::AppendPinchGestureUpdate(const GesturePoint& point,
float scale,
Gestures* gestures) {
// TODO(sad): Compute rotation and include it in delta_y.
// http://crbug.com/113145
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_GESTURE_PINCH_UPDATE,
bounding_box_.CenterPoint(),
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
scale, 0.f,
ComputeTouchBitmask(points_)));
}
void GestureSequence::AppendSwipeGesture(const GesturePoint& point,
int swipe_x,
int swipe_y,
Gestures* gestures) {
gestures->push_back(helper_->CreateGestureEvent(
ui::ET_GESTURE_MULTIFINGER_SWIPE,
bounding_box_.CenterPoint(),
flags_,
base::Time::FromDoubleT(point.last_touch_time()),
swipe_x, swipe_y, ComputeTouchBitmask(points_)));
}
bool GestureSequence::Click(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
DCHECK(state_ == GS_PENDING_SYNTHETIC_CLICK);
if (point.IsInClickWindow(event)) {
AppendClickGestureEvent(point, gestures);
if (point.IsInDoubleClickWindow(event))
AppendDoubleClickGestureEvent(point, gestures);
return true;
}
return false;
}
bool GestureSequence::ScrollStart(const TouchEvent& event,
GesturePoint& point, Gestures* gestures) {
DCHECK(state_ == GS_PENDING_SYNTHETIC_CLICK);
if (point.IsInClickWindow(event) ||
!point.IsInScrollWindow(event) ||
!point.HasEnoughDataToEstablishRail())
return false;
AppendScrollGestureBegin(point, point.first_touch_position(), gestures);
if (point.IsInHorizontalRailWindow())
scroll_type_ = ST_HORIZONTAL;
else if (point.IsInVerticalRailWindow())
scroll_type_ = ST_VERTICAL;
else
scroll_type_ = ST_FREE;
return true;
}
void GestureSequence::BreakRailScroll(const TouchEvent& event,
GesturePoint& point, Gestures* gestures) {
DCHECK(state_ == GS_SCROLL);
if (scroll_type_ == ST_HORIZONTAL &&
point.BreaksHorizontalRail())
scroll_type_ = ST_FREE;
else if (scroll_type_ == ST_VERTICAL &&
point.BreaksVerticalRail())
scroll_type_ = ST_FREE;
}
bool GestureSequence::ScrollUpdate(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
DCHECK(state_ == GS_SCROLL);
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) {
DCHECK(state_ == GS_NO_GESTURE);
AppendTapDownGestureEvent(point, gestures);
long_press_timer_->Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(
GestureConfiguration::long_press_time_in_seconds() * 1000),
this,
&GestureSequence::AppendLongPressGestureEvent);
return true;
}
void GestureSequence::AppendLongPressGestureEvent() {
const GesturePoint* point = GetPointByPointId(0);
scoped_ptr<GestureEvent> gesture(helper_->CreateGestureEvent(
ui::ET_GESTURE_LONG_PRESS,
point->first_touch_position(),
flags_,
base::Time::FromDoubleT(point->last_touch_time()),
point->point_id(), 0.f, 1 << point->touch_id()));
helper_->DispatchLongPressGestureEvent(gesture.get());
}
bool GestureSequence::ScrollEnd(const TouchEvent& event,
GesturePoint& point, Gestures* gestures) {
DCHECK(state_ == GS_SCROLL);
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);
}
return true;
}
bool GestureSequence::PinchStart(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
DCHECK(state_ == GS_SCROLL ||
state_ == GS_PENDING_SYNTHETIC_CLICK);
AppendTapDownGestureEvent(point, gestures);
const GesturePoint* point1 = GetPointByPointId(0);
const GesturePoint* point2 = GetPointByPointId(1);
pinch_distance_current_ = BoundingBoxDiagonal(bounding_box_);
pinch_distance_start_ = pinch_distance_current_;
AppendPinchGestureBegin(*point1, *point2, gestures);
if (state_ == GS_PENDING_SYNTHETIC_CLICK) {
gfx::Point center = bounding_box_.CenterPoint();
AppendScrollGestureBegin(point, center, gestures);
}
return true;
}
bool GestureSequence::PinchUpdate(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
DCHECK(state_ == GS_PINCH);
float distance = BoundingBoxDiagonal(bounding_box_);
if (abs(distance - pinch_distance_current_) >=
GestureConfiguration::min_pinch_update_distance_in_pixels()) {
AppendPinchGestureUpdate(point,
distance / pinch_distance_current_, gestures);
pinch_distance_current_ = distance;
} else {
gfx::Point center = bounding_box_.CenterPoint();
AppendScrollGestureUpdate(point, center, gestures);
}
return true;
}
bool GestureSequence::PinchEnd(const TouchEvent& event,
const GesturePoint& point, Gestures* gestures) {
DCHECK(state_ == GS_PINCH);
GesturePoint* point1 = GetPointByPointId(0);
GesturePoint* point2 = GetPointByPointId(1);
float distance = BoundingBoxDiagonal(bounding_box_);
AppendPinchGestureEnd(*point1, *point2,
distance / pinch_distance_start_, gestures);
pinch_distance_start_ = 0;
pinch_distance_current_ = 0;
return true;
}
bool GestureSequence::MaybeSwipe(const TouchEvent& event,
const GesturePoint& point,
Gestures* gestures) {
DCHECK(state_ == GS_PINCH);
float velocity_x = 0.f, velocity_y = 0.f;
bool swipe_x = true, swipe_y = true;
int sign_x = 0, sign_y = 0;
int i = 0;
for (i = 0; i < kMaxGesturePoints; ++i) {
if (points_[i].in_use())
break;
}
DCHECK(i < kMaxGesturePoints);
velocity_x = points_[i].XVelocity();
velocity_y = points_[i].YVelocity();
sign_x = velocity_x < 0 ? -1 : 1;
sign_y = velocity_y < 0 ? -1 : 1;
for (++i; i < kMaxGesturePoints; ++i) {
if (!points_[i].in_use())
continue;
if (sign_x * points_[i].XVelocity() < 0)
swipe_x = false;
if (sign_y * points_[i].YVelocity() < 0)
swipe_y = false;
velocity_x += points_[i].XVelocity();
velocity_y += points_[i].YVelocity();
}
float min_velocity = GestureConfiguration::min_swipe_speed();
min_velocity *= min_velocity;
velocity_x = fabs(velocity_x / point_count_);
velocity_y = fabs(velocity_y / point_count_);
if (velocity_x < min_velocity)
swipe_x = false;
if (velocity_y < min_velocity)
swipe_y = false;
if (!swipe_x && !swipe_y)
return false;
if (!swipe_x)
velocity_x = 0.001f;
if (!swipe_y)
velocity_y = 0.001f;
float ratio = velocity_x > velocity_y ? velocity_x / velocity_y :
velocity_y / velocity_x;
if (ratio < GestureConfiguration::max_swipe_deviation_ratio())
return false;
if (velocity_x > velocity_y)
sign_y = 0;
else
sign_x = 0;
AppendSwipeGesture(point, sign_x, sign_y, gestures);
return true;
}
} // namespace ui
|