2 * Copyright (c) 2014 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/constraint-base.h>
22 #include <dali/public-api/object/handle.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/internal/event/common/event-thread-services.h>
25 #include <dali/internal/event/common/property-helper.h>
26 #include <dali/internal/event/common/stage-impl.h>
27 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
28 #include <dali/internal/update/common/animatable-property.h>
29 #include <dali/internal/update/common/property-owner-messages.h>
31 using Dali::Internal::SceneGraph::AnimatableProperty;
39 ConstraintBase::ConstraintBase( Object& object, Property::Index targetPropertyIndex, SourceContainer& sources )
40 : mEventThreadServices( *Stage::GetCurrent() ),
41 mTargetObject( &object ),
42 mSceneGraphConstraint( NULL ),
45 mTargetPropertyIndex( targetPropertyIndex ),
46 mRemoveAction( Dali::Constraint::DEFAULT_REMOVE_ACTION ),
49 mSourceDestroyed( false )
51 ObserveObject( object );
54 ConstraintBase::~ConstraintBase()
61 void ConstraintBase::AddSource( Source source )
63 mSources.push_back( source );
65 // Observe the object providing this property
66 if ( OBJECT_PROPERTY == source.sourceType )
68 if ( source.object != NULL )
70 ObserveObject( *source.object );
74 DALI_LOG_ERROR( "Constraint source object not found\n" );
79 void ConstraintBase::Apply()
81 if ( mTargetObject && !mApplied && !mSourceDestroyed )
86 mTargetObject->ApplyConstraint( *this );
90 void ConstraintBase::Remove()
96 mTargetObject->RemoveConstraint( *this );
100 void ConstraintBase::RemoveInternal()
106 // Guard against constraint sending messages during core destruction
107 if( Stage::IsInstalled() )
109 const SceneGraph::PropertyOwner* propertyOwner = mTargetObject ? mTargetObject->GetSceneObject() : NULL;
111 if ( propertyOwner &&
112 mSceneGraphConstraint )
114 // Remove from scene-graph
115 RemoveConstraintMessage( GetEventThreadServices(), *propertyOwner, *(mSceneGraphConstraint) );
117 // mSceneGraphConstraint will be deleted in update-thread, remove dangling pointer
118 mSceneGraphConstraint = NULL;
124 Object* ConstraintBase::GetParent()
126 return mTargetObject;
129 Dali::Handle ConstraintBase::GetTargetObject()
131 return Dali::Handle( mTargetObject );
134 Property::Index ConstraintBase::GetTargetProperty()
136 return mTargetPropertyIndex;
139 void ConstraintBase::SetRemoveAction( ConstraintBase::RemoveAction action )
141 mRemoveAction = action;
144 ConstraintBase::RemoveAction ConstraintBase::GetRemoveAction() const
146 return mRemoveAction;
149 void ConstraintBase::SetTag(const unsigned int tag)
154 unsigned int ConstraintBase::GetTag() const
159 void ConstraintBase::SceneObjectAdded( Object& object )
162 ( NULL == mSceneGraphConstraint ) &&
169 void ConstraintBase::SceneObjectRemoved( Object& object )
171 if ( mSceneGraphConstraint )
173 // An input property owning source has been deleted, need to tell the scene-graph-constraint owner to remove it
174 if ( &object != mTargetObject )
176 const SceneGraph::PropertyOwner* propertyOwner = mTargetObject ? mTargetObject->GetSceneObject() : NULL;
180 // Remove from scene-graph
181 RemoveConstraintMessage( GetEventThreadServices(), *propertyOwner, *(mSceneGraphConstraint) );
185 // mSceneGraphConstraint will be deleted in update-thread, remove dangling pointer
186 mSceneGraphConstraint = NULL;
190 void ConstraintBase::ObjectDestroyed( Object& object )
192 // Remove object pointer from observation set
193 ObjectIter iter = std::find( mObservedObjects.Begin(), mObservedObjects.End(), &object );
194 DALI_ASSERT_DEBUG( mObservedObjects.End() != iter );
195 mObservedObjects.Erase( iter );
197 // Constraint is not useful anymore as an input-source has been destroyed
198 mSourceDestroyed = true;
200 // Stop observing the remaining objects
203 // Clear our sources as well
206 // Discard all object & scene-graph pointers
207 mSceneGraphConstraint = NULL;
208 mTargetObject = NULL;
211 void ConstraintBase::ObserveObject( Object& object )
213 ObjectIter iter = std::find( mObservedObjects.Begin(), mObservedObjects.End(), &object );
214 if ( mObservedObjects.End() == iter )
216 object.AddObserver( *this );
217 mObservedObjects.PushBack( &object );
221 void ConstraintBase::StopObservation()
223 const ObjectIter end = mObservedObjects.End();
224 for( ObjectIter iter = mObservedObjects.Begin(); iter != end; ++iter )
226 (*iter)->RemoveObserver( *this );
229 mObservedObjects.Clear();
232 } // namespace Internal