3ce2a78a6f0a0fb5954527244d9e1181a53ccdf2
[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 Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/event/animation/constraint-impl.h>
19
20 // EXTERNAL INCLUDES
21 #include <boost/any.hpp>
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>( boost::any_cast<boost::function<P (const P&)> >( func ) );
49       break;
50     }
51
52     case 1u:
53     {
54       propertyConstraint = new PropertyConstraint1<P, PropertyInputAccessor>( boost::any_cast< boost::function<P (const P&,
55                                                                                                                   const PropertyInput&)> >( func ) );
56       break;
57     }
58
59     case 2u:
60     {
61       propertyConstraint = new PropertyConstraint2<P, PropertyInputAccessor>( boost::any_cast< 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>( boost::any_cast< 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>( boost::any_cast< 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>( boost::any_cast< 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>( boost::any_cast< 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 = ActiveConstraint<bool>::New( targetIndex,
137                                                                sources,
138                                                                funcPtr,
139                                                                boost::any_cast< BoolInterpolator >( interpolator ) );
140       break;
141     }
142
143     case Property::FLOAT:
144     {
145       PropertyConstraintPtr<float>::Type funcPtr( CreatePropertyConstraint<float>( func, sources.size() ) );
146
147       mActiveConstraintTemplate = ActiveConstraint<float>::New( targetIndex,
148                                                                 sources,
149                                                                 funcPtr,
150                                                                 boost::any_cast< FloatInterpolator >( interpolator ) );
151       break;
152     }
153
154     case Property::VECTOR2:
155     {
156       PropertyConstraintPtr<Vector2>::Type funcPtr( CreatePropertyConstraint<Vector2>( func, sources.size() ) );
157
158       mActiveConstraintTemplate = ActiveConstraint<Vector2>::New( targetIndex,
159                                                                   sources,
160                                                                   funcPtr,
161                                                                   boost::any_cast< Vector2Interpolator >( interpolator ) );
162       break;
163     }
164
165     case Property::VECTOR3:
166     {
167       PropertyConstraintPtr<Vector3>::Type funcPtr( CreatePropertyConstraint<Vector3>( func, sources.size() ) );
168
169       mActiveConstraintTemplate = ActiveConstraint<Vector3>::New( targetIndex,
170                                                                   sources,
171                                                                   funcPtr,
172                                                                   boost::any_cast< Vector3Interpolator >( interpolator ) );
173       break;
174     }
175
176     case Property::VECTOR4:
177     {
178       PropertyConstraintPtr<Vector4>::Type funcPtr( CreatePropertyConstraint<Vector4>( func, sources.size() ) );
179
180       mActiveConstraintTemplate = ActiveConstraint<Vector4>::New( targetIndex,
181                                                                   sources,
182                                                                   funcPtr,
183                                                                   boost::any_cast< Vector4Interpolator >( interpolator ) );
184       break;
185     }
186
187     case Property::ROTATION:
188     {
189       PropertyConstraintPtr<Quaternion>::Type funcPtr( CreatePropertyConstraint<Quaternion>( func, sources.size() ) );
190
191       mActiveConstraintTemplate = ActiveConstraint<Quaternion>::New( targetIndex,
192                                                                      sources,
193                                                                      funcPtr,
194                                                                      boost::any_cast< QuaternionInterpolator >( interpolator ) );
195       break;
196     }
197
198     case Property::MATRIX:
199     {
200       PropertyConstraintPtr<Matrix>::Type funcPtr( CreatePropertyConstraint<Matrix>( func, sources.size() ) );
201
202       mActiveConstraintTemplate = ActiveConstraint<Matrix>::New( targetIndex,
203                                                                  sources,
204                                                                  funcPtr,
205                                                                  boost::any_cast< MatrixInterpolator >( interpolator ) );
206       break;
207     }
208
209     case Property::MATRIX3:
210     {
211       PropertyConstraintPtr<Matrix3>::Type funcPtr( CreatePropertyConstraint<Matrix3>( func, sources.size() ) );
212
213       mActiveConstraintTemplate = ActiveConstraint<Matrix3>::New( targetIndex,
214                                                                   sources,
215                                                                   funcPtr,
216                                                                   boost::any_cast< Matrix3Interpolator >( interpolator ) );
217       break;
218     }
219
220     default:
221     {
222       DALI_ASSERT_ALWAYS( false && "Property type enumeration out of bounds" ); // should never come here
223       break;
224     }
225   }
226 }
227
228 ActiveConstraintBase* Constraint::CreateActiveConstraint()
229 {
230   return GetImplementation( mActiveConstraintTemplate ).Clone();
231 }
232
233 void Constraint::SetApplyTime( TimePeriod timePeriod )
234 {
235   mApplyTime = timePeriod;
236 }
237
238 TimePeriod Constraint::GetApplyTime() const
239 {
240   return mApplyTime;
241 }
242
243 void Constraint::SetRemoveTime( TimePeriod timePeriod )
244 {
245   GetImplementation( mActiveConstraintTemplate ).SetRemoveTime( timePeriod );
246 }
247
248 TimePeriod Constraint::GetRemoveTime() const
249 {
250   return GetImplementation( mActiveConstraintTemplate ).GetRemoveTime();
251 }
252
253 void Constraint::SetAlphaFunction( Dali::AlphaFunction func )
254 {
255   GetImplementation( mActiveConstraintTemplate ).SetAlphaFunction( func );
256 }
257
258 Dali::AlphaFunction Constraint::GetAlphaFunction() const
259 {
260   return GetImplementation( mActiveConstraintTemplate ).GetAlphaFunction();
261 }
262
263 void Constraint::SetRemoveAction( Dali::Constraint::RemoveAction action )
264 {
265   GetImplementation( mActiveConstraintTemplate ).SetRemoveAction( action );
266 }
267
268 Dali::Constraint::RemoveAction Constraint::GetRemoveAction() const
269 {
270   return GetImplementation( mActiveConstraintTemplate ).GetRemoveAction();
271 }
272
273 Constraint::~Constraint()
274 {
275 }
276
277 } // namespace Internal
278
279 } // namespace Dali