use modern construct 'nullptr' instead of 'NULL' or '0'
[platform/core/uifw/dali-core.git] / dali / internal / update / common / property-condition-variable-step-functions.cpp
1 /*
2  * Copyright (c) 2018 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 int32_t ARGINDEX_STEP_INDEX = 0;
38 const int32_t ARGINDEX_LIST_SIZE = 1;
39 const int32_t ARGINDEX_LIST_START = 2;
40
41 }
42
43 ConditionFunction VariableStep::GetFunction( Property::Type valueType )
44 {
45   ConditionFunction function = nullptr;
46
47   switch( valueType )
48   {
49     case Property::INTEGER:
50     {
51       function = EvalInteger;
52       break;
53     }
54     case Property::FLOAT:
55     {
56       function = EvalFloat;
57       break;
58     }
59     case Property::VECTOR2:
60     {
61       function = EvalVector2;
62       break;
63     }
64     case Property::VECTOR3:
65     {
66       function = EvalVector3;
67       break;
68     }
69     case Property::VECTOR4:
70     {
71       function = EvalVector4;
72       break;
73     }
74     default:
75     {
76       function = EvalDefault;
77       break;
78     }
79   } // end switch
80
81   return function;
82 }
83
84 bool VariableStep::Evaluate( const float propertyValue, PropertyNotification::RawArgumentContainer& arg )
85 {
86   const int32_t currentIndex = static_cast<int32_t>( arg[ARGINDEX_STEP_INDEX] ); // truncated
87   const int32_t numSteps = static_cast<int32_t>( arg[ARGINDEX_LIST_SIZE] ); // truncated
88   const float first = arg[ARGINDEX_LIST_START];
89   const float last = arg[ARGINDEX_LIST_START + (numSteps - 1)];
90   const bool ascending = (last > first) ? true : false;
91   int32_t newIndex = currentIndex;
92
93   // avoid loop if property currently not within any of the range values
94   if( ascending )
95   {
96     if( propertyValue < first )
97     {
98       newIndex = -1;
99     }
100     else if( propertyValue >= last )
101     {
102       newIndex = numSteps - 1;
103     }
104   }
105   else
106   {
107     // increments are in negative direction
108     if( propertyValue > first )
109     {
110       newIndex = -1;
111     }
112     else if( propertyValue <= last )
113     {
114       newIndex = numSteps - 1;
115     }
116   }
117   int32_t i = 0;
118   for( i = 0 ; i < numSteps - 1 ; ++i )
119   {
120     const float arg1 = arg[ARGINDEX_LIST_START + i];
121     const float arg2 = arg[ARGINDEX_LIST_START + (i + 1)];
122     if( ascending )
123     {
124       if( ( propertyValue >= arg1 )
125           && ( propertyValue < arg2 ) )
126       {
127         newIndex = i;
128         break;
129       }
130     }
131     else
132     {
133       // increments are in negative direction
134       if( ( propertyValue > arg2 )
135           && ( propertyValue <= arg1 ) )
136       {
137         newIndex = i;
138         break;
139       }
140     }
141   }
142   if( newIndex != currentIndex )
143   {
144     // have changed to new step
145     arg[ARGINDEX_STEP_INDEX] = static_cast<float>(newIndex);
146     return true;
147   }
148   return false;
149 }
150
151 bool VariableStep::EvalInteger( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
152 {
153   const float propertyValue = static_cast<float>( value.GetInteger() );
154   return Evaluate( propertyValue, arg );
155 }
156
157 bool VariableStep::EvalFloat( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
158 {
159   const float propertyValue = value.GetFloat();
160   return Evaluate( propertyValue, arg );
161 }
162
163 bool VariableStep::EvalVector2( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
164 {
165   const float propertyValue = value.GetVector2().LengthSquared();
166   return Evaluate( propertyValue, arg );
167 }
168
169 bool VariableStep::EvalVector3( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
170 {
171   float propertyValue = value.GetVector3().LengthSquared();
172   return Evaluate( propertyValue, arg );
173 }
174
175 bool VariableStep::EvalVector4( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
176 {
177   const float propertyValue = value.GetVector4().LengthSquared();
178   return Evaluate( propertyValue, arg );
179 }
180
181 bool VariableStep::EvalDefault( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
182 {
183   return false;
184 }
185
186 } // namespace SceneGraph
187
188 } // namespace Internal
189
190 } // namespace Dali