[NUI]public the converter for Xaml (#2837)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / RotationTypeConverter.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.ComponentModel;
20 using System.Globalization;
21
22 namespace Tizen.NUI.Binding
23 {
24     //Internal used, will never open
25     [EditorBrowsable(EditorBrowsableState.Never)]
26     public class RotationTypeConverter : TypeConverter
27     {
28         //Internal used, will never open
29         [EditorBrowsable(EditorBrowsableState.Never)]
30         public override object ConvertFromInvariantString(string value)
31         {
32             // public Rotation(Radian radian(float), Vector3 vector3)
33             // Default: <View Orientation="45.0,12,13,0" />
34             // Orientation="D:23, 0, 0, 1"
35             // Orientation="R:23, 0, 0, 1"
36             if (value != null)
37             {
38                 string[] parts = value.Split(',');
39                 if (parts.Length == 4)
40                 {
41                     bool useDefault = true;
42                     Radian radian = null;
43                     string[] head = parts[0].Trim().Split(':');
44                     if (head.Length == 2)
45                     {
46                         useDefault = false;
47                         string radianOrDegree = head[0].Trim().ToUpperInvariant();
48                         if (radianOrDegree == "D" || radianOrDegree == "DEGREE")
49                         {
50                             // Orientation="D:23, 0, 0, 1"
51                             var degree = new Degree(Single.Parse(head[1].Trim(), CultureInfo.InvariantCulture));
52                             radian = new Radian(degree);
53                             degree.Dispose();
54                         }
55                         else if (radianOrDegree == "R" || radianOrDegree == "RADIAN")
56                         {
57                             // Orientation="R:23, 0, 0, 1"
58                             radian = new Radian(Single.Parse(head[1].Trim(), CultureInfo.InvariantCulture));
59                         }
60                         else
61                         {
62                             throw new InvalidOperationException($"Cannot convert the first parameter \"{value}\" into Radian of {typeof(Rotation)}");
63                         }
64                     }
65
66                     if (useDefault)
67                     {
68                         // Default: <View Orientation="45.0,12,13,0" />
69                         radian = new Radian(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture));
70                     }
71
72                     Vector3 vector3 = new Vector3(Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
73                                                   Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture),
74                                                   Single.Parse(parts[3].Trim(), CultureInfo.InvariantCulture));
75                     var ret = new Rotation(radian, vector3);
76                     radian?.Dispose();
77                     vector3.Dispose();
78                     return ret;
79                 }
80             }
81
82             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Rotation)}");
83         }
84
85         //Internal used, will never open
86         [EditorBrowsable(EditorBrowsableState.Never)]
87         public override string ConvertToString(object value)
88         {
89             Rotation rotation = value as Rotation;
90             return rotation?.ToString();
91         }
92     }
93 }