View Javadoc
1   /*
2    * Copyright 2010 Daniel Kurka
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package com.googlecode.mgwt.dom.client.event.mouse;
15  
16  import com.google.gwt.core.client.JsArray;
17  import com.google.gwt.dom.client.NativeEvent;
18  import com.google.gwt.event.dom.client.MouseMoveEvent;
19  import com.googlecode.mgwt.collection.client.JsLightArray;
20  import com.googlecode.mgwt.collection.shared.LightArray;
21  import com.googlecode.mgwt.dom.client.event.touch.JsTouch;
22  import com.googlecode.mgwt.dom.client.event.touch.Touch;
23  import com.googlecode.mgwt.dom.client.event.touch.TouchMoveEvent;
24  
25  /**
26   * A simulated TouchMoveEvent is really a mouse move event. This is used mostly in dev mode and for
27   * blackberry devices to handle them equally to real touch devices
28   * 
29   * @author Daniel Kurka
30   * @version $Id: $
31   */
32  public class SimulatedTouchMoveEvent extends TouchMoveEvent {
33  
34    private int x;
35    private int y;
36  
37    private int x_start;
38    private int y_start;
39    private boolean multiTouch;
40  
41    /**
42     * <p>
43     * Constructor for SimulatedTouchMoveEvent.
44     * </p>
45     * 
46     * @param event a {@link com.google.gwt.event.dom.client.MouseMoveEvent} object.
47     */
48    public SimulatedTouchMoveEvent(MouseMoveEvent event) {
49      x = event.getClientX();
50      y = event.getClientY();
51      multiTouch = false;
52  
53      if (event.isAltKeyDown() && MultiTouchMouseEmulator.isHasValues()) {
54        multiTouch = true;
55        int[] start = MultiTouchMouseEmulator.getTouchStart();
56        x_start = start[0];
57        y_start = start[1];
58      }
59  
60      setNativeEvent(event.getNativeEvent());
61      setSource(event.getSource());
62    }
63  
64    @Override
65    public LightArray<Touch> getTouches() {
66      return new JsLightArray<Touch>(touches(getNativeEvent()));
67    }
68  
69    @Override
70    public LightArray<Touch> getChangedTouches() {
71      return new JsLightArray<Touch>(changedTouches(getNativeEvent()));
72    }
73  
74    /** {@inheritDoc} */
75    @Override
76    protected native JsArray<JsTouch> touches(NativeEvent nativeEvent) /*-{
77  		var touch = {};
78  
79  		touch.pageX = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchMoveEvent::x;
80  		touch.pageY = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchMoveEvent::y;
81  
82  		var toucharray = [];
83  
84  		if (this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchMoveEvent::multiTouch) {
85  			var touch_start = {};
86  			touch_start.pageX = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchMoveEvent::x_start;
87  			touch_start.pageY = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchMoveEvent::y_start;
88  			toucharray.push(touch_start);
89  		}
90  
91  		toucharray.push(touch);
92  
93  		return toucharray;
94    }-*/;
95  
96    /** {@inheritDoc} */
97    @Override
98    protected native JsArray<JsTouch> changedTouches(NativeEvent nativeEvent) /*-{
99  		var touch = {};
100 
101 		touch.pageX = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchMoveEvent::x;
102 		touch.pageY = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchMoveEvent::y;
103 
104 		var toucharray = [];
105 
106 		if (this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchMoveEvent::multiTouch) {
107 			var touch_start = {};
108 			touch_start.pageX = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchMoveEvent::x_start;
109 			touch_start.pageY = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchMoveEvent::y_start;
110 			toucharray.push(touch_start);
111 		}
112 
113 		toucharray.push(touch);
114 
115 		return toucharray;
116   }-*/;
117 }