Merge "Added api function to specify speed factor of an animation" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / active-constraint-base.h
1 #ifndef __DALI_INTERNAL_ACTIVE_CONSTRAINT_BASE_H__
2 #define __DALI_INTERNAL_ACTIVE_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/internal/common/owner-pointer.h>
23 #include <dali/internal/event/common/proxy-object.h>
24 #include <dali/public-api/animation/active-constraint.h>
25 #include <dali/public-api/animation/alpha-functions.h>
26 #include <dali/public-api/animation/animation.h>
27 #include <dali/public-api/animation/constraint.h>
28 #include <dali/public-api/animation/time-period.h>
29 #include <dali/public-api/common/dali-common.h>
30 #include <dali/public-api/common/set-wrapper.h>
31 #include <dali/internal/event/animation/constraint-source-impl.h>
32
33 namespace Dali
34 {
35
36 namespace Internal
37 {
38
39 class EventToUpdate;
40 typedef std::set<ProxyObject*>         ProxyObjectContainer;
41 typedef ProxyObjectContainer::iterator ProxyObjectIter;
42
43 namespace SceneGraph
44 {
45 class ConstraintBase;
46
47 template <typename T>
48 class AnimatableProperty;
49 }
50
51 /**
52  * An abstract base class for active constraints.
53  */
54 class ActiveConstraintBase : public ProxyObject, public ProxyObject::Observer
55 {
56 public:
57
58   typedef Dali::Constraint::RemoveAction RemoveAction;
59   typedef Any AnyFunction;
60
61   /**
62    * Constructor.
63    * @param[in] messageController Used to send messages to the update-thread.
64    * @param[in] targetPropertyIndex The index of the property being constrained.
65    * @param[in] sources The sources of the input properties.
66    * @param[in] sourceCount The original number of sources; this may not match sources.size() if objects have died.
67    */
68   ActiveConstraintBase( EventToUpdate& messageController, Property::Index targetPropertyIndex, SourceContainer& sources, unsigned int sourceCount );
69
70   /**
71    * Virtual destructor.
72    */
73   virtual ~ActiveConstraintBase();
74
75   /**
76    * Clone an active-constraint.
77    * @return A new active-constraint.
78    */
79   virtual ActiveConstraintBase* Clone() = 0;
80
81   /**
82    * Set a custom "weight" property.
83    * @param[in] weightObject An object with a "weight" float property.
84    * @param[in] weightIndex The index of the weight property.
85    */
86   void SetCustomWeightObject( ProxyObject& weightObject, Property::Index weightIndex );
87
88   /**
89    * Called when the ActiveConstraint is first applied.
90    * @pre The active-constraint does not already have a parent.
91    * @param[in] parent The parent object.
92    * @param[in] applyTime The apply-time for this constraint.
93    */
94   void FirstApply( ProxyObject& parent, TimePeriod applyTime );
95
96   /**
97    * Called when the ActiveConstraint is removed.
98    */
99   void BeginRemove();
100
101   /**
102    * Called when the target object is destroyed.
103    */
104   void OnParentDestroyed();
105
106   /**
107    * Called when the target object is connected to the scene-graph
108    */
109   void OnParentSceneObjectAdded();
110
111   /**
112    * Called when the target object is disconnected from the scene-graph
113    */
114   void OnParentSceneObjectRemoved();
115
116   /**
117    * Retrieve the parent of the active-constraint.
118    * @return The parent object, or NULL.
119    */
120   ProxyObject* GetParent();
121
122   /**
123    * @copydoc Dali::Internal::Object::Supports()
124    */
125   virtual bool Supports( Object::Capability capability ) const;
126
127   /**
128    * @copydoc Dali::ActiveConstraint::GetTargetObject()
129    */
130   Dali::Handle GetTargetObject();
131
132   /**
133    * @copydoc Dali::ActiveConstraint::GetTargetProperty()
134    */
135   Property::Index GetTargetProperty();
136
137   /**
138    * @copydoc Dali::ActiveConstraint::SetWeight()
139    */
140   void SetWeight( float weight );
141
142   /**
143    * @copydoc Dali::ActiveConstraint::GetCurrentWeight()
144    */
145   float GetCurrentWeight() const;
146
147   /**
148    * @copydoc Dali::ActiveConstraint::AppliedSignal()
149    */
150   ActiveConstraintSignalV2& AppliedSignal();
151
152   /**
153    * @copydoc Dali::Constraint::SetAlphaFunction()
154    */
155   void SetAlphaFunction(AlphaFunction func);
156
157   /**
158    * @copydoc Dali::Constraint::GetAlphaFunction()
159    */
160   AlphaFunction GetAlphaFunction() const;
161
162   /**
163    * @copydoc Dali::Constraint::SetRemoveAction()
164    */
165   void SetRemoveAction(RemoveAction action);
166
167   /**
168    * @copydoc Dali::Constraint::GetRemoveAction()
169    */
170   RemoveAction GetRemoveAction() const;
171
172   /**
173    * @copydoc Dali::Constraint::SetTag()
174    */
175   void SetTag(const unsigned int tag);
176
177   /**
178    * @copydoc Dali::Constraint::GetTag()
179    */
180   unsigned int GetTag() const;
181
182   /**
183    * Connects a callback function with the object's signals.
184    * @param[in] object The object providing the signal.
185    * @param[in] tracker Used to disconnect the signal.
186    * @param[in] signalName The signal to connect to.
187    * @param[in] functor A newly allocated FunctorDelegate.
188    * @return True if the signal was connected.
189    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
190    */
191   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
192
193 public: // Default property extensions from ProxyObject
194
195   /**
196    * @copydoc Dali::Internal::ProxyObject::IsSceneObjectRemovable()
197    */
198   virtual bool IsSceneObjectRemovable() const;
199
200   /**
201    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyCount()
202    */
203   virtual unsigned int GetDefaultPropertyCount() const;
204
205   /**
206    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndices()
207    */
208   virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const;
209
210   /**
211    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyName()
212    */
213   virtual const std::string& GetDefaultPropertyName( Property::Index index ) const;
214
215   /**
216    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndex()
217    */
218   virtual Property::Index GetDefaultPropertyIndex( const std::string& name ) const;
219
220   /**
221    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyWritable()
222    */
223   virtual bool IsDefaultPropertyWritable( Property::Index index ) const;
224
225   /**
226    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAnimatable()
227    */
228   virtual bool IsDefaultPropertyAnimatable( Property::Index index ) const;
229
230   /**
231    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAConstraintInput()
232    */
233   virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const;
234
235   /**
236    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyType()
237    */
238   virtual Property::Type GetDefaultPropertyType( Property::Index index ) const;
239
240   /**
241    * @copydoc Dali::Internal::ProxyObject::SetDefaultProperty()
242    */
243   virtual void SetDefaultProperty( Property::Index index, const Property::Value& propertyValue );
244
245   /**
246    * @copydoc Dali::Internal::ProxyObject::SetCustomProperty()
247    */
248   virtual void SetCustomProperty( Property::Index index, const CustomProperty& entry, const Property::Value& value );
249
250   /**
251    * @copydoc Dali::Internal::ProxyObject::GetDefaultProperty()
252    */
253   virtual Property::Value GetDefaultProperty( Property::Index index ) const;
254
255   /**
256    * @copydoc Dali::Internal::ProxyObject::InstallSceneObjectProperty()
257    */
258   virtual void InstallSceneObjectProperty( SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index );
259
260   /**
261    * @copydoc Dali::Internal::ProxyObject::GetSceneObject()
262    */
263   virtual const SceneGraph::PropertyOwner* GetSceneObject() const;
264
265   /**
266    * @copydoc Dali::Internal::ProxyObject::GetSceneObjectAnimatableProperty()
267    */
268   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
269
270   /**
271    * @copydoc Dali::Internal::ProxyObject::GetSceneObjectInputProperty()
272    */
273   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
274
275 public: // ProxyObject::Observer methods
276
277   /**
278    * @copydoc ProxyObject::Observer::SceneObjectAdded()
279    */
280   virtual void SceneObjectAdded( ProxyObject& proxy );
281
282   /**
283    * @copydoc ProxyObject::Observer::SceneObjectRemoved()
284    */
285   virtual void SceneObjectRemoved( ProxyObject& proxy );
286
287   /**
288    * @copydoc ProxyObject::Observer::ProxyDestroyed()
289    */
290   virtual void ProxyDestroyed( ProxyObject& proxy );
291
292 private:
293
294   /**
295    * Helper to observe a proxy, if not already observing it
296    */
297   void ObserveProxy( ProxyObject& proxy );
298
299   /**
300    * Helper to stop observing proxies
301    */
302   void StopObservation();
303
304   /**
305    * Helper called after the first apply animation.
306    * @param [in] object The active constraint.
307    */
308   static void FirstApplyFinished( Object* object );
309
310   // To be implemented in derived classes
311
312   /**
313    * Create and connect a constraint for a scene-object.
314    */
315   virtual void ConnectConstraint() = 0;
316
317 protected:
318
319   EventToUpdate& mEventToUpdate;
320
321   Property::Index mTargetPropertyIndex;
322   SourceContainer mSources;
323   const unsigned int mSourceCount;
324
325   ProxyObject* mTargetProxy; ///< The proxy-object owns the active-constraint.
326   ProxyObjectContainer mObservedProxies; // We don't observe the same object twice
327
328   const SceneGraph::ConstraintBase* mSceneGraphConstraint;
329
330   const SceneGraph::AnimatableProperty<float>* mCustomWeight;
331
332   float mOffstageWeight;
333
334   AlphaFunction mAlphaFunction;
335
336   RemoveAction mRemoveAction;
337   unsigned int mTag;
338
339 private:
340
341   ActiveConstraintSignalV2 mAppliedSignal;
342
343   Dali::Animation mApplyAnimation;  ///< Used to automatically animate weight from 0.0f -> 1.0f
344
345 };
346
347 } // namespace Internal
348
349 // Helpers for public-api forwarding methods
350
351 inline Internal::ActiveConstraintBase& GetImplementation(Dali::ActiveConstraint& constraint)
352 {
353   DALI_ASSERT_ALWAYS( constraint && "ActiveConstraint handle is empty" );
354
355   BaseObject& handle = constraint.GetBaseObject();
356
357   return static_cast<Internal::ActiveConstraintBase&>(handle);
358 }
359
360 inline const Internal::ActiveConstraintBase& GetImplementation(const Dali::ActiveConstraint& constraint)
361 {
362   DALI_ASSERT_ALWAYS( constraint && "ActiveConstraint handle is empty" );
363
364   const BaseObject& handle = constraint.GetBaseObject();
365
366   return static_cast<const Internal::ActiveConstraintBase&>(handle);
367 }
368
369 } // namespace Dali
370
371 #endif // __DALI_INTERNAL_ACTIVE_CONSTRAINT_BASE_H__