2 * Copyright (c) 2018 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.Globalization;
19 using Tizen.NUI.Binding;
25 /// Position2D is a two-dimensional vector.
27 /// <since_tizen> 3 </since_tizen>
28 [Tizen.NUI.Binding.TypeConverter(typeof(Position2DTypeConverter))]
29 public class Position2D : global::System.IDisposable
34 /// <since_tizen> 3 </since_tizen>
35 protected bool swigCMemOwn;
37 /// A Flat to check if it is already disposed.
39 /// <since_tizen> 3 </since_tizen>
40 protected bool disposed = false;
42 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
43 //A Flag to check who called Dispose(). (By User or DisposeQueue)
44 private bool isDisposeQueued = false;
46 private Position2DChangedCallback callback = null;
51 /// <since_tizen> 3 </since_tizen>
52 public Position2D() : this(NDalicPINVOKE.new_Vector2__SWIG_0(), true)
54 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
60 /// <param name="position">Position to create this vector from</param>
61 /// <since_tizen> 3 </since_tizen>
62 public Position2D(Position position) : this(NDalicPINVOKE.new_Vector2__SWIG_3(Position.getCPtr(position)), true)
64 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
70 /// <param name="x">x component</param>
71 /// <param name="y">y component</param>
72 /// <since_tizen> 3 </since_tizen>
73 public Position2D(int x, int y) : this(NDalicPINVOKE.new_Vector2__SWIG_1((float)x, (float)y), true)
75 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
78 internal Position2D(global::System.IntPtr cPtr, bool cMemoryOwn)
80 swigCMemOwn = cMemoryOwn;
81 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
84 internal Position2D(Position2DChangedCallback cb, int x, int y) : this(NDalicPINVOKE.new_Vector2__SWIG_1((float)x, (float)y), true)
87 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
93 /// <since_tizen> 3 </since_tizen>
98 isDisposeQueued = true;
99 DisposeQueue.Instance.Add(this);
103 internal delegate void Position2DChangedCallback(int x, int y);
108 /// <since_tizen> 3 </since_tizen>
113 NDalicPINVOKE.Vector2_X_set(swigCPtr, (float)value);
114 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
116 callback?.Invoke(X, Y);
120 float ret = NDalicPINVOKE.Vector2_X_get(swigCPtr);
121 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
129 /// <since_tizen> 3 </since_tizen>
134 NDalicPINVOKE.Vector2_Y_set(swigCPtr, (float)value);
135 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
137 callback?.Invoke(X, Y);
141 float ret = NDalicPINVOKE.Vector2_Y_get(swigCPtr);
142 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
148 /// The const array subscript operator overload. Should be 0, or 1.
150 /// <param name="index">The subscript index.</param>
151 /// <returns>The float at the given index.</returns>
152 /// <since_tizen> 3 </since_tizen>
153 public float this[uint index]
157 return ValueOfIndex(index);
162 /// Convert a string to Position2D.
164 /// <param name="value">The string to convert.</param>
165 /// <returns>The converted value.</returns>
166 static public Position2D ConvertFromString(System.String value)
170 string[] parts = value.Split(',');
171 if (parts.Length == 2)
173 return new Position2D(int.Parse(parts[0].Trim()), int.Parse(parts[1].Trim()));
177 throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Position2D)}");
181 /// Constructor a Position2D from a stirng.
183 public static implicit operator Position2D(System.String value)
185 return ConvertFromString(value);
191 /// <since_tizen> 3 </since_tizen>
192 public void Dispose()
194 //Throw excpetion if Dispose() is called in separate thread.
195 if (!Window.IsInstalled())
197 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
202 Dispose(DisposeTypes.Implicit);
206 Dispose(DisposeTypes.Explicit);
207 System.GC.SuppressFinalize(this);
212 /// The addition operator.
214 /// <param name="arg1">The vector to add.</param>
215 /// <param name="arg2">The vector to add.</param>
216 /// <returns>The vector containing the result of the addition.</returns>
217 /// <since_tizen> 3 </since_tizen>
218 public static Position2D operator +(Position2D arg1, Position2D arg2)
220 return arg1.Add(arg2);
224 /// The subtraction operator.
226 /// <param name="arg1">The vector to subtract.</param>
227 /// <param name="arg2">The vector to subtract.</param>
228 /// <returns>The vector containing the result of the subtraction.</returns>
229 /// <since_tizen> 3 </since_tizen>
230 public static Position2D operator -(Position2D arg1, Position2D arg2)
232 return arg1.Subtract(arg2);
236 /// The unary negation operator.
238 /// <param name="arg1">The vector to negate.</param>
239 /// <returns>The vector containing the negation.</returns>
240 /// <since_tizen> 3 </since_tizen>
241 public static Position2D operator -(Position2D arg1)
243 return arg1.Subtract();
247 /// The multiplication operator.
249 /// <param name="arg1">The vector to multiply.</param>
250 /// <param name="arg2">The vector to multiply.</param>
251 /// <returns>The vector containing the result of the multiplication.</returns>
252 /// <since_tizen> 3 </since_tizen>
253 public static Position2D operator *(Position2D arg1, Position2D arg2)
255 return arg1.Multiply(arg2);
259 /// The multiplication operator.
261 /// <param name="arg1">The vector to multiply.</param>
262 /// <param name="arg2">The integer value to scale the vector.</param>
263 /// <returns>The vector containing the result of the multiplication.</returns>
264 /// <since_tizen> 3 </since_tizen>
265 public static Position2D operator *(Position2D arg1, int arg2)
267 return arg1.Multiply(arg2);
271 /// The division operator.
273 /// <param name="arg1">The vector to divide.</param>
274 /// <param name="arg2">The vector to divide.</param>
275 /// <returns>The vector containing the result of the division.</returns>
276 /// <since_tizen> 3 </since_tizen>
277 public static Position2D operator /(Position2D arg1, Position2D arg2)
279 return arg1.Divide(arg2);
283 /// The division operator.
285 /// <param name="arg1">The vector to divide.</param>
286 /// <param name="arg2">The integer value to scale the vector by.</param>
287 /// <returns>The vector containing the result of the division.</returns>
288 /// <since_tizen> 3 </since_tizen>
289 public static Position2D operator /(Position2D arg1, int arg2)
291 return arg1.Divide(arg2);
295 /// Determines whether the specified object is equal to the current object.
297 /// <param name="obj">The object to compare with the current object.</param>
298 /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
299 public override bool Equals(System.Object obj)
301 Position2D position2D = obj as Position2D;
303 if (X == position2D?.X && Y == position2D?.Y)
311 /// Gets the the hash code of this Position2D.
313 /// <returns>The Hash Code.</returns>
314 /// <since_tizen> 5 </since_tizen>
315 public override int GetHashCode()
317 return X.GetHashCode();
321 /// Compares if the rhs is equal to.
323 /// <param name="rhs">The vector to compare</param>
324 /// <returns>Returns true if the two vectors are equal, otherwise false</returns>
325 /// <since_tizen> 3 </since_tizen>
326 public bool EqualTo(Position2D rhs)
328 bool ret = NDalicPINVOKE.Vector2_EqualTo(swigCPtr, Position2D.getCPtr(rhs));
329 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
334 /// Compares if the rhs is not equal to.
336 /// <param name="rhs">The vector to compare.</param>
337 /// <returns>Returns true if the two vectors are not equal, otherwise false.</returns>
338 /// <since_tizen> 3 </since_tizen>
339 public bool NotEqualTo(Position2D rhs)
341 bool ret = NDalicPINVOKE.Vector2_NotEqualTo(swigCPtr, Position2D.getCPtr(rhs));
342 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
347 /// Converts a Position2D instance to a Vector2 instance.
349 /// <param name="position2d">An object of the Position2D type.</param>
350 /// <returns>return an object of the Vector2 type</returns>
351 /// <since_tizen> 3 </since_tizen>
352 public static implicit operator Vector2(Position2D position2d)
354 return new Vector2((float)position2d.X, (float)position2d.Y);
358 /// Converts a Vector2 instance to a Position2D instance.
360 /// <param name="vec">An object of the Vector2 type.</param>
361 /// <returns>return an object of the Position2D type</returns>
362 /// <since_tizen> 3 </since_tizen>
363 public static implicit operator Position2D(Vector2 vec)
365 return new Position2D((int)vec.X, (int)vec.Y);
368 internal static Position2D GetPosition2DFromPtr(global::System.IntPtr cPtr)
370 Position2D ret = new Position2D(cPtr, false);
371 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
375 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Position2D obj)
377 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
383 /// <param name="type">The dispose type.</param>
384 /// <since_tizen> 3 </since_tizen>
385 protected virtual void Dispose(DisposeTypes type)
392 if (type == DisposeTypes.Explicit)
395 //Release your own managed resources here.
396 //You should release all of your own disposable objects here.
399 //Release your own unmanaged resources here.
400 //You should not access any managed member here except static instance.
401 //because the execution order of Finalizes is non-deterministic.
403 if (swigCPtr.Handle != global::System.IntPtr.Zero)
408 NDalicPINVOKE.delete_Vector2(swigCPtr);
410 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
415 private Position2D Add(Position2D rhs)
417 Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Add(swigCPtr, Position2D.getCPtr(rhs)), true);
418 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
422 private Position2D Subtract(Position2D rhs)
424 Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Subtract__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
425 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
430 private Position2D Multiply(Position2D rhs)
432 Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Multiply__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
433 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
437 private Position2D Multiply(int rhs)
439 Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Multiply__SWIG_1(swigCPtr, (float)rhs), true);
440 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
445 private Position2D Divide(Position2D rhs)
447 Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Divide__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
448 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
452 private Position2D Divide(int rhs)
454 Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Divide__SWIG_1(swigCPtr, (float)rhs), true);
455 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
459 private Position2D Subtract()
461 Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Subtract__SWIG_1(swigCPtr), true);
462 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
466 private int ValueOfIndex(uint index)
468 int ret = (int)NDalicPINVOKE.Vector2_ValueOfIndex__SWIG_0(swigCPtr, index);
469 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();