1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.event;
16
17 import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
18 import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
19 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
20
21 import org.htmlunit.corejs.javascript.Context;
22 import org.htmlunit.corejs.javascript.Function;
23 import org.htmlunit.corejs.javascript.NativeObject;
24 import org.htmlunit.corejs.javascript.Scriptable;
25 import org.htmlunit.corejs.javascript.ScriptableObject;
26 import org.htmlunit.html.DomNode;
27 import org.htmlunit.javascript.JavaScriptEngine;
28 import org.htmlunit.javascript.configuration.JsxClass;
29 import org.htmlunit.javascript.configuration.JsxConstructor;
30 import org.htmlunit.javascript.configuration.JsxGetter;
31
32
33
34
35
36
37
38
39
40
41 @JsxClass
42 public class PointerEvent extends MouseEvent {
43
44 private int pointerId_;
45 private int width_;
46 private int height_;
47 private double pressure_;
48 private int tiltX_;
49 private int tiltY_;
50 private String pointerType_ = "";
51 private boolean isPrimary_;
52
53
54
55
56 public PointerEvent() {
57 super();
58 }
59
60
61
62
63
64
65
66
67
68
69 @JsxConstructor
70 public static Scriptable jsConstructor(final Context cx, final Scriptable scope,
71 final Object[] args, final Function ctorObj, final boolean inNewExpr) {
72 final PointerEvent event = new PointerEvent();
73 if (args.length != 0) {
74 event.setType(JavaScriptEngine.toString(args[0]));
75 event.setBubbles(false);
76 event.setCancelable(false);
77 event.width_ = 1;
78 event.height_ = 1;
79 }
80
81 if (args.length > 1) {
82 final NativeObject object = (NativeObject) args[1];
83 event.setBubbles((boolean) getValue(object, "bubbles", event.isBubbles()));
84 event.pointerId_ = (int) getValue(object, "pointerId", event.pointerId_);
85 event.width_ = (int) getValue(object, "width", event.width_);
86 event.height_ = (int) getValue(object, "height", event.height_);
87 event.pressure_ = (double) getValue(object, "pressure", event.pressure_);
88 event.tiltX_ = (int) getValue(object, "tiltX", event.tiltX_);
89 event.tiltY_ = (int) getValue(object, "tiltY", event.tiltY_);
90 event.pointerType_ = (String) getValue(object, "pointerType", event.pointerType_);
91 event.isPrimary_ = (boolean) getValue(object, "isPrimary", event.isPrimary_);
92 }
93 return event;
94 }
95
96 private static Object getValue(final ScriptableObject object, final String name, final Object defaulValue) {
97 Object value = object.get(name);
98 if (value == null) {
99 value = defaulValue;
100 }
101 else {
102 if (defaulValue instanceof String) {
103 value = String.valueOf(value);
104 }
105 else if (defaulValue instanceof Double) {
106 value = JavaScriptEngine.toNumber(value);
107 }
108 else if (defaulValue instanceof Number) {
109 value = (int) JavaScriptEngine.toNumber(value);
110 }
111 else {
112 value = JavaScriptEngine.toBoolean(value);
113 }
114 }
115 return value;
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129 public PointerEvent(final DomNode domNode, final String type, final boolean shiftKey,
130 final boolean ctrlKey, final boolean altKey, final int button, final int detail) {
131 super(domNode, type, shiftKey, ctrlKey, altKey, button, detail);
132
133 pointerId_ = 1;
134 width_ = 1;
135 height_ = 1;
136 pointerType_ = "mouse";
137 isPrimary_ = true;
138 }
139
140
141
142
143 @JsxGetter
144 public long getPointerId() {
145 return pointerId_;
146 }
147
148
149
150
151 @JsxGetter
152 public long getWidth() {
153 return width_;
154 }
155
156
157
158
159 @JsxGetter
160 public long getHeight() {
161 return height_;
162 }
163
164
165
166
167 @JsxGetter
168 public double getPressure() {
169 return pressure_;
170 }
171
172
173
174
175 @JsxGetter
176 public long getTiltX() {
177 return tiltX_;
178 }
179
180
181
182
183 @JsxGetter
184 public long getTiltY() {
185 return tiltY_;
186 }
187
188
189
190
191 @JsxGetter
192 public String getPointerType() {
193 return pointerType_;
194 }
195
196
197
198
199 @JsxGetter(propertyName = "isPrimary")
200 public boolean isPrimary_js() {
201 return isPrimary_;
202 }
203
204
205
206
207 @JsxGetter({CHROME, EDGE, FF})
208 @SuppressWarnings("PMD.UseUnderscoresInNumericLiterals")
209 public double getAltitudeAngle() {
210 return 1.5707963267948966;
211 }
212
213
214
215
216 @JsxGetter({CHROME, EDGE, FF})
217 public double getAzimuthAngle() {
218 return 0d;
219 }
220 }