[NUI] Add GraphicsTypeManager for Density Independent Pixel (#821)
authordongsug-song <35130733+dongsug-song@users.noreply.github.com>
Thu, 9 May 2019 05:41:25 +0000 (14:41 +0900)
committerGitHub <noreply@github.com>
Thu, 9 May 2019 05:41:25 +0000 (14:41 +0900)
src/Tizen.NUI/src/internal/XamlBinding/BindableProperty.cs
src/Tizen.NUI/src/internal/XamlBinding/PositionTypeConverter.cs
src/Tizen.NUI/src/internal/XamlBinding/SizeTypeConverter.cs
src/Tizen.NUI/src/internal/XamlBinding/Style.cs
src/Tizen.NUI/src/public/BaseComponents/View.cs
src/Tizen.NUI/src/public/FocusManager.cs
src/Tizen.NUI/src/public/GraphicsTypeConverter.cs [new file with mode: 0755]
src/Tizen.NUI/src/public/GraphicsTypeManager.cs [new file with mode: 0755]
src/Tizen.NUI/src/public/Layer.cs
src/Tizen.NUI/src/public/Position2D.cs

index 8f4c1f3..9aa62d8 100755 (executable)
@@ -120,6 +120,11 @@ namespace Tizen.NUI.Binding
             { typeof(RelativeVector4), new RelativeVector4TypeConverter() },
         };
 
+        //Modification for NUI XAML : user defined converter for DynamicResource can be added
+        static internal Dictionary<Type, TypeConverter> UserCustomConvertTypes = new Dictionary<Type, TypeConverter>
+        {
+        };
+
         // more or less the encoding of this, without the need to reflect
         // http://msdn.microsoft.com/en-us/library/y5b434w4.aspx
         static readonly Dictionary<Type, Type[]> SimpleConvertTypes = new Dictionary<Type, Type[]>
@@ -531,6 +536,11 @@ namespace Tizen.NUI.Binding
             {
                 value = typeConverterTo.ConvertFromInvariantString(value.ToString());
             }
+            else if (UserCustomConvertTypes.TryGetValue(type, out typeConverterTo) && typeConverterTo.CanConvertFrom(valueType))
+            {
+                //Modification for NUI XAML : user defined converter for DynamicResource can be added
+                value = typeConverterTo.ConvertFromInvariantString(value.ToString());
+            }
             else if (!ReturnTypeInfo.IsAssignableFrom(valueType.GetTypeInfo()))
             {
                 var cast = type.GetImplicitConversionOperator(fromType: valueType, toType: type)
index 3425135..f059ae4 100755 (executable)
@@ -54,9 +54,10 @@ namespace Tizen.NUI.Binding
                 parts = value.Split(',');
                 if (parts.Length == 3)
                 {
-                    return new Position(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
-                                    Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
-                                    Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture));
+                    int x = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[0].Trim());
+                    int y = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[1].Trim());
+                    int z = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[2].Trim());
+                    return new Position(x, y, z);
                 }
             }
 
index 2a2d06c..d527715 100755 (executable)
@@ -18,9 +18,10 @@ namespace Tizen.NUI.Binding
                 string[] parts = value.Split(',');
                 if (parts.Length == 3)
                 {
-                    return new Size(Single.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
-                                    Single.Parse(parts[1].Trim(), CultureInfo.InvariantCulture),
-                                    Single.Parse(parts[2].Trim(), CultureInfo.InvariantCulture));
+                    int x = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[0].Trim());
+                    int y = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[1].Trim());
+                    int z = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[2].Trim());
+                    return new Size(x, y, z);
                 }
             }
 
@@ -38,8 +39,9 @@ namespace Tizen.NUI.Binding
                 string[] parts = value.Split(',');
                 if (parts.Length == 2)
                 {
-                    return new Size2D(Int32.Parse(parts[0].Trim(), CultureInfo.InvariantCulture),
-                                    Int32.Parse(parts[1].Trim(), CultureInfo.InvariantCulture));
+                    int x = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[0].Trim());
+                    int y = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[1].Trim());
+                    return new Size2D(x, y);
                 }
             }
 
index a571964..1c9b2a9 100755 (executable)
@@ -2,6 +2,7 @@ using System;
 using System.Collections.Generic;
 using System.Reflection;
 using Tizen.NUI.StyleSheets;
