2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/internal/event/animation/active-constraint-base.h>
21 #include <dali/public-api/animation/active-constraint.h>
22 #include <dali/public-api/object/handle.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/internal/common/event-to-update.h>
25 #include <dali/internal/event/animation/animation-impl.h>
26 #include <dali/internal/update/common/animatable-property.h>
27 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
28 #include <dali/internal/update/common/property-owner-messages.h>
30 using Dali::Internal::SceneGraph::AnimatableProperty;
35 const Property::Index ActiveConstraint::WEIGHT = 0;
40 namespace // unnamed namespace
45 // not directly creatable
49 TypeRegistration mType( typeid(Dali::ActiveConstraint), typeid(Dali::Handle), Create );
51 SignalConnectorType signalConnector1( mType, Dali::ActiveConstraint::SIGNAL_APPLIED, &ActiveConstraintBase::DoConnectSignal );
56 namespace // unnamed namespace
60 * We want to discourage the use of property strings (minimize string comparisons),
61 * particularly for the default properties.
63 const std::string DEFAULT_PROPERTY_NAMES[] =
67 const int DEFAULT_PROPERTY_COUNT = sizeof( DEFAULT_PROPERTY_NAMES ) / sizeof( std::string );
69 const Property::Type DEFAULT_PROPERTY_TYPES[DEFAULT_PROPERTY_COUNT] =
71 Property::FLOAT // WEIGHT
74 } // unnamed namespace
76 ActiveConstraintBase::ActiveConstraintBase( EventToUpdate& eventToUpdate, Property::Index targetPropertyIndex )
77 : mEventToUpdate( eventToUpdate ),
78 mTargetPropertyIndex( targetPropertyIndex ),
80 mSceneGraphConstraint( NULL ),
81 mOffstageWeight( Dali::ActiveConstraint::DEFAULT_WEIGHT ),
83 mAlphaFunction( Dali::Constraint::DEFAULT_ALPHA_FUNCTION ),
84 mRemoveAction( Dali::Constraint::DEFAULT_REMOVE_ACTION ),
90 ActiveConstraintBase::~ActiveConstraintBase()
92 // Disconnect from internal animation signals
94 if ( mApplyAnimation )
96 GetImplementation(mApplyAnimation).SetFinishedCallback( NULL, NULL );
99 if( mRemoveAnimation )
101 GetImplementation(mRemoveAnimation).SetFinishedCallback( NULL, NULL );
105 void ActiveConstraintBase::FirstApply( ProxyObject& parent, TimePeriod applyTime, ActiveConstraintCallbackType* callback )
107 // Notify derived classes
108 OnFirstApply( parent );
110 if ( applyTime.durationSeconds > 0.0f )
112 DALI_ASSERT_DEBUG( !mApplyAnimation );
117 // Automatically animate (increase) the weight, until the constraint is fully applied
118 mApplyAnimation = Dali::Animation::New( applyTime.delaySeconds + applyTime.durationSeconds );
119 Dali::ActiveConstraint self( this );
120 mApplyAnimation.AnimateTo( Property( self, Dali::ActiveConstraint::WEIGHT ), Dali::ActiveConstraint::FINAL_WEIGHT, mAlphaFunction, applyTime );
121 mApplyAnimation.Play();
123 // Chain "Finish" to "Applied" signal
124 GetImplementation(mApplyAnimation).SetFinishedCallback( &ActiveConstraintBase::FirstApplyFinished, this );
128 void ActiveConstraintBase::BeginRemove()
130 // Notify derived classes
133 // Remove gradually by animating weight down to zero
134 if ( mRemoveTime.durationSeconds > 0.0f )
136 // Stop baking behaviour from interfering with remove animation
137 if ( mSceneGraphConstraint )
139 // Immediately remove from scene-graph
140 SetRemoveActionMessage( mEventToUpdate, *mSceneGraphConstraint, Dali::Constraint::Discard );
143 // Interrupt ongoing apply-animations
144 if ( mApplyAnimation )
146 mApplyAnimation.Stop();
149 // Reduce the weight to zero
150 mRemoveAnimation = Dali::Animation::New( mRemoveTime.delaySeconds + mRemoveTime.durationSeconds );
151 Dali::ActiveConstraint self( this );
152 mRemoveAnimation.AnimateTo( Property( self, Dali::ActiveConstraint::WEIGHT ), 0.0f, mAlphaFunction, mRemoveTime );
153 mRemoveAnimation.Play();
155 // Finish removal when animation ends
156 GetImplementation(mRemoveAnimation).SetFinishedCallback( &ActiveConstraintBase::OnRemoveFinished, this );
160 OnRemoveFinished( this );
164 bool ActiveConstraintBase::IsRemoving()
166 return mRemoveAnimation;
169 ProxyObject* ActiveConstraintBase::GetParent()
174 bool ActiveConstraintBase::Supports( Capability capability ) const
176 return false; // switch-off support for dynamic properties
179 Dali::Handle ActiveConstraintBase::GetTargetObject()
181 return Dali::Handle( mTargetProxy );
184 Property::Index ActiveConstraintBase::GetTargetProperty()
186 return mTargetPropertyIndex;
189 void ActiveConstraintBase::SetWeight( float weight )
191 if ( mSceneGraphConstraint )
193 BakeWeightMessage( mEventToUpdate, *mSceneGraphConstraint, weight );
197 mOffstageWeight = weight;
201 float ActiveConstraintBase::GetCurrentWeight() const
203 float currentWeight( mOffstageWeight );
205 if ( mSceneGraphConstraint )
207 currentWeight = mSceneGraphConstraint->GetWeight( mEventToUpdate.GetEventBufferIndex() );
210 return currentWeight;
213 ActiveConstraintSignalV2& ActiveConstraintBase::AppliedSignal()
215 return mAppliedSignal;
218 bool ActiveConstraintBase::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
220 bool connected( true );
221 ActiveConstraintBase* constraint = dynamic_cast<ActiveConstraintBase*>(object);
223 if ( Dali::ActiveConstraint::SIGNAL_APPLIED == signalName )
225 constraint->AppliedSignal().Connect( tracker, functor );
229 // signalName does not match any signal
236 void ActiveConstraintBase::SetRemoveTime( TimePeriod removeTime )
238 mRemoveTime = removeTime;
241 TimePeriod ActiveConstraintBase::GetRemoveTime() const
246 void ActiveConstraintBase::SetAlphaFunction( AlphaFunction alphaFunc )
248 mAlphaFunction = alphaFunc;
251 AlphaFunction ActiveConstraintBase::GetAlphaFunction() const
253 return mAlphaFunction;
256 void ActiveConstraintBase::SetRemoveAction( ActiveConstraintBase::RemoveAction action )
258 mRemoveAction = action;
261 ActiveConstraintBase::RemoveAction ActiveConstraintBase::GetRemoveAction() const
263 return mRemoveAction;
266 bool ActiveConstraintBase::IsSceneObjectRemovable() const
268 return true; // The constraint removed when target SceneGraph::PropertyOwner is destroyed
271 unsigned int ActiveConstraintBase::GetDefaultPropertyCount() const
273 return DEFAULT_PROPERTY_COUNT;
276 void ActiveConstraintBase::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
278 indices.reserve( DEFAULT_PROPERTY_COUNT );
280 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
282 indices.push_back( i );
286 const std::string& ActiveConstraintBase::GetDefaultPropertyName( Property::Index index ) const
288 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
290 return DEFAULT_PROPERTY_NAMES[index];
294 // index out of range..return empty string
295 static const std::string INVALID_PROPERTY_NAME;
296 return INVALID_PROPERTY_NAME;
300 Property::Index ActiveConstraintBase::GetDefaultPropertyIndex( const std::string& name ) const
302 Property::Index index = Property::INVALID_INDEX;
304 // Only one name to compare with...
305 if ( name == DEFAULT_PROPERTY_NAMES[0] )
313 bool ActiveConstraintBase::IsDefaultPropertyWritable( Property::Index index ) const
315 return true; // All default properties are currently writable
318 bool ActiveConstraintBase::IsDefaultPropertyAnimatable( Property::Index index ) const
320 return true; // All default properties are currently animatable
323 bool ActiveConstraintBase::IsDefaultPropertyAConstraintInput( Property::Index index ) const
325 return true; // All default properties can currently be used as a constraint input
328 Property::Type ActiveConstraintBase::GetDefaultPropertyType( Property::Index index ) const
330 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
332 return DEFAULT_PROPERTY_TYPES[index];
336 // Index out-of-range
337 return Property::NONE;
341 void ActiveConstraintBase::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
345 case Dali::ActiveConstraint::WEIGHT:
347 SetWeight( propertyValue.Get<float>() );
353 DALI_ASSERT_ALWAYS( false && "ActiveConstraint property out of bounds" ); // should not come here
359 void ActiveConstraintBase::SetCustomProperty( Property::Index index, const CustomProperty& entry, const Property::Value& value )
361 DALI_ASSERT_ALWAYS( false && "ActiveConstraintBase does not have custom properties"); // should not come here
364 Property::Value ActiveConstraintBase::GetDefaultProperty( Property::Index index ) const
366 Property::Value value;
370 case Dali::ActiveConstraint::WEIGHT:
372 value = GetCurrentWeight();
378 DALI_ASSERT_ALWAYS( false && "ActiveConstraint property out of bounds" ); // should not come here
386 void ActiveConstraintBase::InstallSceneObjectProperty( SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index )
388 DALI_ASSERT_ALWAYS( false && "ActiveConstraintBase does not have custom properties" ); // should not come here
391 const SceneGraph::PropertyOwner* ActiveConstraintBase::GetSceneObject() const
393 return mSceneGraphConstraint;
396 const SceneGraph::PropertyBase* ActiveConstraintBase::GetSceneObjectAnimatableProperty( Property::Index index ) const
398 DALI_ASSERT_DEBUG( 0 == index ); // only 1 property supported
400 // This method should only return a property which is part of the scene-graph
401 if ( !mSceneGraphConstraint )
406 return &mSceneGraphConstraint->mWeight;
409 const PropertyInputImpl* ActiveConstraintBase::GetSceneObjectInputProperty( Property::Index index ) const
411 DALI_ASSERT_DEBUG( 0 == index ); // only 1 property supported
413 // This method should only return a property which is part of the scene-graph
414 if ( !mSceneGraphConstraint )
419 return &mSceneGraphConstraint->mWeight;
422 void ActiveConstraintBase::FirstApplyFinished( Object* object )
424 ActiveConstraintBase& self = dynamic_cast<ActiveConstraintBase&>( *object );
426 // This is necessary when the constraint was not added to scene-graph during the animation
427 self.SetWeight( Dali::ActiveConstraint::FINAL_WEIGHT );
429 // The animation is no longer needed
430 GetImplementation(self.mApplyAnimation).SetFinishedCallback( NULL, NULL );
431 self.mApplyAnimation.Reset();
433 // Chain "Finish" to "Applied" signal
435 if ( !self.mAppliedSignal.Empty() )
437 Dali::ActiveConstraint handle( &self );
439 self.mAppliedSignal.Emit( handle );
442 // WARNING - this constraint may now have been deleted; don't do anything else here
445 void ActiveConstraintBase::OnRemoveFinished( Object* object )
447 ActiveConstraintBase& self = dynamic_cast<ActiveConstraintBase&>( *object );
449 const SceneGraph::PropertyOwner* propertyOwner = self.mTargetProxy ? self.mTargetProxy->GetSceneObject() : NULL;
451 if ( propertyOwner &&
452 self.mSceneGraphConstraint )
454 // Notify base class that the scene-graph constraint is being removed
455 self.OnSceneObjectRemove();
457 // Remove from scene-graph
458 RemoveConstraintMessage( self.mEventToUpdate, *propertyOwner, *(self.mSceneGraphConstraint) );
460 // mSceneGraphConstraint will be deleted in update-thread, remove dangling pointer
461 self.mSceneGraphConstraint = NULL;
464 // The animation is no longer needed
465 self.mRemoveAnimation.Reset();
468 } // namespace Internal