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.MouseDownEvent;
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.TouchStartEvent;
24  
25  /**
26   * A simulated TouchMoveEvent is really a mouse down 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 SimulatedTouchStartEvent extends TouchStartEvent {
33    private int x;
34    private int y;
35    private final MouseDownEvent event;
36    private boolean multiTouch;
37  
38    /**
39     * <p>
40     * Constructor for SimulatedTouchStartEvent.
41     * </p>
42     * 
43     * @param event a {@link com.google.gwt.event.dom.client.MouseDownEvent} object.
44     * @param multiTouch
45     */
46    public SimulatedTouchStartEvent(MouseDownEvent event, boolean multiTouch) {
47      this.event = event;
48      x = event.getClientX();
49      y = event.getClientY();
50      this.multiTouch = multiTouch;
51      if (this.multiTouch) {
52        MultiTouchMouseEmulator.onTouchStart(x, y);
53      }
54      setNativeEvent(event.getNativeEvent());
55      setSource(event.getSource());
56    }
57  
58    @Override
59    public LightArray<Touch> getTouches() {
60      return new JsLightArray<Touch>(touches(getNativeEvent()));
61    }
62  
63    @Override
64    public LightArray<Touch> getChangedTouches() {
65      return new JsLightArray<Touch>(changedTouches(getNativeEvent()));
66    }
67  
68    /** {@inheritDoc} */
69    @Override
70    protected native JsArray<JsTouch> touches(NativeEvent nativeEvent) /*-{
71  		var touch = {};
72  
73  		touch.pageX = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchStartEvent::x;
74  		touch.pageY = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchStartEvent::y;
75  
76  		var toucharray = [];
77  
78  		if (this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchStartEvent::multiTouch) {
79  			var touch_start = {};
80  			touch_start.pageX = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchStartEvent::x;
81  			touch_start.pageY = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchStartEvent::y;
82  			toucharray.push(touch_start);
83  		}
84  
85  		toucharray.push(touch);
86  
87  		return toucharray;
88    }-*/;
89  
90    /** {@inheritDoc} */
91    @Override
92    protected native JsArray<JsTouch> changedTouches(NativeEvent nativeEvent) /*-{
93  		var touch = {};
94  
95  		touch.pageX = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchStartEvent::x;
96  		touch.pageY = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchStartEvent::y;
97  
98  		var toucharray = [];
99  
100 		if (this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchStartEvent::multiTouch) {
101 			var touch_start = {};
102 			touch_start.pageX = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchStartEvent::x;
103 			touch_start.pageY = this.@com.googlecode.mgwt.dom.client.event.mouse.SimulatedTouchStartEvent::y;
104 			toucharray.push(touch_start);
105 		}
106 
107 		toucharray.push(touch);
108 
109 		return toucharray;
110   }-*/;
111 
112   @Override
113   public void stopPropagation() {
114     event.stopPropagation();
115   }
116 
117 }