View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * JavaScript object representing a {@code PointerEvent}.
34   * @see <a href="http://www.w3.org/TR/pointerevents/">W3C Spec</a>
35   * @see <a href="http://msdn.microsoft.com/en-us/library/ie/hh772103.aspx">MSDN</a>
36   *
37   * @author Frank Danek
38   * @author Ahmed Ashour
39   * @author Ronald Brill
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       * Default constructor.
55       */
56      public PointerEvent() {
57          super();
58      }
59  
60      /**
61       * JavaScript constructor.
62       * @param cx the current context
63       * @param scope the scope
64       * @param args the arguments to the WebSocket constructor
65       * @param ctorObj the function object
66       * @param inNewExpr Is new or not
67       * @return the java object to allow JavaScript to access
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      * Creates a new event instance.
120      *
121      * @param domNode the DOM node that triggered the event
122      * @param type the event type
123      * @param shiftKey true if SHIFT is pressed
124      * @param ctrlKey true if CTRL is pressed
125      * @param altKey true if ALT is pressed
126      * @param detail the detail value
127      * @param button the button code, must be {@link #BUTTON_LEFT}, {@link #BUTTON_MIDDLE} or {@link #BUTTON_RIGHT}
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      * @return the pointerId
142      */
143     @JsxGetter
144     public long getPointerId() {
145         return pointerId_;
146     }
147 
148     /**
149      * @return the width
150      */
151     @JsxGetter
152     public long getWidth() {
153         return width_;
154     }
155 
156     /**
157      * @return the height
158      */
159     @JsxGetter
160     public long getHeight() {
161         return height_;
162     }
163 
164     /**
165      * @return the pressure
166      */
167     @JsxGetter
168     public double getPressure() {
169         return pressure_;
170     }
171 
172     /**
173      * @return the tiltX
174      */
175     @JsxGetter
176     public long getTiltX() {
177         return tiltX_;
178     }
179 
180     /**
181      * @return the tiltY
182      */
183     @JsxGetter
184     public long getTiltY() {
185         return tiltY_;
186     }
187 
188     /**
189      * @return the pointerType
190      */
191     @JsxGetter
192     public String getPointerType() {
193         return pointerType_;
194     }
195 
196     /**
197      * @return the isPrimary
198      */
199     @JsxGetter(propertyName = "isPrimary")
200     public boolean isPrimary_js() {
201         return isPrimary_;
202     }
203 
204     /**
205      * @return the pointerType
206      */
207     @JsxGetter({CHROME, EDGE, FF})
208     @SuppressWarnings("PMD.UseUnderscoresInNumericLiterals")
209     public double getAltitudeAngle() {
210         return 1.5707963267948966;
211     }
212 
213     /**
214      * @return the pointerType
215      */
216     @JsxGetter({CHROME, EDGE, FF})
217     public double getAzimuthAngle() {
218         return 0d;
219     }
220 }