1bf916af053957cdecc1382ce3ed323c50d14b17
[platform/core/uifw/dali-core.git] / dali / public-api / animation / constraint.h
1 #ifndef __DALI_CONSTRAINT_H__
2 #define __DALI_CONSTRAINT_H__
3
4 /*
5  * Copyright (c) 2015 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-source.h>
23 #include <dali/public-api/common/dali-vector.h>
24 #include <dali/public-api/object/base-handle.h>
25 #include <dali/public-api/object/property.h>
26 #include <dali/public-api/object/property-input.h>
27 #include <dali/public-api/signals/callback.h>
28
29 namespace Dali
30 {
31 /**
32  * @addtogroup dali_core_animation
33  * @{
34  */
35
36 class Handle;
37
38 namespace Internal DALI_INTERNAL
39 {
40 class ConstraintBase;
41 }
42
43 typedef Vector< PropertyInput* > PropertyInputContainer;
44
45 /**
46  * @brief An abstract base class for Constraints.
47  *
48  * This can be used to constrain a property of an object, after animations have been applied.
49  * Constraints are applied in the following order:
50  *   - Constraints are applied to on-stage actors in a depth-first traversal.
51  *   - For each actor, the constraints are applied in the same order as the calls to Apply().
52  *   - Constraints are not applied to off-stage actors.
53  *
54  * Create a constraint using one of the New methods depending on the type of callback function used.
55  * Try to use a C function unless some data needs to be stored, otherwise functors and class methods
56  * are also supported.
57  *
58  * A constraint can be applied to an object in the following manner:
59  *
60  * @code
61  * Handle handle = CreateMyObject();
62  * Constraint constraint = Constraint::New< Vector3 >( handle, CONSTRAINING_PROPERTY_INDEX, &MyFunction );
63  * constraint.AddSource( LocalSource( INPUT_PROPERTY_INDEX ) );
64  * constraint.Apply();
65  * @endcode
66  * @SINCE_1_0.0
67  */
68 class DALI_IMPORT_API Constraint : public BaseHandle
69 {
70 public:
71
72   /**
73    * @brief Template for the Function that is called by the Constraint system.
74    *
75    * Supports:
76    *  - C style functions
77    *  - Static member methods of an object
78    *  - Member functions of a particular class
79    *  - Functors of a particular class
80    *  - If a functor or method is provided, then a copy of the object is made.
81    *
82    * The expected signature of the callback should be:
83    * @code
84    *   void Function( P&, const PropertyInputContainer& );
85    * @endcode
86    *
87    * The P& parameter is an in,out parameter which stores the current value of the property. The callback
88    * should change this value to the desired one. The PropertyInputContainer is a const reference to the property inputs
89    * added to the Constraint in the order they were added via AddSource().
90    *
91    * @tparam  P  The property type to constrain.
92    * @SINCE_1_0.0
93    */
94   template< typename P >
95   class DALI_INTERNAL Function : public CallbackBase
96   {
97   public:
98
99     /**
100      * @brief Constructor which connects to the provided C function (or a static member function).
101      *
102      * The expected signature of the function is:
103      * @code
104      *   void MyFunction( P&, const PropertyInputContainer& );
105      * @endcode
106      *
107      * @SINCE_1_0.0
108      * @param[in]  function  The function to call.
109      */
110     Function( void( *function )( P&, const PropertyInputContainer& ) )
111     : CallbackBase( reinterpret_cast< CallbackBase::Function >( function ) ),
112       mCopyConstructorDispatcher( NULL )
113     {
114     }
115
116     /**
117      * @brief Constructor which copies a function object and connects to the functor of that object.
118      *
119      * The function object should have a functor with the following signature:
120      * @code
121      *   void operator()( P&, const PropertyInputContainer& );
122      * @endcode
123      *
124      * @SINCE_1_0.0
125      * @param[in]  object  The object to copy.
126      *
127      * @tparam  T  The type of the object.
128      */
129     template< class T >
130     Function( const T& object )
131     : CallbackBase( reinterpret_cast< void* >( new T( object ) ), // copy the object
132                     NULL, // uses operator() instead of member function
133                     reinterpret_cast< CallbackBase::Dispatcher >( &FunctorDispatcher2< T, P&, const PropertyInputContainer& >::Dispatch ),
134                     reinterpret_cast< CallbackBase::Destructor >( &Destroyer< T >::Delete ) ),
135       mCopyConstructorDispatcher( reinterpret_cast< CopyConstructorDispatcher >( &ObjectCopyConstructorDispatcher< T >::Copy ) )
136     {
137     }
138
139     /**
140      * @brief Constructor which copies a function object and allows a connection to a member method.
141      *
142      * The object should have a method with the signature:
143      * @code
144      *   void MyObject::MyMethod( P&, const PropertyInputContainer& );
145      * @endcode
146      *
147      * @SINCE_1_0.0
148      * @param[in]  object          The object to copy.
149      * @param[in]  memberFunction  The member function to call. This has to be a member of the same class.
150      *
151      * @tparam  T  The type of the object.
152      */
153     template< class T >
154     Function( const T& object, void ( T::*memberFunction ) ( P&, const PropertyInputContainer& ) )
155     : CallbackBase( reinterpret_cast< void* >( new T( object ) ), // copy the object
156                     reinterpret_cast< CallbackBase::MemberFunction >( memberFunction ),
157                     reinterpret_cast< CallbackBase::Dispatcher >( &Dispatcher2< T, P&, const PropertyInputContainer& >::Dispatch ),
158                     reinterpret_cast< CallbackBase::Destructor >( &Destroyer< T >::Delete ) ),
159       mCopyConstructorDispatcher( reinterpret_cast< CopyConstructorDispatcher >( &ObjectCopyConstructorDispatcher< T >::Copy ) )
160     {
161     }
162
163     /**
164      * @brief Clones the Function object.
165      *
166      * The object, if held by this object, is also copied.
167      *
168      * @SINCE_1_0.0
169      * @return A pointer to a newly-allocation Function.
170      */
171     CallbackBase* Clone()
172     {
173       CallbackBase* callback = NULL;
174       if ( mImpl && mImpl->mObjectPointer && mCopyConstructorDispatcher )
175       {
176         callback = new Function( mCopyConstructorDispatcher( reinterpret_cast< UndefinedClass* >( mImpl->mObjectPointer ) ) /* Copy the object */,
177                                  mMemberFunction,
178                                  mImpl->mMemberFunctionDispatcher,
179                                  mImpl->mDestructorDispatcher,
180                                  mCopyConstructorDispatcher );
181       }
182       else
183       {
184         callback = new Function( mFunction );
185       }
186       return callback;
187     }
188
189   private:
190
191     /**
192      * @brief Must not be declared.
193      *
194      * This is used so that no optimisations are done by the compiler when using void*.
195      */
196     class UndefinedClass;
197
198     /**
199      * @brief Used to call the function to copy the stored object
200      * @SINCE_1_0.0
201      */
202     typedef UndefinedClass* (*CopyConstructorDispatcher) ( UndefinedClass* object );
203
204     /**
205      * @brief Copies the actual object in Constraint::Function.
206      *
207      * @tparam  T  The type of the object.
208      * @SINCE_1_0.0
209      */
210     template< class T >
211     struct ObjectCopyConstructorDispatcher
212     {
213       /**
214        * @brief Copy the object stored in Constraint::Function.
215        *
216        * @SINCE_1_0.0
217        * @param[in]  object  The object to copy.
218        *
219        * @return Newly allocated clone of the object.
220        */
221       static UndefinedClass* Copy( const UndefinedClass* object )
222       {
223         T* copy = new T( *( reinterpret_cast< const T* >( object ) ) );
224         return reinterpret_cast< UndefinedClass* >( copy );
225       }
226     };
227
228     /**
229      * @brief Undefined copy constructor
230      * @SINCE_1_0.0
231      */
232     Function( const Function& );
233
234     /**
235      * @brief Undefined assignment operator
236      * @SINCE_1_0.0
237      */
238     Function& operator=( const Function& );
239
240     /**
241      * @brief Constructor used when copying the stored object.
242      *
243      * @SINCE_1_0.0
244      * @param[in]  object                     A newly copied object
245      * @param[in]  memberFunction             The member function of the object.
246      * @param[in]  dispatcher                 Used to call the actual object.
247      * @param[in]  destructor                 Used to delete the owned object.
248      * @param[in]  copyConstructorDispatcher  Used to create a copy of the owned object.
249      */
250     Function( void* object,
251               CallbackBase::MemberFunction memberFunction,
252               CallbackBase::Dispatcher dispatcher,
253               CallbackBase::Destructor destructor,
254               CopyConstructorDispatcher copyConstructorDispatcher )
255     : CallbackBase( object, memberFunction, dispatcher, destructor ),
256       mCopyConstructorDispatcher( copyConstructorDispatcher )
257     {
258     }
259
260     /**
261      * @brief Constructor used when copying a simple stored function.
262      *
263      * @SINCE_1_0.0
264      * @param[in]  function   The function to call.
265      */
266     Function( CallbackBase::Function function )
267     : CallbackBase( function ),
268       mCopyConstructorDispatcher( NULL )
269     {
270     }
271
272     // Data
273
274     CopyConstructorDispatcher mCopyConstructorDispatcher; ///< Function to call to copy the stored object
275   };
276
277   /**
278    * @brief The action that will happen when the constraint is removed.
279    *
280    * The final value may be "baked" i.e. saved permanently.
281    * Alternatively the constrained value may be discarded when the constraint is removed.
282    * @SINCE_1_0.0
283    */
284   enum RemoveAction
285   {
286     Bake,   ///< When the constraint is fully-applied, the constrained value is saved. @SINCE_1_0.0
287     Discard ///< When the constraint is removed, the constrained value is discarded. @SINCE_1_0.0
288   };
289
290   static const RemoveAction  DEFAULT_REMOVE_ACTION;  ///< Bake
291
292   /**
293    * @brief Create an uninitialized Constraint; this can be initialized with Constraint::New().
294    *
295    * Calling member functions with an uninitialized Constraint handle is not allowed.
296    * @SINCE_1_0.0
297    */
298   Constraint();
299
300   /**
301    * @brief Create a constraint which targets a property using a function or a static class member.
302    *
303    * The expected signature, for a Vector3 type for example, of the function is:
304    * @code
305    *   void MyFunction( Vector3&, const PropertyInputContainer& );
306    * @endcode
307    *
308    * Create the constraint with this function as follows:
309    *
310    * @code
311    *   Constraint constraint = Constraint::New< Vector3 >( handle, CONSTRAINING_PROPERTY_INDEX, &MyFunction );
312    * @endcode
313    *
314    * @SINCE_1_0.0
315    * @param[in]  handle       The handle to the property-owning object.
316    * @param[in]  targetIndex  The index of the property to constrain.
317    * @param[in]  function     The function to call to set the constrained property value.
318    * @return The new constraint.
319    *
320    * @tparam P The type of the property to constrain.
321    */
322   template< class P >
323   static Constraint New( Handle handle, Property::Index targetIndex, void( *function )( P&, const PropertyInputContainer& ) )
324   {
325     CallbackBase* callback = new Constraint::Function< P >( function );
326     return New( handle, targetIndex, PropertyTypes::Get< P >(), callback );
327   }
328
329   /**
330    * @brief Create a constraint which targets a property using a functor object.
331    *
332    * The expected structure, for a Vector3 type for example, of the functor object is:
333    * @code
334    *   struct MyObject
335    *   {
336    *     void operator() ( Vector3&, const PropertyInputContainer& );
337    *   };
338    * @endcode
339    *
340    * Create the constraint with this object as follows:
341    *
342    * @code
343    *   Constraint constraint = Constraint::New< Vector3 >( handle, CONSTRAINING_PROPERTY_INDEX, MyObject() );
344    * @endcode
345    *
346    * @SINCE_1_0.0
347    * @param[in]  handle       The handle to the property-owning object.
348    * @param[in]  targetIndex  The index of the property to constrain.
349    * @param[in]  object       The functor object whose functor is called to set the constrained property value.
350    * @return The new constraint.
351    *
352    * @tparam P The type of the property to constrain.
353    * @tparam T The type of the object.
354    */
355   template< class P, class T >
356   static Constraint New( Handle handle, Property::Index targetIndex, const T& object )
357   {
358     CallbackBase* function = new Constraint::Function< P >( object );
359     return New( handle, targetIndex, PropertyTypes::Get< P >(), function );
360   }
361
362   /**
363    * @brief Create a constraint which targets a property using an object method.
364    *
365    * The expected structure, for a Vector3 type for example, of the object is:
366    * @code
367    *   struct MyObject
368    *   {
369    *     void MyMethod( Vector3&, const PropertyInputContainer& );
370    *   };
371    * @endcode
372    *
373    * Create the constraint with this object as follows:
374    *
375    * @code
376    *   Constraint constraint = Constraint::New< Vector3 >( handle, CONSTRAINING_PROPERTY_INDEX, MyObject(), &MyObject::MyMethod );
377    * @endcode
378    *
379    * @SINCE_1_0.0
380    * @param[in]  handle          The handle to the property-owning object.
381    * @param[in]  targetIndex     The index of the property to constrain.
382    * @param[in]  object          The object whose member function is called to set the constrained property value.
383    * @param[in]  memberFunction  The member function to call to set the constrained property value.
384    * @return The new constraint.
385    *
386    * @tparam P The type of the property to constrain.
387    * @tparam T The type of the object.
388    */
389   template< class P, class T >
390   static Constraint New( Handle handle, Property::Index targetIndex, const T& object, void ( T::*memberFunction ) ( P&, const PropertyInputContainer& ) )
391   {
392     CallbackBase* function = new Constraint::Function< P >( object, memberFunction );
393     return New( handle, targetIndex, PropertyTypes::Get< P >(), function );
394   }
395
396   /**
397    * @brief Creates a clones of this constraint for another object.
398    *
399    * @SINCE_1_0.0
400    * @param[in]  handle  The handle to the property-owning object this constraint is to be cloned for.
401    *
402    * @return The new constraint.
403    */
404   Constraint Clone( Handle handle );
405
406   /**
407    * @brief Destructor
408    *
409    * This is non-virtual since derived Handle types must not contain data or virtual methods.
410    * @SINCE_1_0.0
411    */
412   ~Constraint();
413
414   /**
415    * @brief This copy constructor is required for (smart) pointer semantics.
416    *
417    * @SINCE_1_0.0
418    * @param [in]  constraint  A reference to the copied handle
419    */
420   Constraint( const Constraint& constraint );
421
422   /**
423    * @brief This assignment operator is required for (smart) pointer semantics.
424    *
425    * @SINCE_1_0.0
426    * @param [in] rhs  A reference to the copied handle
427    * @return A reference to this
428    */
429   Constraint& operator=( const Constraint& rhs );
430
431   /**
432    * @brief Downcast a handle to Constraint handle.
433    *
434    * If handle points to a Constraint object the
435    * downcast produces valid handle. If not the returned handle is left uninitialized.
436    * @SINCE_1_0.0
437    * @param[in] baseHandle BaseHandle to an object
438    * @return Handle to a Constraint object or an uninitialized handle
439    */
440   static Constraint DownCast( BaseHandle baseHandle );
441
442   /**
443    * @brief Adds a constraint source to the constraint
444    *
445    * @SINCE_1_0.0
446    * @param[in] source The constraint source input to add
447    */
448   void AddSource( ConstraintSource source );
449
450   /**
451    * @brief Applies this constraint.
452    *
453    * @SINCE_1_0.0
454    * @pre The constraint must be initialized
455    * @pre The target object must still be alive
456    * @pre The source inputs should not have been destroyed
457    */
458   void Apply();
459
460   /**
461    * @brief Removes this constraint.
462    * @SINCE_1_0.0
463    */
464   void Remove();
465
466   /**
467    * @brief Retrieve the object which this constraint is targeting.
468    *
469    * @SINCE_1_0.0
470    * @return The target object.
471    */
472   Handle GetTargetObject();
473
474   /**
475    * @brief Retrieve the property which this constraint is targeting.
476    *
477    * @SINCE_1_0.0
478    * @return The target property.
479    */
480   Dali::Property::Index GetTargetProperty();
481
482   /**
483    * @brief Set the remove action. Constraint::Bake will "bake" a value when fully-applied.
484    *
485    * In case of Constraint::Discard, the constrained value will be discarded, when the constraint is removed.
486    * The default value is Constraint::Bake.
487    * @SINCE_1_0.0
488    * @param[in] action The remove-action.
489    */
490   void SetRemoveAction( RemoveAction action );
491
492   /**
493    * @brief Retrieve the remove action that will happen when the constraint is removed.
494    *
495    * @SINCE_1_0.0
496    * @return The remove-action.
497    */
498   RemoveAction GetRemoveAction() const;
499
500   /**
501    * @brief Set a tag for the constraint so it can be identified later
502    *
503    * @SINCE_1_0.0
504    * @param[in] tag An integer to identify the constraint
505    */
506   void SetTag( const unsigned int tag );
507
508   /**
509    * @brief Get the tag
510    *
511    * @SINCE_1_0.0
512    * @return The tag
513    */
514   unsigned int GetTag() const;
515
516 public: // Not intended for use by Application developers
517
518   /**
519    * @brief This constructor is used by Constraint::New() methods
520    * @SINCE_1_0.0
521    * @param [in] constraint A pointer to a newly allocated Dali resource
522    */
523   explicit DALI_INTERNAL Constraint( Internal::ConstraintBase* constraint );
524
525 private: // Not intended for use by Application developers
526
527   /**
528    * @brief Construct a new constraint which targets a property.
529    *
530    * @SINCE_1_0.0
531    * @param[in]  handle       The handle to the property-owning object.
532    * @param[in]  targetIndex  The index of the property to constrain.
533    * @param[in]  targetType   Type The type of the constrained property.
534    * @param[in]  function     The constraint function.
535    * @return The new constraint.
536    */
537   static Constraint New( Handle handle, Property::Index targetIndex, Property::Type targetType, CallbackBase* function );
538 };
539
540 /**
541  * @}
542  */
543 } // namespace Dali
544
545 #endif // __DALI_CONSTRAINT_H__