1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/animation/constraint.h>
23 #include <dali/public-api/common/dali-common.h>
24 #include <dali/internal/common/message.h>
25 #include <dali/internal/event/common/event-thread-services.h>
26 #include <dali/internal/update/common/animatable-property.h>
27 #include <dali/internal/update/common/property-owner.h>
28 #include <dali/internal/update/common/scene-graph-buffers.h>
29 #include <dali/internal/update/animation/scene-graph-constraint-declarations.h>
37 // value types used by messages
38 template <> struct ParameterType< Dali::Constraint::RemoveAction >
39 : public BasicType< Dali::Constraint::RemoveAction > {};
44 typedef Dali::Vector<PropertyOwner*> PropertyOwnerContainer;
45 typedef PropertyOwnerContainer::Iterator PropertyOwnerIter;
48 * An abstract base class for Constraints.
49 * This can be used to constrain a property of a scene-object, after animations have been applied.
51 class ConstraintBase : public PropertyOwner::Observer
55 typedef Dali::Constraint::RemoveAction RemoveAction;
60 ConstraintBase( PropertyOwnerContainer& ownerContainer );
65 virtual ~ConstraintBase();
68 * Initialize the constraint.
69 * This should by called by a scene-object, when the constraint is connected.
75 mDisconnected = false;
79 * @copydoc Dali::Constraint::SetRemoveAction()
81 void SetRemoveAction( RemoveAction action )
83 mRemoveAction = action;
87 * @copydoc Dali::Constraint::GetRemoveAction()
89 RemoveAction GetRemoveAction() const
95 * Constrain the associated scene object.
96 * @param[in] updateBufferIndex The current update buffer index.
98 virtual void Apply( BufferIndex updateBufferIndex ) = 0;
101 * Helper for internal test cases; only available for debug builds.
102 * @return The current number of Constraint instances in existence.
104 static unsigned int GetCurrentInstanceCount();
107 * Helper for internal test cases; only available for debug builds.
108 * @return The total number of Constraint instances created during the Dali core lifetime.
110 static unsigned int GetTotalInstanceCount();
115 * Helper to start observing property owners
117 void StartObservation()
119 const PropertyOwnerIter end = mObservedOwners.End();
120 for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
122 (*iter)->AddObserver( *this );
127 * Helper to stop observing property owners
129 void StopObservation()
131 const PropertyOwnerIter end = mObservedOwners.End();
132 for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
134 (*iter)->RemoveObserver( *this );
137 mObservedOwners.Clear();
141 * @copydoc PropertyOwner::Observer::PropertyOwnerConnected()
143 virtual void PropertyOwnerConnected( PropertyOwner& owner )
148 * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
150 virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
152 if ( !mDisconnected )
154 // Stop observing property owners
157 // Notification for derived class
160 mDisconnected = true;
165 * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
167 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
169 if ( !mDisconnected )
171 // Discard pointer to destroyed property owner. Otherwise StopObservation() would crash when trying to remove
172 //the constraint from the destroyed PropertyOwner's observers list
173 PropertyOwnerIter iter = std::find( mObservedOwners.Begin(), mObservedOwners.End(), &owner );
174 if( mObservedOwners.End() != iter )
176 mObservedOwners.Erase( iter );
179 // Stop observing the rest of property owners
182 // Notification for derived class
185 mDisconnected = true;
191 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
193 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
196 * Notify the derived class to disconnect from property owners
198 virtual void OnDisconnect() = 0;
202 RemoveAction mRemoveAction;
204 bool mFirstApply : 1;
205 bool mDisconnected : 1;
209 PropertyOwnerContainer mObservedOwners; ///< A set of pointers to each observed object. Not owned.
212 static unsigned int mCurrentInstanceCount; ///< The current number of Constraint instances in existence.
213 static unsigned int mTotalInstanceCount; ///< The total number of Constraint instances created during the Dali core lifetime.
217 // Messages for ConstraintBase
219 inline void SetRemoveActionMessage( EventThreadServices& eventThreadServices, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction )
221 typedef MessageValue1< ConstraintBase, Dali::Constraint::RemoveAction > LocalType;
223 // Reserve some memory inside the message queue
224 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
226 // Construct message in the message queue memory; note that delete should not be called on the return value
227 new (slot) LocalType( &constraint, &ConstraintBase::SetRemoveAction, removeAction );
230 } // namespace SceneGraph
232 } // namespace Internal
236 #endif // __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__