2 * Copyright(c) 2019 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 [EditorBrowsable(EditorBrowsableState.Never)]
67 public Vector2(Vector2 other) : this((float)other?.X, (float)other.Y)
74 /// <param name="vec3">Vector3 to create this vector from.</param>
75 /// <since_tizen> 3 </since_tizen>
76 public Vector2(Vector3 vec3) : this(Interop.Vector2.NewVector2WithVector3(Vector3.getCPtr(vec3)), true)
78 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
84 /// <param name="vec4">Vector4 to create this vector from.</param>
85 /// <since_tizen> 3 </since_tizen>
86 public Vector2(Vector4 vec4) : this(Interop.Vector2.NewVector2WithVector4(Vector4.getCPtr(vec4)), true)
88 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
91 internal Vector2(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
95 internal Vector2(Vector2ChangedCallback cb, float x, float y) : this(Interop.Vector2.NewVector2(x, y), true)
98 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
101 internal Vector2(Vector2ChangedCallback cb, Vector2 other) : this(cb, other.X, other.Y)
105 internal delegate void Vector2ChangedCallback(float x, float y);
106 private Vector2ChangedCallback callback = null;
111 /// <since_tizen> 3 </since_tizen>
112 public static Vector2 One
116 global::System.IntPtr cPtr = Interop.Vector2.OneGet();
117 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
118 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
124 /// The vector representing the x-axis.
126 /// <since_tizen> 3 </since_tizen>
127 public static Vector2 XAxis
131 global::System.IntPtr cPtr = Interop.Vector2.XaxisGet();
132 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
133 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
139 /// The vector representing the y-axis.
141 /// <since_tizen> 3 </since_tizen>
142 public static Vector2 YAxis
146 global::System.IntPtr cPtr = Interop.Vector2.YaxisGet();
147 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
148 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
154 /// The vector representing the negative x-axis.
156 /// <since_tizen> 3 </since_tizen>
157 public static Vector2 NegativeXAxis
161 global::System.IntPtr cPtr = Interop.Vector2.NegativeXaxisGet();
162 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
163 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
169 /// The vector representing the negative y-axis.
171 /// <since_tizen> 3 </since_tizen>
172 public static Vector2 NegativeYAxis
176 global::System.IntPtr cPtr = Interop.Vector2.NegativeYaxisGet();
177 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
178 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
186 /// <since_tizen> 3 </since_tizen>
187 public static Vector2 Zero
191 global::System.IntPtr cPtr = Interop.Vector2.ZeroGet();
192 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
193 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
202 /// The setter is deprecated in API8 and will be removed in API10. Please use new Vector2(...) constructor.
205 /// // DO NOT use like the followings!
206 /// Vector2 vector2 = new Vector2();
207 /// vector2.X = 0.1f;
208 /// // Please USE like this
209 /// float x = 0.1f, y = 0.5f;
210 /// Vector2 vector2 = new Vector2(x, y);
212 /// <since_tizen> 3 </since_tizen>
217 Tizen.Log.Fatal("NUI", "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>
251 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Vector2(...) constructor");
253 Interop.Vector2.WidthSet(SwigCPtr, value);
254 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
256 callback?.Invoke(X, Y);
260 float ret = Interop.Vector2.WidthGet(SwigCPtr);
261 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
270 /// The setter is deprecated in API8 and will be removed in API10. Please use new Vector2(...) constructor.
273 /// // DO NOT use like the followings!
274 /// Vector2 vector2 = new Vector2();
275 /// vector2.Y = 0.5f;
276 /// // Please USE like this
277 /// float x = 0.1f, y = 0.5f;
278 /// Vector2 vector2 = new Vector2(x, y);
280 /// <since_tizen> 3 </since_tizen>
285 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Vector2(...) constructor");
287 Interop.Vector2.YSet(SwigCPtr, value);
288 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
290 callback?.Invoke(X, Y);
294 float ret = Interop.Vector2.YGet(SwigCPtr);
295 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
304 /// The setter is deprecated in API8 and will be removed in API10. Please use new Vector2(...) constructor.
307 /// // DO NOT use like the followings!
308 /// Vector2 vector2 = new Vector2();
309 /// vector2.Height = 2.0f;
310 /// // Please USE like this
311 /// float width = 1.0f, height = 2.0f;
312 /// Vector2 vector2 = new Vector2(x, y);
314 /// <since_tizen> 3 </since_tizen>
319 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Vector2(...) constructor");
321 Interop.Vector2.HeightSet(SwigCPtr, value);
322 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
324 callback?.Invoke(X, Y);
328 float ret = Interop.Vector2.HeightGet(SwigCPtr);
329 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
335 /// The array subscript operator overload.
337 /// <param name="index">The subscript index.</param>
338 /// <returns>The float at the given index.</returns>
339 /// <since_tizen> 3 </since_tizen>
340 public float this[uint index]
344 return ValueOfIndex(index);
349 /// The addition operator.
351 /// <param name="arg1">The first value.</param>
352 /// <param name="arg2">The second value.</param>
353 /// <returns>The vector containing the result of the addition.</returns>
354 /// <since_tizen> 3 </since_tizen>
355 public static Vector2 operator +(Vector2 arg1, Vector2 arg2)
357 return arg1?.Add(arg2);
361 /// The subtraction operator.
363 /// <param name="arg1">The first value.</param>
364 /// <param name="arg2">The second value.</param>
365 /// <returns>The vector containing the result of the subtraction.</returns>
366 /// <since_tizen> 3 </since_tizen>
367 public static Vector2 operator -(Vector2 arg1, Vector2 arg2)
369 return arg1?.Subtract(arg2);
373 /// The unary negation operator.
375 /// <param name="arg1">The target value.</param>
376 /// <returns>The vector containing the negation.</returns>
377 /// <since_tizen> 3 </since_tizen>
378 public static Vector2 operator -(Vector2 arg1)
380 return arg1?.Subtract();
384 /// The multiplication operator.
386 /// <param name="arg1">The first value.</param>
387 /// <param name="arg2">The second value.</param>
388 /// <returns>The vector containing the result of the multiplication.</returns>
389 /// <since_tizen> 3 </since_tizen>
390 public static Vector2 operator *(Vector2 arg1, Vector2 arg2)
392 return arg1?.Multiply(arg2);
396 /// Th multiplication operator.
398 /// <param name="arg1">The first value.</param>
399 /// <param name="arg2">The float value to scale the vector.</param>
400 /// <returns>The vector containing the result of the scaling.</returns>
401 /// <since_tizen> 3 </since_tizen>
402 public static Vector2 operator *(Vector2 arg1, float arg2)
404 return arg1?.Multiply(arg2);
408 /// The division operator.
410 /// <param name="arg1">The first value.</param>
411 /// <param name="arg2">The second value.</param>
412 /// <returns>The vector containing the result of the division.</returns>
413 /// <since_tizen> 3 </since_tizen>
414 public static Vector2 operator /(Vector2 arg1, Vector2 arg2)
416 return arg1?.Divide(arg2);
420 /// Th division operator.
422 /// <param name="arg1">The first value.</param>
423 /// <param name="arg2">The float value to scale the vector by.</param>
424 /// <returns>The vector containing the result of the scaling.</returns>
425 /// <since_tizen> 3 </since_tizen>
426 public static Vector2 operator /(Vector2 arg1, float arg2)
428 return arg1?.Divide(arg2);
432 /// Determines whether the specified object is equal to the current object.
434 /// <param name="obj">The object to compare with the current object.</param>
435 /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
436 public override bool Equals(System.Object obj)
438 Vector2 vector2 = obj as Vector2;
440 if (X == vector2?.X && Y == vector2?.Y)
448 /// Gets the hash code of this Vector2.
450 /// <returns>The Hash Code.</returns>
451 /// <since_tizen> 6 </since_tizen>
452 public override int GetHashCode()
454 return SwigCPtr.Handle.GetHashCode();
458 /// Returns the length of the vector.
460 /// <returns>The length of the vector.</returns>
461 /// <since_tizen> 3 </since_tizen>
462 public float Length()
464 float ret = Interop.Vector2.Length(SwigCPtr);
465 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
470 /// Returns the length of the vector squared.<br />
471 /// This is more efficient than Length() for threshold
472 /// testing as it avoids the use of a square root.<br />
474 /// <returns>The length of the vector squared</returns>
475 /// <since_tizen> 3 </since_tizen>
476 public float LengthSquared()
478 float ret = Interop.Vector2.LengthSquared(SwigCPtr);
479 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
484 /// Sets the vector to be the unit length, whilst maintaining its direction.
486 /// <since_tizen> 3 </since_tizen>
487 public void Normalize()
489 Interop.Vector2.Normalize(SwigCPtr);
490 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
494 [EditorBrowsable(EditorBrowsableState.Never)]
495 public object Clone() => new Vector2(this);
498 /// Clamps the vector between minimum and maximum vectors.
500 /// <param name="min">The minimum vector.</param>
501 /// <param name="max">The maximum vector.</param>
502 /// <since_tizen> 3 </since_tizen>
503 public void Clamp(Vector2 min, Vector2 max)
505 Interop.Vector2.Clamp(SwigCPtr, Vector2.getCPtr(min), Vector2.getCPtr(max));
506 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
509 internal static Vector2 GetVector2FromPtr(global::System.IntPtr cPtr)
511 Vector2 ret = new Vector2(cPtr, false);
512 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
516 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Vector2 obj)
518 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
521 internal SWIGTYPE_p_float AsFloat()
523 global::System.IntPtr cPtr = Interop.Vector2.AsFloat(SwigCPtr);
524 SWIGTYPE_p_float ret = (cPtr == global::System.IntPtr.Zero) ? null : new SWIGTYPE_p_float(cPtr);
525 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
529 /// This will not be public opened.
530 [EditorBrowsable(EditorBrowsableState.Never)]
531 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
533 Interop.Vector2.DeleteVector2(swigCPtr);
536 private Vector2 Add(Vector2 rhs)
538 Vector2 ret = new Vector2(Interop.Vector2.Add(SwigCPtr, Vector2.getCPtr(rhs)), true);
539 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
543 private Vector2 AddAssign(Vector2 rhs)
545 Vector2 ret = new Vector2(Interop.Vector2.AddAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
546 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
550 private Vector2 Subtract(Vector2 rhs)
552 Vector2 ret = new Vector2(Interop.Vector2.Subtract(SwigCPtr, Vector2.getCPtr(rhs)), true);
553 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
557 private Vector2 SubtractAssign(Vector2 rhs)
559 Vector2 ret = new Vector2(Interop.Vector2.SubtractAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
560 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
564 private Vector2 Multiply(Vector2 rhs)
566 Vector2 ret = new Vector2(Interop.Vector2.Multiply(SwigCPtr, Vector2.getCPtr(rhs)), true);
567 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
571 private Vector2 Multiply(float rhs)
573 Vector2 ret = new Vector2(Interop.Vector2.Multiply(SwigCPtr, rhs), true);
574 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
578 private Vector2 MultiplyAssign(Vector2 rhs)
580 Vector2 ret = new Vector2(Interop.Vector2.MultiplyAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
581 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
585 private Vector2 MultiplyAssign(float rhs)
587 Vector2 ret = new Vector2(Interop.Vector2.MultiplyAssign(SwigCPtr, rhs), false);
588 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
592 private Vector2 Divide(Vector2 rhs)
594 Vector2 ret = new Vector2(Interop.Vector2.Divide(SwigCPtr, Vector2.getCPtr(rhs)), true);
595 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
599 private Vector2 Divide(float rhs)
601 Vector2 ret = new Vector2(Interop.Vector2.Divide(SwigCPtr, rhs), true);
602 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
606 private Vector2 DivideAssign(Vector2 rhs)
608 Vector2 ret = new Vector2(Interop.Vector2.DivideAssign(SwigCPtr, Vector2.getCPtr(rhs)), false);
609 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
613 private Vector2 DivideAssign(float rhs)
615 Vector2 ret = new Vector2(Interop.Vector2.DivideAssign(SwigCPtr, rhs), false);
616 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
620 private Vector2 Subtract()
622 Vector2 ret = new Vector2(Interop.Vector2.Subtract(SwigCPtr), true);
623 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
627 private bool EqualTo(Vector2 rhs)
629 bool ret = Interop.Vector2.EqualTo(SwigCPtr, Vector2.getCPtr(rhs));
630 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
634 private bool NotEqualTo(Vector2 rhs)
636 bool ret = Interop.Vector2.NotEqualTo(SwigCPtr, Vector2.getCPtr(rhs));
637 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
641 private float ValueOfIndex(uint index)
643 float ret = Interop.Vector2.ValueOfIndex(SwigCPtr, index);
644 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();