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