replace Console.WriteLine() with Tizen.Log.Debug()
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / PropertyRangeManager.cs
1
2 #define DOT_NET_CORE
3 #if (DOT_NET_CORE)
4 using System.Reflection;
5 #endif
6 using System;
7 using System.Runtime.InteropServices;
8 using System.Collections.Generic;
9
10 namespace Tizen.NUI
11 {
12     /// <summary>
13     /// Helper class for calculating what property indexes should be assigned to C# View (view) classes.
14     /// </summary>
15     internal class PropertyRangeManager
16   {
17     private Dictionary<String, PropertyRange > _propertyRange;
18
19         /// <summary>
20         /// Initializes a new instance of the <see cref="Tizen.NUI.PropertyRangeManager"/> class.
21         /// </summary>
22         public PropertyRangeManager()
23         {
24             _propertyRange = new Dictionary<String, PropertyRange>();
25         }
26
27         /// <summary>
28         /// Only called if a View has scriptable properties
29         /// </summary>
30         private PropertyRange RegisterView(string viewName, System.Type viewType)
31         {
32             PropertyRange range;
33
34             if (_propertyRange.TryGetValue(viewName, out range))
35             {
36                 // already registered
37                 return range;
38             }
39
40             // Find out the event and animatable start indexes for the type
41             range = new PropertyRange();
42
43             GetPropertyStartRange(viewType, ref range);
44
45             // add it to our dictionary
46             _propertyRange.Add(viewName, range);
47
48             return range;
49
50         }
51
52         /// <summary>
53         /// Gets the index of the property.
54         /// Each property has to have unique index for this view type
55         /// </summary>
56         /// <returns>The property index.</returns>
57         /// <param name="viewName">View name</param>
58         /// <param name="viewType">View type</param>
59         /// <param name="type">Type.</param>
60         public int GetPropertyIndex(string viewName, System.Type viewType, ScriptableProperty.ScriptableType type)
61         {
62
63             PropertyRange range;
64
65             if (!_propertyRange.TryGetValue(viewName, out range))
66             {
67                 // view not found, register it now
68                 range = RegisterView(viewName, viewType);
69             }
70
71             int index = range.GetNextFreePropertyIndex(type);
72
73             // update the dictionary
74             _propertyRange[viewName] = range;
75
76             return index;
77
78         }
79
80         ///<summary>
81         /// We calculate the start property indices, based on the type and it's class  heirachy, e.g. DateView (70,000)- > Spin (60,000) -> View (50,000)
82         /// </summary>
83         private void GetPropertyStartRange(System.Type viewType, ref PropertyRange range)
84         {
85             const int maxCountPerDerivation = 1000; // For child and animtable properties we use a gap of 1000 between each
86                                                     // views property range in the heirachy
87
88             // custom views start there property index, at view_PROPERTY_END_INDEX
89             // we add 1000, just incase View class (our C# custom view base) starts using scriptable properties
90             int startEventPropertyIndex = (int)View.PropertyRange.CONTROL_PROPERTY_END_INDEX + maxCountPerDerivation;
91
92             // for animatable properties current range starts at ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX,
93             // we add 1000, just incase View class starts using animatable properties
94             int startAnimatablePropertyIndex = (int)Tizen.NUI.PropertyRanges.ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX + maxCountPerDerivation;
95
96 #if (DOT_NET_CORE)
97             while ( viewType.GetTypeInfo().BaseType.Name != "CustomView" )   // custom view is our C# view base class. we don't go any deeper.
98 #else
99             while (viewType.BaseType.Name != "CustomView")   // custom view is our C# view base class. we don't go any deeper.
100 #endif
101             {
102                 // for every base class increase property start index
103                 startEventPropertyIndex += (int)Tizen.NUI.PropertyRanges.DEFAULT_PROPERTY_MAX_COUNT_PER_DERIVATION; // DALi uses 10,000
104                 startAnimatablePropertyIndex += maxCountPerDerivation;
105
106                 //Tizen.Log.Debug("NUI", "getStartPropertyIndex =  " + viewType.Name +"current index " + startEventPropertyIndex);
107 #if (DOT_NET_CORE)
108                 viewType = viewType.GetTypeInfo().BaseType;
109 #else
110                 viewType = viewType.BaseType;
111 #endif
112             }
113
114             range.startEventIndex = startEventPropertyIndex;
115             range.lastUsedEventIndex = startEventPropertyIndex;
116
117             range.startAnimationIndex = startAnimatablePropertyIndex;
118             range.lastUsedAnimationIndex = startAnimatablePropertyIndex;
119
120         }
121
122
123         public struct PropertyRange
124         {
125
126             public int GetNextFreePropertyIndex(ScriptableProperty.ScriptableType type)
127             {
128                 if (type == ScriptableProperty.ScriptableType.Default)
129                 {
130                     lastUsedEventIndex++;
131                     return lastUsedEventIndex;
132                 }
133                 else
134                 {
135                     lastUsedAnimationIndex++;
136                     return lastUsedAnimationIndex;
137                 }
138             }
139
140
141             public int startEventIndex;    /// start of the property range
142             public int lastUsedEventIndex;    /// last used of the property index
143
144             public int startAnimationIndex;  /// start of the property range
145             public int lastUsedAnimationIndex; /// last used of the property index
146         };
147
148
149
150     }
151 }