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