(PropertyNotifications) Added incremental step notification
[platform/core/uifw/dali-core.git] / dali / internal / update / common / property-condition-variable-step-functions.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include <dali/public-api/common/dali-common.h>
18 #include <dali/public-api/math/vector2.h>
19 #include <dali/public-api/math/vector3.h>
20 #include <dali/public-api/math/vector4.h>
21 #include <dali/public-api/object/property-input.h>
22 #include <dali/internal/update/common/property-condition-variable-step-functions.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 namespace SceneGraph
31 {
32
33 namespace
34 {
35
36 const int ARGINDEX_STEP_INDEX = 0;
37 const int ARGINDEX_LIST_SIZE = 1;
38 const int ARGINDEX_LIST_START = 2;
39
40 }
41
42 ConditionFunction VariableStep::GetFunction( Property::Type valueType )
43 {
44   ConditionFunction function = NULL;
45
46   switch( valueType )
47   {
48     case Property::FLOAT:
49     {
50       function = EvalFloat;
51       break;
52     }
53     case Property::VECTOR2:
54     {
55       function = EvalVector2;
56       break;
57     }
58     case Property::VECTOR3:
59     {
60       function = EvalVector3;
61       break;
62     }
63     case Property::VECTOR4:
64     {
65       function = EvalVector4;
66       break;
67     }
68     default:
69     {
70       function = EvalDefault;
71       break;
72     }
73   } // end switch
74
75   return function;
76 }
77
78 bool VariableStep::Evaluate( const float propertyValue, PropertyNotification::RawArgumentContainer& arg )
79 {
80   const int currentIndex = arg[ARGINDEX_STEP_INDEX];
81   const int numSteps = arg[ARGINDEX_LIST_SIZE];
82   const float first = arg[ARGINDEX_LIST_START];
83   const float last = arg[ARGINDEX_LIST_START + (numSteps - 1)];
84   const bool ascending = (last > first) ? true : false;
85   int newIndex = currentIndex;
86
87   // avoid loop if property currently not within any of the range values
88   if( ascending )
89   {
90     if( propertyValue < first )
91     {
92       newIndex = -1;
93     }
94     else if( propertyValue >= last )
95     {
96       newIndex = numSteps - 1;
97     }
98   }
99   else
100   {
101     // increments are in negative direction
102     if( propertyValue > first )
103     {
104       newIndex = -1;
105     }
106     else if( propertyValue <= last )
107     {
108       newIndex = numSteps - 1;
109     }
110   }
111   int i = 0;
112   for( i = 0 ; i < numSteps - 1 ; ++i )
113   {
114     const float arg1 = arg[ARGINDEX_LIST_START + i];
115     const float arg2 = arg[ARGINDEX_LIST_START + (i + 1)];
116     if( ascending )
117     {
118       if( ( propertyValue >= arg1 )
119           && ( propertyValue < arg2 ) )
120       {
121         newIndex = i;
122         break;
123       }
124     }
125     else
126     {
127       // increments are in negative direction
128       if( ( propertyValue > arg2 )
129           && ( propertyValue <= arg1 ) )
130       {
131         newIndex = i;
132         break;
133       }
134     }
135   }
136   if( newIndex != currentIndex )
137   {
138     // have changed to new step
139     arg[ARGINDEX_STEP_INDEX] = static_cast<float>(newIndex);
140     return true;
141   }
142   return false;
143 }
144
145 bool VariableStep::EvalFloat( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
146 {
147   const float propertyValue = value.GetFloat();
148   return Evaluate( propertyValue, arg );
149 }
150
151 bool VariableStep::EvalVector2( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
152 {
153   const float propertyValue = value.GetVector2().LengthSquared();
154   return Evaluate( propertyValue, arg );
155 }
156
157 bool VariableStep::EvalVector3( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
158 {
159   float propertyValue = value.GetVector3().LengthSquared();
160   return Evaluate( propertyValue, arg );
161 }
162
163 bool VariableStep::EvalVector4( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
164 {
165   const float propertyValue = value.GetVector4().LengthSquared();
166   return Evaluate( propertyValue, arg );
167 }
168
169 bool VariableStep::EvalDefault( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
170 {
171   return false;
172 }
173
174 } // namespace SceneGraph
175
176 } // namespace Internal
177
178 } // namespace Dali