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;
24 /// Position2D is a two-dimensional vector.
26 /// <since_tizen> 3 </since_tizen>
27 [Tizen.NUI.Binding.TypeConverter(typeof(Position2DTypeConverter))]
28 public class Position2D : Disposable, ICloneable
30 private Position2DChangedCallback callback = null;
36 /// Position2D and Position are implicitly converted to each other, so these are compatible and can be replaced without any type casting. <br />
37 /// For example, the followings are possible. <br />
38 /// view.Position2D = new Position(10.0f, 10.0f, 10.0f); // be aware that here the z value(10.0f) will be lost. <br />
39 /// view.Position = new Position2D(10, 10); // be aware that here the z value is 0.0f by default. <br />
41 /// <since_tizen> 3 </since_tizen>
42 public Position2D() : this(Interop.Vector2.NewVector2(), true)
44 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
50 /// <param name="position">Position to create this vector from</param>
51 /// <since_tizen> 3 </since_tizen>
52 public Position2D(Position position) : this(Interop.Vector2.NewVector2WithVector3(Position.getCPtr(position)), true)
54 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
60 /// <param name="x">x component</param>
61 /// <param name="y">y component</param>
63 /// Position2D and Position are implicitly converted to each other, so these are compatible and can be replaced without any type casting. <br />
64 /// For example, the followings are possible. <br />
65 /// view.Position2D = new Position(10.0f, 10.0f, 10.0f); // be aware that here the z value(10.0f) will be lost. <br />
66 /// view.Position = new Position2D(10, 10); // be aware that here the z value is 0.0f by default. <br />
68 /// <since_tizen> 3 </since_tizen>
69 public Position2D(int x, int y) : this(Interop.Vector2.NewVector2((float)x, (float)y), true)
71 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
74 internal Position2D(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
78 internal Position2D(Position2DChangedCallback cb, int x, int y) : this(Interop.Vector2.NewVector2((float)x, (float)y), true)
81 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
84 internal delegate void Position2DChangedCallback(int x, int y);
90 /// The setter is deprecated in API8 and will be removed in API10. Please use new Position2D(...) constructor.
93 /// // DO NOT use like the followings!
94 /// Position2D position2d = new Position2D();
96 /// // Please USE like this
98 /// Position2D position2d = new Position2D(x, y);
100 /// <since_tizen> 3 </since_tizen>
103 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Position2D(...) constructor")]
106 Interop.Vector2.XSet(SwigCPtr, (float)value);
107 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
109 callback?.Invoke(value, Y);
113 float ret = Interop.Vector2.XGet(SwigCPtr);
114 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
123 /// The setter is deprecated in API8 and will be removed in API10. Please use new Position2D(...) constructor.
126 /// // DO NOT use like the followings!
127 /// Position2D position2d = new Position2D();
128 /// position2d.Y = 2;
129 /// // Please USE like this
130 /// int x = 1, y = 2;
131 /// Position2D position2d = new Position2D(x, y);
133 /// <since_tizen> 3 </since_tizen>
136 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Position2D(...) constructor")]
139 Interop.Vector2.YSet(SwigCPtr, (float)value);
140 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
142 callback?.Invoke(X, value);
146 float ret = Interop.Vector2.YGet(SwigCPtr);
147 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
153 /// The const array subscript operator overload. Should be 0, or 1.
155 /// <param name="index">The subscript index.</param>
156 /// <returns>The float at the given index.</returns>
157 /// <since_tizen> 3 </since_tizen>
158 public float this[uint index]
162 return ValueOfIndex(index);
167 /// Convert a string to Position2D.
169 /// <param name="value">The string to convert.</param>
170 /// <returns>The converted value.</returns>
171 static public Position2D ConvertFromString(System.String value)
175 string[] parts = value.Split(',');
176 if (parts.Length == 2)
178 int x = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[0].Trim());
179 int y = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[1].Trim());
180 return new Position2D(x, y);
184 throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Position2D)}");
188 /// Constructor a Position2D from a string.
190 public static implicit operator Position2D(System.String value)
192 return ConvertFromString(value);
196 /// The addition operator.
198 /// <param name="arg1">The vector to add.</param>
199 /// <param name="arg2">The vector to add.</param>
200 /// <returns>The vector containing the result of the addition.</returns>
201 /// <since_tizen> 3 </since_tizen>
202 public static Position2D operator +(Position2D arg1, Position2D arg2)
204 return arg1?.Add(arg2);
208 /// The subtraction operator.
210 /// <param name="arg1">The vector to subtract.</param>
211 /// <param name="arg2">The vector to subtract.</param>
212 /// <returns>The vector containing the result of the subtraction.</returns>
213 /// <since_tizen> 3 </since_tizen>
214 public static Position2D operator -(Position2D arg1, Position2D arg2)
216 return arg1?.Subtract(arg2);
220 /// The unary negation operator.
222 /// <param name="arg1">The vector to negate.</param>
223 /// <returns>The vector containing the negation.</returns>
224 /// <since_tizen> 3 </since_tizen>
225 public static Position2D operator -(Position2D arg1)
227 return arg1?.Subtract();
231 /// The multiplication operator.
233 /// <param name="arg1">The vector to multiply.</param>
234 /// <param name="arg2">The vector to multiply.</param>
235 /// <returns>The vector containing the result of the multiplication.</returns>
236 /// <since_tizen> 3 </since_tizen>
237 public static Position2D operator *(Position2D arg1, Position2D arg2)
239 return arg1?.Multiply(arg2);
243 /// The multiplication operator.
245 /// <param name="arg1">The vector to multiply.</param>
246 /// <param name="arg2">The integer value to scale the vector.</param>
247 /// <returns>The vector containing the result of the multiplication.</returns>
248 /// <since_tizen> 3 </since_tizen>
249 public static Position2D operator *(Position2D arg1, int arg2)
251 return arg1?.Multiply(arg2);
255 /// The division operator.
257 /// <param name="arg1">The vector to divide.</param>
258 /// <param name="arg2">The vector to divide.</param>
259 /// <returns>The vector containing the result of the division.</returns>
260 /// <since_tizen> 3 </since_tizen>
261 public static Position2D operator /(Position2D arg1, Position2D arg2)
263 return arg1?.Divide(arg2);
267 /// The division operator.
269 /// <param name="arg1">The vector to divide.</param>
270 /// <param name="arg2">The integer value to scale the vector by.</param>
271 /// <returns>The vector containing the result of the division.</returns>
272 /// <since_tizen> 3 </since_tizen>
273 public static Position2D operator /(Position2D arg1, int arg2)
275 return arg1?.Divide(arg2);
279 /// Determines whether the specified object is equal to the current object.
281 /// <param name="obj">The object to compare with the current object.</param>
282 /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
283 public override bool Equals(System.Object obj)
285 Position2D position2D = obj as Position2D;
287 if (X == position2D?.X && Y == position2D?.Y)
295 /// Gets the hash code of this Position2D.
297 /// <returns>The Hash Code.</returns>
298 /// <since_tizen> 6 </since_tizen>
299 public override int GetHashCode()
301 return SwigCPtr.Handle.GetHashCode();
305 /// Compares if the rhs is equal to.
307 /// <param name="rhs">The vector to compare</param>
308 /// <returns>Returns true if the two vectors are equal, otherwise false</returns>
309 /// <since_tizen> 3 </since_tizen>
310 public bool EqualTo(Position2D rhs)
312 bool ret = Interop.Vector2.EqualTo(SwigCPtr, Position2D.getCPtr(rhs));
313 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
318 /// Compares if the rhs is not equal to.
320 /// <param name="rhs">The vector to compare.</param>
321 /// <returns>Returns true if the two vectors are not equal, otherwise false.</returns>
322 /// <since_tizen> 3 </since_tizen>
323 public bool NotEqualTo(Position2D rhs)
325 bool ret = Interop.Vector2.NotEqualTo(SwigCPtr, Position2D.getCPtr(rhs));
326 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
331 /// Converts a Position2D instance to a Vector2 instance.
333 /// <param name="position2d">An object of the Position2D type.</param>
334 /// <returns>return an object of the Vector2 type</returns>
335 /// <since_tizen> 3 </since_tizen>
336 public static implicit operator Vector2(Position2D position2d)
338 if (position2d == null)
342 return new Vector2(position2d.X, position2d.Y);
346 /// Converts a Vector2 instance to a Position2D instance.
348 /// <param name="vec">An object of the Vector2 type.</param>
349 /// <returns>return an object of the Position2D type</returns>
350 /// <since_tizen> 3 </since_tizen>
351 public static implicit operator Position2D(Vector2 vec)
357 return new Position2D((int)vec.X, (int)vec.Y);
361 /// Implicit type cast operator, Position to Position2D
363 /// <param name="position">The object of Position type.</param>
364 /// <since_tizen> none </since_tizen>
365 /// This will be public opened in tizen_next by ACR.
366 [EditorBrowsable(EditorBrowsableState.Never)]
367 public static implicit operator Position2D(Position position)
369 if (position == null)
373 return new Position2D((int)position.X, (int)position.Y);
377 [EditorBrowsable(EditorBrowsableState.Never)]
378 public object Clone() => new Position2D(this);
380 internal static Position2D GetPosition2DFromPtr(global::System.IntPtr cPtr)
382 Position2D ret = new Position2D(cPtr, false);
383 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
387 /// This will not be public opened.
388 [EditorBrowsable(EditorBrowsableState.Never)]
389 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
391 Interop.Vector2.DeleteVector2(swigCPtr);
394 private Position2D Add(Position2D rhs)
396 Position2D ret = new Position2D(Interop.Vector2.Add(SwigCPtr, Position2D.getCPtr(rhs)), true);
397 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
401 private Position2D Subtract(Position2D rhs)
403 Position2D ret = new Position2D(Interop.Vector2.Subtract(SwigCPtr, Position2D.getCPtr(rhs)), true);
404 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
408 private Position2D Multiply(Position2D rhs)
410 Position2D ret = new Position2D(Interop.Vector2.Multiply(SwigCPtr, Position2D.getCPtr(rhs)), true);
411 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
415 private Position2D Multiply(int rhs)
417 Position2D ret = new Position2D(Interop.Vector2.Multiply(SwigCPtr, (float)rhs), true);
418 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
422 private Position2D Divide(Position2D rhs)
424 Position2D ret = new Position2D(Interop.Vector2.Divide(SwigCPtr, Position2D.getCPtr(rhs)), true);
425 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
429 private Position2D Divide(int rhs)
431 Position2D ret = new Position2D(Interop.Vector2.Divide(SwigCPtr, (float)rhs), true);
432 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
436 private Position2D Subtract()
438 Position2D ret = new Position2D(Interop.Vector2.Subtract(SwigCPtr), true);
439 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
443 private int ValueOfIndex(uint index)
445 int ret = (int)Interop.Vector2.ValueOfIndex(SwigCPtr, index);
446 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();