2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Reflection;
20 using System.Collections.Generic;
21 using Tizen.NUI.BaseComponents;
26 /// The helper class for calculating what property indexes should be assigned to the C# View (view) classes.
28 internal class PropertyRangeManager
30 private Dictionary<String, PropertyRange> propertyRange;
33 /// Initializes a new instance of the <see cref="Tizen.NUI.PropertyRangeManager"/> class.
35 public PropertyRangeManager()
37 propertyRange = new Dictionary<String, PropertyRange>();
41 /// Only called if a view has scriptable properties.
43 private PropertyRange RegisterView(string viewName, System.Type viewType)
47 if (propertyRange.TryGetValue(viewName, out range))
53 // Find out the event and animatable start indexes for the type
54 range = new PropertyRange();
56 GetPropertyStartRange(viewType, ref range);
58 // add it to our dictionary
59 propertyRange.Add(viewName, range);
66 /// Gets the index of the property.
67 /// Each property has to have unique index for this view type.
69 /// <returns>The property index.</returns>
70 /// <param name="viewName">The view name.</param>
71 /// <param name="viewType">The view type.</param>
72 /// <param name="type">Type.</param>
73 public int GetPropertyIndex(string viewName, System.Type viewType, ScriptableProperty.ScriptableType type)
78 if (!propertyRange.TryGetValue(viewName, out range))
80 // view not found, register it now
81 range = RegisterView(viewName, viewType);
84 int index = range.GetNextFreePropertyIndex(type);
86 // update the dictionary
87 propertyRange[viewName] = range;
94 /// We calculate the start property indices, based on the type and it's class hierarchy, for example, DateView (70,000)- > Spin (60,000) -> View (50,000).
96 private void GetPropertyStartRange(System.Type viewType, ref PropertyRange range)
98 const int maxCountPerDerivation = 1000; // For child and animtable properties we use a gap of 1000 between each
99 // views property range in the heirachy
101 // custom views start there property index, at view_PROPERTY_END_INDEX
102 // we add 1000, just incase View class (our C# custom view base) starts using scriptable properties
103 int startEventPropertyIndex = (int)View.PropertyRange.CONTROL_PROPERTY_END_INDEX + maxCountPerDerivation;
105 // for animatable properties current range starts at ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX,
106 // we add 1000, just incase View class starts using animatable properties
107 int startAnimatablePropertyIndex = (int)Tizen.NUI.PropertyRanges.ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX + maxCountPerDerivation;
109 if (viewType != null)
111 while (viewType.GetTypeInfo().BaseType?.Name != "CustomView") // custom view is our C# view base class. we don't go any deeper.
113 // for every base class increase property start index
114 startEventPropertyIndex += (int)Tizen.NUI.PropertyRanges.DEFAULT_PROPERTY_MAX_COUNT_PER_DERIVATION; // DALi uses 10,000
115 startAnimatablePropertyIndex += maxCountPerDerivation;
116 NUILog.Debug("getStartPropertyIndex = " + viewType.Name + "current index " + startEventPropertyIndex);
117 viewType = viewType.GetTypeInfo().BaseType;
121 range.startEventIndex = startEventPropertyIndex;
122 range.lastUsedEventIndex = startEventPropertyIndex;
124 range.startAnimationIndex = startAnimatablePropertyIndex;
125 range.lastUsedAnimationIndex = startAnimatablePropertyIndex;
129 public struct PropertyRange
132 public int GetNextFreePropertyIndex(ScriptableProperty.ScriptableType type)
134 if (type == ScriptableProperty.ScriptableType.Default)
136 lastUsedEventIndex++;
137 return lastUsedEventIndex;
141 lastUsedAnimationIndex++;
142 return lastUsedAnimationIndex;
146 public int startEventIndex; // start of the property range
147 public int lastUsedEventIndex; // last used of the property index
149 public int startAnimationIndex; // start of the property range
150 public int lastUsedAnimationIndex; // last used of the property index