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() = default;
69 Property::Value PathConstrainer::GetDefaultProperty( Property::Index index ) const
71 if( index == Dali::PathConstrainer::Property::FORWARD )
73 return Property::Value( mForward );
77 if( index == Dali::PathConstrainer::Property::POINTS )
79 Property::Value value( Property::ARRAY );
80 Property::Array* array = value.GetArray();
81 const Dali::Vector<Vector3>& point = mPath->GetPoints();
82 Property::Array::SizeType pointCount = static_cast<Property::Array::SizeType>( point.Size() );
86 array->Reserve( pointCount );
87 for( Property::Array::SizeType i = 0; i < pointCount; ++i )
89 array->PushBack( point[i] );
94 else if( index == Dali::PathConstrainer::Property::CONTROL_POINTS )
96 Property::Value value( Property::ARRAY );
97 Property::Array* array = value.GetArray();
98 const Dali::Vector<Vector3>& point = mPath->GetControlPoints();
99 Property::Array::SizeType pointCount = static_cast<Property::Array::SizeType>( point.Size() );
103 array->Reserve( pointCount );
104 for( Property::Array::SizeType i = 0; i < pointCount; ++i )
106 array->PushBack( point[i] );
113 return Property::Value();
116 Property::Value PathConstrainer::GetDefaultPropertyCurrentValue( Property::Index index ) const
118 return GetDefaultProperty( index ); // Event-side only properties
121 void PathConstrainer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
123 if( index == Dali::PathConstrainer::Property::FORWARD )
125 propertyValue.Get(mForward);
127 else if( index == Dali::PathConstrainer::Property::POINTS )
129 const Property::Array* array = propertyValue.GetArray();
130 mPath->ClearPoints();
133 for( Property::Array::SizeType i = 0, count = array->Count(); i < count; ++i )
136 array->GetElementAt( i ).Get( point );
137 mPath->AddPoint( point );
141 else if( index == Dali::PathConstrainer::Property::CONTROL_POINTS )
143 const Property::Array* array = propertyValue.GetArray();
144 mPath->ClearControlPoints();
147 for( Property::Array::SizeType i = 0, count = array->Count(); i < count; ++i )
150 array->GetElementAt( i ).Get( point );
151 mPath->AddControlPoint( point );
157 void PathConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
159 Dali::Property::Type propertyType = target.object.GetPropertyType( target.propertyIndex);
160 if( propertyType == Dali::Property::VECTOR3)
162 // If property type is Vector3, constrain its value to the position of the path
163 Dali::Constraint constraint = Dali::Constraint::New<Vector3>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, wrap ) );
164 constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
166 constraint.SetTag( static_cast<uint32_t>( reinterpret_cast<uintptr_t>( this ) ) ); // taking 32bits of this as tag
167 constraint.SetRemoveAction( Dali::Constraint::DISCARD );
170 else if( propertyType == Dali::Property::ROTATION )
172 // If property type is Rotation, constrain its value to align the forward vector to the tangent of the path
173 Dali::Constraint constraint = Dali::Constraint::New<Quaternion>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, mForward, wrap) );
174 constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
176 constraint.SetTag( static_cast<uint32_t>( reinterpret_cast<uintptr_t>( this ) ) ); // taking 32bits of this as tag
177 constraint.SetRemoveAction( Dali::Constraint::DISCARD );
181 //Start observing the object
182 Observe( target.object );