195197b0718eb04c5b26124b621282c4a7cf145e
[platform/core/uifw/dali-core.git] / dali / internal / event / common / object-impl.h
1 #ifndef __DALI_INTERNAL_OBJECT_H__
2 #define __DALI_INTERNAL_OBJECT_H__
3
4 /*
5  * Copyright (c) 2018 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-vector.h>
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/public-api/object/handle.h>
27 #include <dali/public-api/object/property.h>
28 #include <dali/public-api/object/property-index-ranges.h>
29 #include <dali/public-api/object/property-input.h>
30 #include <dali/public-api/object/property-notification.h>
31 #include <dali/devel-api/common/owner-container.h>
32 #include <dali/devel-api/object/handle-devel.h>
33 #include <dali/internal/event/animation/animation-impl.h>
34 #include <dali/internal/event/common/event-thread-services.h>
35 #include <dali/internal/event/common/property-input-impl.h>
36 #include <dali/internal/event/common/property-metadata.h>
37 #include <dali/internal/update/common/property-base.h>
38
39 namespace Dali
40 {
41
42 class PropertyNotification;
43
44 namespace Internal
45 {
46 class ConstraintBase;
47 class EventThreadServices;
48 class PropertyCondition;
49 class PropertyInputImpl;
50 class Stage;
51 class TypeInfo;
52
53 namespace SceneGraph
54 {
55 class PropertyBase;
56 class PropertyOwner;
57 }
58
59 typedef std::vector< Dali::Constraint >     ConstraintContainer;
60 typedef ConstraintContainer::iterator       ConstraintIter;
61 typedef ConstraintContainer::const_iterator ConstraintConstIter;
62
63
64 /**
65  * A base class for objects which optionally provide properties.
66  * The concrete derived class is responsible for implementing the property system methods.
67  * Classes may derive from Dali::BaseObject, until any properties are required.
68  *
69  * An object for a property-owning object in the scene-graph.
70  * This provides an interface for observing the addition/removal of scene-objects.
71  *
72  * The concrete derived class is responsible for:
73  *   1) Adding & removing an object from the scene-graph. The OnSceneObjectAdd() and OnSceneObjectRemove()
74  *      methods should be called by the derived class, to trigger observer callbacks.
75  *   3) Implementing the GetSceneObject() methods, used to access the scene-object.
76  *   4) Providing access to properties stored by the scene-graph object. These should match the properties
77  *      reported by the base Dali::Internal::Object methods.
78  *
79  */
80 class Object : public Dali::BaseObject
81 {
82 public:
83
84   typedef Dali::Handle::Capability Capability;
85
86   class Observer
87   {
88   public:
89
90     /**
91      * Called immediately after the object has created & passed ownership of a scene-graph object.
92      * @param[in] object The object object.
93      */
94     virtual void SceneObjectAdded(Object& object) = 0;
95
96     /**
97      * Called shortly before the object sends a message to remove its scene object.
98      * @param[in] object The object object.
99      */
100     virtual void SceneObjectRemoved(Object& object) = 0;
101
102     /**
103      * Called shortly before the object itself is destroyed; no further callbacks will be received.
104      * @param[in] object The object object.
105      */
106     virtual void ObjectDestroyed(Object& object) = 0;
107
108   protected:
109
110     /**
111      * Virtual destructor
112      */
113     virtual ~Observer(){}
114   };
115
116   /**
117    * Constructor.
118    */
119   Object();
120
121   /**
122    * Add an observer to the object.
123    * @param[in] observer The observer to add.
124    */
125   virtual void AddObserver( Observer& observer );
126
127   /**
128    * Remove an observer from the object
129    * @pre The observer has already been added.
130    * @param[in] observer The observer to remove.
131    */
132   virtual void RemoveObserver( Observer& observer );
133
134   /**
135    * @copydoc Dali::Handle::Supports()
136    */
137   virtual bool Supports( Capability capability ) const;
138
139   /**
140    * @copydoc Dali::Handle::GetPropertyCount()
141    */
142   virtual uint32_t GetPropertyCount() const;
143
144   /**
145    * @copydoc Dali::Handle::GetPropertyName()
146    */
147   virtual std::string GetPropertyName( Property::Index index ) const;
148
149   /**
150    * @copydoc Dali::Handle::GetPropertyIndex()
151    */
152   virtual Property::Index GetPropertyIndex( const std::string& name ) const;
153
154   /**
155    * @copydoc Dali::Handle::GetPropertyIndex()
156    */
157   virtual Property::Index GetPropertyIndex( Property::Index key ) const;
158
159   /**
160    * @copydoc Dali::Handle::GetPropertyIndex()
161    */
162   virtual Property::Index GetPropertyIndex( Property::Key key ) const;
163
164   /**
165    * @copydoc Dali::Handle::IsPropertyWritable()
166    */
167   virtual bool IsPropertyWritable( Property::Index index ) const;
168
169   /**
170    * @copydoc Dali::Handle::IsPropertyAnimatable()
171    */
172   virtual bool IsPropertyAnimatable( Property::Index index ) const;
173
174   /**
175    * @copydoc Dali::Handle::IsPropertyAConstraintInput()
176    */
177   virtual bool IsPropertyAConstraintInput( Property::Index index ) const;
178
179   /**
180    * @copydoc Dali::Handle::GetPropertyType()
181    */
182   virtual Property::Type GetPropertyType( Property::Index index ) const;
183
184   /**
185    * @copydoc Dali::Handle::SetProperty()
186    */
187   virtual void SetProperty( Property::Index index, const Property::Value& propertyValue );
188
189   /**
190    * @copydoc Dali::Handle::GetProperty()
191    */
192   virtual Property::Value GetProperty( Property::Index index ) const;
193
194   /**
195    * @brief Retrieves the latest value of the property on the scene-graph.
196    * @param[in]  index  The index of the property required.
197    * @return The latest value of the property on the scene-graph.
198    */
199   virtual Property::Value GetCurrentProperty( Property::Index index ) const;
200
201   /**
202    * @copydoc Dali::Handle::GetPropertyIndices()
203    */
204   virtual void GetPropertyIndices( Property::IndexContainer& indices ) const;
205
206   /**
207    * @copydoc Dali::Handle::RegisterProperty()
208    */
209   virtual Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue );
210
211   /**
212    * @copydoc Dali::Handle::RegisterProperty()
213    */
214   virtual Property::Index RegisterProperty( const std::string& name, Property::Index key, const Property::Value& propertyValue );
215
216   /**
217    * @copydoc Dali::Handle::RegisterProperty(std::string name, Property::Value propertyValue, Property::AccessMode accessMode)
218    */
219   virtual Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode );
220
221   /**
222    * @brief Implementing method for this override
223    */
224   virtual Property::Index RegisterProperty( const std::string& name, Property::Index key, const Property::Value& propertyValue, Property::AccessMode accessMode );
225
226   /**
227    * @brief returns true if the custom property exists on this object.
228    *
229    * @note The property may be defined for a type within the type registry, but it isn't explicity
230    * defined in every consequent instantiation. It can be automatically added, e.g. parenting an actor
231    * automatically registers it's parent container's child properties.
232    *
233    * @param[in] handle The handle of the object to test
234    * @param[in] index The property index to look for.
235    * @return true if the property exists on the object, false otherwise.
236    */
237   bool DoesCustomPropertyExist( Property::Index index );
238
239   /**
240    * @copydoc Dali::Handle::AddPropertyNotification()
241    */
242   virtual Dali::PropertyNotification AddPropertyNotification( Property::Index index,
243                                                               int componentIndex,
244                                                               const Dali::PropertyCondition& condition );
245
246   /**
247    * @copydoc Dali::Handle::RemovePropertyNotification()
248    */
249   virtual void RemovePropertyNotification( Dali::PropertyNotification propertyNotification );
250
251   /**
252    * @copydoc Dali::Handle::RemovePropertyNotifications()
253    */
254   virtual void RemovePropertyNotifications();
255
256   /**
257    * Notifies that a property is being animated.
258    * @param[in] animation The animation animating the property.
259    * @param[in] index The index of the property.
260    * @param[in] value The value of the property after the animation.
261    * @param[in] animationType Whether the property value given is the target or a relative value.
262    */
263   void NotifyPropertyAnimation( Animation& animation, Property::Index index, const Property::Value& value, Animation::Type animationType );
264
265   /******************************** Uniform Mappings ********************************/
266
267   /**
268    * @copydoc Dali::Handle::AddUniformMapping()
269    */
270   void AddUniformMapping( Property::Index propertyIndex, const std::string& uniformName ) const;
271
272   /**
273    * @copydoc Dali::Handle::RemoveUniformMapping( )
274    */
275   void RemoveUniformMapping( const std::string& uniformName );
276
277   /******************************** Constraints ********************************/
278
279   /**
280    * Apply a constraint to an Object.
281    * @param[in] constraint The constraint to apply.
282    */
283   void ApplyConstraint( ConstraintBase& constraint );
284
285   /**
286    * Remove one constraint from an Object.
287    * @param[in] constraint The constraint to remove.
288    */
289   void RemoveConstraint( ConstraintBase& constraint );
290
291   /**
292    * Remove all constraints from a Object.
293    */
294   void RemoveConstraints();
295
296   /**
297    * @copydoc Dali::Handle::RemoveConstraints( uint32_t )
298    */
299   void RemoveConstraints( uint32_t tag );
300
301   /**
302    * Called by TypeInfo to set the type-info that this object-impl is created by.
303    * @param[in] typeInfo The TypeInfo that creates this object-impl.
304    */
305   void SetTypeInfo( const TypeInfo* typeInfo );
306
307   /**
308    * @return the index from which custom properties start
309    */
310   uint32_t CustomPropertyStartIndex()
311   {
312     return PROPERTY_CUSTOM_START_INDEX;
313   }
314
315   /********************  To be overridden by deriving classes ********************/
316
317   /**
318    * Retrieve the scene-graph object added by this object.
319    * @return A pointer to the object, or NULL if no object has been added to the scene-graph.
320    */
321   virtual const SceneGraph::PropertyOwner* GetSceneObject() const = 0;
322
323   /**
324    * Retrieve an animatable property owned by the scene-graph object.
325    * @pre -1 < index < GetPropertyCount().
326    * @param[in] index The index of the property.
327    * @return A dereferenceable pointer to a property, or NULL if a scene-object does not exist with this property.
328    */
329   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const = 0;
330
331   /**
332    * Retrieve a constraint input-property owned by the scene-graph object.
333    * @pre -1 < index < GetPropertyCount().
334    * @param[in] index The index of the property.
335    * @return A dereferenceable pointer to an input property, or NULL if a scene-object does not exist with this property.
336    */
337   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const = 0;
338
339   /**
340    * Query whether the property is a component of a scene-graph property.
341    * @pre -1 < index < GetPropertyCount().
342    * @param[in] index The index of the property.
343    * @return The index or Property::INVALID_COMPONENT_INDEX.
344    */
345   virtual int GetPropertyComponentIndex( Property::Index index ) const;
346
347   /**
348    * @copydoc Dali::Handle::PropertySetSignal()
349    */
350   DevelHandle::PropertySetSignalType& PropertySetSignal();
351
352 protected:
353
354   /**
355    * A reference counted object may only be deleted by calling Unreference()
356    */
357   virtual ~Object();
358
359   /**
360    * Called immediately by derived classes, after the scene-object has been created & passed to the scene-graph.
361    */
362   void OnSceneObjectAdd();
363
364   /**
365    * Called by derived classes, shortly before send a message to remove the scene-object.
366    */
367   void OnSceneObjectRemove();
368
369   /**
370    * For overriding by derived classes to return the parent of this object.
371    */
372   virtual Object* GetParentObject() const
373   {
374     // By default the Object does not have a parent
375     return NULL;
376   };
377
378   /**
379    * For use in derived classes.
380    * This is called after a property is set.
381    * @param [in] index The index of the property.
382    * @param [in] propertyValue The value of the property.
383    */
384   virtual void OnPropertySet( Property::Index index, Property::Value propertyValue ) {}
385
386   /**
387    * Retrieves the TypeInfo for this object. Only retrieves it from the type-registry once and then stores a pointer
388    * to it locally there-after. The type info will not change during the life-time of the application.
389    * @return The type-info for this object (Can be NULL)
390    */
391   const TypeInfo* GetTypeInfo() const;
392
393   /**
394    * Helper to find custom property
395    * @param index
396    * @return pointer to the property
397    */
398   CustomPropertyMetadata* FindCustomProperty( Property::Index index ) const;
399
400   /**
401    * Helper to find animatable property
402    * @param index
403    * @return pointer to the property
404    */
405   AnimatablePropertyMetadata* FindAnimatableProperty( Property::Index index ) const;
406
407   /**
408    * Helper to register a scene-graph property
409    * @param [in] name The name of the property.
410    * @param [in] key The key of the property
411    * @param [in] index The index of the property
412    * @param [in] value The value of the property.
413    * @return The index of the registered property or Property::INVALID_INDEX if registration failed.
414    */
415   Property::Index RegisterSceneGraphProperty(const std::string& name, Property::Index key, Property::Index index, const Property::Value& propertyValue) const;
416
417   /**
418    * Check whether the animatable property is registered already, if not then register one.
419    * @param [in] index The index of the property
420    * @return pointer to the property.
421    */
422   AnimatablePropertyMetadata* RegisterAnimatableProperty(Property::Index index) const;
423
424   /**
425    * Resolve the index and name of child properties if any.
426    */
427   void ResolveChildProperties();
428
429 private: // Default property extensions for derived classes
430
431   /**
432    * Query how many default properties the derived class supports.
433    * @return The number of default properties.
434    */
435   virtual uint32_t GetDefaultPropertyCount() const = 0;
436
437   /**
438    * Retrieve all the indices that are associated with the default properties supported by the derived class.
439    * @return A container of default property indices.
440    * @note The deriving class must not modify the existing elements in the container.
441    */
442   virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const = 0;
443
444   /**
445    * Query how many default properties the derived class supports.
446    * @return The number of default properties.
447    */
448   virtual const char* GetDefaultPropertyName( Property::Index index ) const = 0;
449
450   /**
451    * Query the index of a default property.
452    * @param [in] name The name of the property.
453    * @return The index of the property, or Property::INVALID_INDEX if no default property exists with the given name.
454    */
455   virtual Property::Index GetDefaultPropertyIndex( const std::string& name ) const = 0;
456
457   /**
458    * Query whether a default property is writable.
459    * @param [in] index The index of the property.
460    * @return True if the property is animatable.
461    */
462   virtual bool IsDefaultPropertyWritable( Property::Index index ) const = 0;
463
464   /**
465    * Query whether a default property is animatable.
466    * This determines whether the property can be the target of an animation or constraint.
467    * @param [in] index The index of the property.
468    * @return True if the property is animatable.
469    */
470   virtual bool IsDefaultPropertyAnimatable( Property::Index index ) const = 0;
471
472   /**
473    * @brief Query whether a default property can be used as an input to a constraint.
474    *
475    * @param [in] index The index of the property.
476    * @return True if the property can be used as an input to a constraint.
477    */
478   virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const = 0;
479
480   /**
481    * Query the type of a default property.
482    * @param [in] index The index of the property.
483    * @return The type of the property.
484    */
485   virtual Property::Type GetDefaultPropertyType( Property::Index index ) const = 0;
486
487   /**
488    * Set the value of a default property.
489    * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
490    * @param [in] index The index of the property.
491    * @param [in] propertyValue The new value of the property.
492    */
493   virtual void SetDefaultProperty( Property::Index index, const Property::Value& propertyValue ) = 0;
494
495   /**
496    * Retrieve a default property value.
497    * @param [in] index The index of the property.
498    * @return The property value.
499    */
500   virtual Property::Value GetDefaultProperty( Property::Index index ) const = 0;
501
502   /**
503    * Retrieve the latest scene-graph value of a default property.
504    * @param[in] index The index of the property.
505    * @return The latest scene-graph value of a default property.
506    */
507   virtual Property::Value GetDefaultPropertyCurrentValue( Property::Index index ) const = 0;
508
509   /**
510    * Notifies that a default property is being animated so the deriving class should update the cached value.
511    * @param[in] animation The animation animating the property.
512    * @param[in] index The index of the property.
513    * @param[in] value The value of the property after the animation.
514    * @param[in] animationType Whether the property value given is the target or a relative value.
515    */
516   virtual void OnNotifyDefaultPropertyAnimation( Animation& animation, Property::Index index, const Property::Value& value, Animation::Type propertyChangeType )
517   { }
518
519   /**
520    * @todo this is virtual so that for now actor can override it,
521    * it needs to be removed and only have GetSceneObject but that requires changing actor and constraint logic
522    * Retrieve the scene-graph object added by this object.
523    * @return A pointer to the object, or NULL if no object has been added to the scene-graph.
524    */
525   virtual const SceneGraph::PropertyOwner* GetPropertyOwner() const
526   {
527     return GetSceneObject();
528   }
529
530 private:
531
532   Object(const Object& rhs) = delete;
533   Object& operator=(const Object& rhs) = delete;
534
535   /**
536    * Enable property notifications in scene graph
537    */
538   void EnablePropertyNotifications();
539
540   /**
541    * Enable property notifications in scene graph
542    */
543   void DisablePropertyNotifications();
544
545   /**
546    * Get the latest value of the property on the scene-graph.
547    * @param[in] entry An entry from the property lookup container.
548    * @return The latest value of the property.
549    */
550   Property::Value GetCurrentPropertyValue( const PropertyMetadata* entry ) const;
551
552   /**
553    * Set the value of scene graph property.
554    * @param [in] index The index of the property.
555    * @param [in] entry An entry from the property lookup container.
556    * @param [in] value The new value of the property.
557    */
558   virtual void SetSceneGraphProperty( Property::Index index, const PropertyMetadata& entry, const Property::Value& value );
559
560 protected:
561   /**
562    * Get the event thread services object - used for sending messages to the scene graph
563    * Assert if called from the wrong thread.
564    * This is intentionally inline for performance reasons.
565    *
566    * @return The event thread services object
567    */
568   inline EventThreadServices& GetEventThreadServices()
569   {
570     DALI_ASSERT_DEBUG( EventThreadServices::IsCoreRunning() );
571     return mEventThreadServices;
572   }
573
574   /**
575    * Get the event thread services object - used for sending messages to the scene graph
576    * Assert if called from the wrong thread
577    * This is intentionally inline for performance reasons.
578    *
579    * @return The event thread services object
580    */
581   inline const EventThreadServices& GetEventThreadServices() const
582   {
583     DALI_ASSERT_DEBUG( EventThreadServices::IsCoreRunning() );
584     return mEventThreadServices;
585   }
586
587 private:
588   EventThreadServices& mEventThreadServices;
589
590 private:
591
592   typedef OwnerContainer<PropertyMetadata*> PropertyMetadataLookup;
593   mutable PropertyMetadataLookup mCustomProperties; ///< Used for accessing custom Node properties
594   mutable PropertyMetadataLookup mAnimatableProperties; ///< Used for accessing animatable Node properties
595   mutable TypeInfo const *  mTypeInfo; ///< The type-info for this object, mutable so it can be lazy initialized from const method if it is required
596
597   Dali::Vector<Observer*> mObservers;
598
599   ConstraintContainer* mConstraints;               ///< Container of owned -constraints.
600
601   typedef std::vector< Dali::PropertyNotification >     PropertyNotificationContainer;
602   typedef PropertyNotificationContainer::iterator       PropertyNotificationContainerIter;
603   typedef PropertyNotificationContainer::const_iterator PropertyNotificationContainerConstIter;
604   PropertyNotificationContainer* mPropertyNotifications; ///< Container of owned property notifications.
605   DevelHandle::PropertySetSignalType mPropertySetSignal;
606 };
607 } // namespace Internal
608
609 // Helpers for public-api forwarding methods
610
611 inline Internal::Object& GetImplementation(Dali::Handle& object)
612 {
613   DALI_ASSERT_ALWAYS( object && "Object handle is empty" );
614
615   BaseObject& handle = object.GetBaseObject();
616
617   return static_cast<Internal::Object&>(handle);
618 }
619
620 inline const Internal::Object& GetImplementation(const Dali::Handle& object)
621 {
622   DALI_ASSERT_ALWAYS( object && "Object handle is empty" );
623
624   const BaseObject& handle = object.GetBaseObject();
625
626   return static_cast<const Internal::Object&>(handle);
627 }
628
629 } // namespace Dali
630
631 #endif // __DALI_INTERNAL_OBJECT_H__