[NUI] Add license, delete unnecessary code (#2679)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Common / PropertyRangeManager.cs
1 /*
2  * Copyright(c) 2021 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.Reflection;
19 using System;
20 using System.Collections.Generic;
21 using Tizen.NUI.BaseComponents;
22
23 namespace Tizen.NUI
24 {
25     /// <summary>
26     /// The helper class for calculating what property indexes should be assigned to the C# View (view) classes.
27     /// </summary>
28     internal class PropertyRangeManager
29     {
30         private Dictionary<String, PropertyRange> propertyRange;
31
32         /// <summary>
33         /// Initializes a new instance of the <see cref="Tizen.NUI.PropertyRangeManager"/> class.
34         /// </summary>
35         public PropertyRangeManager()
36         {
37             propertyRange = new Dictionary<String, PropertyRange>();
38         }
39
40         /// <summary>
41         /// Only called if a view has scriptable properties.
42         /// </summary>
43         private PropertyRange RegisterView(string viewName, System.Type viewType)
44         {
45             PropertyRange range;
46
47             if (propertyRange.TryGetValue(viewName, out range))
48             {
49                 // already registered
50                 return range;
51             }
52
53             // Find out the event and animatable start indexes for the type
54             range = new PropertyRange();
55
56             GetPropertyStartRange(viewType, ref range);
57
58             // add it to our dictionary
59             propertyRange.Add(viewName, range);
60
61             return range;
62
63         }
64
65         /// <summary>
66         /// Gets the index of the property.
67         /// Each property has to have unique index for this view type.
68         /// </summary>
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)
74         {
75
76             PropertyRange range;
77
78             if (!propertyRange.TryGetValue(viewName, out range))
79             {
80                 // view not found, register it now
81                 range = RegisterView(viewName, viewType);
82             }
83
84             int index = range.GetNextFreePropertyIndex(type);
85
86             // update the dictionary
87             propertyRange[viewName] = range;
88
89             return index;
90
91         }
92
93         ///<summary>
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).
95         /// </summary>
96         private void GetPropertyStartRange(System.Type viewType, ref PropertyRange range)
97         {
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
100
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;
104
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;
108
109             if (viewType != null)
110             {
111                 while (viewType.GetTypeInfo().BaseType?.Name != "CustomView")   // custom view is our C# view base class. we don't go any deeper.
112                 {
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;
118                 }
119             }
120
121             range.startEventIndex = startEventPropertyIndex;
122             range.lastUsedEventIndex = startEventPropertyIndex;
123
124             range.startAnimationIndex = startAnimatablePropertyIndex;
125             range.lastUsedAnimationIndex = startAnimatablePropertyIndex;
126
127         }
128
129         public struct PropertyRange
130         {
131
132             public int GetNextFreePropertyIndex(ScriptableProperty.ScriptableType type)
133             {
134                 if (type == ScriptableProperty.ScriptableType.Default)
135                 {
136                     lastUsedEventIndex++;
137                     return lastUsedEventIndex;
138                 }
139                 else
140                 {
141                     lastUsedAnimationIndex++;
142                     return lastUsedAnimationIndex;
143                 }
144             }
145
146             public int startEventIndex;        // start of the property range
147             public int lastUsedEventIndex;     // last used of the property index
148
149             public int startAnimationIndex;    // start of the property range
150             public int lastUsedAnimationIndex; // last used of the property index
151         };
152     }
153 }