Revert "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 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
10 //
11 //     http://floralicense.org/license/
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 // INTERNAL INCLUDES
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>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 // value types used by messages
37 template <> struct ParameterType< Dali::Constraint::RemoveAction >
38 : public BasicType< Dali::Constraint::RemoveAction > {};
39
40 namespace SceneGraph
41 {
42
43 /**
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.
46  */
47 class ConstraintBase : public PropertyOwner, public PropertyOwner::Observer
48 {
49 public:
50
51   typedef Dali::Constraint::RemoveAction RemoveAction;
52
53   /**
54    * Constructor
55    */
56   ConstraintBase( PropertyOwnerSet& ownerSet );
57
58   /**
59    * Virtual destructor.
60    */
61   virtual ~ConstraintBase();
62
63   /**
64    * Initialize the constraint.
65    * This should by called by a scene-object, when the constraint is connected.
66    */
67   void OnConnect()
68   {
69     StartObservation();
70
71     mDisconnected = false;
72   }
73
74   /**
75    * @copydoc Dali::Constraint::SetRemoveAction()
76    */
77   void SetRemoveAction( RemoveAction action )
78   {
79     mRemoveAction = action;
80   }
81
82   /**
83    * @copydoc Dali::Constraint::GetRemoveAction()
84    */
85   RemoveAction GetRemoveAction() const
86   {
87     return mRemoveAction;
88   }
89
90   /**
91    * Bake the weight property.
92    * @param[in] updateBufferIndex The current update buffer index.
93    * @param[in] weight The new weight.
94    */
95   void BakeWeight( BufferIndex updateBufferIndex, float weight )
96   {
97     mWeight.Bake( updateBufferIndex, weight );
98   }
99
100   /**
101    * Set the initial weight.
102    * @pre The constraint has not been connected to the scene-graph.
103    * @param[in] weight The new weight.
104    */
105   void SetInitialWeight( float weight )
106   {
107     mWeight.SetInitial( weight );
108   }
109
110   /**
111    * Retrieve the weight property.
112    * @param[in] bufferIndex The buffer index to read from.
113    * @return The current weight.
114    */
115   float GetWeight( BufferIndex bufferIndex ) const
116   {
117     return mWeight[ bufferIndex ];
118   }
119
120   /**
121    * Constrain the associated scene object.
122    * @param[in] updateBufferIndex The current update buffer index.
123    */
124   virtual void Apply( BufferIndex updateBufferIndex ) = 0;
125
126 private:
127
128   /**
129    * Helper to start observing property owners
130    */
131   void StartObservation()
132   {
133     for( PropertyOwnerIter iter = mObservedOwners.begin(); mObservedOwners.end() != iter; ++iter )
134     {
135       (*iter)->AddObserver( *this );
136     }
137   }
138
139   /**
140    * Helper to stop observing property owners
141    */
142   void StopObservation()
143   {
144     for( PropertyOwnerIter iter = mObservedOwners.begin(); mObservedOwners.end() != iter; ++iter )
145     {
146       (*iter)->RemoveObserver( *this );
147     }
148
149     mObservedOwners.clear();
150   }
151
152   /**
153    * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
154    */
155   virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
156   {
157     // Discard pointer to destroyed property owner
158     PropertyOwnerIter iter = mObservedOwners.find( &owner );
159     DALI_ASSERT_DEBUG( mObservedOwners.end() != iter );
160     mObservedOwners.erase( iter );
161
162     // Stop observing the remaining property owners
163     StopObservation();
164
165     // Notification for derived class
166     OnDisconnect();
167
168     mDisconnected = true;
169   }
170
171   /**
172    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
173    */
174   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
175
176   /**
177    * Notify the derived class to disconnect from property owners
178    */
179   virtual void OnDisconnect() = 0;
180
181 public:
182
183   AnimatableProperty<float> mWeight; ///< The constraint is "fully-applied" when weight = 1
184
185 protected:
186
187   RemoveAction mRemoveAction;
188
189   bool mFirstApply   : 1;
190   bool mDisconnected : 1;
191
192 private:
193
194   PropertyOwnerSet mObservedOwners; ///< A set of pointers to each observed object. Not owned.
195 };
196
197 // Messages for ConstraintBase
198
199 inline void  BakeWeightMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, float weight )
200 {
201   typedef MessageDoubleBuffered1< ConstraintBase, float > LocalType;
202
203   // Reserve some memory inside the message queue
204   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
205
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 );
208 }
209
210 inline void  SetRemoveActionMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction )
211 {
212   typedef MessageValue1< ConstraintBase, Dali::Constraint::RemoveAction > LocalType;
213
214   // Reserve some memory inside the message queue
215   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
216
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 );
219 }
220
221 } // namespace SceneGraph
222
223 } // namespace Internal
224
225 } // namespace Dali
226
227 #endif // __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__