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.dom;
16  
17  import org.htmlunit.corejs.javascript.Context;
18  import org.htmlunit.corejs.javascript.Function;
19  import org.htmlunit.corejs.javascript.FunctionObject;
20  import org.htmlunit.corejs.javascript.Scriptable;
21  import org.htmlunit.javascript.HtmlUnitScriptable;
22  import org.htmlunit.javascript.JavaScriptEngine;
23  import org.htmlunit.javascript.configuration.JsxClass;
24  import org.htmlunit.javascript.configuration.JsxConstructor;
25  import org.htmlunit.javascript.configuration.JsxFunction;
26  import org.htmlunit.javascript.configuration.JsxGetter;
27  import org.htmlunit.javascript.host.Window;
28  
29  /**
30   * A JavaScript object for {@code DOMPointReadOnly}.
31   *
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   */
35  @JsxClass
36  public class DOMPointReadOnly extends HtmlUnitScriptable {
37  
38      private double xVal_;
39      private double yVal_;
40      private double zVal_;
41      private double wVal_;
42  
43      /**
44       * Creates an instance.
45       */
46      public DOMPointReadOnly() {
47          wVal_ = 1;
48      }
49  
50      /**
51       * Creates an instance with the given coordinates.
52       *
53       * @param x the x coordinate
54       * @param y the y coordinate
55       * @param z the z coordinate
56       * @param w the w perspective value
57       */
58      public DOMPointReadOnly(final double x, final double y, final double z, final double w) {
59          xVal_ = x;
60          yVal_ = y;
61          zVal_ = z;
62          wVal_ = w;
63      }
64  
65      /**
66       * JavaScript constructor.
67       * @param cx the current context
68       * @param scope the scope
69       * @param args the arguments to the constructor
70       * @param ctorObj the function object
71       * @param inNewExpr Is new or not
72       * @return the java object to allow JavaScript to access
73       */
74      @JsxConstructor
75      public static DOMPointReadOnly jsConstructor(final Context cx, final Scriptable scope,
76              final Object[] args, final Function ctorObj, final boolean inNewExpr) {
77  
78          final DOMPointReadOnly point = new DOMPointReadOnly(0, 0, 0, 1);
79          point.init(args, ctorObj);
80          return point;
81      }
82  
83      protected void init(final Object[] args, final Function ctorObj) {
84          final Window window = getWindow(ctorObj);
85          setParentScope(window);
86          setPrototype(((FunctionObject) ctorObj).getClassPrototype());
87  
88          if (args.length == 0 || JavaScriptEngine.isUndefined(args[0])) {
89              return;
90          }
91  
92          if (args.length > 0) {
93              xVal_ = JavaScriptEngine.toNumber(args[0]);
94          }
95  
96          if (args.length > 1) {
97              yVal_ = JavaScriptEngine.toNumber(args[1]);
98          }
99  
100         if (args.length > 2) {
101             zVal_ = JavaScriptEngine.toNumber(args[2]);
102         }
103 
104         if (args.length > 3) {
105             wVal_ = JavaScriptEngine.toNumber(args[3]);
106         }
107     }
108 
109     /**
110      * @return x coordinate
111      */
112     @JsxGetter
113     public double getX() {
114         return xVal_;
115     }
116 
117     /**
118      * @param x the new value
119      */
120     public void setX(final double x) {
121         xVal_ = x;
122     }
123 
124     /**
125      * @return y coordinate
126      */
127     @JsxGetter
128     public double getY() {
129         return yVal_;
130     }
131 
132     /**
133      * @param y the new value
134      */
135     public void setY(final double y) {
136         yVal_ = y;
137     }
138 
139     /**
140      * @return z coordinate
141      */
142     @JsxGetter
143     public double getZ() {
144         return zVal_;
145     }
146 
147     /**
148      * @param z the new value
149      */
150     public void setZ(final double z) {
151         zVal_ = z;
152     }
153 
154     /**
155      * @return w perspective value
156      */
157     @JsxGetter
158     public double getW() {
159         return wVal_;
160     }
161 
162     /**
163      * @param w the new value
164      */
165     public void setW(final double w) {
166         wVal_ = w;
167     }
168 
169     /**
170      * @return a JSON representation of the DOMPointReadOnly object.
171      */
172     @JsxFunction
173     public Scriptable toJSON() {
174         final Scriptable json = JavaScriptEngine.newObject(getParentScope());
175         json.put("x", json, xVal_);
176         json.put("y", json, yVal_);
177         json.put("z", json, zVal_);
178         json.put("w", json, wVal_);
179         return json;
180     }
181 }