+using System.ComponentModel;
 
 namespace Tizen.NUI.Binding
 {
@@ -85,7 +86,9 @@ namespace Tizen.NUI.Binding
 
         public IList<Setter> Setters { get; }
 
-        internal IList<TriggerBase> Triggers
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public IList<TriggerBase> Triggers
         {
             get { return _triggers ?? (_triggers = new AttachedCollection<TriggerBase>()); }
         }
index 614f2aa..56a4fee 100755 (executable)
  *
  */
 using System;
+using System.Collections.Generic;
 using System.ComponentModel;
+using System.IO;
 using System.Runtime.InteropServices;
 using Tizen.NUI.Binding;
+using Tizen.NUI.Xaml;
 
 namespace Tizen.NUI.BaseComponents
 {
@@ -5741,6 +5744,56 @@ namespace Tizen.NUI.BaseComponents
             }
         }
 
+
+        private Dictionary<string, Transition> transDictionary = new Dictionary<string, Transition>();
+
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Transition GetTransition(string transitionName)
+        {
+            Transition trans = null;
+            transDictionary.TryGetValue(transitionName, out trans);
+            return trans;
+        }
+
+        private void LoadTransitions()
+        {
+            foreach (string str in transitionNames)
+            {
+                string resourceName = str + ".xaml";
+                Transition trans = null;
+
+                string resource = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
+
+                string likelyResourcePath = resource + "animation/" + resourceName;
+
+                if (File.Exists(likelyResourcePath))
+                {
+                    trans = Extensions.LoadObject<Transition>(likelyResourcePath);
+                }
+                if (trans)
+                {
+                    transDictionary.Add(trans.Name, trans);
+                }
+            }
+        }
+
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public string[] TransitionNames
+        {
+            get
+            {
+                return transitionNames;
+            }
+            set
+            {
+                transitionNames = value;
+                LoadTransitions();
+            }
+        }
+        private string[] transitionNames;
+
         internal class BackgroundResourceLoadedEventArgs : EventArgs
         {
             private ResourceLoadingStatusType status = ResourceLoadingStatusType.Invalid;
index 2c750b3..0aa7875 100755 (executable)
@@ -405,11 +405,19 @@ namespace Tizen.NUI
         /// <since_tizen> 3 </since_tizen>
         public void SetCustomAlgorithm(ICustomFocusAlgorithm arg0)
         {
-            _customAlgorithmInterfaceWrapper = new CustomAlgorithmInterfaceWrapper();
-            _customAlgorithmInterfaceWrapper.SetFocusAlgorithm(arg0);
+            if(arg0 != null)
+            {
+                _customAlgorithmInterfaceWrapper = new CustomAlgorithmInterfaceWrapper();
+                _customAlgorithmInterfaceWrapper.SetFocusAlgorithm(arg0);
 
-            Interop.NDalic.SetCustomAlgorithm(swigCPtr, CustomAlgorithmInterface.getCPtr(_customAlgorithmInterfaceWrapper));
-            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+                Interop.NDalic.SetCustomAlgorithm(swigCPtr, CustomAlgorithmInterface.getCPtr(_customAlgorithmInterfaceWrapper));
+                if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+            }
+            else
+            {
+                Interop.NDalic.SetCustomAlgorithm(swigCPtr, new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero));
+                if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+            }
         }
 
         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FocusManager obj)
