Conversion to Apache 2.0 license
[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::FLOAT:
50     {
51       function = EvalFloat;
52       break;
53     }
54     case Property::VECTOR2:
55     {
56       function = EvalVector2;
57       break;
58     }
59     case Property::VECTOR3:
60     {
61       function = EvalVector3;
62       break;
63     }
64     case Property::VECTOR4:
65     {
66       function = EvalVector4;
67       break;
68     }
69     default:
70     {
71       function = EvalDefault;
72       break;
73     }
74   } // end switch
75
76   return function;
77 }
78
79 bool VariableStep::Evaluate( const float propertyValue, PropertyNotification::RawArgumentContainer& arg )
80 {
81   const int currentIndex = arg[ARGINDEX_STEP_INDEX];
82   const int numSteps = arg[ARGINDEX_LIST_SIZE];
83   const float first = arg[ARGINDEX_LIST_START];
84   const float last = arg[ARGINDEX_LIST_START + (numSteps - 1)];
85   const bool ascending = (last > first) ? true : false;
86   int newIndex = currentIndex;
87
88   // avoid loop if property currently not within any of the range values
89   if( ascending )
90   {
91     if( propertyValue < first )
92     {
93       newIndex = -1;
94     }
95     else if( propertyValue >= last )
96     {
97       newIndex = numSteps - 1;
98     }
99   }
100   else
101   {
102     // increments are in negative direction
103     if( propertyValue > first )
104     {
105       newIndex = -1;
106     }
107     else if( propertyValue <= last )
108     {
109       newIndex = numSteps - 1;
110     }
111   }
112   int i = 0;
113   for( i = 0 ; i < numSteps - 1 ; ++i )
114   {
115     const float arg1 = arg[ARGINDEX_LIST_START + i];
116     const float arg2 = arg[ARGINDEX_LIST_START + (i + 1)];
117     if( ascending )
118     {
119       if( ( propertyValue >= arg1 )
120           && ( propertyValue < arg2 ) )
121       {
122         newIndex = i;
123         break;
124       }
125     }
126     else
127     {
128       // increments are in negative direction
129       if( ( propertyValue > arg2 )
130           && ( propertyValue <= arg1 ) )
131       {
132         newIndex = i;
133         break;
134       }
135     }
136   }
137   if( newIndex != currentIndex )
138   {
139     // have changed to new step
140     arg[ARGINDEX_STEP_INDEX] = static_cast<float>(newIndex);
141     return true;
142   }
143   return false;
144 }
145
146 bool VariableStep::EvalFloat( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
147 {
148   const float propertyValue = value.GetFloat();
149   return Evaluate( propertyValue, arg );
150 }
151
152 bool VariableStep::EvalVector2( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
153 {
154   const float propertyValue = value.GetVector2().LengthSquared();
155   return Evaluate( propertyValue, arg );
156 }
157
158 bool VariableStep::EvalVector3( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
159 {
160   float propertyValue = value.GetVector3().LengthSquared();
161   return Evaluate( propertyValue, arg );
162 }
163
164 bool VariableStep::EvalVector4( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
165 {
166   const float propertyValue = value.GetVector4().LengthSquared();
167   return Evaluate( propertyValue, arg );
168 }
169
170 bool VariableStep::EvalDefault( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
171 {
172   return false;
173 }
174
175 } // namespace SceneGraph
176
177 } // namespace Internal
178
179 } // namespace Dali