2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.ComponentModel;
19 using Tizen.NUI.Binding;
25 /// A two-dimensional vector.
27 /// <since_tizen> 3 </since_tizen>
28 [Binding.TypeConverter(typeof(Vector2TypeConverter))]
29 public class Vector2 : Disposable, ICloneable
33 /// The default constructor initializes the vector to 0.
35 /// <since_tizen> 3 </since_tizen>
36 public Vector2() : this(Interop.Vector2.NewVector2(), true)
38 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
44 /// <param name="x">The x or width component.</param>
45 /// <param name="y">The y or height component.</param>
46 /// <since_tizen> 3 </since_tizen>
47 public Vector2(float x, float y) : this(Interop.Vector2.NewVector2(x, y), true)
49 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
53 /// The conversion constructor from an array of two floats.
55 /// <param name="array">The array of xy.</param>
56 /// <since_tizen> 3 </since_tizen>
57 public Vector2(float[] array) : this(Interop.Vector2.NewVector2(array), true)
59 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
63 /// The copy constructor.
65 /// <param name="other">The copy target.</param>
66 /// <exception cref="ArgumentNullException"> Thrown when other is null. </exception>
67 [EditorBrowsable(EditorBrowsableState.Never)]
68 public Vector2(Vector2 other) : this(other == null ? throw new ArgumentNullException(nameof(other)) : other.X, other.Y)
75 /// <param name="vec3">Vector3 to create this vector from.</param>
76 /// <since_tizen> 3 </since_tizen>
77 public Vector2(Vector3 vec3) : this(Interop.Vector2.NewVector2WithVector3(Vector3.getCPtr(vec3)), true)
79 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
85 /// <param name="vec4">Vector4 to create this vector from.</param>
86 /// <since_tizen> 3 </since_tizen>
87 public Vector2(Vector4 vec4) : this(Interop.Vector2.NewVector2WithVector4(Vector4.getCPtr(vec4)), true)
89 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
92 internal Vector2(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
96 internal Vector2(Vector2ChangedCallback cb, float x, float y) : this(Interop.Vector2.NewVector2(x, y), true)
99 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
102 internal Vector2(Vector2ChangedCallback cb, Vector2 other) : this(cb, other.X, other.Y)
106 internal delegate void Vector2ChangedCallback(float x, float y);
107 private Vector2ChangedCallback callback = null;
112 /// <since_tizen> 3 </since_tizen>
113 public static Vector2 One
117 global::System.IntPtr cPtr = Interop.Vector2.OneGet();
118 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
119 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
125 /// The vector representing the x-axis.
127 /// <since_tizen> 3 </since_tizen>
128 public static Vector2 XAxis
132 global::System.IntPtr cPtr = Interop.Vector2.XaxisGet();
133 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
134 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
140 /// The vector representing the y-axis.
142 /// <since_tizen> 3 </since_tizen>
143 public static Vector2 YAxis
147 global::System.IntPtr cPtr = Interop.Vector2.YaxisGet();
148 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
149 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
155 /// The vector representing the negative x-axis.
157 /// <since_tizen> 3 </since_tizen>
158 public static Vector2 NegativeXAxis
162 global::System.IntPtr cPtr = Interop.Vector2.NegativeXaxisGet();
163 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
164 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
170 /// The vector representing the negative y-axis.
172 /// <since_tizen> 3 </since_tizen>
173 public static Vector2 NegativeYAxis
177 global::System.IntPtr cPtr = Interop.Vector2.NegativeYaxisGet();
178 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
179 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
187 /// <since_tizen> 3 </since_tizen>
188 public static Vector2 Zero
192 global::System.IntPtr cPtr = Interop.Vector2.ZeroGet();
193 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
194 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
203 /// The setter is deprecated in API8 and will be removed in API10. Please use new Vector2(...) constructor.
206 /// // DO NOT use like the followings!
207 /// Vector2 vector2 = new Vector2();
208 /// vector2.X = 0.1f;
209 /// // Please USE like this
210 /// float x = 0.1f, y = 0.5f;
211 /// Vector2 vector2 = new Vector2(x, y);
213 /// <since_tizen> 3 </since_tizen>
216 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Vector2(...) constructor")]
219 Interop.Vector2.XSet(SwigCPtr, value);
220 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
222 callback?.Invoke(X, Y);
226 float ret = Interop.Vector2.XGet(SwigCPtr);
227 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
236 /// The setter is deprecated in API8 and will be removed in API10. Please use new Vector2(...) constructor.
239 /// // DO NOT use like the followings!
240 /// Vector2 vector2 = new Vector2();
241 /// vector2.Width = 1.0f;
242 /// // Please USE like this
243 /// float width = 1.0f, height = 2.0f;
244 /// Vector2 vector2 = new Vector2(x, y);
246 /// <since_tizen> 3 </since_tizen>
249 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Vector2(...) constructor")]
252 Interop.Vector2.WidthSet(SwigCPtr, value);
253 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
255 callback?.Invoke(X, Y);
259 float ret = Interop.Vector2.WidthGet(SwigCPtr);
260 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
269 /// The setter is deprecated in API8 and will be removed in API10. Please use new Vector2(...) constructor.
272 /// // DO NOT use like the followings!
273 /// Vector2 vector2 = new Vector2();
274 /// vector2.Y = 0.5f;
275 /// // Please USE like this
276 /// float x = 0.1f, y = 0.5f;
277 /// Vector2 vector2 = new Vector2(x, y);
279 /// <since_tizen> 3 </since_tizen>
282 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Vector2(...) constructor")]
285 Interop.Vector2.YSet(SwigCPtr, value);
286 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
288 callback?.Invoke(X, Y);
292 float ret = Interop.Vector2.YGet(SwigCPtr);
293 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
302 /// The setter is deprecated in API8 and will be removed in API10. Please use new Vector2(...) constructor.
305 /// // DO NOT use like the followings!
306 /// Vector2 vector2 = new Vector2();
307 /// vector2.Height = 2.0f;
308 /// // Please USE like this
309 /// float width = 1.0f, height = 2.0f;
310 /// Vector2 vector2 = new Vector2(x, y);
312 /// <since_tizen> 3 </since_tizen>
315 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Vector2(...) constructor")]
318 Interop.Vector2.HeightSet(SwigCPtr, value);
319 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
321 callback?.Invoke(X, Y);
325 float ret = Interop.Vector2.HeightGet(SwigCPtr);
326 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
332 /// The array subscript operator overload.
334 /// <param name="index">The subscript index.</param>
335 /// <returns>The float at the given index.</returns>
336 /// <since_tizen> 3 </since_tizen>
337 public float this[uint index]
341 return ValueOfIndex(index);
346 /// The addition operator.
348 /// <param name="arg1">The first value.</param>
349 /// <param name="arg2">The second value.</param>
350 /// <returns>The vector containing the result of the addition.</returns>
351 /// <since_tizen> 3 </since_tizen>
352 public static Vector2 operator +(Vector2 arg1, Vector2 arg2)
354 return arg1?.Add(arg2);
358 /// The subtraction operator.
360 /// <param name="arg1">The first value.</param>
361 /// <param name="arg2">The second value.</param>
362 /// <returns>The vector containing the result of the subtraction.</returns>
363 /// <since_tizen> 3 </since_tizen>
364 public static Vector2 operator -(Vector2 arg1, Vector2 arg2)
366 return arg1?.Subtract(arg2);
370 /// The unary negation operator.
372 /// <param name="arg1">The target value.</param>
373 /// <returns>The vector containing the negation.</returns>
374 /// <since_tizen> 3 </since_tizen>
375 public static Vector2 operator -(Vector2 arg1)
377 return arg1?.Subtract();
381 /// The multiplication operator.
383 /// <param name="arg1">The first value.</param>
384 /// <param name="arg2">The second value.</param>
385 /// <returns>The vector containing the result of the multiplication.</returns>
386 /// <since_tizen> 3 </since_tizen>
387 public static Vector2 operator *(Vector2 arg1, Vector2 arg2)
389 return arg1?.Multiply(arg2);
393 /// Th multiplication operator.
395 /// <param name="arg1">The first value.</param>
396 /// <param name="arg2">The float value to scale the vector.</param>
397 /// <returns>The vector containing the result of the scaling.</returns>
398 /// <since_tizen> 3 </since_tizen>
399 public static Vector2 operator *(Vector2 arg1, float arg2)
401 return arg1?.Multiply(arg2);
405 /// The division operator.
407 /// <param name="arg1">The first value.</param>
408 /// <param name="arg2">The second value.</param>
409 /// <returns>The vector containing the result of the division.</returns>
410 /// <since_tizen> 3 </since_tizen>
411 public static Vector2 operator /(Vector2 arg1, Vector2 arg2)
413 return arg1?.Divide(arg2);
417 /// Th division operator.
419 /// <param name="arg1">The first value.</param>
420 /// <param name="arg2">The float value to scale the vector by.</param>
421 /// <returns>The vector containing the result of the scaling.</returns>
422 /// <since_tizen> 3 </since_tizen>
423 public static Vector2 operator /(Vector2 arg1, float arg2)
425 return arg1?.Divide(arg2);
429 /// Determines whether the specified object is equal to the current object.
431 /// <param name="obj">The object to compare with the current object.</param>
432 /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
433 public override bool Equals(System.Object obj)
435 Vector2 vector2 = obj as Vector2;
437 if (X == vector2?.X && Y == vector2?.Y)
445 /// Gets the hash code of this Vector2.
447 /// <returns>The Hash Code.</returns>
448 /// <since_tizen> 6 </since_tizen>
449 public override int GetHashCode()
451 return SwigCPtr.Handle.GetHashCode();
455 /// Returns the length of the vector.
457 /// <returns>The length of the vector.</returns>
458 /// <since_tizen> 3 </since_tizen>
459 public float Length()
461 float ret = Interop.Vector2.Length(SwigCPtr);
462 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
467 /// Returns the length of the vector squared.<br />
468 /// This is more efficient than Length() for threshold
469 /// testing as it avoids the use of a square root.<br />
471 /// <returns>The length of the vector squared</returns>
472 /// <since_tizen> 3 </since_tizen>
473 public float LengthSquared()
475 float ret = Interop.Vector2.LengthSquared(SwigCPtr);
476 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
481 /// Sets the vector to be the unit length, whilst maintaining its direction.
483 /// <since_tizen> 3 </since_tizen>
484 public void Normalize()
486 Interop.Vector2.Normalize(SwigCPtr);
487 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
491 [EditorBrowsable(EditorBrowsableState.Never)]
492 public object Clone() => new Vector2(this);
495 /// Clamps the vector between minimum and maximum vectors.
497 /// <param name="min">The minimum vector.</param>
498 /// <param name="max">The maximum vector.</param>
499 /// <since_tizen> 3 </since_tizen>
500 public void Clamp(Vector2 min, Vector2 max)
502 Interop.Vector2.Clamp(SwigCPtr, Vector2.getCPtr(min), Vector2.getCPtr(max));
503 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
506 internal static Vector2 GetVector2FromPtr(global::System.IntPtr cPtr)
508 Vector2 ret = new Vector2(cPtr, false);
509 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
513 internal SWIGTYPE_p_float AsFloat()
515 global::System.IntPtr cPtr = Interop.Vector2.AsFloat(SwigCPtr);
516 SWIGTYPE_p_float ret = (cPtr == global::System.IntPtr.Zero) ? null : new SWIGTYPE_p_float(cPtr);
517 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
521 /// This will not be public opened.
522 [EditorBrowsable(EditorBrowsableState.Never)]
523 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
525 Interop.Vector2.DeleteVector2(swigCPtr);
528 private Vector2 Add(Vector2 rhs)
530 Vector2 ret = new Vector2(Interop.Vector2.Add(SwigCPtr, Vector2.getCPtr(rhs)), true);
531 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
535 private Vector2 AddAssign(Vector2 rhs)
537 Vector2 ret = new Vector2(Interop.Vector2.AddAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
538 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
542 private Vector2 Subtract(Vector2 rhs)
544 Vector2 ret = new Vector2(Interop.Vector2.Subtract(SwigCPtr, Vector2.getCPtr(rhs)), true);
545 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
549 private Vector2 SubtractAssign(Vector2 rhs)
551 Vector2 ret = new Vector2(Interop.Vector2.SubtractAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
552 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
556 private Vector2 Multiply(Vector2 rhs)
558 Vector2 ret = new Vector2(Interop.Vector2.Multiply(SwigCPtr, Vector2.getCPtr(rhs)), true);
559 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
563 private Vector2 Multiply(float rhs)
565 Vector2 ret = new Vector2(Interop.Vector2.Multiply(SwigCPtr, rhs), true);
566 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
570 private Vector2 MultiplyAssign(Vector2 rhs)
572 Vector2 ret = new Vector2(Interop.Vector2.MultiplyAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
573 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
577 private Vector2 MultiplyAssign(float rhs)
579 Vector2 ret = new Vector2(Interop.Vector2.MultiplyAssign(SwigCPtr, rhs), false);
580 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
584 private Vector2 Divide(Vector2 rhs)
586 Vector2 ret = new Vector2(Interop.Vector2.Divide(SwigCPtr, Vector2.getCPtr(rhs)), true);
587 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
591 private Vector2 Divide(float rhs)
593 Vector2 ret = new Vector2(Interop.Vector2.Divide(SwigCPtr, rhs), true);
594 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
598 private Vector2 DivideAssign(Vector2 rhs)
600 Vector2 ret = new Vector2(Interop.Vector2.DivideAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
601 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
605 private Vector2 DivideAssign(float rhs)
607 Vector2 ret = new Vector2(Interop.Vector2.DivideAssign(SwigCPtr, rhs), false);
608 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
612 private Vector2 Subtract()
614 Vector2 ret = new Vector2(Interop.Vector2.Subtract(SwigCPtr), true);
615 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
619 private bool EqualTo(Vector2 rhs)
621 bool ret = Interop.Vector2.EqualTo(SwigCPtr, Vector2.getCPtr(rhs));
622 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
626 private bool NotEqualTo(Vector2 rhs)
628 bool ret = Interop.Vector2.NotEqualTo(SwigCPtr, Vector2.getCPtr(rhs));
629 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
633 private float ValueOfIndex(uint index)
635 float ret = Interop.Vector2.ValueOfIndex(SwigCPtr, index);
636 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();