[dali_1.0.17] Merge branch 'tizen'
[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 typedef Dali::Vector<PropertyOwner*>     PropertyOwnerContainer;
45 typedef PropertyOwnerContainer::Iterator PropertyOwnerIter;
46
47 /**
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.
50  */
51 class ConstraintBase : public PropertyOwner, public PropertyOwner::Observer
52 {
53 public:
54
55   typedef Dali::Constraint::RemoveAction RemoveAction;
56
57   /**
58    * Constructor
59    */
60   ConstraintBase( PropertyOwnerContainer& ownerContainer );
61
62   /**
63    * Virtual destructor.
64    */
65   virtual ~ConstraintBase();
66
67   /**
68    * Initialize the constraint.
69    * This should by called by a scene-object, when the constraint is connected.
70    */
71   void OnConnect()
72   {
73     StartObservation();
74
75     mDisconnected = false;
76   }
77
78   /**
79    * @copydoc Dali::Constraint::SetRemoveAction()
80    */
81   void SetRemoveAction( RemoveAction action )
82   {
83     mRemoveAction = action;
84   }
85
86   /**
87    * @copydoc Dali::Constraint::GetRemoveAction()
88    */
89   RemoveAction GetRemoveAction() const
90   {
91     return mRemoveAction;
92   }
93
94   /**
95    * Bake the weight property.
96    * @param[in] updateBufferIndex The current update buffer index.
97    * @param[in] weight The new weight.
98    */
99   void BakeWeight( BufferIndex updateBufferIndex, float weight )
100   {
101     mWeight.Bake( updateBufferIndex, weight );
102   }
103
104   /**
105    * Set the initial weight.
106    * @pre The constraint has not been connected to the scene-graph.
107    * @param[in] weight The new weight.
108    */
109   void SetInitialWeight( float weight )
110   {
111     mWeight.SetInitial( weight );
112   }
113
114   /**
115    * Retrieve the weight property.
116    * @param[in] bufferIndex The buffer index to read from.
117    * @return The current weight.
118    */
119   float GetWeight( BufferIndex bufferIndex ) const
120   {
121     return mWeight[ bufferIndex ];
122   }
123
124   /**
125    * Constrain the associated scene object.
126    * @param[in] updateBufferIndex The current update buffer index.
127    */
128   virtual void Apply( BufferIndex updateBufferIndex ) = 0;
129
130   /**
131    * Helper for internal test cases; only available for debug builds.
132    * @return The current number of Constraint instances in existence.
133    */
134   static unsigned int GetCurrentInstanceCount();
135
136   /**
137    * Helper for internal test cases; only available for debug builds.
138    * @return The total number of Constraint instances created during the Dali core lifetime.
139    */
140   static unsigned int GetTotalInstanceCount();
141
142 private:
143
144   /**
145    * Helper to start observing property owners
146    */
147   void StartObservation()
148   {
149     const PropertyOwnerIter end =  mObservedOwners.End();
150     for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
151     {
152       (*iter)->AddObserver( *this );
153     }
154   }
155
156   /**
157    * Helper to stop observing property owners
158    */
159   void StopObservation()
160   {
161     const PropertyOwnerIter end =  mObservedOwners.End();
162     for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
163     {
164       (*iter)->RemoveObserver( *this );
165     }
166
167     mObservedOwners.Clear();
168   }
169
170   /**
171    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
172    */
173   virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
174   {
175     PropertyOwnerDestroyed( owner );
176   }
177
178   /**
179    * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
180    */
181   virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
182   {
183     if ( !mDisconnected )
184     {
185       // Discard pointer to disconnected property owner
186       PropertyOwnerIter iter = std::find( mObservedOwners.Begin(), mObservedOwners.End(), &owner );
187       if( mObservedOwners.End() != iter )
188       {
189         mObservedOwners.Erase( iter );
190
191         // Stop observing the remaining property owners
192         StopObservation();
193
194         // Notification for derived class
195         OnDisconnect();
196
197         mDisconnected = true;
198       }
199     }
200   }
201
202   /**
203    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
204    */
205   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
206
207   /**
208    * Notify the derived class to disconnect from property owners
209    */
210   virtual void OnDisconnect() = 0;
211
212 public:
213
214   AnimatableProperty<float> mWeight; ///< The constraint is "fully-applied" when weight = 1
215
216 protected:
217
218   RemoveAction mRemoveAction;
219
220   bool mFirstApply   : 1;
221   bool mDisconnected : 1;
222
223 private:
224
225   PropertyOwnerContainer mObservedOwners; ///< A set of pointers to each observed object. Not owned.
226
227 #ifdef DEBUG_ENABLED
228   static unsigned int mCurrentInstanceCount;  ///< The current number of Constraint instances in existence.
229   static unsigned int mTotalInstanceCount;    ///< The total number of Constraint instances created during the Dali core lifetime.
230 #endif
231 };
232
233 // Messages for ConstraintBase
234
235 inline void  BakeWeightMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, float weight )
236 {
237   typedef MessageDoubleBuffered1< ConstraintBase, float > LocalType;
238
239   // Reserve some memory inside the message queue
240   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
241
242   // Construct message in the message queue memory; note that delete should not be called on the return value
243   new (slot) LocalType( &constraint, &ConstraintBase::BakeWeight, weight );
244 }
245
246 inline void  SetRemoveActionMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction )
247 {
248   typedef MessageValue1< ConstraintBase, Dali::Constraint::RemoveAction > LocalType;
249
250   // Reserve some memory inside the message queue
251   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
252
253   // Construct message in the message queue memory; note that delete should not be called on the return value
254   new (slot) LocalType( &constraint, &ConstraintBase::SetRemoveAction, removeAction );
255 }
256
257 } // namespace SceneGraph
258
259 } // namespace Internal
260
261 } // namespace Dali
262
263 #endif // __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__