1 #ifndef __DALI_CONSTRAINT_H__
2 #define __DALI_CONSTRAINT_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
32 * @addtogroup dali_core_animation
38 namespace Internal DALI_INTERNAL
43 typedef Vector< PropertyInput* > PropertyInputContainer;
46 * @brief An abstract base class for Constraints.
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.
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
58 * A constraint can be applied to an object in the following manner:
61 * Handle handle = CreateMyObject();
62 * Constraint constraint = Constraint::New< Vector3 >( handle, CONSTRAINING_PROPERTY_INDEX, &MyFunction );
63 * constraint.AddSource( LocalSource( INPUT_PROPERTY_INDEX ) );
68 class DALI_IMPORT_API Constraint : public BaseHandle
73 * @brief Template for the Function that is called by the Constraint system.
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.
82 * The expected signature of the callback should be:
84 * void Function( P&, const PropertyInputContainer& );
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().
92 * @tparam P The property type to constrain
94 template< typename P >
95 class DALI_INTERNAL Function : public CallbackBase
100 * @brief Constructor which connects to the provided C function (or a static member function).
102 * The expected signature of the function is:
104 * void MyFunction( P&, const PropertyInputContainer& );
108 * @param[in] function The function to call
110 Function( void( *function )( P&, const PropertyInputContainer& ) )
111 : CallbackBase( reinterpret_cast< CallbackBase::Function >( function ) ),
112 mCopyConstructorDispatcher( NULL )
117 * @brief Constructor which copies a function object and connects to the functor of that object.
119 * The function object should have a functor with the following signature:
121 * void operator()( P&, const PropertyInputContainer& );
125 * @param[in] object The object to copy
126 * @tparam T The type of the object
129 Function( const T& object )
130 : CallbackBase( reinterpret_cast< void* >( new T( object ) ), // copy the object
131 NULL, // uses operator() instead of member function
132 reinterpret_cast< CallbackBase::Dispatcher >( &FunctorDispatcher2< T, P&, const PropertyInputContainer& >::Dispatch ),
133 reinterpret_cast< CallbackBase::Destructor >( &Destroyer< T >::Delete ) ),
134 mCopyConstructorDispatcher( reinterpret_cast< CopyConstructorDispatcher >( &ObjectCopyConstructorDispatcher< T >::Copy ) )
139 * @brief Constructor which copies a function object and allows a connection to a member method.
141 * The object should have a method with the signature:
143 * void MyObject::MyMethod( P&, const PropertyInputContainer& );
147 * @param[in] object The object to copy
148 * @param[in] memberFunction The member function to call. This has to be a member of the same class
149 * @tparam T The type of the object
152 Function( const T& object, void ( T::*memberFunction ) ( P&, const PropertyInputContainer& ) )
153 : CallbackBase( reinterpret_cast< void* >( new T( object ) ), // copy the object
154 reinterpret_cast< CallbackBase::MemberFunction >( memberFunction ),
155 reinterpret_cast< CallbackBase::Dispatcher >( &Dispatcher2< T, P&, const PropertyInputContainer& >::Dispatch ),
156 reinterpret_cast< CallbackBase::Destructor >( &Destroyer< T >::Delete ) ),
157 mCopyConstructorDispatcher( reinterpret_cast< CopyConstructorDispatcher >( &ObjectCopyConstructorDispatcher< T >::Copy ) )
162 * @brief Clones the Function object.
164 * The object, if held by this object, is also copied.
167 * @return A pointer to a newly-allocated Function
169 CallbackBase* Clone()
171 CallbackBase* callback = NULL;
172 if ( mImpl && mImpl->mObjectPointer && mCopyConstructorDispatcher )
174 callback = new Function( mCopyConstructorDispatcher( reinterpret_cast< UndefinedClass* >( mImpl->mObjectPointer ) ) /* Copy the object */,
176 mImpl->mMemberFunctionDispatcher,
177 mImpl->mDestructorDispatcher,
178 mCopyConstructorDispatcher );
182 callback = new Function( mFunction );
190 * @brief Must not be declared.
192 * This is used so that no optimisations are done by the compiler when using void*.
194 class UndefinedClass;
197 * @brief Used to call the function to copy the stored object.
200 typedef UndefinedClass* (*CopyConstructorDispatcher) ( UndefinedClass* object );
203 * @brief Copies the actual object in Constraint::Function.
206 * @tparam T The type of the object
209 struct ObjectCopyConstructorDispatcher
212 * @brief Copies the object stored in Constraint::Function.
215 * @param[in] object The object to copy
216 * @return Newly allocated clone of the object
218 static UndefinedClass* Copy( const UndefinedClass* object )
220 T* copy = new T( *( reinterpret_cast< const T* >( object ) ) );
221 return reinterpret_cast< UndefinedClass* >( copy );
226 * @brief Undefined copy constructor.
229 Function( const Function& );
232 * @brief Undefined assignment operator.
235 Function& operator=( const Function& );
238 * @brief Constructor used when copying the stored object.
241 * @param[in] object A newly copied object
242 * @param[in] memberFunction The member function of the object
243 * @param[in] dispatcher Used to call the actual object
244 * @param[in] destructor Used to delete the owned object
245 * @param[in] copyConstructorDispatcher Used to create a copy of the owned object
247 Function( void* object,
248 CallbackBase::MemberFunction memberFunction,
249 CallbackBase::Dispatcher dispatcher,
250 CallbackBase::Destructor destructor,
251 CopyConstructorDispatcher copyConstructorDispatcher )
252 : CallbackBase( object, memberFunction, dispatcher, destructor ),
253 mCopyConstructorDispatcher( copyConstructorDispatcher )
258 * @brief Constructor used when copying a simple stored function.
261 * @param[in] function The function to call
263 Function( CallbackBase::Function function )
264 : CallbackBase( function ),
265 mCopyConstructorDispatcher( NULL )
271 CopyConstructorDispatcher mCopyConstructorDispatcher; ///< Function to call to copy the stored object
275 * @brief Enumeration for the action that will happen when the constraint is removed.
277 * The final value may be "baked" i.e. saved permanently.
278 * Alternatively the constrained value may be discarded when the constraint is removed.
283 Bake, ///< When the constraint is fully-applied, the constrained value is saved. @SINCE_1_0.0
284 Discard ///< When the constraint is removed, the constrained value is discarded. @SINCE_1_0.0
287 static const RemoveAction DEFAULT_REMOVE_ACTION; ///< Bake
290 * @brief Creates an uninitialized Constraint; this can be initialized with Constraint::New().
292 * Calling member functions with an uninitialized Constraint handle is not allowed.
298 * @brief Creates a constraint which targets a property using a function or a static class member.
300 * The expected signature, for a Vector3 type for example, of the function is:
302 * void MyFunction( Vector3&, const PropertyInputContainer& );
305 * Create the constraint with this function as follows:
308 * Constraint constraint = Constraint::New< Vector3 >( handle, CONSTRAINING_PROPERTY_INDEX, &MyFunction );
312 * @param[in] handle The handle to the property-owning object
313 * @param[in] targetIndex The index of the property to constrain
314 * @param[in] function The function to call to set the constrained property value
316 * @tparam P The type of the property to constrain
317 * @return The new constraint
320 static Constraint New( Handle handle, Property::Index targetIndex, void( *function )( P&, const PropertyInputContainer& ) )
322 CallbackBase* callback = new Constraint::Function< P >( function );
323 return New( handle, targetIndex, PropertyTypes::Get< P >(), callback );
327 * @brief Creates a constraint which targets a property using a functor object.
329 * The expected structure, for a Vector3 type for example, of the functor object is:
333 * void operator() ( Vector3&, const PropertyInputContainer& );
337 * Create the constraint with this object as follows:
340 * Constraint constraint = Constraint::New< Vector3 >( handle, CONSTRAINING_PROPERTY_INDEX, MyObject() );
344 * @param[in] handle The handle to the property-owning object
345 * @param[in] targetIndex The index of the property to constraint
346 * @param[in] object The functor object whose functor is called to set the constrained property value
348 * @tparam P The type of the property to constrain
349 * @tparam T The type of the object
350 * @return The new constraint
352 template< class P, class T >
353 static Constraint New( Handle handle, Property::Index targetIndex, const T& object )
355 CallbackBase* function = new Constraint::Function< P >( object );
356 return New( handle, targetIndex, PropertyTypes::Get< P >(), function );
360 * @brief Creates a constraint which targets a property using an object method.
362 * The expected structure, for a Vector3 type for example, of the object is:
366 * void MyMethod( Vector3&, const PropertyInputContainer& );
370 * Create the constraint with this object as follows:
373 * Constraint constraint = Constraint::New< Vector3 >( handle, CONSTRAINING_PROPERTY_INDEX, MyObject(), &MyObject::MyMethod );
377 * @param[in] handle The handle to the property-owning object
378 * @param[in] targetIndex The index of the property to constraint
379 * @param[in] object The object whose member function is called to set the constrained property value
380 * @param[in] memberFunction The member function to call to set the constrained property value
381 * @return The new constraint
383 * @tparam P The type of the property to constrain
384 * @tparam T The type of the object
386 template< class P, class T >
387 static Constraint New( Handle handle, Property::Index targetIndex, const T& object, void ( T::*memberFunction ) ( P&, const PropertyInputContainer& ) )
389 CallbackBase* function = new Constraint::Function< P >( object, memberFunction );
390 return New( handle, targetIndex, PropertyTypes::Get< P >(), function );
394 * @brief Creates a clone of this constraint for another object.
397 * @param[in] handle The handle to the property-owning object this constraint is to be cloned for
398 * @return The new constraint
400 Constraint Clone( Handle handle );
405 * This is non-virtual since derived Handle types must not contain data or virtual methods.
411 * @brief This copy constructor is required for (smart) pointer semantics.
414 * @param[in] constraint A reference to the copied handle
416 Constraint( const Constraint& constraint );
419 * @brief This assignment operator is required for (smart) pointer semantics.
422 * @param[in] rhs A reference to the copied handle
423 * @return A reference to this
425 Constraint& operator=( const Constraint& rhs );
428 * @brief Downcasts a handle to Constraint handle.
430 * If handle points to a Constraint object, the downcast produces valid handle.
431 * If not, the returned handle is left uninitialized.
433 * @param[in] baseHandle BaseHandle to an object
434 * @return Handle to a Constraint object or an uninitialized handle
436 static Constraint DownCast( BaseHandle baseHandle );
439 * @brief Adds a constraint source to the constraint.
442 * @param[in] source The constraint source input to add
444 void AddSource( ConstraintSource source );
447 * @brief Applies this constraint.
450 * @pre The constraint must be initialized.
451 * @pre The target object must still be alive.
452 * @pre The source inputs should not have been destroyed.
457 * @brief Removes this constraint.
463 * @brief Retrieves the object which this constraint is targeting.
466 * @return The target object
468 Handle GetTargetObject();
471 * @brief Retrieves the property which this constraint is targeting.
474 * @return The target property
476 Dali::Property::Index GetTargetProperty();
479 * @brief Sets the remove action. Constraint::Bake will "bake" a value when fully-applied.
481 * In case of Constraint::Discard, the constrained value will be discarded, when the constraint is removed.
482 * The default value is Constraint::Bake.
484 * @param[in] action The remove-action
486 void SetRemoveAction( RemoveAction action );
489 * @brief Retrieves the remove action that will happen when the constraint is removed.
492 * @return The remove-action
494 RemoveAction GetRemoveAction() const;
497 * @brief Sets a tag for the constraint so it can be identified later.
500 * @param[in] tag An integer to identify the constraint
502 void SetTag( const unsigned int tag );
505 * @brief Gets the tag.
510 unsigned int GetTag() const;
512 public: // Not intended for use by Application developers
516 * @brief This constructor is used by Constraint::New() methods.
518 * @param[in] constraint A pointer to a newly allocated Dali resource
520 explicit DALI_INTERNAL Constraint( Internal::ConstraintBase* constraint );
523 private: // Not intended for use by Application developers
527 * @brief Constructs a new constraint which targets a property.
530 * @param[in] handle The handle to the property-owning object
531 * @param[in] targetIndex The index of the property to constrain
532 * @param[in] targetType Type The type of the constrained property
533 * @param[in] function The constraint function
534 * @return The new constraint
536 static Constraint New( Handle handle, Property::Index targetIndex, Property::Type targetType, CallbackBase* function );
545 #endif // __DALI_CONSTRAINT_H__