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/linear-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>
40 // Name Type writable animatable constraint-input enum for index-checking
41 DALI_PROPERTY_TABLE_BEGIN
42 DALI_PROPERTY( "value", ARRAY, true, false, false, Dali::LinearConstrainer::Property::VALUE )
43 DALI_PROPERTY( "progress", ARRAY, true, false, false, Dali::LinearConstrainer::Property::PROGRESS )
44 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
48 LinearConstrainer* LinearConstrainer::New()
50 return new LinearConstrainer();
53 LinearConstrainer::LinearConstrainer()
58 LinearConstrainer::~LinearConstrainer()
62 unsigned int LinearConstrainer::GetDefaultPropertyCount() const
64 return DEFAULT_PROPERTY_COUNT;
67 void LinearConstrainer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
69 indices.Reserve( DEFAULT_PROPERTY_COUNT );
71 for ( Property::Index i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
73 indices.PushBack( i );
77 const char* LinearConstrainer::GetDefaultPropertyName(Property::Index index) const
79 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
81 return DEFAULT_PROPERTY_DETAILS[index].name;
88 Property::Index LinearConstrainer::GetDefaultPropertyIndex(const std::string& name) const
90 Property::Index index = Property::INVALID_INDEX;
92 // Look for name in default properties
93 for( Property::Index i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
95 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
96 if( 0 == strcmp( name.c_str(), property->name ) )
105 Property::Type LinearConstrainer::GetDefaultPropertyType(Property::Index index) const
107 if( index < DEFAULT_PROPERTY_COUNT )
109 return DEFAULT_PROPERTY_DETAILS[index].type;
112 // index out of range
113 return Property::NONE;
116 Property::Value LinearConstrainer::GetDefaultProperty( Property::Index index ) const
118 if( index == Dali::LinearConstrainer::Property::VALUE )
120 Property::Value value( Property::ARRAY );
121 Property::Array* array = value.GetArray();
122 size_t count( mValue.Size() );
126 array->Reserve( count );
127 for( size_t i( 0 ); i != count; ++i )
129 array->PushBack( mValue[i] );
134 else if( index == Dali::LinearConstrainer::Property::PROGRESS )
136 Property::Value value( Property::ARRAY );
137 Property::Array* array = value.GetArray();
138 size_t count( mProgress.Size() );
142 array->Reserve( count );
143 for( size_t i( 0 ); i != count; ++i )
145 array->PushBack( mProgress[i] );
151 return Property::Value();
154 Property::Value LinearConstrainer::GetDefaultPropertyCurrentValue( Property::Index index ) const
156 return GetDefaultProperty( index ); // Event-side only properties
159 void LinearConstrainer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
161 const Property::Array* array = propertyValue.GetArray();
164 size_t propertyArrayCount = array->Count();
165 if( index == Dali::LinearConstrainer::Property::VALUE )
167 mValue.Clear(); // remove old values
168 mValue.Resize( propertyArrayCount );
169 for( size_t i(0); i != propertyArrayCount; ++i )
171 array->GetElementAt( i ).Get( mValue[ i ] );
174 else if( index == Dali::LinearConstrainer::Property::PROGRESS )
176 mProgress.Clear(); // remove old values
177 mProgress.Resize( propertyArrayCount );
178 for( size_t i(0); i != propertyArrayCount; ++i )
180 array->GetElementAt( i ).Get( mProgress[ i ] );
186 bool LinearConstrainer::IsDefaultPropertyWritable(Property::Index index) const
188 if( index < DEFAULT_PROPERTY_COUNT )
190 return DEFAULT_PROPERTY_DETAILS[index].writable;
196 bool LinearConstrainer::IsDefaultPropertyAnimatable(Property::Index index) const
198 if( index < DEFAULT_PROPERTY_COUNT )
200 return DEFAULT_PROPERTY_DETAILS[index].animatable;
206 bool LinearConstrainer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
208 if( index < DEFAULT_PROPERTY_COUNT )
210 return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
216 void LinearConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
218 Dali::Constraint constraint = Dali::Constraint::New<float>( target.object, target.propertyIndex, LinearConstraintFunctor( mValue, mProgress, range, wrap ) );
219 constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
221 constraint.SetTag( static_cast<uint32_t>( reinterpret_cast<uintptr_t>( this ) ) ); // taking 32bits of this as tag
222 constraint.SetRemoveAction( Dali::Constraint::Discard );
225 //Start observing the object
226 Observe( target.object );