Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / dali / internal / update / common / property-condition-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-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_REF_VALUE = 0;
38 const int32_t ARGINDEX_STEP_SIZE = 1;
39 const int32_t ARGINDEX_CURRENT_STEP = 2;
40 const int32_t ARGINDEX_FIRST_VALUE = 3;
41 const int32_t ARGINDEX_SECOND_VALUE = 4;
42 const int32_t ARGINDEX_THIRD_VALUE = 5;
43
44 } // namespace
45
46 ConditionFunction Step::GetFunction(Property::Type valueType)
47 {
48   ConditionFunction function = NULL;
49
50   switch(valueType)
51   {
52     case Property::INTEGER:
53     {
54       function = EvalInteger;
55       break;
56     }
57     case Property::FLOAT:
58     {
59       function = EvalFloat;
60       break;
61     }
62     case Property::VECTOR2:
63     {
64       function = EvalVector2;
65       break;
66     }
67     case Property::VECTOR3:
68     {
69       function = EvalVector3;
70       break;
71     }
72     case Property::VECTOR4:
73     {
74       function = EvalVector4;
75       break;
76     }
77     default:
78     {
79       function = EvalDefault;
80       break;
81     }
82   } // end switch
83
84   return function;
85 }
86
87 ConditionFunction Step::GetCompareFunction( Property::Type valueType )
88 {
89     ConditionFunction function = NULL;
90     if( valueType == Property::VECTOR3 )
91     {
92       function = EvalAndCompareVector3;
93     }
94     else
95     {
96       function = GetFunction( valueType );
97     }
98
99     return function;
100 }
101
102 bool Step::Evaluate( const float propertyValue, PropertyNotification::RawArgumentContainer& arg )
103 {
104   const float refValue = arg[ARGINDEX_REF_VALUE];
105   const float step = arg[ARGINDEX_STEP_SIZE];
106   const int32_t currentStep = static_cast<int32_t>(arg[ARGINDEX_CURRENT_STEP]);
107   const float distance = (propertyValue - refValue);
108   // step is actual 1.0f / step so can multiply instead of dividing
109   const int32_t newStep = static_cast<int32_t>(floorf(distance * step));
110
111   if( newStep != currentStep )
112   {
113     // in new section
114     arg[ARGINDEX_CURRENT_STEP] = static_cast<float>(newStep);
115     return true;
116   }
117   return false;
118 }
119
120
121 bool Step::EvalInteger( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
122 {
123   const float propertyValue = static_cast<float>( value.GetInteger() );
124   return Evaluate( propertyValue, arg );
125 }
126
127 bool Step::EvalFloat( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
128 {
129   const float propertyValue = value.GetFloat();
130   return Evaluate( propertyValue, arg );
131 }
132
133 bool Step::EvalVector2( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
134 {
135   const float propertyValue = value.GetVector2().LengthSquared();
136   return Evaluate( propertyValue, arg );
137 }
138
139 bool Step::EvalVector3( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
140 {
141   float propertyValue = value.GetVector3().LengthSquared();
142   return Evaluate( propertyValue, arg );
143 }
144
145 bool Step::EvalAndCompareVector3( const Dali::PropertyInput& value, PropertyNotification::RawArgumentContainer& arg )
146 {
147   float propertyValue = value.GetVector3().LengthSquared();
148   bool result = Evaluate( propertyValue, arg );
149   if( result == false )
150   {
151     if( ( fabsf( arg[ARGINDEX_FIRST_VALUE] - value.GetVector3().x ) > Math::MACHINE_EPSILON_1 )
152         || ( fabsf( arg[ARGINDEX_SECOND_VALUE] - value.GetVector3().y ) > Math::MACHINE_EPSILON_1 )
153         || ( fabsf( arg[ARGINDEX_THIRD_VALUE] - value.GetVector3().z ) > Math::MACHINE_EPSILON_1 ) )
154     {
155       result = true;
156     }
157   }
158   arg[ARGINDEX_FIRST_VALUE] = value.GetVector3().x;
159   arg[ARGINDEX_SECOND_VALUE] = value.GetVector3().y;
160   arg[ARGINDEX_THIRD_VALUE] = value.GetVector3().z;
161   return result;
162 }
163
164 bool Step::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 Step::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