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 Flora License, Version 1.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://floralicense.org/license/
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.
21 #include <dali/public-api/animation/constraint.h>
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/internal/common/message.h>
24 #include <dali/internal/common/event-to-update.h>
25 #include <dali/internal/update/common/animatable-property.h>
26 #include <dali/internal/update/common/property-owner.h>
27 #include <dali/internal/update/common/scene-graph-buffers.h>
28 #include <dali/internal/update/animation/scene-graph-constraint-declarations.h>
36 // value types used by messages
37 template <> struct ParameterType< Dali::Constraint::RemoveAction >
38 : public BasicType< Dali::Constraint::RemoveAction > {};
44 * An abstract base class for Constraints.
45 * This can be used to constrain a property of a scene-object, after animations have been applied.
47 class ConstraintBase : public PropertyOwner, public PropertyOwner::Observer
51 typedef Dali::Constraint::RemoveAction RemoveAction;
56 ConstraintBase( PropertyOwnerSet& ownerSet );
61 virtual ~ConstraintBase();
64 * Initialize the constraint.
65 * This should by called by a scene-object, when the constraint is connected.
71 mDisconnected = false;
75 * @copydoc Dali::Constraint::SetRemoveAction()
77 void SetRemoveAction( RemoveAction action )
79 mRemoveAction = action;
83 * @copydoc Dali::Constraint::GetRemoveAction()
85 RemoveAction GetRemoveAction() const
91 * Bake the weight property.
92 * @param[in] updateBufferIndex The current update buffer index.
93 * @param[in] weight The new weight.
95 void BakeWeight( BufferIndex updateBufferIndex, float weight )
97 mWeight.Bake( updateBufferIndex, weight );
101 * Set the initial weight.
102 * @pre The constraint has not been connected to the scene-graph.
103 * @param[in] weight The new weight.
105 void SetInitialWeight( float weight )
107 mWeight.SetInitial( weight );
111 * Retrieve the weight property.
112 * @param[in] bufferIndex The buffer index to read from.
113 * @return The current weight.
115 float GetWeight( BufferIndex bufferIndex ) const
117 return mWeight[ bufferIndex ];
121 * Constrain the associated scene object.
122 * @param[in] updateBufferIndex The current update buffer index.
124 virtual void Apply( BufferIndex updateBufferIndex ) = 0;
129 * Helper to start observing property owners
131 void StartObservation()
133 for( PropertyOwnerIter iter = mObservedOwners.begin(); mObservedOwners.end() != iter; ++iter )
135 (*iter)->AddObserver( *this );
140 * Helper to stop observing property owners
142 void StopObservation()
144 for( PropertyOwnerIter iter = mObservedOwners.begin(); mObservedOwners.end() != iter; ++iter )
146 (*iter)->RemoveObserver( *this );
149 mObservedOwners.clear();
153 * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
155 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
157 // Discard pointer to destroyed property owner
158 PropertyOwnerIter iter = mObservedOwners.find( &owner );
159 DALI_ASSERT_DEBUG( mObservedOwners.end() != iter );
160 mObservedOwners.erase( iter );
162 // Stop observing the remaining property owners
165 // Notification for derived class
168 mDisconnected = true;
172 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
174 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
177 * Notify the derived class to disconnect from property owners
179 virtual void OnDisconnect() = 0;
183 AnimatableProperty<float> mWeight; ///< The constraint is "fully-applied" when weight = 1
187 RemoveAction mRemoveAction;
189 bool mFirstApply : 1;
190 bool mDisconnected : 1;
194 PropertyOwnerSet mObservedOwners; ///< A set of pointers to each observed object. Not owned.
197 // Messages for ConstraintBase
199 inline void BakeWeightMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, float weight )
201 typedef MessageDoubleBuffered1< ConstraintBase, float > LocalType;
203 // Reserve some memory inside the message queue
204 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
206 // Construct message in the message queue memory; note that delete should not be called on the return value
207 new (slot) LocalType( &constraint, &ConstraintBase::BakeWeight, weight );
210 inline void SetRemoveActionMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction )
212 typedef MessageValue1< ConstraintBase, Dali::Constraint::RemoveAction > LocalType;
214 // Reserve some memory inside the message queue
215 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
217 // Construct message in the message queue memory; note that delete should not be called on the return value
218 new (slot) LocalType( &constraint, &ConstraintBase::SetRemoveAction, removeAction );
221 } // namespace SceneGraph
223 } // namespace Internal
227 #endif // __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__