diff --git a/src/Tizen.NUI/src/public/GraphicsTypeConverter.cs b/src/Tizen.NUI/src/public/GraphicsTypeConverter.cs
new file mode 100755 (executable)
index 0000000..0778de1
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright(c) 2019 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+using System;
+using System.ComponentModel;
+using System.Globalization;
+
+namespace Tizen.NUI
+{
+
+    /// <summary>
+    /// GraphicsTypeConverter class to convert types.
+    /// </summary>
+    /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public class GraphicsTypeConverter
+    {
+
+        private const float defaultDpi = 160.0f;
+
+        /// <summary>
+        /// </summary>
+        /// <returns>Convert script to px</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public virtual float ConvertScriptToPixel(string scriptValue)
+        {
+            float convertedValue = 0;
+            if (scriptValue.EndsWith("dp"))
+            {
+                convertedValue = ConvertToPixel(float.Parse(scriptValue.Substring(0, scriptValue.LastIndexOf("dp")), CultureInfo.InvariantCulture));
+            }
+            else if (scriptValue.EndsWith("px"))
+            {
+                convertedValue = float.Parse(scriptValue.Substring(0, scriptValue.LastIndexOf("px")), CultureInfo.InvariantCulture);
+            }
+            else
+            {
+                if (!float.TryParse(scriptValue, NumberStyles.Any, CultureInfo.InvariantCulture, out convertedValue))
+                {
+                    NUILog.Error("Cannot convert the script {scriptValue}\n");
+                    convertedValue = 0;
+                }
+            }
+            return convertedValue;
+        }
+
+        /// <summary>
+        /// </summary>
+        /// <returns>Convert other type to px</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public virtual float ConvertToPixel(float value)
+        {
+            return value * (Window.Instance.Dpi.X / defaultDpi);
+        }
+
+        /// <summary>
+        /// </summary>
+        /// <returns>Convert px to other type</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public virtual float ConvertFromPixel(float value)
+        {
+            return value * (defaultDpi / Window.Instance.Dpi.X);
+        }
+
+    }
+
+}
diff --git a/src/Tizen.NUI/src/public/GraphicsTypeManager.cs b/src/Tizen.NUI/src/public/GraphicsTypeManager.cs
new file mode 100755 (executable)
index 0000000..29b5c5c
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Copyright(c) 2019 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+using System.ComponentModel;
+
+namespace Tizen.NUI
+{
+
+    /// <summary>
+    /// GraphicsTypeManager class to manage various types.
+    /// </summary>
+    /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public class GraphicsTypeManager
+    {
+
+        /// <summary>
+        /// Creates private GraphicsTypeManager object.
+        /// </summary>
+        /// <since_tizen> 5 </since_tizen>
+        private GraphicsTypeManager()
+        {
+            _typeConverter = new GraphicsTypeConverter();
+        }
+
+        /// <summary>
+        /// </summary>
+        /// <returns>Returns Singleton instance of GraphicsTypeManager</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static GraphicsTypeManager Instance
+        {
+            get
+            {
+                if (_graphicsTypeManager == null)
+                {
+                    _graphicsTypeManager = new GraphicsTypeManager();
+                }
+
+                return _graphicsTypeManager;
+            }
+
+        }
+
+        /// <summary>
+        /// </summary>
+        /// <returns>Set Custom GraphicsTypeConverter</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public void SetTypeConverter(GraphicsTypeConverter typeConverter)
+        {
+            _typeConverter = typeConverter;
+        }
+
+        /// <summary>
+        /// </summary>
+        /// <returns>Convert script to px</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public float ConvertScriptToPixel(string scriptValue)
+        {
+            return _typeConverter.ConvertScriptToPixel(scriptValue);
+        }
+
+        /// <summary>
+        /// </summary>
+        /// <returns>Convert other type to px</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public virtual float ConvertToPixel(float value)
+        {
+            return _typeConverter.ConvertToPixel(value);
+        }
+
+        /// <summary>
+        /// </summary>
+        /// <returns>Convert px to other type</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public virtual float ConvertFromPixel(float value)
+        {
+            return _typeConverter.ConvertFromPixel(value);
+        }
+
+        internal void RegisterCustomConverterForDynamicResourceBinding(global::System.Type type, Tizen.NUI.Binding.TypeConverter userConverter)
+        {
+            if (Tizen.NUI.Binding.BindableProperty.UserCustomConvertTypes.ContainsKey(type) == false)
+            {
+                Tizen.NUI.Binding.BindableProperty.UserCustomConvertTypes.Add(type, userConverter);
+            }
+            //NUILog.Error($"user custom converter ditionary count={Tizen.NUI.Binding.BindableProperty.UserCustomConvertTypes.Count}");
+        }
+
+        private volatile static GraphicsTypeManager _graphicsTypeManager;
+        private GraphicsTypeConverter _typeConverter;
+    }
+
+}
\ No newline at end of file
index 04821f5..63345a0 100755 (executable)
@@ -18,6 +18,7 @@ using System;
 using Tizen.NUI.BaseComponents;
 using System.ComponentModel;
 using System.Runtime.InteropServices;
+using Tizen.NUI.Binding;
 
 namespace Tizen.NUI
 {
@@ -245,6 +246,20 @@ namespace Tizen.NUI
             }
         }
 
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public ResourceDictionary XamlResources
+        {
+            get
+            {
+                return Application.Current.XamlResources;
+            }
+            set
+            {
+                Application.Current.XamlResources = value;
+            }
+        }
+
         /// From the Container base class.
 
         /// <summary>
index e2fcd0f..41cf4f0 100755 (executable)
@@ -170,7 +170,9 @@ namespace Tizen.NUI
                 string[] parts = value.Split(',');
                 if (parts.Length == 2)
                 {
-                    return new Position2D(int.Parse(parts[0].Trim()), int.Parse(parts[1].Trim()));
+                    int x = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[0].Trim());
+                    int y = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[1].Trim());
+                    return new Position2D(x, y);
                 }
             }