[NUI] Fix build warning[CA1064]
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / GraphicsTypeConverter.cs
1 /*
2  * Copyright(c) 2019 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
23 {
24
25     /// <summary>
26     /// GraphicsTypeConverter class to convert types.
27     /// </summary>
28     /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
29     [EditorBrowsable(EditorBrowsableState.Never)]
30     public class GraphicsTypeConverter
31     {
32
33         private const float defaultDpi = 160.0f;
34
35         /// <summary>
36         /// Converts script to px
37         /// </summary>
38         /// <returns>Pixel value that is converted from input string</returns>
39         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
40         [EditorBrowsable(EditorBrowsableState.Never)]
41         public virtual float ConvertScriptToPixel(string scriptValue)
42         {
43             float convertedValue = 0;
44             if (scriptValue != null)
45             {
46                 if (scriptValue.EndsWith("dp"))
47                 {
48                     convertedValue = ConvertToPixel(float.Parse(scriptValue.Substring(0, scriptValue.LastIndexOf("dp")), CultureInfo.InvariantCulture));
49                 }
50                 else if (scriptValue.EndsWith("px"))
51                 {
52                     convertedValue = float.Parse(scriptValue.Substring(0, scriptValue.LastIndexOf("px")), CultureInfo.InvariantCulture);
53                 }
54                 else
55                 {
56                     if (!float.TryParse(scriptValue, NumberStyles.Any, CultureInfo.InvariantCulture, out convertedValue))
57                     {
58                         NUILog.Error("Cannot convert the script {scriptValue}\n");
59                         convertedValue = 0;
60                     }
61                 }
62             }
63             return convertedValue;
64         }
65
66         /// <summary>
67         /// Converts other type to px
68         /// </summary>
69         /// <returns>Pixel value that is converted by the the display matric</returns>
70         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
71         [EditorBrowsable(EditorBrowsableState.Never)]
72         public virtual float ConvertToPixel(float value)
73         {
74             return value * (NUIApplication.GetDefaultWindow().Dpi.X / defaultDpi);
75         }
76
77         /// <summary>
78         /// Converts px to other type
79         /// </summary>
80         /// <returns>An converted value from pixel</returns>
81         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
82         [EditorBrowsable(EditorBrowsableState.Never)]
83         public virtual float ConvertFromPixel(float value)
84         {
85             return value * (defaultDpi / NUIApplication.GetDefaultWindow().Dpi.X);
86         }
87     }
88 }