View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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.corejs.javascript.VarScope;
22  import org.htmlunit.javascript.HtmlUnitScriptable;
23  import org.htmlunit.javascript.JavaScriptEngine;
24  import org.htmlunit.javascript.configuration.JsxClass;
25  import org.htmlunit.javascript.configuration.JsxConstructor;
26  import org.htmlunit.javascript.configuration.JsxFunction;
27  import org.htmlunit.javascript.configuration.JsxGetter;
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 VarScope 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, scope, ctorObj);
80          return point;
81      }
82  
83      protected void init(final Object[] args, final VarScope scope, final Function ctorObj) {
84          setParentScope(scope);
85          setPrototype(((FunctionObject) ctorObj).getClassPrototype());
86  
87          if (args.length == 0 || JavaScriptEngine.isUndefined(args[0])) {
88              return;
89          }
90  
91          if (args.length > 0) {
92              xVal_ = JavaScriptEngine.toNumber(args[0]);
93          }
94  
95          if (args.length > 1) {
96              yVal_ = JavaScriptEngine.toNumber(args[1]);
97          }
98  
99          if (args.length > 2) {
100             zVal_ = JavaScriptEngine.toNumber(args[2]);
101         }
102 
103         if (args.length > 3) {
104             wVal_ = JavaScriptEngine.toNumber(args[3]);
105         }
106     }
107 
108     /**
109      * @return x coordinate
110      */
111     @JsxGetter
112     public double getX() {
113         return xVal_;
114     }
115 
116     /**
117      * @param x the new value
118      */
119     public void setX(final double x) {
120         xVal_ = x;
121     }
122 
123     /**
124      * @return y coordinate
125      */
126     @JsxGetter
127     public double getY() {
128         return yVal_;
129     }
130 
131     /**
132      * @param y the new value
133      */
134     public void setY(final double y) {
135         yVal_ = y;
136     }
137 
138     /**
139      * @return z coordinate
140      */
141     @JsxGetter
142     public double getZ() {
143         return zVal_;
144     }
145 
146     /**
147      * @param z the new value
148      */
149     public void setZ(final double z) {
150         zVal_ = z;
151     }
152 
153     /**
154      * @return w perspective value
155      */
156     @JsxGetter
157     public double getW() {
158         return wVal_;
159     }
160 
161     /**
162      * @param w the new value
163      */
164     public void setW(final double w) {
165         wVal_ = w;
166     }
167 
168     /**
169      * @return a JSON representation of the DOMPointReadOnly object.
170      */
171     @JsxFunction
172     public Scriptable toJSON() {
173         final Scriptable json = JavaScriptEngine.newObject(getParentScope());
174         json.put("x", json, xVal_);
175         json.put("y", json, yVal_);
176         json.put("z", json, zVal_);
177         json.put("w", json, wVal_);
178         return json;
179     }
180 }