Merge "Added api function to specify speed factor of an animation" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / constraint-impl.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 // CLASS HEADER
19 #include <dali/internal/event/animation/constraint-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <boost/function.hpp>
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/event/animation/active-constraint-impl.h>
26 #include <dali/internal/event/animation/property-constraint-ptr.h>
27 #include <dali/internal/event/animation/property-constraint.h>
28 #include <dali/internal/event/animation/property-input-accessor.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace // unnamed namespace
37 {
38
39 template <class P>
40 PropertyConstraintBase<P>* CreatePropertyConstraint( Constraint::AnyFunction& func, unsigned int sourceCount )
41 {
42   PropertyConstraintBase<P>* propertyConstraint( NULL );
43
44   switch ( sourceCount )
45   {
46     case 0u:
47     {
48       propertyConstraint = new PropertyConstraint0<P>( AnyCast<boost::function<P (const P&)> >( func ) );
49       break;
50     }
51
52     case 1u:
53     {
54       propertyConstraint = new PropertyConstraint1<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
55                                                                                                                   const PropertyInput&)> >( func ) );
56       break;
57     }
58
59     case 2u:
60     {
61       propertyConstraint = new PropertyConstraint2<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
62                                                                                                                   const PropertyInput&,
63                                                                                                                   const PropertyInput&)> >( func ) );
64       break;
65     }
66
67     case 3u:
68     {
69       propertyConstraint = new PropertyConstraint3<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
70                                                                                                                   const PropertyInput&,
71                                                                                                                   const PropertyInput&,
72                                                                                                                   const PropertyInput&)> >( func ) );
73       break;
74     }
75
76     case 4u:
77     {
78       propertyConstraint = new PropertyConstraint4<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
79                                                                                                                   const PropertyInput&,
80                                                                                                                   const PropertyInput&,
81                                                                                                                   const PropertyInput&,
82                                                                                                                   const PropertyInput&)> >( func ) );
83       break;
84     }
85
86     case 5u:
87     {
88       propertyConstraint = new PropertyConstraint5<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
89                                                                                                                   const PropertyInput&,
90                                                                                                                   const PropertyInput&,
91                                                                                                                   const PropertyInput&,
92                                                                                                                   const PropertyInput&,
93                                                                                                                   const PropertyInput&)> >( func ) );
94       break;
95     }
96
97     case 6u:
98     {
99       propertyConstraint = new PropertyConstraint6<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
100                                                                                                                   const PropertyInput&,
101                                                                                                                   const PropertyInput&,
102                                                                                                                   const PropertyInput&,
103                                                                                                                   const PropertyInput&,
104                                                                                                                   const PropertyInput&,
105                                                                                                                   const PropertyInput&)> >( func ) );
106       break;
107     }
108
109
110     default:
111     {
112       // should never come here
113       DALI_ASSERT_ALWAYS( false && "Cannot have more than 6 property constraints" );
114       break;
115     }
116   }
117
118   return propertyConstraint;
119 }
120
121 } // unnamed namespace
122
123 Constraint::Constraint( Property::Index targetIndex,
124                         Property::Type targetType,
125                         SourceContainer& sources,
126                         AnyFunction& func,
127                         AnyFunction& interpolator )
128 : mApplyTime( 0.0f )
129 {
130   switch ( targetType )
131   {
132     case Property::BOOLEAN:
133     {
134       PropertyConstraintPtr<bool>::Type funcPtr( CreatePropertyConstraint<bool>( func, sources.size() ) );
135
136       mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<bool>::New( targetIndex,
137                                                                                        sources,
138                                                                                        funcPtr,
139                                                                                        AnyCast< BoolInterpolator >( interpolator ) ) );
140       break;
141     }
142
143     case Property::FLOAT:
144     {
145       PropertyConstraintPtr<float>::Type funcPtr( CreatePropertyConstraint<float>( func, sources.size() ) );
146
147       mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<float>::New( targetIndex,
148                                                                                         sources,
149                                                                                         funcPtr,
150                                                                                         AnyCast< FloatInterpolator >( interpolator ) ) );
151       break;
152     }
153
154     case Property::INTEGER:
155     {
156       PropertyConstraintPtr<int>::Type funcPtr( CreatePropertyConstraint<int>( func, sources.size() ) );
157
158       mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<int>::New( targetIndex,
159                                                                                       sources,
160                                                                                       funcPtr,
161                                                                                       AnyCast< IntegerInterpolator >( interpolator ) ) );
162       break;
163     }
164
165     case Property::VECTOR2:
166     {
167       PropertyConstraintPtr<Vector2>::Type funcPtr( CreatePropertyConstraint<Vector2>( func, sources.size() ) );
168
169       mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Vector2>::New( targetIndex,
170                                                                                           sources,
171                                                                                           funcPtr,
172                                                                                           AnyCast< Vector2Interpolator >( interpolator ) ) );
173       break;
174     }
175
176     case Property::VECTOR3:
177     {
178       PropertyConstraintPtr<Vector3>::Type funcPtr( CreatePropertyConstraint<Vector3>( func, sources.size() ) );
179
180       mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Vector3>::New( targetIndex,
181                                                                                           sources,
182                                                                                           funcPtr,
183                                                                                           AnyCast< Vector3Interpolator >( interpolator ) ) );
184       break;
185     }
186
187     case Property::VECTOR4:
188     {
189       PropertyConstraintPtr<Vector4>::Type funcPtr( CreatePropertyConstraint<Vector4>( func, sources.size() ) );
190
191       mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Vector4>::New( targetIndex,
192                                                                                           sources,
193                                                                                           funcPtr,
194                                                                                           AnyCast< Vector4Interpolator >( interpolator ) ) );
195       break;
196     }
197
198     case Property::ROTATION:
199     {
200       PropertyConstraintPtr<Quaternion>::Type funcPtr( CreatePropertyConstraint<Quaternion>( func, sources.size() ) );
201
202       mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Quaternion>::New( targetIndex,
203                                                                                              sources,
204                                                                                              funcPtr,
205                                                                                              AnyCast< QuaternionInterpolator >( interpolator ) ) );
206       break;
207     }
208
209     case Property::MATRIX:
210     {
211       PropertyConstraintPtr<Matrix>::Type funcPtr( CreatePropertyConstraint<Matrix>( func, sources.size() ) );
212
213       mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Matrix>::New( targetIndex,
214                                                                                          sources,
215                                                                                          funcPtr,
216                                                                                          AnyCast< MatrixInterpolator >( interpolator ) ) );
217       break;
218     }
219
220     case Property::MATRIX3:
221     {
222       PropertyConstraintPtr<Matrix3>::Type funcPtr( CreatePropertyConstraint<Matrix3>( func, sources.size() ) );
223
224       mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Matrix3>::New( targetIndex,
225                                                                                           sources,
226                                                                                           funcPtr,
227                                                                                           AnyCast< Matrix3Interpolator >( interpolator ) ) );
228       break;
229     }
230
231     default:
232     {
233       DALI_ASSERT_ALWAYS( false && "Property type enumeration out of bounds" ); // should never come here
234       break;
235     }
236   }
237 }
238
239 ActiveConstraintBase* Constraint::CreateActiveConstraint()
240 {
241   return GetImplementation( mActiveConstraintTemplate ).Clone();
242 }
243
244 void Constraint::SetApplyTime( TimePeriod timePeriod )
245 {
246   mApplyTime = timePeriod;
247 }
248
249 TimePeriod Constraint::GetApplyTime() const
250 {
251   return mApplyTime;
252 }
253
254 void Constraint::SetAlphaFunction( Dali::AlphaFunction func )
255 {
256   GetImplementation( mActiveConstraintTemplate ).SetAlphaFunction( func );
257 }
258
259 Dali::AlphaFunction Constraint::GetAlphaFunction() const
260 {
261   return GetImplementation( mActiveConstraintTemplate ).GetAlphaFunction();
262 }
263
264 void Constraint::SetRemoveAction( Dali::Constraint::RemoveAction action )
265 {
266   GetImplementation( mActiveConstraintTemplate ).SetRemoveAction( action );
267 }
268
269 Dali::Constraint::RemoveAction Constraint::GetRemoveAction() const
270 {
271   return GetImplementation( mActiveConstraintTemplate ).GetRemoveAction();
272 }
273
274
275 void Constraint::SetTag(unsigned int tag)
276 {
277   GetImplementation( mActiveConstraintTemplate ).SetTag(tag);
278 }
279
280 unsigned int Constraint::GetTag() const
281 {
282   return GetImplementation( mActiveConstraintTemplate ).GetTag();
283 }
284
285
286
287 Constraint::~Constraint()
288 {
289 }
290
291 } // namespace Internal
292
293 } // namespace Dali