-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTouchEvent.java
More file actions
113 lines (96 loc) · 2.7 KB
/
TouchEvent.java
File metadata and controls
113 lines (96 loc) · 2.7 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package snap.webapi;
/**
* This class is a wrapper for Web API TouchEvent (https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent).
*/
public class TouchEvent extends UIEvent {
/**
* Constructor.
*/
public TouchEvent(Object jsObj)
{
super(jsObj);
}
public int getClientX()
{
Touch touch = getTouch();
return touch != null ? touch.getClientX() : 0;
}
public int getClientY()
{
Touch touch = getTouch();
return touch != null ? touch.getClientY() : 0;
}
public int getScreenX()
{
Touch touch = getTouch();
return touch != null ? touch.getScreenX() : 0;
}
public int getScreenY()
{
Touch touch = getTouch();
return touch != null ? touch.getScreenY() : 0;
}
public int getPageX()
{
Touch touch = getTouch();
return touch != null ? touch.getPageX() : 0;
}
public int getPageY()
{
Touch touch = getTouch();
return touch != null ? touch.getPageY() : 0;
}
/**
* Returns the first touch.
*/
public Touch getTouch()
{
// Get Touches
Touch[] touches = getTouches();
// If at end, see if there no are changed touches
String type = getType();
boolean isTouchEnd = type.equals("touchend");
if (isTouchEnd) {
Touch[] changedTouches = getChangedTouches();
if (changedTouches != null && changedTouches.length > 0)
touches = changedTouches;
}
// Get First touch
Touch touch = touches != null && touches.length > 0 ? touches[0] : null;
if (touch == null)
System.err.println("TouchEvent.getTouch: No touches?");
// Return touch
return touch;
}
/**
* Returns the array of touches.
*/
public Touch[] getTouches()
{
Object touchList = getMember("touches");
return getTouchArrayForTouchList(touchList);
}
/**
* Returns the changed touches.
*/
public Touch[] getChangedTouches()
{
Object touchList = getMember("changedTouches");
return getTouchArrayForTouchList(touchList);
}
/**
* Returns an array of touches for given JavaScript TouchList.
*/
private static Touch[] getTouchArrayForTouchList(Object touchList)
{
int length = WebEnv.get().getMemberInt(touchList, "length");
// Convert to Touches array
Touch[] touches = new Touch[length];
for (int i = 0; i < length; i++) {
Object touchJS = WebEnv.get().call(touchList, "item", i);
touches[i] = new Touch(touchJS);
}
// Return
return touches;
}
}