[dali_1.4.21] Merge branch 'devel/master'
[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) 2019 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 // EXTERNAL INCLUDES
22 #include <cstdint> // uint32_t
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/animation/constraint.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/common/vector-wrapper.h>
28 #include <dali/public-api/object/base-object.h>
29 #include <dali/public-api/object/handle.h>
30 #include <dali/public-api/object/property.h>
31 #include <dali/public-api/object/property-index-ranges.h>
32 #include <dali/public-api/object/property-input.h>
33 #include <dali/public-api/object/property-notification.h>
34 #include <dali/devel-api/common/owner-container.h>
35 #include <dali/devel-api/object/handle-devel.h>
36 #include <dali/internal/event/animation/animation-impl.h>
37 #include <dali/internal/event/common/event-thread-services.h>
38 #include <dali/internal/event/common/property-input-impl.h>
39 #include <dali/internal/event/common/property-metadata.h>
40 #include <dali/internal/update/common/property-base.h>
41
42 namespace Dali
43 {
44
45 class PropertyNotification;
46
47 namespace Internal
48 {
49 class ConstraintBase;
50 class EventThreadServices;
51 class PropertyCondition;
52 class PropertyInputImpl;
53 class Stage;
54 class TypeInfo;
55
56 namespace SceneGraph
57 {
58 class PropertyBase;
59 class PropertyOwner;
60 }
61
62 using ConstraintContainer = std::vector< Dali::Constraint >;
63 using ConstraintIter = ConstraintContainer::iterator;
64 using ConstraintConstIter = ConstraintContainer::const_iterator;
65
66 /**
67  * A base class for objects which optionally provide properties.
68  * The concrete derived class is responsible for implementing the property system methods.
69  * Classes may derive from Dali::BaseObject, until any properties are required.
70  *
71  * An object for a property-owning object in the scene-graph.
72  * This provides an interface for observing the addition/removal of scene-objects.
73  *
74  * The derived class should either:
75  *   a) Create their own scene-graph object and pass it to Object constructor.
76  *   b) pass nullptr to Object constructor, in this case Object will create default scene object for property handling
77  */
78 class Object : public Dali::BaseObject
79 {
80 public:
81
82   using Capability = Dali::Handle::Capability;
83
84   class Observer
85   {
86   public:
87
88     /**
89      * Called immediately after the object has created & passed ownership of a scene-graph object.
90      * @param[in] object The object object.
91      */
92     virtual void SceneObjectAdded(Object& object) = 0;
93
94     /**
95      * Called shortly before the object sends a message to remove its scene object.
96      * @param[in] object The object object.
97      */
98     virtual void SceneObjectRemoved(Object& object) = 0;
99
100     /**
101      * Called shortly before the object itself is destroyed; no further callbacks will be received.
102      * @param[in] object The object object.
103      */
104     virtual void ObjectDestroyed(Object& object) = 0;
105
106   protected:
107
108     /**
109      * Virtual destructor
110      */
111     virtual ~Observer(){}
112   };
113
114   /**
115    * Creates a new object
116    *
117    * @return an smart pointer to the object
118    */
119   static IntrusivePtr<Object> New();
120
121   /**
122    * Add an observer to the object.
123    * @param[in] observer The observer to add.
124    */
125   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   void RemoveObserver( Observer& observer );
133
134   /**
135    * @copydoc Dali::Handle::Supports()
136    */
137   bool Supports( Capability capability ) const;
138
139   /**
140    * @copydoc Dali::Handle::GetPropertyCount()
141    */
142   uint32_t GetPropertyCount() const;
143
144   /**
145    * @copydoc Dali::Handle::GetPropertyName()
146    */
147   std::string GetPropertyName( Property::Index index ) const;
148
149   /**
150    * @copydoc Dali::Handle::GetPropertyIndex()
151    */
152   Property::Index GetPropertyIndex( const std::string& name ) const;
153
154   /**
155    * @copydoc Dali::Handle::GetPropertyIndex()
156    */
157   Property::Index GetPropertyIndex( Property::Index key ) const;
158
159   /**
160    * @copydoc Dali::Handle::GetPropertyIndex()
161    */
162   Property::Index GetPropertyIndex( Property::Key key ) const;
163
164   /**
165    * @copydoc Dali::Handle::IsPropertyWritable()
166    */
167   bool IsPropertyWritable( Property::Index index ) const;
168
169   /**
170    * @copydoc Dali::Handle::IsPropertyAnimatable()
171    */
172   bool IsPropertyAnimatable( Property::Index index ) const;
173
174   /**
175    * @copydoc Dali::Handle::IsPropertyAConstraintInput()
176    */
177   bool IsPropertyAConstraintInput( Property::Index index ) const;
178
179   /**
180    * @copydoc Dali::Handle::GetPropertyType()
181    */
182   Property::Type GetPropertyType( Property::Index index ) const;
183
184   /**
185    * @copydoc Dali::Handle::SetProperty()
186    */
187   void SetProperty( Property::Index index, const Property::Value& propertyValue );
188
189   /**
190    * @copydoc Dali::Handle::GetProperty()
191    */
192   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   Property::Value GetCurrentProperty( Property::Index index ) const;
200
201   /**
202    * @copydoc Dali::Handle::GetPropertyIndices()
203    */
204   void GetPropertyIndices( Property::IndexContainer& indices ) const;
205
206   /**
207    * @copydoc Dali::Handle::RegisterProperty()
208    */
209   Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue );
210
211   /**
212    * @copydoc Dali::Handle::RegisterProperty()
213    */
214   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   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   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   Dali::PropertyNotification AddPropertyNotification( Property::Index index,
243                                                       int32_t componentIndex,
244                                                       const Dali::PropertyCondition& condition );
245
246   /**
247    * @copydoc Dali::Handle::RemovePropertyNotification()
248    */
249   void RemovePropertyNotification( Dali::PropertyNotification propertyNotification );
250
251   /**
252    * @copydoc Dali::Handle::RemovePropertyNotifications()
253    */
254   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    * Adds uniform mapping for given property
269    * @param propertyIndex index of the property
270    * @param uniformName name of the uniform (same as property name)
271    */
272   void AddUniformMapping( Property::Index propertyIndex, const std::string& uniformName ) const;
273
274   /**
275    * Removes uniform mapping for given property
276    * @param uniformName name of the uniform (same as property name)
277    */
278   void RemoveUniformMapping( const std::string& uniformName ) const;
279
280   /******************************** Constraints ********************************/
281
282   /**
283    * Apply a constraint to an Object.
284    * @param[in] constraint The constraint to apply.
285    */
286   void ApplyConstraint( ConstraintBase& constraint );
287
288   /**
289    * Remove one constraint from an Object.
290    * @param[in] constraint The constraint to remove.
291    */
292   void RemoveConstraint( ConstraintBase& constraint );
293
294   /**
295    * Remove all constraints from a Object.
296    */
297   void RemoveConstraints();
298
299   /**
300    * @copydoc Dali::Handle::RemoveConstraints( uint32_t )
301    */
302   void RemoveConstraints( uint32_t tag );
303
304   /**
305    * Called by TypeInfo to set the type-info that this object-impl is created by.
306    * @param[in] typeInfo The TypeInfo that creates this object-impl.
307    */
308   void SetTypeInfo( const TypeInfo* typeInfo );
309
310   /**
311    * @return the index from which custom properties start
312    */
313   uint32_t CustomPropertyStartIndex()
314   {
315     return PROPERTY_CUSTOM_START_INDEX;
316   }
317
318   /**
319    * Retrieve the scene-graph object added by this object.
320    * @return reference to the scene-graph object, it will always exist
321    */
322   const SceneGraph::PropertyOwner& GetSceneObject() const;
323
324   /********************  Can be overridden by deriving classes ********************/
325
326   /**
327    * Retrieve an animatable property owned by the scene-graph object.
328    * @pre -1 < index < GetPropertyCount().
329    * @param[in] index The index of the property.
330    * @return A dereferenceable pointer to a property, or NULL if a scene-object does not exist with this property.
331    */
332   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
333
334   /**
335    * Retrieve a constraint input-property owned by the scene-graph object.
336    * @pre -1 < index < GetPropertyCount().
337    * @param[in] index The index of the property.
338    * @return A dereferenceable pointer to an input property, or NULL if a scene-object does not exist with this property.
339    */
340   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
341
342   /**
343    * Query whether the property is a component of a scene-graph property.
344    * @pre -1 < index < GetPropertyCount().
345    * @param[in] index The index of the property.
346    * @return The index or Property::INVALID_COMPONENT_INDEX.
347    */
348   virtual int32_t GetPropertyComponentIndex( Property::Index index ) const;
349
350   /**
351    * @copydoc Dali::Handle::PropertySetSignal()
352    */
353   DevelHandle::PropertySetSignalType& PropertySetSignal();
354
355 protected:
356
357   /**
358    * Constructor. Protected so use New to construct an instance of this class
359    *
360    * @param sceneObject the scene graph property owner
361    */
362   Object( const SceneGraph::PropertyOwner* sceneObject );
363
364   /**
365    * A reference counted object may only be deleted by calling Unreference()
366    */
367   virtual ~Object();
368
369   /**
370    * Called immediately by derived classes, after the scene-object has been created & passed to the scene-graph.
371    */
372   void OnSceneObjectAdd();
373
374   /**
375    * Called by derived classes, shortly before send a message to remove the scene-object.
376    */
377   void OnSceneObjectRemove();
378
379   /**
380    * For overriding by derived classes to return the parent of this object.
381    */
382   virtual Object* GetParentObject() const
383   {
384     // By default the Object does not have a parent
385     return nullptr;
386   };
387
388   /**
389    * For use in derived classes.
390    * This is called after a property is set.
391    * @param [in] index The index of the property.
392    * @param [in] propertyValue The value of the property.
393    */
394   virtual void OnPropertySet( Property::Index index, Property::Value propertyValue ) {}
395
396   /**
397    * Retrieves the TypeInfo for this object. Only retrieves it from the type-registry once and then stores a pointer
398    * to it locally there-after. The type info will not change during the life-time of the application.
399    * @return The type-info for this object (Can be NULL)
400    */
401   const TypeInfo* GetTypeInfo() const;
402
403   /**
404    * Helper to find custom property
405    * @param index
406    * @return pointer to the property
407    */
408   CustomPropertyMetadata* FindCustomProperty( Property::Index index ) const;
409
410   /**
411    * Helper to find animatable property
412    * @param index
413    * @return pointer to the property
414    */
415   AnimatablePropertyMetadata* FindAnimatableProperty( Property::Index index ) const;
416
417   /**
418    * Helper to register a scene-graph property
419    * @param [in] name The name of the property.
420    * @param [in] key The key of the property
421    * @param [in] index The index of the property
422    * @param [in] value The value of the property.
423    * @return The index of the registered property or Property::INVALID_INDEX if registration failed.
424    */
425   Property::Index RegisterSceneGraphProperty( const std::string& name, Property::Index key, Property::Index index, const Property::Value& propertyValue ) const;
426
427   /**
428    * Registers animatable scene property
429    * @param typeInfo to check the default value
430    * @param index of the property to register
431    * @param value initial value or nullptr
432    */
433   void RegisterAnimatableProperty( const TypeInfo& typeInfo, Property::Index index, const Property::Value* value ) const;
434
435   /**
436    * Check whether the animatable property is registered already, if not then register on.
437    * @param [in] index The index of the property
438    * @param [in] value optional value for the property
439    * @return pointer to the property metadata
440    */
441   AnimatablePropertyMetadata* GetSceneAnimatableProperty( Property::Index index, const Property::Value* value ) const;
442
443   /**
444    * Resolve the index and name of child properties if any.
445    */
446   void ResolveChildProperties();
447
448 private: // Default property extensions for derived classes
449
450   /**
451    * Set the value of a default property.
452    * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
453    * @param [in] index The index of the property.
454    * @param [in] propertyValue The new value of the property.
455    */
456   virtual void SetDefaultProperty( Property::Index index, const Property::Value& propertyValue );
457
458   /**
459    * Retrieve a default property value.
460    * @param [in] index The index of the property.
461    * @return The property value.
462    */
463   virtual Property::Value GetDefaultProperty( Property::Index index ) const;
464
465   /**
466    * Retrieve the latest scene-graph value of a default property.
467    * @param[in] index The index of the property.
468    * @return The latest scene-graph value of a default property.
469    */
470   virtual Property::Value GetDefaultPropertyCurrentValue( Property::Index index ) const;
471
472   /**
473    * Notifies that a default property is being animated so the deriving class should update the cached value.
474    * @param[in] animation The animation animating the property.
475    * @param[in] index The index of the property.
476    * @param[in] value The value of the property after the animation.
477    * @param[in] animationType Whether the property value given is the target or a relative value.
478    */
479   virtual void OnNotifyDefaultPropertyAnimation( Animation& animation, Property::Index index, const Property::Value& value, Animation::Type propertyChangeType )
480   { }
481
482 private:
483
484   // no default, copy or assignment
485   Object() = delete;
486   Object(const Object& rhs) = delete;
487   Object& operator=(const Object& rhs) = delete;
488
489   /**
490    * Enable property notifications in scene graph
491    */
492   void EnablePropertyNotifications();
493
494   /**
495    * Enable property notifications in scene graph
496    */
497   void DisablePropertyNotifications();
498
499   /**
500    * Get the latest value of the property on the scene-graph.
501    * @param[in] entry An entry from the property lookup container.
502    * @return The latest value of the property.
503    */
504   Property::Value GetCurrentPropertyValue( const PropertyMetadata& entry ) const;
505
506   /**
507    * Set the value of scene graph property.
508    * @param [in] index The index of the property.
509    * @param [in] entry An entry from the property lookup container.
510    * @param [in] value The new value of the property.
511    */
512   virtual void SetSceneGraphProperty( Property::Index index, const PropertyMetadata& entry, const Property::Value& value );
513
514 protected:
515   /**
516    * Get the event thread services object - used for sending messages to the scene graph
517    * Assert if called from the wrong thread.
518    * This is intentionally inline for performance reasons.
519    *
520    * @return The event thread services object
521    */
522   inline EventThreadServices& GetEventThreadServices()
523   {
524     DALI_ASSERT_DEBUG( EventThreadServices::IsCoreRunning() );
525     return mEventThreadServices;
526   }
527
528   /**
529    * Get the event thread services object - used for sending messages to the scene graph
530    * Assert if called from the wrong thread
531    * This is intentionally inline for performance reasons.
532    *
533    * @return The event thread services object
534    */
535   inline const EventThreadServices& GetEventThreadServices() const
536   {
537     DALI_ASSERT_DEBUG( EventThreadServices::IsCoreRunning() );
538     return mEventThreadServices;
539   }
540
541 private:
542
543   EventThreadServices& mEventThreadServices;
544
545 protected:
546
547   // mutable because it's lazy initialised and GetSceneObject has to be const so it can be called from const methods
548   // const to prevent accidentally calling setters directly from event thread
549   // protected to allow fast access from derived classes that have their own scene object (no function call overhead)
550   mutable const SceneGraph::PropertyOwner* mUpdateObject; ///< Reference to object to hold the scene graph properties
551
552 private:
553
554   Dali::Vector<Observer*> mObservers;
555   mutable OwnerContainer<PropertyMetadata*> mCustomProperties; ///< Used for accessing custom Node properties
556   mutable OwnerContainer<PropertyMetadata*> mAnimatableProperties; ///< Used for accessing animatable Node properties
557   mutable const TypeInfo* mTypeInfo; ///< The type-info for this object, mutable so it can be lazy initialized from const method if it is required
558
559   ConstraintContainer* mConstraints;               ///< Container of owned -constraints.
560
561   using PropertyNotificationContainer = std::vector< Dali::PropertyNotification >;
562   PropertyNotificationContainer* mPropertyNotifications; ///< Container of owned property notifications.
563   DevelHandle::PropertySetSignalType mPropertySetSignal;
564
565 };
566
567 } // namespace Internal
568
569 // Helpers for public-api forwarding methods
570
571 inline Internal::Object& GetImplementation(Dali::Handle& object)
572 {
573   DALI_ASSERT_ALWAYS( object && "Object handle is empty" );
574
575   BaseObject& handle = object.GetBaseObject();
576
577   return static_cast<Internal::Object&>(handle);
578 }
579
580 inline const Internal::Object& GetImplementation(const Dali::Handle& object)
581 {
582   DALI_ASSERT_ALWAYS( object && "Object handle is empty" );
583
584   const BaseObject& handle = object.GetBaseObject();
585
586   return static_cast<const Internal::Object&>(handle);
587 }
588
589 } // namespace Dali
590
591 #endif // DALI_INTERNAL_OBJECT_H