1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__
5 * Copyright (c) 2017 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;
59 * @param ownerContainer the properties to constraint
60 * @oparam removeAction perform when removed
62 ConstraintBase( PropertyOwnerContainer& ownerContainer, RemoveAction removeAction );
67 virtual ~ConstraintBase();
70 * Initialize the constraint.
71 * This should by called by a scene-object, when the constraint is connected.
77 mDisconnected = false;
81 * @copydoc Dali::Constraint::SetRemoveAction()
83 void SetRemoveAction( RemoveAction action )
85 mRemoveAction = action;
89 * @copydoc Dali::Constraint::GetRemoveAction()
91 RemoveAction GetRemoveAction() const
97 * Constrain the associated scene object.
98 * @param[in] updateBufferIndex The current update buffer index.
100 virtual void Apply( BufferIndex updateBufferIndex ) = 0;
103 * Helper for internal test cases; only available for debug builds.
104 * @return The current number of Constraint instances in existence.
106 static unsigned int GetCurrentInstanceCount();
109 * Helper for internal test cases; only available for debug builds.
110 * @return The total number of Constraint instances created during the Dali core lifetime.
112 static unsigned int GetTotalInstanceCount();
117 * Helper to start observing property owners
119 void StartObservation()
121 const PropertyOwnerIter end = mObservedOwners.End();
122 for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
124 (*iter)->AddObserver( *this );
129 * Helper to stop observing property owners
131 void StopObservation()
133 const PropertyOwnerIter end = mObservedOwners.End();
134 for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
136 (*iter)->RemoveObserver( *this );
139 mObservedOwners.Clear();
143 * @copydoc PropertyOwner::Observer::PropertyOwnerConnected()
145 virtual void PropertyOwnerConnected( PropertyOwner& owner )
150 * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
152 virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
154 if ( !mDisconnected )
156 // Stop observing property owners
159 // Notification for derived class
162 mDisconnected = true;
167 * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
169 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
171 if ( !mDisconnected )
173 // Discard pointer to destroyed property owner. Otherwise StopObservation() would crash when trying to remove
174 //the constraint from the destroyed PropertyOwner's observers list
175 PropertyOwnerIter iter = std::find( mObservedOwners.Begin(), mObservedOwners.End(), &owner );
176 if( mObservedOwners.End() != iter )
178 mObservedOwners.Erase( iter );
181 // Stop observing the rest of property owners
184 // Notification for derived class
187 mDisconnected = true;
193 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
195 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
198 * Notify the derived class to disconnect from property owners
200 virtual void OnDisconnect() = 0;
204 RemoveAction mRemoveAction;
206 bool mFirstApply : 1;
207 bool mDisconnected : 1;
211 PropertyOwnerContainer mObservedOwners; ///< A set of pointers to each observed object. Not owned.
214 static unsigned int mCurrentInstanceCount; ///< The current number of Constraint instances in existence.
215 static unsigned int mTotalInstanceCount; ///< The total number of Constraint instances created during the Dali core lifetime.
219 // Messages for ConstraintBase
221 inline void SetRemoveActionMessage( EventThreadServices& eventThreadServices, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction )
223 typedef MessageValue1< ConstraintBase, Dali::Constraint::RemoveAction > LocalType;
225 // Reserve some memory inside the message queue
226 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
228 // Construct message in the message queue memory; note that delete should not be called on the return value
229 new (slot) LocalType( &constraint, &ConstraintBase::SetRemoveAction, removeAction );
232 } // namespace SceneGraph
234 } // namespace Internal
238 #endif // __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__