/* * Copyright(c) 2021 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System; using System.ComponentModel; using Tizen.NUI.Binding; namespace Tizen.NUI { /// /// A two-dimensional vector. /// /// 3 [Binding.TypeConverter(typeof(Vector2TypeConverter))] public class Vector2 : Disposable, ICloneable { /// /// The default constructor initializes the vector to 0. /// /// 3 public Vector2() : this(Interop.Vector2.NewVector2(), true) { if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// The constructor. /// /// The x or width component. /// The y or height component. /// 3 public Vector2(float x, float y) : this(Interop.Vector2.NewVector2(x, y), true) { if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// The conversion constructor from an array of two floats. /// /// The array of xy. /// 3 public Vector2(float[] array) : this(Interop.Vector2.NewVector2(array), true) { if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// The copy constructor. /// /// The copy target. /// Thrown when other is null. [EditorBrowsable(EditorBrowsableState.Never)] public Vector2(Vector2 other) : this(other == null ? throw new ArgumentNullException(nameof(other)) : other.X, other.Y) { } /// /// The constructor. /// /// Vector3 to create this vector from. /// 3 public Vector2(Vector3 vec3) : this(Interop.Vector2.NewVector2WithVector3(Vector3.getCPtr(vec3)), true) { if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// The constructor. /// /// Vector4 to create this vector from. /// 3 public Vector2(Vector4 vec4) : this(Interop.Vector2.NewVector2WithVector4(Vector4.getCPtr(vec4)), true) { if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } internal Vector2(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn) { } internal Vector2(Vector2ChangedCallback cb, float x, float y) : this(Interop.Vector2.NewVector2(x, y), true) { callback = cb; if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } internal Vector2(Vector2ChangedCallback cb, Vector2 other) : this(cb, other.X, other.Y) { } internal delegate void Vector2ChangedCallback(float x, float y); private Vector2ChangedCallback callback = null; /// /// (1.0f,1.0f). /// /// 3 public static Vector2 One { get { global::System.IntPtr cPtr = Interop.Vector2.OneGet(); Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve()); return ret; } } /// /// The vector representing the x-axis. /// /// 3 public static Vector2 XAxis { get { global::System.IntPtr cPtr = Interop.Vector2.XaxisGet(); Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve()); return ret; } } /// /// The vector representing the y-axis. /// /// 3 public static Vector2 YAxis { get { global::System.IntPtr cPtr = Interop.Vector2.YaxisGet(); Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve()); return ret; } } /// /// The vector representing the negative x-axis. /// /// 3 public static Vector2 NegativeXAxis { get { global::System.IntPtr cPtr = Interop.Vector2.NegativeXaxisGet(); Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve()); return ret; } } /// /// The vector representing the negative y-axis. /// /// 3 public static Vector2 NegativeYAxis { get { global::System.IntPtr cPtr = Interop.Vector2.NegativeYaxisGet(); Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve()); return ret; } } /// /// (0.0f, 0.0f). /// /// 3 public static Vector2 Zero { get { global::System.IntPtr cPtr = Interop.Vector2.ZeroGet(); Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve()); return ret; } } /// /// The x component. /// /// /// The setter is deprecated in API8 and will be removed in API10. Use new Vector2(...) constructor. /// /// /// // DO NOT use like the followings! /// Vector2 vector2 = new Vector2(); /// vector2.X = 0.1f; /// // USE like this /// float x = 0.1f, y = 0.5f; /// Vector2 vector2 = new Vector2(x, y); /// /// 3 public float X { [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Vector2(...) constructor")] set { Interop.Vector2.XSet(SwigCPtr, value); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); callback?.Invoke(X, Y); } get { float ret = Interop.Vector2.XGet(SwigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve()); return ret; } } /// /// The width. /// /// /// The setter is deprecated in API8 and will be removed in API10. Use new Vector2(...) constructor. /// /// /// // DO NOT use like the followings! /// Vector2 vector2 = new Vector2(); /// vector2.Width = 1.0f; /// // USE like this /// float width = 1.0f, height = 2.0f; /// Vector2 vector2 = new Vector2(x, y); /// /// 3 public float Width { [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Vector2(...) constructor")] set { Interop.Vector2.WidthSet(SwigCPtr, value); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); callback?.Invoke(X, Y); } get { float ret = Interop.Vector2.WidthGet(SwigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve()); return ret; } } /// /// The y component. /// /// /// The setter is deprecated in API8 and will be removed in API10. Use new Vector2(...) constructor. /// /// /// // DO NOT use like the followings! /// Vector2 vector2 = new Vector2(); /// vector2.Y = 0.5f; /// // USE like this /// float x = 0.1f, y = 0.5f; /// Vector2 vector2 = new Vector2(x, y); /// /// 3 public float Y { [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Vector2(...) constructor")] set { Interop.Vector2.YSet(SwigCPtr, value); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); callback?.Invoke(X, Y); } get { float ret = Interop.Vector2.YGet(SwigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve()); return ret; } } /// /// The height. /// /// /// The setter is deprecated in API8 and will be removed in API10. Use new Vector2(...) constructor. /// /// /// // DO NOT use like the followings! /// Vector2 vector2 = new Vector2(); /// vector2.Height = 2.0f; /// // USE like this /// float width = 1.0f, height = 2.0f; /// Vector2 vector2 = new Vector2(x, y); /// /// 3 public float Height { [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Vector2(...) constructor")] set { Interop.Vector2.HeightSet(SwigCPtr, value); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); callback?.Invoke(X, Y); } get { float ret = Interop.Vector2.HeightGet(SwigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve()); return ret; } } /// /// The array subscript operator overload. /// /// The subscript index. /// The float at the given index. /// 3 public float this[uint index] { get { return ValueOfIndex(index); } } /// /// The addition operator. /// /// The first value. /// The second value. /// The vector containing the result of the addition. /// 3 public static Vector2 operator +(Vector2 arg1, Vector2 arg2) { return arg1?.Add(arg2); } /// /// The subtraction operator. /// /// The first value. /// The second value. /// The vector containing the result of the subtraction. /// 3 public static Vector2 operator -(Vector2 arg1, Vector2 arg2) { return arg1?.Subtract(arg2); } /// /// The unary negation operator. /// /// The target value. /// The vector containing the negation. /// 3 public static Vector2 operator -(Vector2 arg1) { return arg1?.Subtract(); } /// /// The multiplication operator. /// /// The first value. /// The second value. /// The vector containing the result of the multiplication. /// 3 public static Vector2 operator *(Vector2 arg1, Vector2 arg2) { return arg1?.Multiply(arg2); } /// /// Th multiplication operator. /// /// The first value. /// The float value to scale the vector. /// The vector containing the result of the scaling. /// 3 public static Vector2 operator *(Vector2 arg1, float arg2) { return arg1?.Multiply(arg2); } /// /// The division operator. /// /// The first value. /// The second value. /// The vector containing the result of the division. /// 3 public static Vector2 operator /(Vector2 arg1, Vector2 arg2) { return arg1?.Divide(arg2); } /// /// Th division operator. /// /// The first value. /// The float value to scale the vector by. /// The vector containing the result of the scaling. /// 3 public static Vector2 operator /(Vector2 arg1, float arg2) { return arg1?.Divide(arg2); } /// /// Determines whether the specified object is equal to the current object. /// /// The object to compare with the current object. /// true if the specified object is equal to the current object; otherwise, false. public override bool Equals(System.Object obj) { Vector2 vector2 = obj as Vector2; bool equal = false; if (X == vector2?.X && Y == vector2?.Y) { equal = true; } return equal; } /// /// Gets the hash code of this Vector2. /// /// The Hash Code. /// 6 public override int GetHashCode() { return SwigCPtr.Handle.GetHashCode(); } /// /// Returns the length of the vector. /// /// The length of the vector. /// 3 public float Length() { float ret = Interop.Vector2.Length(SwigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Returns the length of the vector squared.
/// This is more efficient than Length() for threshold /// testing as it avoids the use of a square root.
///
/// The length of the vector squared /// 3 public float LengthSquared() { float ret = Interop.Vector2.LengthSquared(SwigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Sets the vector to be the unit length, whilst maintaining its direction. /// /// 3 public void Normalize() { Interop.Vector2.Normalize(SwigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// [EditorBrowsable(EditorBrowsableState.Never)] public object Clone() => new Vector2(this); /// /// Clamps the vector between minimum and maximum vectors. /// /// The minimum vector. /// The maximum vector. /// 3 public void Clamp(Vector2 min, Vector2 max) { Interop.Vector2.Clamp(SwigCPtr, Vector2.getCPtr(min), Vector2.getCPtr(max)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } internal static Vector2 GetVector2FromPtr(global::System.IntPtr cPtr) { Vector2 ret = new Vector2(cPtr, false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } internal SWIGTYPE_p_float AsFloat() { global::System.IntPtr cPtr = Interop.Vector2.AsFloat(SwigCPtr); SWIGTYPE_p_float ret = (cPtr == global::System.IntPtr.Zero) ? null : new SWIGTYPE_p_float(cPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// This will not be public opened. [EditorBrowsable(EditorBrowsableState.Never)] protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr) { Interop.Vector2.DeleteVector2(swigCPtr); } private Vector2 Add(Vector2 rhs) { Vector2 ret = new Vector2(Interop.Vector2.Add(SwigCPtr, Vector2.getCPtr(rhs)), true); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 AddAssign(Vector2 rhs) { Vector2 ret = new Vector2(Interop.Vector2.AddAssign(SwigCPtr, Vector2.getCPtr(rhs)), false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 Subtract(Vector2 rhs) { Vector2 ret = new Vector2(Interop.Vector2.Subtract(SwigCPtr, Vector2.getCPtr(rhs)), true); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 SubtractAssign(Vector2 rhs) { Vector2 ret = new Vector2(Interop.Vector2.SubtractAssign(SwigCPtr, Vector2.getCPtr(rhs)), false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 Multiply(Vector2 rhs) { Vector2 ret = new Vector2(Interop.Vector2.Multiply(SwigCPtr, Vector2.getCPtr(rhs)), true); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 Multiply(float rhs) { Vector2 ret = new Vector2(Interop.Vector2.Multiply(SwigCPtr, rhs), true); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 MultiplyAssign(Vector2 rhs) { Vector2 ret = new Vector2(Interop.Vector2.MultiplyAssign(SwigCPtr, Vector2.getCPtr(rhs)), false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 MultiplyAssign(float rhs) { Vector2 ret = new Vector2(Interop.Vector2.MultiplyAssign(SwigCPtr, rhs), false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 Divide(Vector2 rhs) { Vector2 ret = new Vector2(Interop.Vector2.Divide(SwigCPtr, Vector2.getCPtr(rhs)), true); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 Divide(float rhs) { Vector2 ret = new Vector2(Interop.Vector2.Divide(SwigCPtr, rhs), true); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 DivideAssign(Vector2 rhs) { Vector2 ret = new Vector2(Interop.Vector2.DivideAssign(SwigCPtr, Vector2.getCPtr(rhs)), false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 DivideAssign(float rhs) { Vector2 ret = new Vector2(Interop.Vector2.DivideAssign(SwigCPtr, rhs), false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private Vector2 Subtract() { Vector2 ret = new Vector2(Interop.Vector2.Subtract(SwigCPtr), true); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private bool EqualTo(Vector2 rhs) { bool ret = Interop.Vector2.EqualTo(SwigCPtr, Vector2.getCPtr(rhs)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private bool NotEqualTo(Vector2 rhs) { bool ret = Interop.Vector2.NotEqualTo(SwigCPtr, Vector2.getCPtr(rhs)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private float ValueOfIndex(uint index) { float ret = Interop.Vector2.ValueOfIndex(SwigCPtr, index); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } } }