1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.googlecode.mgwt.dom.client.recognizer;
17
18 import com.google.gwt.core.client.GWT;
19 import com.google.gwt.event.shared.HasHandlers;
20 import com.google.gwt.user.client.Element;
21 import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
22 import com.googlecode.mgwt.dom.client.event.touch.Touch;
23 import com.googlecode.mgwt.dom.client.event.touch.TouchCancelEvent;
24 import com.googlecode.mgwt.dom.client.event.touch.TouchEndEvent;
25 import com.googlecode.mgwt.dom.client.event.touch.TouchHandler;
26 import com.googlecode.mgwt.dom.client.event.touch.TouchMoveEvent;
27 import com.googlecode.mgwt.dom.client.event.touch.TouchStartEvent;
28
29
30
31
32
33
34
35
36
37 public class TapRecognizer implements TouchHandler {
38
39 public static final int DEFAULT_DISTANCE = 15;
40
41 private final int distance;
42
43 private boolean touchCanceled;
44
45 private boolean hasMoved;
46
47 private int start_x;
48
49 private int start_y;
50
51 private Element targetElement;
52
53 private final HasHandlers source;
54
55 private EventPropagator eventPropagator;
56
57 private static EventPropagator DEFAULT_EVENT_PROPAGATOR;
58
59 public TapRecognizer(HasHandlers source) {
60 this(source, DEFAULT_DISTANCE);
61 }
62
63 public TapRecognizer(HasHandlers source, int distance) {
64 if (source == null)
65 throw new IllegalArgumentException("source can not be null");
66 if (distance < 0)
67 throw new IllegalArgumentException("distance has to be greater than zero");
68 this.source = source;
69 this.distance = distance;
70
71 }
72
73 @Override
74 public void onTouchStart(TouchStartEvent event) {
75 touchCanceled = false;
76 hasMoved = false;
77 if(event.getNativeEvent() != null){
78 targetElement = event.getNativeEvent().getEventTarget().<Element>cast();
79 }else {
80 targetElement = null;
81 }
82
83 start_x = event.getTouches().get(0).getPageX();
84 start_y = event.getTouches().get(0).getPageY();
85 }
86
87 @Override
88 public void onTouchMove(TouchMoveEvent event) {
89 Touch touch = event.getTouches().get(0);
90 if (Math.abs(touch.getPageX() - start_x) > distance || Math.abs(touch.getPageY() - start_y) > distance) {
91 hasMoved = true;
92 }
93
94 }
95
96 @Override
97 public void onTouchEnd(TouchEndEvent event) {
98 if (!hasMoved && !touchCanceled) {
99 TapEvent tapEvent = new TapEvent(source, targetElement, start_x, start_y);
100 getEventPropagator().fireEvent(source, tapEvent);
101 }
102
103 }
104
105 @Override
106 public void onTouchCanceled(TouchCancelEvent event) {
107 touchCanceled = true;
108
109 }
110
111 public int getDistance() {
112 return distance;
113 }
114
115 protected EventPropagator getEventPropagator() {
116 if (eventPropagator == null) {
117 if (DEFAULT_EVENT_PROPAGATOR == null) {
118 DEFAULT_EVENT_PROPAGATOR = GWT.create(EventPropagator.class);
119 }
120 eventPropagator = DEFAULT_EVENT_PROPAGATOR;
121 }
122 return eventPropagator;
123 }
124
125 public Element getTargetElement() {
126 return targetElement;
127 }
128
129 }