License conversion from Flora to Apache 2.0
[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 private:
128
129   /**
130    * Helper to start observing property owners
131    */
132   void StartObservation()
133   {
134     for( PropertyOwnerIter iter = mObservedOwners.begin(); mObservedOwners.end() != iter; ++iter )
135     {
136       (*iter)->AddObserver( *this );
137     }
138   }
139
140   /**
141    * Helper to stop observing property owners
142    */
143   void StopObservation()
144   {
145     for( PropertyOwnerIter iter = mObservedOwners.begin(); mObservedOwners.end() != iter; ++iter )
146     {
147       (*iter)->RemoveObserver( *this );
148     }
149
150     mObservedOwners.clear();
151   }
152
153   /**
154    * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
155    */
156   virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
157   {
158     // Discard pointer to destroyed property owner
159     PropertyOwnerIter iter = mObservedOwners.find( &owner );
160     DALI_ASSERT_DEBUG( mObservedOwners.end() != iter );
161     mObservedOwners.erase( iter );
162
163     // Stop observing the remaining property owners
164     StopObservation();
165
166     // Notification for derived class
167     OnDisconnect();
168
169     mDisconnected = true;
170   }
171
172   /**
173    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
174    */
175   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
176
177   /**
178    * Notify the derived class to disconnect from property owners
179    */
180   virtual void OnDisconnect() = 0;
181
182 public:
183
184   AnimatableProperty<float> mWeight; ///< The constraint is "fully-applied" when weight = 1
185
186 protected:
187
188   RemoveAction mRemoveAction;
189
190   bool mFirstApply   : 1;
191   bool mDisconnected : 1;
192
193 private:
194
195   PropertyOwnerSet mObservedOwners; ///< A set of pointers to each observed object. Not owned.
196 };
197
198 // Messages for ConstraintBase
199
200 inline void  BakeWeightMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, float weight )
201 {
202   typedef MessageDoubleBuffered1< ConstraintBase, float > LocalType;
203
204   // Reserve some memory inside the message queue
205   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
206
207   // Construct message in the message queue memory; note that delete should not be called on the return value
208   new (slot) LocalType( &constraint, &ConstraintBase::BakeWeight, weight );
209 }
210
211 inline void  SetRemoveActionMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction )
212 {
213   typedef MessageValue1< ConstraintBase, Dali::Constraint::RemoveAction > LocalType;
214
215   // Reserve some memory inside the message queue
216   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
217
218   // Construct message in the message queue memory; note that delete should not be called on the return value
219   new (slot) LocalType( &constraint, &ConstraintBase::SetRemoveAction, removeAction );
220 }
221
222 } // namespace SceneGraph
223
224 } // namespace Internal
225
226 } // namespace Dali
227
228 #endif // __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__