[NUI]public the converter for Xaml (#2837)
[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(',');
54
55             if (parts.Length != 2)
56             {
57                 throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Vector2)}");
58             }
59
60             return new Vector2(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture), Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture));
61         }
62
63         internal static string ToString(Vector2 value)
64         {
65             if (null != value)
66             {
67                 return value.X.ToString() + " " + value.Y.ToString();
68             }
69             else
70             {
71                 return null;
72             }
73         }
74     }
75
76     //Internal used, will never open
77     [EditorBrowsable(EditorBrowsableState.Never)]
78     public class Vector3TypeConverter : TypeConverter
79     {
80         //Internal used, will never open
81         [EditorBrowsable(EditorBrowsableState.Never)]
82         public override object ConvertFromInvariantString(string value)
83         {
84             if (value != null)
85             {
86                 string[] parts = value.Split(',');
87                 if (parts.Length == 3)
88                 {
89                     return new Vector3(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
90                                        Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
91                                        Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture));
92                 }
93             }
94
95             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Vector3)}");
96         }
97
98         //Internal used, will never open
99         [EditorBrowsable(EditorBrowsableState.Never)]
100         public override string ConvertToString(object value)
101         {
102             Vector3 vector = value as Vector3;
103             if (null != vector)
104             {
105                 return vector.X.ToString() + " " + vector.Y.ToString() + " " + vector.Z.ToString();
106             }
107             else
108             {
109                 return null;
110             }
111         }
112     }
113
114     //Internal used, will never open
115     [EditorBrowsable(EditorBrowsableState.Never)]
116     public class Vector4TypeConverter : TypeConverter
117     {
118         //Internal used, will never open
119         [EditorBrowsable(EditorBrowsableState.Never)]
120         public override object ConvertFromInvariantString(string value)
121         {
122             if (value != null)
123             {
124                 string[] parts = value.Split(',');
125                 if (parts.Length == 4)
126                 {
127                     return new Vector4(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
128                                        Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
129                                        Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture),
130                                        Single.Parse(parts[3].Trim(), CultureInfo.InvariantCulture));
131                 }
132             }
133
134             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Vector4)}");
135         }
136
137         //Internal used, will never open
138         [EditorBrowsable(EditorBrowsableState.Never)]
139         public override string ConvertToString(object value)
140         {
141             Vector4 vector = value as Vector4;
142             if (null != vector)
143             {
144                 return vector.X.ToString() + " " + vector.Y.ToString() + " " + vector.Z.ToString() + " " + vector.W.ToString();
145             }
146             else
147             {
148                 return null;
149             }
150         }
151     }
152
153     //Internal used, will never open
154     [EditorBrowsable(EditorBrowsableState.Never)]
155     public class RelativeVector2TypeConverter : TypeConverter
156     {
157         //Internal used, will never open
158         [EditorBrowsable(EditorBrowsableState.Never)]
159         public override object ConvertFromInvariantString(string value)
160         {
161             if (value != null)
162             {
163                 string[] parts = value.Split(',');
164                 if (parts.Length == 2)
165                 {
166                     return new RelativeVector2(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
167                                                Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture));
168                 }
169             }
170
171             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(RelativeVector2)}");
172         }
173
174         //Internal used, will never open
175         [EditorBrowsable(EditorBrowsableState.Never)]
176         public override string ConvertToString(object value)
177         {
178             RelativeVector2 vector = value as RelativeVector2;
179             if (null != vector)
180             {
181                 return vector.X.ToString() + " " + vector.Y.ToString();
182             }
183             else
184             {
185                 return null;
186             }
187         }
188     }
189
190     //Internal used, will never open
191     [EditorBrowsable(EditorBrowsableState.Never)]
192     public class RelativeVector3TypeConverter : TypeConverter
193     {
194         //Internal used, will never open
195         [EditorBrowsable(EditorBrowsableState.Never)]
196         public override object ConvertFromInvariantString(string value)
197         {
198             if (value != null)
199             {
200                 string[] parts = value.Split(',');
201                 if (parts.Length == 3)
202                 {
203                     return new RelativeVector3(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
204                                                Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
205                                                Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture));
206                 }
207             }
208
209             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(RelativeVector3)}");
210         }
211
212         //Internal used, will never open
213         [EditorBrowsable(EditorBrowsableState.Never)]
214         public override string ConvertToString(object value)
215         {
216             RelativeVector3 vector = value as RelativeVector3;
217             if (null != vector)
218             {
219                 return vector.X.ToString() + " " + vector.Y.ToString() + " " + vector.Z.ToString();
220             }
221             else
222             {
223                 return null;
224             }
225         }
226     }
227
228     //Internal used, will never open
229     [EditorBrowsable(EditorBrowsableState.Never)]
230     public class RelativeVector4TypeConverter : TypeConverter
231     {
232         //Internal used, will never open
233         [EditorBrowsable(EditorBrowsableState.Never)]
234         public override object ConvertFromInvariantString(string value)
235         {
236             if (value != null)
237             {
238                 string[] parts = value.Split(',');
239                 if (parts.Length == 4)
240                 {
241                     return new RelativeVector4(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
242                                                Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
243                                                Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture),
244                                                Single.Parse(parts[3].Trim(), CultureInfo.InvariantCulture));
245                 }
246             }
247
248             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(RelativeVector4)}");
249         }
250
251         //Internal used, will never open
252         [EditorBrowsable(EditorBrowsableState.Never)]
253         public override string ConvertToString(object value)
254         {
255             RelativeVector4 vector = value as RelativeVector4;
256             if (null != vector)
257             {
258                 return vector.X.ToString() + " " + vector.Y.ToString() + " " + vector.Z.ToString() + " " + vector.W.ToString();
259             }
260             else
261             {
262                 return null;
263             }
264         }
265     }
266 }