From b1fa1d487363650590dd3cf96ed918b40a3b6b0f Mon Sep 17 00:00:00 2001 From: dongsug-song <35130733+dongsug-song@users.noreply.github.com> Date: Thu, 9 May 2019 14:41:25 +0900 Subject: [PATCH] [NUI] Add GraphicsTypeManager for Density Independent Pixel (#821) --- .../internal/XamlBinding/BindableProperty.cs | 10 ++ .../XamlBinding/PositionTypeConverter.cs | 7 +- .../internal/XamlBinding/SizeTypeConverter.cs | 12 +- .../src/internal/XamlBinding/Style.cs | 5 +- .../src/public/BaseComponents/View.cs | 53 +++++++++ src/Tizen.NUI/src/public/FocusManager.cs | 16 ++- .../src/public/GraphicsTypeConverter.cs | 84 +++++++++++++ .../src/public/GraphicsTypeManager.cs | 112 ++++++++++++++++++ src/Tizen.NUI/src/public/Layer.cs | 15 +++ src/Tizen.NUI/src/public/Position2D.cs | 4 +- 10 files changed, 304 insertions(+), 14 deletions(-) create mode 100755 src/Tizen.NUI/src/public/GraphicsTypeConverter.cs create mode 100755 src/Tizen.NUI/src/public/GraphicsTypeManager.cs diff --git a/src/Tizen.NUI/src/internal/XamlBinding/BindableProperty.cs b/src/Tizen.NUI/src/internal/XamlBinding/BindableProperty.cs index 8f4c1f38a..9aa62d802 100755 --- a/src/Tizen.NUI/src/internal/XamlBinding/BindableProperty.cs +++ b/src/Tizen.NUI/src/internal/XamlBinding/BindableProperty.cs @@ -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 UserCustomConvertTypes = new Dictionary + { + }; + // more or less the encoding of this, without the need to reflect // http://msdn.microsoft.com/en-us/library/y5b434w4.aspx static readonly Dictionary SimpleConvertTypes = new Dictionary @@ -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) diff --git a/src/Tizen.NUI/src/internal/XamlBinding/PositionTypeConverter.cs b/src/Tizen.NUI/src/internal/XamlBinding/PositionTypeConverter.cs index 342513541..f059ae49c 100755 --- a/src/Tizen.NUI/src/internal/XamlBinding/PositionTypeConverter.cs +++ b/src/Tizen.NUI/src/internal/XamlBinding/PositionTypeConverter.cs @@ -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); } } diff --git a/src/Tizen.NUI/src/internal/XamlBinding/SizeTypeConverter.cs b/src/Tizen.NUI/src/internal/XamlBinding/SizeTypeConverter.cs index 2a2d06c14..d5277159a 100755 --- a/src/Tizen.NUI/src/internal/XamlBinding/SizeTypeConverter.cs +++ b/src/Tizen.NUI/src/internal/XamlBinding/SizeTypeConverter.cs @@ -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); } } diff --git a/src/Tizen.NUI/src/internal/XamlBinding/Style.cs b/src/Tizen.NUI/src/internal/XamlBinding/Style.cs index a5719643a..1c9b2a9da 100755 --- a/src/Tizen.NUI/src/internal/XamlBinding/Style.cs +++ b/src/Tizen.NUI/src/internal/XamlBinding/Style.cs @@ -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 Setters { get; } - internal IList 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 Triggers { get { return _triggers ?? (_triggers = new AttachedCollection()); } } diff --git a/src/Tizen.NUI/src/public/BaseComponents/View.cs b/src/Tizen.NUI/src/public/BaseComponents/View.cs index 614f2aa2d..56a4fee05 100755 --- a/src/Tizen.NUI/src/public/BaseComponents/View.cs +++ b/src/Tizen.NUI/src/public/BaseComponents/View.cs @@ -15,9 +15,12 @@ * */ 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 transDictionary = new Dictionary(); + + /// 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(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; diff --git a/src/Tizen.NUI/src/public/FocusManager.cs b/src/Tizen.NUI/src/public/FocusManager.cs index 2c750b372..0aa7875b8 100755 --- a/src/Tizen.NUI/src/public/FocusManager.cs +++ b/src/Tizen.NUI/src/public/FocusManager.cs @@ -405,11 +405,19 @@ namespace Tizen.NUI /// 3 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 index 000000000..0778de198 --- /dev/null +++ b/src/Tizen.NUI/src/public/GraphicsTypeConverter.cs @@ -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 +{ + + /// + /// GraphicsTypeConverter class to convert types. + /// + /// 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; + + /// + /// + /// Convert script to px + /// 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; + } + + /// + /// + /// Convert other type to px + /// 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); + } + + /// + /// + /// Convert px to other type + /// 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 index 000000000..29b5c5cb4 --- /dev/null +++ b/src/Tizen.NUI/src/public/GraphicsTypeManager.cs @@ -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 +{ + + /// + /// GraphicsTypeManager class to manage various types. + /// + /// 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 + { + + /// + /// Creates private GraphicsTypeManager object. + /// + /// 5 + private GraphicsTypeManager() + { + _typeConverter = new GraphicsTypeConverter(); + } + + /// + /// + /// Returns Singleton instance of GraphicsTypeManager + /// 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; + } + + } + + /// + /// + /// Set Custom GraphicsTypeConverter + /// 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; + } + + /// + /// + /// Convert script to px + /// 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); + } + + /// + /// + /// Convert other type to px + /// 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); + } + + /// + /// + /// Convert px to other type + /// 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 diff --git a/src/Tizen.NUI/src/public/Layer.cs b/src/Tizen.NUI/src/public/Layer.cs index 04821f56c..63345a04f 100755 --- a/src/Tizen.NUI/src/public/Layer.cs +++ b/src/Tizen.NUI/src/public/Layer.cs @@ -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. /// diff --git a/src/Tizen.NUI/src/public/Position2D.cs b/src/Tizen.NUI/src/public/Position2D.cs index e2fcd0f82..41cf4f051 100755 --- a/src/Tizen.NUI/src/public/Position2D.cs +++ b/src/Tizen.NUI/src/public/Position2D.cs @@ -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); } } -- 2.34.1