2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/animation/constraint-impl.h>
22 #include <boost/function.hpp>
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>
38 // Constraint Interpolation function prototypes
39 typedef boost::function<bool (const bool& start, const bool& target, float progress)> BoolInterpolator;
40 typedef boost::function<float (const float& start, const float& target, float progress)> FloatInterpolator;
41 typedef boost::function<int (const int& start, const int& target, float progress)> IntegerInterpolator;
42 typedef boost::function<Dali::Vector2 (const Dali::Vector2& current, const Dali::Vector2& target, float progress)> Vector2Interpolator;
43 typedef boost::function<Dali::Vector3 (const Dali::Vector3& current, const Dali::Vector3& target, float progress)> Vector3Interpolator;
44 typedef boost::function<Dali::Vector4 (const Dali::Vector4& current, const Dali::Vector4& target, float progress)> Vector4Interpolator;
45 typedef boost::function<Dali::Quaternion (const Dali::Quaternion& current, const Dali::Quaternion& target, float progress)> QuaternionInterpolator;
46 typedef boost::function<Dali::Matrix3 (const Dali::Matrix3& current, const Dali::Matrix3& target, float progress)> Matrix3Interpolator;
47 typedef boost::function<Dali::Matrix (const Dali::Matrix& current, const Dali::Matrix& target, float progress)> MatrixInterpolator;
57 namespace // unnamed namespace
61 PropertyConstraintBase<P>* CreatePropertyConstraint( Constraint::AnyFunction& func, unsigned int sourceCount )
63 PropertyConstraintBase<P>* propertyConstraint( NULL );
65 switch ( sourceCount )
69 propertyConstraint = new PropertyConstraint0<P>( AnyCast<boost::function<P (const P&)> >( func ) );
75 propertyConstraint = new PropertyConstraint1<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
76 const PropertyInput&)> >( func ) );
82 propertyConstraint = new PropertyConstraint2<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
84 const PropertyInput&)> >( func ) );
90 propertyConstraint = new PropertyConstraint3<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
93 const PropertyInput&)> >( func ) );
99 propertyConstraint = new PropertyConstraint4<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
100 const PropertyInput&,
101 const PropertyInput&,
102 const PropertyInput&,
103 const PropertyInput&)> >( func ) );
109 propertyConstraint = new PropertyConstraint5<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
110 const PropertyInput&,
111 const PropertyInput&,
112 const PropertyInput&,
113 const PropertyInput&,
114 const PropertyInput&)> >( func ) );
120 propertyConstraint = new PropertyConstraint6<P, PropertyInputAccessor>( AnyCast< boost::function<P (const P&,
121 const PropertyInput&,
122 const PropertyInput&,
123 const PropertyInput&,
124 const PropertyInput&,
125 const PropertyInput&,
126 const PropertyInput&)> >( func ) );
133 // should never come here
134 DALI_ASSERT_ALWAYS( false && "Cannot have more than 6 property constraints" );
139 return propertyConstraint;
142 } // unnamed namespace
144 Constraint::Constraint( Property::Index targetIndex,
145 Property::Type targetType,
146 SourceContainer& sources,
148 AnyFunction& interpolator )
151 switch ( targetType )
153 case Property::BOOLEAN:
155 PropertyConstraintPtr<bool>::Type funcPtr( CreatePropertyConstraint<bool>( func, sources.size() ) );
157 mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<bool>::New( targetIndex,
160 AnyCast< BoolInterpolator >( interpolator ) ) );
164 case Property::FLOAT:
166 PropertyConstraintPtr<float>::Type funcPtr( CreatePropertyConstraint<float>( func, sources.size() ) );
168 mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<float>::New( targetIndex,
171 AnyCast< FloatInterpolator >( interpolator ) ) );
175 case Property::INTEGER:
177 PropertyConstraintPtr<int>::Type funcPtr( CreatePropertyConstraint<int>( func, sources.size() ) );
179 mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<int>::New( targetIndex,
182 AnyCast< IntegerInterpolator >( interpolator ) ) );
186 case Property::VECTOR2:
188 PropertyConstraintPtr<Vector2>::Type funcPtr( CreatePropertyConstraint<Vector2>( func, sources.size() ) );
190 mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Vector2>::New( targetIndex,
193 AnyCast< Vector2Interpolator >( interpolator ) ) );
197 case Property::VECTOR3:
199 PropertyConstraintPtr<Vector3>::Type funcPtr( CreatePropertyConstraint<Vector3>( func, sources.size() ) );
201 mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Vector3>::New( targetIndex,
204 AnyCast< Vector3Interpolator >( interpolator ) ) );
208 case Property::VECTOR4:
210 PropertyConstraintPtr<Vector4>::Type funcPtr( CreatePropertyConstraint<Vector4>( func, sources.size() ) );
212 mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Vector4>::New( targetIndex,
215 AnyCast< Vector4Interpolator >( interpolator ) ) );
219 case Property::ROTATION:
221 PropertyConstraintPtr<Quaternion>::Type funcPtr( CreatePropertyConstraint<Quaternion>( func, sources.size() ) );
223 mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Quaternion>::New( targetIndex,
226 AnyCast< QuaternionInterpolator >( interpolator ) ) );
230 case Property::MATRIX:
232 PropertyConstraintPtr<Matrix>::Type funcPtr( CreatePropertyConstraint<Matrix>( func, sources.size() ) );
234 mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Matrix>::New( targetIndex,
237 AnyCast< MatrixInterpolator >( interpolator ) ) );
241 case Property::MATRIX3:
243 PropertyConstraintPtr<Matrix3>::Type funcPtr( CreatePropertyConstraint<Matrix3>( func, sources.size() ) );
245 mActiveConstraintTemplate = Dali::ActiveConstraint( ActiveConstraint<Matrix3>::New( targetIndex,
248 AnyCast< Matrix3Interpolator >( interpolator ) ) );
254 DALI_ASSERT_ALWAYS( false && "Property type enumeration out of bounds" ); // should never come here
260 ActiveConstraintBase* Constraint::CreateActiveConstraint()
262 return GetImplementation( mActiveConstraintTemplate ).Clone();
265 void Constraint::SetApplyTime( TimePeriod timePeriod )
267 mApplyTime = timePeriod;
270 TimePeriod Constraint::GetApplyTime() const
275 void Constraint::SetAlphaFunction( Dali::AlphaFunction func )
277 GetImplementation( mActiveConstraintTemplate ).SetAlphaFunction( func );
280 Dali::AlphaFunction Constraint::GetAlphaFunction() const
282 return GetImplementation( mActiveConstraintTemplate ).GetAlphaFunction();
285 void Constraint::SetRemoveAction( Dali::Constraint::RemoveAction action )
287 GetImplementation( mActiveConstraintTemplate ).SetRemoveAction( action );
290 Dali::Constraint::RemoveAction Constraint::GetRemoveAction() const
292 return GetImplementation( mActiveConstraintTemplate ).GetRemoveAction();
296 void Constraint::SetTag(unsigned int tag)
298 GetImplementation( mActiveConstraintTemplate ).SetTag(tag);
301 unsigned int Constraint::GetTag() const
303 return GetImplementation( mActiveConstraintTemplate ).GetTag();
308 Constraint::~Constraint()
312 } // namespace Internal