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/common/event-to-update.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 > {};
45 * An abstract base class for Constraints.
46 * This can be used to constrain a property of a scene-object, after animations have been applied.
48 class ConstraintBase : public PropertyOwner, public PropertyOwner::Observer
52 typedef Dali::Constraint::RemoveAction RemoveAction;
57 ConstraintBase( PropertyOwnerSet& ownerSet );
62 virtual ~ConstraintBase();
65 * Initialize the constraint.
66 * This should by called by a scene-object, when the constraint is connected.
72 mDisconnected = false;
76 * @copydoc Dali::Constraint::SetRemoveAction()
78 void SetRemoveAction( RemoveAction action )
80 mRemoveAction = action;
84 * @copydoc Dali::Constraint::GetRemoveAction()
86 RemoveAction GetRemoveAction() const
92 * Bake the weight property.
93 * @param[in] updateBufferIndex The current update buffer index.
94 * @param[in] weight The new weight.
96 void BakeWeight( BufferIndex updateBufferIndex, float weight )
98 mWeight.Bake( updateBufferIndex, weight );
102 * Set the initial weight.
103 * @pre The constraint has not been connected to the scene-graph.
104 * @param[in] weight The new weight.
106 void SetInitialWeight( float weight )
108 mWeight.SetInitial( weight );
112 * Retrieve the weight property.
113 * @param[in] bufferIndex The buffer index to read from.
114 * @return The current weight.
116 float GetWeight( BufferIndex bufferIndex ) const
118 return mWeight[ bufferIndex ];
122 * Constrain the associated scene object.
123 * @param[in] updateBufferIndex The current update buffer index.
125 virtual void Apply( BufferIndex updateBufferIndex ) = 0;
128 * Helper for internal test cases; only available for debug builds.
129 * @return The current number of Constraint instances in existence.
131 static unsigned int GetCurrentInstanceCount();
134 * Helper for internal test cases; only available for debug builds.
135 * @return The total number of Constraint instances created during the Dali core lifetime.
137 static unsigned int GetTotalInstanceCount();
142 * Helper to start observing property owners
144 void StartObservation()
146 for( PropertyOwnerIter iter = mObservedOwners.begin(); mObservedOwners.end() != iter; ++iter )
148 (*iter)->AddObserver( *this );
153 * Helper to stop observing property owners
155 void StopObservation()
157 for( PropertyOwnerIter iter = mObservedOwners.begin(); mObservedOwners.end() != iter; ++iter )
159 (*iter)->RemoveObserver( *this );
162 mObservedOwners.clear();
166 * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
168 virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
170 PropertyOwnerDestroyed( owner );
174 * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
176 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
178 if ( !mDisconnected )
180 // Discard pointer to disconnected property owner
181 PropertyOwnerIter iter = mObservedOwners.find( &owner );
182 if( mObservedOwners.end() != iter )
184 mObservedOwners.erase( iter );
186 // Stop observing the remaining property owners
189 // Notification for derived class
192 mDisconnected = true;
198 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
200 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
203 * Notify the derived class to disconnect from property owners
205 virtual void OnDisconnect() = 0;
209 AnimatableProperty<float> mWeight; ///< The constraint is "fully-applied" when weight = 1
213 RemoveAction mRemoveAction;
215 bool mFirstApply : 1;
216 bool mDisconnected : 1;
220 PropertyOwnerSet mObservedOwners; ///< A set of pointers to each observed object. Not owned.
223 static unsigned int mCurrentInstanceCount; ///< The current number of Constraint instances in existence.
224 static unsigned int mTotalInstanceCount; ///< The total number of Constraint instances created during the Dali core lifetime.
228 // Messages for ConstraintBase
230 inline void BakeWeightMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, float weight )
232 typedef MessageDoubleBuffered1< ConstraintBase, float > LocalType;
234 // Reserve some memory inside the message queue
235 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
237 // Construct message in the message queue memory; note that delete should not be called on the return value
238 new (slot) LocalType( &constraint, &ConstraintBase::BakeWeight, weight );
241 inline void SetRemoveActionMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction )
243 typedef MessageValue1< ConstraintBase, Dali::Constraint::RemoveAction > LocalType;
245 // Reserve some memory inside the message queue
246 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
248 // Construct message in the message queue memory; note that delete should not be called on the return value
249 new (slot) LocalType( &constraint, &ConstraintBase::SetRemoveAction, removeAction );
252 } // namespace SceneGraph
254 } // namespace Internal
258 #endif // __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__