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>
218 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Vector2(...) constructor");
220 Interop.Vector2.XSet(SwigCPtr, value);
221 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
223 callback?.Invoke(X, Y);
227 float ret = Interop.Vector2.XGet(SwigCPtr);
228 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
237 /// The setter is deprecated in API8 and will be removed in API10. Please use new Vector2(...) constructor.
240 /// // DO NOT use like the followings!
241 /// Vector2 vector2 = new Vector2();
242 /// vector2.Width = 1.0f;
243 /// // Please USE like this
244 /// float width = 1.0f, height = 2.0f;
245 /// Vector2 vector2 = new Vector2(x, y);
247 /// <since_tizen> 3 </since_tizen>
252 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Vector2(...) constructor");
254 Interop.Vector2.WidthSet(SwigCPtr, value);
255 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
257 callback?.Invoke(X, Y);
261 float ret = Interop.Vector2.WidthGet(SwigCPtr);
262 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
271 /// The setter is deprecated in API8 and will be removed in API10. Please use new Vector2(...) constructor.
274 /// // DO NOT use like the followings!
275 /// Vector2 vector2 = new Vector2();
276 /// vector2.Y = 0.5f;
277 /// // Please USE like this
278 /// float x = 0.1f, y = 0.5f;
279 /// Vector2 vector2 = new Vector2(x, y);
281 /// <since_tizen> 3 </since_tizen>
286 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Vector2(...) constructor");
288 Interop.Vector2.YSet(SwigCPtr, value);
289 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
291 callback?.Invoke(X, Y);
295 float ret = Interop.Vector2.YGet(SwigCPtr);
296 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
305 /// The setter is deprecated in API8 and will be removed in API10. Please use new Vector2(...) constructor.
308 /// // DO NOT use like the followings!
309 /// Vector2 vector2 = new Vector2();
310 /// vector2.Height = 2.0f;
311 /// // Please USE like this
312 /// float width = 1.0f, height = 2.0f;
313 /// Vector2 vector2 = new Vector2(x, y);
315 /// <since_tizen> 3 </since_tizen>
320 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Vector2(...) constructor");
322 Interop.Vector2.HeightSet(SwigCPtr, value);
323 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
325 callback?.Invoke(X, Y);
329 float ret = Interop.Vector2.HeightGet(SwigCPtr);
330 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
336 /// The array subscript operator overload.
338 /// <param name="index">The subscript index.</param>
339 /// <returns>The float at the given index.</returns>
340 /// <since_tizen> 3 </since_tizen>
341 public float this[uint index]
345 return ValueOfIndex(index);
350 /// The addition operator.
352 /// <param name="arg1">The first value.</param>
353 /// <param name="arg2">The second value.</param>
354 /// <returns>The vector containing the result of the addition.</returns>
355 /// <since_tizen> 3 </since_tizen>
356 public static Vector2 operator +(Vector2 arg1, Vector2 arg2)
358 return arg1?.Add(arg2);
362 /// The subtraction operator.
364 /// <param name="arg1">The first value.</param>
365 /// <param name="arg2">The second value.</param>
366 /// <returns>The vector containing the result of the subtraction.</returns>
367 /// <since_tizen> 3 </since_tizen>
368 public static Vector2 operator -(Vector2 arg1, Vector2 arg2)
370 return arg1?.Subtract(arg2);
374 /// The unary negation operator.
376 /// <param name="arg1">The target value.</param>
377 /// <returns>The vector containing the negation.</returns>
378 /// <since_tizen> 3 </since_tizen>
379 public static Vector2 operator -(Vector2 arg1)
381 return arg1?.Subtract();
385 /// The multiplication operator.
387 /// <param name="arg1">The first value.</param>
388 /// <param name="arg2">The second value.</param>
389 /// <returns>The vector containing the result of the multiplication.</returns>
390 /// <since_tizen> 3 </since_tizen>
391 public static Vector2 operator *(Vector2 arg1, Vector2 arg2)
393 return arg1?.Multiply(arg2);
397 /// Th multiplication operator.
399 /// <param name="arg1">The first value.</param>
400 /// <param name="arg2">The float value to scale the vector.</param>
401 /// <returns>The vector containing the result of the scaling.</returns>
402 /// <since_tizen> 3 </since_tizen>
403 public static Vector2 operator *(Vector2 arg1, float arg2)
405 return arg1?.Multiply(arg2);
409 /// The division operator.
411 /// <param name="arg1">The first value.</param>
412 /// <param name="arg2">The second value.</param>
413 /// <returns>The vector containing the result of the division.</returns>
414 /// <since_tizen> 3 </since_tizen>
415 public static Vector2 operator /(Vector2 arg1, Vector2 arg2)
417 return arg1?.Divide(arg2);
421 /// Th division operator.
423 /// <param name="arg1">The first value.</param>
424 /// <param name="arg2">The float value to scale the vector by.</param>
425 /// <returns>The vector containing the result of the scaling.</returns>
426 /// <since_tizen> 3 </since_tizen>
427 public static Vector2 operator /(Vector2 arg1, float arg2)
429 return arg1?.Divide(arg2);
433 /// Determines whether the specified object is equal to the current object.
435 /// <param name="obj">The object to compare with the current object.</param>
436 /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
437 public override bool Equals(System.Object obj)
439 Vector2 vector2 = obj as Vector2;
441 if (X == vector2?.X && Y == vector2?.Y)
449 /// Gets the hash code of this Vector2.
451 /// <returns>The Hash Code.</returns>
452 /// <since_tizen> 6 </since_tizen>
453 public override int GetHashCode()
455 return SwigCPtr.Handle.GetHashCode();
459 /// Returns the length of the vector.
461 /// <returns>The length of the vector.</returns>
462 /// <since_tizen> 3 </since_tizen>
463 public float Length()
465 float ret = Interop.Vector2.Length(SwigCPtr);
466 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
471 /// Returns the length of the vector squared.<br />
472 /// This is more efficient than Length() for threshold
473 /// testing as it avoids the use of a square root.<br />
475 /// <returns>The length of the vector squared</returns>
476 /// <since_tizen> 3 </since_tizen>
477 public float LengthSquared()
479 float ret = Interop.Vector2.LengthSquared(SwigCPtr);
480 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
485 /// Sets the vector to be the unit length, whilst maintaining its direction.
487 /// <since_tizen> 3 </since_tizen>
488 public void Normalize()
490 Interop.Vector2.Normalize(SwigCPtr);
491 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
495 [EditorBrowsable(EditorBrowsableState.Never)]
496 public object Clone() => new Vector2(this);
499 /// Clamps the vector between minimum and maximum vectors.
501 /// <param name="min">The minimum vector.</param>
502 /// <param name="max">The maximum vector.</param>
503 /// <since_tizen> 3 </since_tizen>
504 public void Clamp(Vector2 min, Vector2 max)
506 Interop.Vector2.Clamp(SwigCPtr, Vector2.getCPtr(min), Vector2.getCPtr(max));
507 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
510 internal static Vector2 GetVector2FromPtr(global::System.IntPtr cPtr)
512 Vector2 ret = new Vector2(cPtr, false);
513 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
517 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Vector2 obj)
519 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
522 internal SWIGTYPE_p_float AsFloat()
524 global::System.IntPtr cPtr = Interop.Vector2.AsFloat(SwigCPtr);
525 SWIGTYPE_p_float ret = (cPtr == global::System.IntPtr.Zero) ? null : new SWIGTYPE_p_float(cPtr);
526 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
530 /// This will not be public opened.
531 [EditorBrowsable(EditorBrowsableState.Never)]
532 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
534 Interop.Vector2.DeleteVector2(swigCPtr);
537 private Vector2 Add(Vector2 rhs)
539 Vector2 ret = new Vector2(Interop.Vector2.Add(SwigCPtr, Vector2.getCPtr(rhs)), true);
540 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
544 private Vector2 AddAssign(Vector2 rhs)
546 Vector2 ret = new Vector2(Interop.Vector2.AddAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
547 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
551 private Vector2 Subtract(Vector2 rhs)
553 Vector2 ret = new Vector2(Interop.Vector2.Subtract(SwigCPtr, Vector2.getCPtr(rhs)), true);
554 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
558 private Vector2 SubtractAssign(Vector2 rhs)
560 Vector2 ret = new Vector2(Interop.Vector2.SubtractAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
561 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
565 private Vector2 Multiply(Vector2 rhs)
567 Vector2 ret = new Vector2(Interop.Vector2.Multiply(SwigCPtr, Vector2.getCPtr(rhs)), true);
568 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
572 private Vector2 Multiply(float rhs)
574 Vector2 ret = new Vector2(Interop.Vector2.Multiply(SwigCPtr, rhs), true);
575 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
579 private Vector2 MultiplyAssign(Vector2 rhs)
581 Vector2 ret = new Vector2(Interop.Vector2.MultiplyAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
582 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
586 private Vector2 MultiplyAssign(float rhs)
588 Vector2 ret = new Vector2(Interop.Vector2.MultiplyAssign(SwigCPtr, rhs), false);
589 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
593 private Vector2 Divide(Vector2 rhs)
595 Vector2 ret = new Vector2(Interop.Vector2.Divide(SwigCPtr, Vector2.getCPtr(rhs)), true);
596 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
600 private Vector2 Divide(float rhs)
602 Vector2 ret = new Vector2(Interop.Vector2.Divide(SwigCPtr, rhs), true);
603 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
607 private Vector2 DivideAssign(Vector2 rhs)
609 Vector2 ret = new Vector2(Interop.Vector2.DivideAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
610 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
614 private Vector2 DivideAssign(float rhs)
616 Vector2 ret = new Vector2(Interop.Vector2.DivideAssign(SwigCPtr, rhs), false);
617 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
621 private Vector2 Subtract()
623 Vector2 ret = new Vector2(Interop.Vector2.Subtract(SwigCPtr), true);
624 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
628 private bool EqualTo(Vector2 rhs)
630 bool ret = Interop.Vector2.EqualTo(SwigCPtr, Vector2.getCPtr(rhs));
631 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
635 private bool NotEqualTo(Vector2 rhs)
637 bool ret = Interop.Vector2.NotEqualTo(SwigCPtr, Vector2.getCPtr(rhs));
638 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
642 private float ValueOfIndex(uint index)
644 float ret = Interop.Vector2.ValueOfIndex(SwigCPtr, index);
645 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();