2 * Copyright (c) 2015 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/path-constraint-impl.h>
22 #include <dali/internal/event/common/property-helper.h>
23 #include <dali/public-api/animation/constraint.h>
36 // Name Type writable animatable constraint-input enum for index-checking
37 DALI_PROPERTY_TABLE_BEGIN
38 DALI_PROPERTY( "range", VECTOR2, true, false, false, Dali::PathConstraint::Property::RANGE )
39 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
44 PathConstraint* PathConstraint::New( Path& path, const Vector2& range)
46 return new PathConstraint( path, range);
49 PathConstraint::PathConstraint( Path& path, const Vector2& range)
51 mPath( Path::Clone(path)),
56 PathConstraint::~PathConstraint()
58 //Remove constraints created by this PathConstraint
59 size_t tag = reinterpret_cast<size_t>( this );
60 const ObjectIter end = mObservedObjects.End();
61 for( ObjectIter iter = mObservedObjects.Begin(); iter != end; ++iter )
63 //Remove PathConstraint from the observers list of the object
64 (*iter)->RemoveObserver( *this );
66 //Remove constraints created by this PathConstraint in the object
67 (*iter)->RemoveConstraints( tag );
71 void PathConstraint::ObjectDestroyed(Object& object)
73 //Remove object from the list of observed
74 const ObjectIter end = mObservedObjects.End();
75 for( ObjectIter iter = mObservedObjects.Begin(); iter != end; ++iter )
77 if( *iter == &object )
79 mObservedObjects.Erase(iter);
85 unsigned int PathConstraint::GetDefaultPropertyCount() const
87 return DEFAULT_PROPERTY_COUNT;
90 void PathConstraint::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
92 indices.reserve( DEFAULT_PROPERTY_COUNT );
94 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
96 indices.push_back( i );
100 const char* PathConstraint::GetDefaultPropertyName(Property::Index index) const
102 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
104 return DEFAULT_PROPERTY_DETAILS[index].name;
107 // index out of range
111 Property::Index PathConstraint::GetDefaultPropertyIndex(const std::string& name) const
113 Property::Index index = Property::INVALID_INDEX;
115 // Look for name in default properties
116 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
118 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
119 if( 0 == strcmp( name.c_str(), property->name ) ) // dont want to convert rhs to string
128 Property::Type PathConstraint::GetDefaultPropertyType(Property::Index index) const
130 if( index < DEFAULT_PROPERTY_COUNT )
132 return DEFAULT_PROPERTY_DETAILS[index].type;
135 // index out of range
136 return Property::NONE;
139 Property::Value PathConstraint::GetDefaultProperty( Property::Index index ) const
141 if( index == Dali::PathConstraint::Property::RANGE )
143 return Property::Value( mRange );
146 return Property::Value();
149 void PathConstraint::SetDefaultProperty(Property::Index index, const Property::Value& propertyValue)
151 if( index == Dali::PathConstraint::Property::RANGE )
153 propertyValue.Get(mRange);
157 bool PathConstraint::IsDefaultPropertyWritable(Property::Index index) const
159 if( index < DEFAULT_PROPERTY_COUNT )
161 return DEFAULT_PROPERTY_DETAILS[index].writable;
167 bool PathConstraint::IsDefaultPropertyAnimatable(Property::Index index) const
169 if( index < DEFAULT_PROPERTY_COUNT )
171 return DEFAULT_PROPERTY_DETAILS[index].animatable;
177 bool PathConstraint::IsDefaultPropertyAConstraintInput( Property::Index index ) const
179 if( index < DEFAULT_PROPERTY_COUNT )
181 return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
187 void PathConstraint::Apply( Property source, Property target, const Vector3& forward)
190 Dali::Property::Type propertyType = target.object.GetPropertyType( target.propertyIndex);
193 if( propertyType == Dali::Property::VECTOR3)
195 //If property is Vector3, contraint its value to the position of the path
197 Dali::Constraint constraint = Dali::Constraint::New<Vector3>( target.propertyIndex,
198 Source(source.object, source.propertyIndex ),
199 PathConstraintFunctor( mPath, mRange ) );
201 constraint.SetTag( reinterpret_cast<size_t>( this ) );
202 constraint.SetRemoveAction( Dali::Constraint::Discard );
203 target.object.ApplyConstraint(constraint);
205 else if( propertyType == Dali::Property::ROTATION )
207 //If property is Rotation, constraint its value to align the forward vector to the tangent of the path
208 Dali::Constraint constraint = Dali::Constraint::New<Quaternion>( target.propertyIndex,
209 Source(source.object, source.propertyIndex ),
210 PathConstraintFunctor( mPath, mRange,forward) );
212 constraint.SetTag( reinterpret_cast<size_t>( this ) );
213 constraint.SetRemoveAction( Dali::Constraint::Discard );
214 target.object.ApplyConstraint(constraint);
217 //Add the object to the list of observed objects if it is not there already
218 Object& object = dynamic_cast<Object&>( GetImplementation(target.object) );
219 const ObjectIter end = mObservedObjects.End();
220 ObjectIter iter = mObservedObjects.Begin();
221 for(; iter != end; ++iter )
223 if( *iter == &object )
231 //Start observing the object
232 object.AddObserver( *this );
234 //Add object in the observed objects vector
235 mObservedObjects.PushBack( &object );
239 void PathConstraint::Remove( Dali::Handle& target )
241 Object& object = dynamic_cast<Object&>( GetImplementation(target) );
242 const ObjectIter end = mObservedObjects.End();
243 for( ObjectIter iter = mObservedObjects.Begin(); iter != end; ++iter )
245 if( *iter == &object )
247 //Stop observing the object
248 (*iter)->RemoveObserver( *this );
250 //Remove constraints created by PathConstraint in the object
251 size_t tag = reinterpret_cast<size_t>( this );
252 target.RemoveConstraints( tag );
254 //Remove object from the vector of observed objects
255 mObservedObjects.Erase(iter);