2 * Copyright (c) 2017 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-constrainer-impl.h>
22 #include <cstring> // for strcmp
25 #include <dali/internal/event/common/property-helper.h>
26 #include <dali/public-api/animation/constraint.h>
27 #include <dali/public-api/object/property-array.h>
39 // Name Type writable animatable constraint-input enum for index-checking
40 DALI_PROPERTY_TABLE_BEGIN
41 DALI_PROPERTY( "forward", VECTOR3, true, false, false, Dali::PathConstrainer::Property::FORWARD )
42 DALI_PROPERTY( "points", ARRAY, true, false, false, Dali::PathConstrainer::Property::POINTS )
43 DALI_PROPERTY( "controlPoints", ARRAY, true, false, false, Dali::PathConstrainer::Property::CONTROL_POINTS )
44 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
48 PathConstrainer* PathConstrainer::New()
50 return new PathConstrainer();
53 PathConstrainer::PathConstrainer()
59 PathConstrainer::~PathConstrainer()
63 unsigned int PathConstrainer::GetDefaultPropertyCount() const
65 return DEFAULT_PROPERTY_COUNT;
68 void PathConstrainer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
70 indices.Reserve( DEFAULT_PROPERTY_COUNT );
72 for ( Property::Index i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
74 indices.PushBack( i );
78 const char* PathConstrainer::GetDefaultPropertyName(Property::Index index) const
80 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
82 return DEFAULT_PROPERTY_DETAILS[index].name;
89 Property::Index PathConstrainer::GetDefaultPropertyIndex(const std::string& name) const
91 Property::Index index = Property::INVALID_INDEX;
93 // Look for name in default properties
94 for( Property::Index i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
96 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
97 if( 0 == strcmp( name.c_str(), property->name ) ) // dont want to convert rhs to string
106 Property::Type PathConstrainer::GetDefaultPropertyType(Property::Index index) const
108 if( index < DEFAULT_PROPERTY_COUNT )
110 return DEFAULT_PROPERTY_DETAILS[index].type;
113 // index out of range
114 return Property::NONE;
117 Property::Value PathConstrainer::GetDefaultProperty( Property::Index index ) const
119 if( index == Dali::PathConstrainer::Property::FORWARD )
121 return Property::Value( mForward );
125 if( index == Dali::PathConstrainer::Property::POINTS )
127 Property::Value value( Property::ARRAY );
128 Property::Array* array = value.GetArray();
129 const Dali::Vector<Vector3>& point = mPath->GetPoints();
130 Property::Array::SizeType pointCount = point.Size();
134 array->Reserve( pointCount );
135 for( Property::Array::SizeType i = 0; i < pointCount; ++i )
137 array->PushBack( point[i] );
142 else if( index == Dali::PathConstrainer::Property::CONTROL_POINTS )
144 Property::Value value( Property::ARRAY );
145 Property::Array* array = value.GetArray();
146 const Dali::Vector<Vector3>& point = mPath->GetControlPoints();
147 Property::Array::SizeType pointCount = point.Size();
151 array->Reserve( pointCount );
152 for( Property::Array::SizeType i = 0; i < pointCount; ++i )
154 array->PushBack( point[i] );
161 return Property::Value();
164 Property::Value PathConstrainer::GetDefaultPropertyCurrentValue( Property::Index index ) const
166 return GetDefaultProperty( index ); // Event-side only properties
169 void PathConstrainer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
171 if( index == Dali::PathConstrainer::Property::FORWARD )
173 propertyValue.Get(mForward);
175 else if( index == Dali::PathConstrainer::Property::POINTS )
177 const Property::Array* array = propertyValue.GetArray();
178 mPath->ClearPoints();
181 for( Property::Array::SizeType i = 0, count = array->Count(); i < count; ++i )
184 array->GetElementAt( i ).Get( point );
185 mPath->AddPoint( point );
189 else if( index == Dali::PathConstrainer::Property::CONTROL_POINTS )
191 const Property::Array* array = propertyValue.GetArray();
192 mPath->ClearControlPoints();
195 for( Property::Array::SizeType i = 0, count = array->Count(); i < count; ++i )
198 array->GetElementAt( i ).Get( point );
199 mPath->AddControlPoint( point );
205 bool PathConstrainer::IsDefaultPropertyWritable(Property::Index index) const
207 if( index < DEFAULT_PROPERTY_COUNT )
209 return DEFAULT_PROPERTY_DETAILS[index].writable;
215 bool PathConstrainer::IsDefaultPropertyAnimatable(Property::Index index) const
217 if( index < DEFAULT_PROPERTY_COUNT )
219 return DEFAULT_PROPERTY_DETAILS[index].animatable;
225 bool PathConstrainer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
227 if( index < DEFAULT_PROPERTY_COUNT )
229 return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
235 void PathConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
237 Dali::Property::Type propertyType = target.object.GetPropertyType( target.propertyIndex);
238 if( propertyType == Dali::Property::VECTOR3)
240 // If property type is Vector3, constrain its value to the position of the path
241 Dali::Constraint constraint = Dali::Constraint::New<Vector3>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, wrap ) );
242 constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
244 constraint.SetTag( static_cast<uint32_t>( reinterpret_cast<uintptr_t>( this ) ) ); // taking 32bits of this as tag
245 constraint.SetRemoveAction( Dali::Constraint::Discard );
248 else if( propertyType == Dali::Property::ROTATION )
250 // If property type is Rotation, constrain its value to align the forward vector to the tangent of the path
251 Dali::Constraint constraint = Dali::Constraint::New<Quaternion>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, mForward, wrap) );
252 constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
254 constraint.SetTag( static_cast<uint32_t>( reinterpret_cast<uintptr_t>( this ) ) ); // taking 32bits of this as tag
255 constraint.SetRemoveAction( Dali::Constraint::Discard );
259 //Start observing the object
260 Observe( target.object );