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