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