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 package info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport;
35
36 import com.google.gwt.core.client.GWT;
37 import com.google.gwt.event.shared.HasHandlers;
38 import com.googlecode.mgwt.dom.client.event.touch.Touch;
39 import com.googlecode.mgwt.dom.client.event.touch.TouchCancelEvent;
40 import com.googlecode.mgwt.dom.client.event.touch.TouchEndEvent;
41 import com.googlecode.mgwt.dom.client.event.touch.TouchHandler;
42 import com.googlecode.mgwt.dom.client.event.touch.TouchMoveEvent;
43 import com.googlecode.mgwt.dom.client.event.touch.TouchStartEvent;
44 import com.googlecode.mgwt.dom.client.recognizer.EventPropagator;
45 import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeEndEvent;
46 import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeEvent.DIRECTION;
47 import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeMoveEvent;
48 import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeStartEvent;
49
50
51
52
53 public class MagnoliaSwipeRecognizer implements TouchHandler {
54
55 private static final int DEFAULT_TOUCH_TRESHOLD = 3;
56
57 private static EventPropagator DEFAULT_EVENT_PROPAGATOR;
58
59 private final HasHandlers source;
60
61 private EventPropagator eventPropagator;
62
63 private final int minDistance;
64
65 private final int threshold;
66
67 private int touchCount;
68
69 private int desiredTouchCount;
70
71 private enum State {
72 INVALID, READY, FINDER_DOWN, FOUND_DIRECTION
73 }
74
75 private State state;
76
77 private DIRECTION direction;
78
79 private int lastDistance;
80
81 private int touchStartX = Integer.MAX_VALUE;
82
83 private int touchStartY = Integer.MIN_VALUE;
84
85 public MagnoliaSwipeRecognizer(HasHandlers source) {
86 this(source, 40);
87 }
88
89 public MagnoliaSwipeRecognizer(HasHandlers source, int minDistance) {
90 this(source, minDistance, 10);
91 }
92
93 public MagnoliaSwipeRecognizer(HasHandlers source, int minDistance, int threshold) {
94 if (source == null) {
95 throw new IllegalArgumentException("source can not be null");
96 }
97
98 if (minDistance <= 0 || minDistance < threshold) {
99 throw new IllegalArgumentException("minDistance > 0 and minDistance > threshold");
100 }
101
102 if (threshold <= 0) {
103 throw new IllegalArgumentException("threshold > 0");
104 }
105
106 this.desiredTouchCount = DEFAULT_TOUCH_TRESHOLD;
107 this.source = source;
108 this.minDistance = minDistance;
109 this.threshold = threshold;
110 this.touchCount = 0;
111 this.state = State.READY;
112 }
113
114 @Override
115 public void onTouchStart(TouchStartEvent event) {
116 touchCount = event.getTouches().length();
117 switch (state) {
118 case INVALID:
119 break;
120 case READY:
121 if (touchCount == desiredTouchCount) {
122 state = State.FINDER_DOWN;
123 touchStartX = event.getTouches().get(0).getPageX();
124 touchStartY = event.getTouches().get(0).getPageY();
125 }
126 break;
127 case FINDER_DOWN:
128 default:
129 break;
130 }
131 }
132
133 @Override
134 public void onTouchMove(TouchMoveEvent event) {
135 Touch touch = event.getTouches().get(0);
136
137 switch (state) {
138 case INVALID:
139 case READY:
140 break;
141 case FINDER_DOWN:
142 if (Math.abs(touch.getPageX() - touchStartX) >= threshold) {
143 state = State.FOUND_DIRECTION;
144 direction = touch.getPageX() - touchStartX > 0 ? DIRECTION.LEFT_TO_RIGHT : DIRECTION.RIGHT_TO_LEFT;
145 SwipeStartEventognizer/swipe/SwipeStartEvent.html#SwipeStartEvent">SwipeStartEvent swipeStartEvent = new SwipeStartEvent(touch, touch.getPageX() - touchStartX, direction);
146 getEventPropagator().fireEvent(source, swipeStartEvent);
147
148 } else {
149 if (Math.abs(touch.getPageY() - touchStartY) >= threshold) {
150 state = State.FOUND_DIRECTION;
151 direction = touch.getPageY() - touchStartY > 0 ? DIRECTION.TOP_TO_BOTTOM : DIRECTION.BOTTOM_TO_TOP;
152 SwipeStartEventognizer/swipe/SwipeStartEvent.html#SwipeStartEvent">SwipeStartEvent swipeStartEvent = new SwipeStartEvent(touch, touch.getPageY() - touchStartY, direction);
153 getEventPropagator().fireEvent(source, swipeStartEvent);
154
155 }
156 }
157 break;
158
159 case FOUND_DIRECTION:
160 DIRECTION currentDirection = null;
161 switch (direction) {
162 case TOP_TO_BOTTOM:
163 case BOTTOM_TO_TOP:
164 lastDistance = Math.abs(touch.getPageY() - touchStartY);
165 currentDirection = touch.getPageY() - touchStartY > 0 ? DIRECTION.TOP_TO_BOTTOM : DIRECTION.BOTTOM_TO_TOP;
166 break;
167 case LEFT_TO_RIGHT:
168 case RIGHT_TO_LEFT:
169 lastDistance = Math.abs(touch.getPageX() - touchStartX);
170 currentDirection = touch.getPageX() - touchStartX > 0 ? DIRECTION.LEFT_TO_RIGHT : DIRECTION.RIGHT_TO_LEFT;
171 break;
172 default:
173 break;
174 }
175 getEventPropagator().fireEvent(source, new SwipeMoveEvent(touch, lastDistance > minDistance, lastDistance, currentDirection));
176 break;
177 default:
178 break;
179 }
180
181 }
182
183 @Override
184 public void onTouchEnd(TouchEndEvent event) {
185 touchCount = event.getTouches().length();
186
187 switch (state) {
188 case FOUND_DIRECTION:
189 if (touchCount < desiredTouchCount) {
190 getEventPropagator().fireEvent(source, new SwipeEndEvent(lastDistance > minDistance, lastDistance, direction));
191 reset();
192 }
193 break;
194
195 default:
196 reset();
197 break;
198 }
199
200 }
201
202 @Override
203 public void onTouchCanceled(TouchCancelEvent event) {
204 touchCount--;
205 if (touchCount <= 0) {
206 reset();
207 }
208
209 }
210
211 public int getThreshold() {
212 return threshold;
213 }
214
215 public int getMinDistance() {
216 return minDistance;
217 }
218
219 protected EventPropagator getEventPropagator() {
220 if (eventPropagator == null) {
221 if (DEFAULT_EVENT_PROPAGATOR == null) {
222 DEFAULT_EVENT_PROPAGATOR = GWT.create(EventPropagator.class);
223 }
224 eventPropagator = DEFAULT_EVENT_PROPAGATOR;
225 }
226 return eventPropagator;
227 }
228
229 protected void setEventPropagator(EventPropagator eventPropagator) {
230 this.eventPropagator = eventPropagator;
231
232 }
233
234 private void reset() {
235 state = State.READY;
236 touchCount = 0;
237 }
238
239 }