[NUI][XamlBinding] Unifying the delimiter for TypeConverter.
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / VectorTypeConverter.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 using System;
19 using System.Linq;
20 using System.Reflection;
21 using System.Globalization;
22
23 using Tizen.NUI;
24 using System.ComponentModel;
25
26 namespace Tizen.NUI.Binding
27 {
28     //Internal used, will never open
29     [EditorBrowsable(EditorBrowsableState.Never)]
30     public class Vector2TypeConverter : TypeConverter
31     {
32         //Internal used, will never open
33         [EditorBrowsable(EditorBrowsableState.Never)]
34         public override object ConvertFromInvariantString(string value)
35         {
36             if (value != null)
37             {
38                 return FromString(value);
39             }
40
41             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Vector2)}");
42         }
43
44         //Internal used, will never open
45         [EditorBrowsable(EditorBrowsableState.Never)]
46         public override string ConvertToString(object value)
47         {
48             return ToString(value as Vector2);
49         }
50
51         internal static Vector2 FromString(string value)
52         {
53             var parts = value.Split(TypeConverter.UnifiedDelimiter);
54
55             if (parts.Length == 2)
56             {
57                 return new Vector2(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture), Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture));
58             }
59             else if (parts.Length == 1)
60             {
61                 var x = Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture);
62                 return new Vector2(x, x);
63             }
64
65             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Vector2)}");
66         }
67
68         internal static string ToString(Vector2 value)
69         {
70             if (null != value)
71             {
72                 return value.X.ToString() + TypeConverter.UnifiedDelimiter + value.Y.ToString();
73             }
74             else
75             {
76                 return null;
77             }
78         }
79     }
80
81     //Internal used, will never open
82     [EditorBrowsable(EditorBrowsableState.Never)]
83     public class Vector3TypeConverter : TypeConverter
84     {
85         //Internal used, will never open
86         [EditorBrowsable(EditorBrowsableState.Never)]
87         public override object ConvertFromInvariantString(string value)
88         {
89             if (value != null)
90             {
91                 string[] parts = value.Split(TypeConverter.UnifiedDelimiter);
92                 if (parts.Length == 3)
93                 {
94                     return new Vector3(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
95                                        Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
96                                        Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture));
97                 }
98                 else if (parts.Length == 2)
99                 {
100                     var vector2 = new Vector2(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
101                                               Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture));
102                     var vector3 = new Vector3(vector2);
103                     vector2.Dispose();
104                     return vector3;
105                 }
106                 else if (parts.Length == 1)
107                 {
108                     var floatValue = Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture);
109                     return new Vector3(floatValue, floatValue, floatValue);
110                 }
111             }
112
113             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Vector3)}");
114         }
115
116         //Internal used, will never open
117         [EditorBrowsable(EditorBrowsableState.Never)]
118         public override string ConvertToString(object value)
119         {
120             Vector3 vector = value as Vector3;
121             if (null != vector)
122             {
123                 return vector.X.ToString() + TypeConverter.UnifiedDelimiter + vector.Y.ToString() + TypeConverter.UnifiedDelimiter + vector.Z.ToString();
124             }
125             else
126             {
127                 return null;
128             }
129         }
130     }
131
132     //Internal used, will never open
133     [EditorBrowsable(EditorBrowsableState.Never)]
134     public class Vector4TypeConverter : TypeConverter
135     {
136         //Internal used, will never open
137         [EditorBrowsable(EditorBrowsableState.Never)]
138         public override object ConvertFromInvariantString(string value)
139         {
140             if (value != null)
141             {
142                 string[] parts = value.Split(TypeConverter.UnifiedDelimiter);
143                 if (parts.Length == 4)
144                 {
145                     return new Vector4(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
146                                        Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
147                                        Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture),
148                                        Single.Parse(parts[3].Trim(), CultureInfo.InvariantCulture));
149                 }
150                 else if (parts.Length == 3)
151                 {
152                     var vector3 = new Vector3(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
153                                               Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
154                                               Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture));
155                     var vector4 = new Vector4(vector3);
156                     vector3.Dispose();
157                     return vector4;
158                 }
159                 else if (parts.Length == 2)
160                 {
161                     var vector2 = new Vector2(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
162                                               Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture));
163                     var vector4 = new Vector4(vector2);
164                     vector2.Dispose();
165                     return vector4;
166                 }
167                 else if (parts.Length == 1)
168                 {
169                     Vector4 ret = Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture);
170                     return ret;
171                 }
172             }
173
174             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Vector4)}");
175         }
176
177         //Internal used, will never open
178         [EditorBrowsable(EditorBrowsableState.Never)]
179         public override string ConvertToString(object value)
180         {
181             Vector4 vector = value as Vector4;
182             if (null != vector)
183             {
184                 return vector.X.ToString() + TypeConverter.UnifiedDelimiter + vector.Y.ToString() + TypeConverter.UnifiedDelimiter + vector.Z.ToString() + TypeConverter.UnifiedDelimiter + vector.W.ToString();
185             }
186             else
187             {
188                 return null;
189             }
190         }
191     }
192
193     //Internal used, will never open
194     [EditorBrowsable(EditorBrowsableState.Never)]
195     public class RelativeVector2TypeConverter : TypeConverter
196     {
197         //Internal used, will never open
198         [EditorBrowsable(EditorBrowsableState.Never)]
199         public override object ConvertFromInvariantString(string value)
200         {
201             if (value != null)
202             {
203                 string[] parts = value.Split(TypeConverter.UnifiedDelimiter);
204                 if (parts.Length == 2)
205                 {
206                     return new RelativeVector2(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
207                                                Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture));
208                 }
209                 else if (parts.Length == 1)
210                 {
211                     var x = Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture);
212                     return new RelativeVector2(x, x);
213                 }
214             }
215
216             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(RelativeVector2)}");
217         }
218
219         //Internal used, will never open
220         [EditorBrowsable(EditorBrowsableState.Never)]
221         public override string ConvertToString(object value)
222         {
223             RelativeVector2 vector = value as RelativeVector2;
224             if (null != vector)
225             {
226                 return vector.X.ToString() + TypeConverter.UnifiedDelimiter + vector.Y.ToString();
227             }
228             else
229             {
230                 return null;
231             }
232         }
233     }
234
235     //Internal used, will never open
236     [EditorBrowsable(EditorBrowsableState.Never)]
237     public class RelativeVector3TypeConverter : TypeConverter
238     {
239         //Internal used, will never open
240         [EditorBrowsable(EditorBrowsableState.Never)]
241         public override object ConvertFromInvariantString(string value)
242         {
243             if (value != null)
244             {
245                 string[] parts = value.Split(TypeConverter.UnifiedDelimiter);
246                 if (parts.Length == 3)
247                 {
248                     return new RelativeVector3(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
249                                                Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
250                                                Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture));
251                 }
252                 else if (parts.Length == 2)
253                 {
254                     var vector2 = new RelativeVector2(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
255                                                       Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture));
256                     var vector3 = new RelativeVector3(vector2);
257                     vector2.Dispose();
258                     return vector3;
259                 }
260                 else if (parts.Length == 1)
261                 {
262                     var x = Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture);
263                     return new RelativeVector3(x, x, x);
264                 }
265             }
266
267             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(RelativeVector3)}");
268         }
269
270         //Internal used, will never open
271         [EditorBrowsable(EditorBrowsableState.Never)]
272         public override string ConvertToString(object value)
273         {
274             RelativeVector3 vector = value as RelativeVector3;
275             if (null != vector)
276             {
277                 return vector.X.ToString() + TypeConverter.UnifiedDelimiter + vector.Y.ToString() + TypeConverter.UnifiedDelimiter + vector.Z.ToString();
278             }
279             else
280             {
281                 return null;
282             }
283         }
284     }
285
286     //Internal used, will never open
287     [EditorBrowsable(EditorBrowsableState.Never)]
288     public class RelativeVector4TypeConverter : TypeConverter
289     {
290         //Internal used, will never open
291         [EditorBrowsable(EditorBrowsableState.Never)]
292         public override object ConvertFromInvariantString(string value)
293         {
294             if (value != null)
295             {
296                 string[] parts = value.Split(TypeConverter.UnifiedDelimiter);
297                 if (parts.Length == 4)
298                 {
299                     return new RelativeVector4(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
300                                                Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
301                                                Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture),
302                                                Single.Parse(parts[3].Trim(), CultureInfo.InvariantCulture));
303                 }
304                 else if (parts.Length == 3)
305                 {
306                     var vector3 = new RelativeVector3(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
307                                                       Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
308                                                       Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture));
309                     var vector4 = new RelativeVector4(vector3);
310                     vector3.Dispose();
311                     return vector4;
312                 }
313                 else if (parts.Length == 2)
314                 {
315                     var vector2 = new RelativeVector2(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
316                                                       Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture));
317                     var vector4 = new RelativeVector4(vector2);
318                     vector2.Dispose();
319                     return vector4;
320                 }
321                 else if (parts.Length == 1)
322                 {
323                     var x = Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture);
324                     return new RelativeVector4(x, x, x, x);
325                 }
326             }
327
328             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(RelativeVector4)}");
329         }
330
331         //Internal used, will never open
332         [EditorBrowsable(EditorBrowsableState.Never)]
333         public override string ConvertToString(object value)
334         {
335             RelativeVector4 vector = value as RelativeVector4;
336             if (null != vector)
337             {
338                 return vector.X.ToString() + TypeConverter.UnifiedDelimiter + vector.Y.ToString() + TypeConverter.UnifiedDelimiter + vector.Z.ToString() + TypeConverter.UnifiedDelimiter + vector.W.ToString();
339             }
340             else
341             {
342                 return null;
343             }
344         }
345     }
346 }