(PropertyOwner) Distinguish between disconnection & destruction
[platform/core/uifw/dali-core.git] / dali / internal / update / animation / scene-graph-constraint-base.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
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>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 // value types used by messages
38 template <> struct ParameterType< Dali::Constraint::RemoveAction >
39 : public BasicType< Dali::Constraint::RemoveAction > {};
40
41 namespace SceneGraph
42 {
43
44 /**
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.
47  */
48 class ConstraintBase : public PropertyOwner, public PropertyOwner::Observer
49 {
50 public:
51
52   typedef Dali::Constraint::RemoveAction RemoveAction;
53
54   /**
55    * Constructor
56    */
57   ConstraintBase( PropertyOwnerSet& ownerSet );
58
59   /**
60    * Virtual destructor.
61    */
62   virtual ~ConstraintBase();
63
64   /**
65    * Initialize the constraint.
66    * This should by called by a scene-object, when the constraint is connected.
67    */
68   void OnConnect()
69   {
70     StartObservation();
71
72     mDisconnected = false;
73   }
74
75   /**
76    * @copydoc Dali::Constraint::SetRemoveAction()
77    */
78   void SetRemoveAction( RemoveAction action )
79   {
80     mRemoveAction = action;
81   }
82
83   /**
84    * @copydoc Dali::Constraint::GetRemoveAction()
85    */
86   RemoveAction GetRemoveAction() const
87   {
88     return mRemoveAction;
89   }
90
91   /**
92    * Bake the weight property.
93    * @param[in] updateBufferIndex The current update buffer index.
94    * @param[in] weight The new weight.
95    */
96   void BakeWeight( BufferIndex updateBufferIndex, float weight )
97   {
98     mWeight.Bake( updateBufferIndex, weight );
99   }
100
101   /**
102    * Set the initial weight.
103    * @pre The constraint has not been connected to the scene-graph.
104    * @param[in] weight The new weight.
105    */
106   void SetInitialWeight( float weight )
107   {
108     mWeight.SetInitial( weight );
109   }
110
111   /**
112    * Retrieve the weight property.
113    * @param[in] bufferIndex The buffer index to read from.
114    * @return The current weight.
115    */
116   float GetWeight( BufferIndex bufferIndex ) const
117   {
118     return mWeight[ bufferIndex ];
119   }
120
121   /**
122    * Constrain the associated scene object.
123    * @param[in] updateBufferIndex The current update buffer index.
124    */
125   virtual void Apply( BufferIndex updateBufferIndex ) = 0;
126
127   /**
128    * Helper for internal test cases; only available for debug builds.
129    * @return The current number of Constraint instances in existence.
130    */
131   static unsigned int GetCurrentInstanceCount();
132
133   /**
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.
136    */
137   static unsigned int GetTotalInstanceCount();
138
139 private:
140
141   /**
142    * Helper to start observing property owners
143    */
144   void StartObservation()
145   {
146     for( PropertyOwnerIter iter = mObservedOwners.begin(); mObservedOwners.end() != iter; ++iter )
147     {
148       (*iter)->AddObserver( *this );
149     }
150   }
151
152   /**
153    * Helper to stop observing property owners
154    */
155   void StopObservation()
156   {
157     for( PropertyOwnerIter iter = mObservedOwners.begin(); mObservedOwners.end() != iter; ++iter )
158     {
159       (*iter)->RemoveObserver( *this );
160     }
161
162     mObservedOwners.clear();
163   }
164
165   /**
166    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
167    */
168   virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
169   {
170     PropertyOwnerDestroyed( owner );
171   }
172
173   /**
174    * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
175    */
176   virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
177   {
178     if ( !mDisconnected )
179     {
180       // Discard pointer to disconnected property owner
181       PropertyOwnerIter iter = mObservedOwners.find( &owner );
182       if( mObservedOwners.end() != iter )
183       {
184         mObservedOwners.erase( iter );
185
186         // Stop observing the remaining property owners
187         StopObservation();
188
189         // Notification for derived class
190         OnDisconnect();
191
192         mDisconnected = true;
193       }
194     }
195   }
196
197   /**
198    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
199    */
200   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
201
202   /**
203    * Notify the derived class to disconnect from property owners
204    */
205   virtual void OnDisconnect() = 0;
206
207 public:
208
209   AnimatableProperty<float> mWeight; ///< The constraint is "fully-applied" when weight = 1
210
211 protected:
212
213   RemoveAction mRemoveAction;
214
215   bool mFirstApply   : 1;
216   bool mDisconnected : 1;
217
218 private:
219
220   PropertyOwnerSet mObservedOwners; ///< A set of pointers to each observed object. Not owned.
221
222 #ifdef DEBUG_ENABLED
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.
225 #endif
226 };
227
228 // Messages for ConstraintBase
229
230 inline void  BakeWeightMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, float weight )
231 {
232   typedef MessageDoubleBuffered1< ConstraintBase, float > LocalType;
233
234   // Reserve some memory inside the message queue
235   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
236
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 );
239 }
240
241 inline void  SetRemoveActionMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction )
242 {
243   typedef MessageValue1< ConstraintBase, Dali::Constraint::RemoveAction > LocalType;
244
245   // Reserve some memory inside the message queue
246   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
247
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 );
250 }
251
252 } // namespace SceneGraph
253
254 } // namespace Internal
255
256 } // namespace Dali
257
258 #endif // __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__