2 * Copyright (c) 2018 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/public-api/animation/constraint.h>
26 #include <dali/public-api/object/property-array.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/internal/event/common/property-helper.h>
40 // Name Type writable animatable constraint-input enum for index-checking
41 DALI_PROPERTY_TABLE_BEGIN
42 DALI_PROPERTY( "forward", VECTOR3, true, false, false, Dali::PathConstrainer::Property::FORWARD )
43 DALI_PROPERTY( "points", ARRAY, true, false, false, Dali::PathConstrainer::Property::POINTS )
44 DALI_PROPERTY( "controlPoints", ARRAY, true, false, false, Dali::PathConstrainer::Property::CONTROL_POINTS )
45 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX, PathConstrainerDefaultProperties )
49 return Dali::PathConstrainer::New();
52 TypeRegistration mType( typeid( Dali::PathConstrainer ), typeid( Dali::Handle ), Create, PathConstrainerDefaultProperties );
56 PathConstrainer* PathConstrainer::New()
58 return new PathConstrainer();
61 PathConstrainer::PathConstrainer()
67 PathConstrainer::~PathConstrainer()
71 Property::Value PathConstrainer::GetDefaultProperty( Property::Index index ) const
73 if( index == Dali::PathConstrainer::Property::FORWARD )
75 return Property::Value( mForward );
79 if( index == Dali::PathConstrainer::Property::POINTS )
81 Property::Value value( Property::ARRAY );
82 Property::Array* array = value.GetArray();
83 const Dali::Vector<Vector3>& point = mPath->GetPoints();
84 Property::Array::SizeType pointCount = static_cast<Property::Array::SizeType>( point.Size() );
88 array->Reserve( pointCount );
89 for( Property::Array::SizeType i = 0; i < pointCount; ++i )
91 array->PushBack( point[i] );
96 else if( index == Dali::PathConstrainer::Property::CONTROL_POINTS )
98 Property::Value value( Property::ARRAY );
99 Property::Array* array = value.GetArray();
100 const Dali::Vector<Vector3>& point = mPath->GetControlPoints();
101 Property::Array::SizeType pointCount = static_cast<Property::Array::SizeType>( point.Size() );
105 array->Reserve( pointCount );
106 for( Property::Array::SizeType i = 0; i < pointCount; ++i )
108 array->PushBack( point[i] );
115 return Property::Value();
118 Property::Value PathConstrainer::GetDefaultPropertyCurrentValue( Property::Index index ) const
120 return GetDefaultProperty( index ); // Event-side only properties
123 void PathConstrainer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
125 if( index == Dali::PathConstrainer::Property::FORWARD )
127 propertyValue.Get(mForward);
129 else if( index == Dali::PathConstrainer::Property::POINTS )
131 const Property::Array* array = propertyValue.GetArray();
132 mPath->ClearPoints();
135 for( Property::Array::SizeType i = 0, count = array->Count(); i < count; ++i )
138 array->GetElementAt( i ).Get( point );
139 mPath->AddPoint( point );
143 else if( index == Dali::PathConstrainer::Property::CONTROL_POINTS )
145 const Property::Array* array = propertyValue.GetArray();
146 mPath->ClearControlPoints();
149 for( Property::Array::SizeType i = 0, count = array->Count(); i < count; ++i )
152 array->GetElementAt( i ).Get( point );
153 mPath->AddControlPoint( point );
159 void PathConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
161 Dali::Property::Type propertyType = target.object.GetPropertyType( target.propertyIndex);
162 if( propertyType == Dali::Property::VECTOR3)
164 // If property type is Vector3, constrain its value to the position of the path
165 Dali::Constraint constraint = Dali::Constraint::New<Vector3>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, wrap ) );
166 constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
168 constraint.SetTag( static_cast<uint32_t>( reinterpret_cast<uintptr_t>( this ) ) ); // taking 32bits of this as tag
169 constraint.SetRemoveAction( Dali::Constraint::Discard );
172 else if( propertyType == Dali::Property::ROTATION )
174 // If property type is Rotation, constrain its value to align the forward vector to the tangent of the path
175 Dali::Constraint constraint = Dali::Constraint::New<Quaternion>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, mForward, wrap) );
176 constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
178 constraint.SetTag( static_cast<uint32_t>( reinterpret_cast<uintptr_t>( this ) ) ); // taking 32bits of this as tag
179 constraint.SetRemoveAction( Dali::Constraint::Discard );
183 //Start observing the object
184 Observe( target.object );