View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * MagnoliaSwipeHandler.
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 }