1 #ifndef DALI_INTERNAL_OBJECT_H
2 #define DALI_INTERNAL_OBJECT_H
5 * Copyright (c) 2019 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 <cstdint> // uint32_t
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-map.h>
34 #include <dali/public-api/object/property-notification.h>
35 #include <dali/devel-api/common/owner-container.h>
36 #include <dali/devel-api/object/handle-devel.h>
37 #include <dali/internal/event/animation/animation-impl.h>
38 #include <dali/internal/event/common/event-thread-services.h>
39 #include <dali/internal/event/common/property-input-impl.h>
40 #include <dali/internal/event/common/property-metadata.h>
41 #include <dali/internal/update/common/property-base.h>
46 class PropertyNotification;
51 class EventThreadServices;
52 class PropertyCondition;
53 class PropertyInputImpl;
63 using ConstraintContainer = std::vector< Dali::Constraint >;
64 using ConstraintIter = ConstraintContainer::iterator;
65 using ConstraintConstIter = ConstraintContainer::const_iterator;
68 * A base class for objects which optionally provide properties.
69 * The concrete derived class is responsible for implementing the property system methods.
70 * Classes may derive from Dali::BaseObject, until any properties are required.
72 * An object for a property-owning object in the scene-graph.
73 * This provides an interface for observing the addition/removal of scene-objects.
75 * The derived class should either:
76 * a) Create their own scene-graph object and pass it to Object constructor.
77 * b) pass nullptr to Object constructor, in this case Object will create default scene object for property handling
79 class Object : public Dali::BaseObject
83 using Capability = Dali::Handle::Capability;
90 * Called immediately after the object has created & passed ownership of a scene-graph object.
91 * @param[in] object The object object.
93 virtual void SceneObjectAdded(Object& object) = 0;
96 * Called shortly before the object sends a message to remove its scene object.
97 * @param[in] object The object object.
99 virtual void SceneObjectRemoved(Object& object) = 0;
102 * Called shortly before the object itself is destroyed; no further callbacks will be received.
103 * @param[in] object The object object.
105 virtual void ObjectDestroyed(Object& object) = 0;
112 virtual ~Observer(){}
116 * Creates a new object
118 * @return an smart pointer to the object
120 static IntrusivePtr<Object> New();
123 * Add an observer to the object.
124 * @param[in] observer The observer to add.
126 void AddObserver( Observer& observer );
129 * Remove an observer from the object
130 * @pre The observer has already been added.
131 * @param[in] observer The observer to remove.
133 void RemoveObserver( Observer& observer );
136 * @copydoc Dali::Handle::Supports()
138 bool Supports( Capability capability ) const;
141 * @copydoc Dali::Handle::GetPropertyCount()
143 uint32_t GetPropertyCount() const;
146 * @copydoc Dali::Handle::GetPropertyName()
148 std::string GetPropertyName( Property::Index index ) const;
151 * @copydoc Dali::Handle::GetPropertyIndex()
153 Property::Index GetPropertyIndex( const std::string& name ) const;
156 * @copydoc Dali::Handle::GetPropertyIndex()
158 Property::Index GetPropertyIndex( Property::Index key ) const;
161 * @copydoc Dali::Handle::GetPropertyIndex()
163 Property::Index GetPropertyIndex( Property::Key key ) const;
166 * @copydoc Dali::Handle::IsPropertyWritable()
168 bool IsPropertyWritable( Property::Index index ) const;
171 * @copydoc Dali::Handle::IsPropertyAnimatable()
173 bool IsPropertyAnimatable( Property::Index index ) const;
176 * @copydoc Dali::Handle::IsPropertyAConstraintInput()
178 bool IsPropertyAConstraintInput( Property::Index index ) const;
181 * @copydoc Dali::Handle::GetPropertyType()
183 Property::Type GetPropertyType( Property::Index index ) const;
186 * @copydoc Dali::Handle::SetProperty()
188 void SetProperty( Property::Index index, const Property::Value& propertyValue );
191 * @copydoc Dali::Handle::GetProperty()
193 Property::Value GetProperty( Property::Index index ) const;
196 * @brief Retrieves the latest value of the property on the scene-graph.
197 * @param[in] index The index of the property required.
198 * @return The latest value of the property on the scene-graph.
200 Property::Value GetCurrentProperty( Property::Index index ) const;
203 * @copydoc Dali::Handle::GetPropertyIndices()
205 void GetPropertyIndices( Property::IndexContainer& indices ) const;
208 * @copydoc Dali::Handle::RegisterProperty()
210 Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue );
213 * @copydoc Dali::Handle::RegisterProperty()
215 Property::Index RegisterProperty( const std::string& name, Property::Index key, const Property::Value& propertyValue );
218 * @copydoc Dali::DevelHandle::SetProperties()
220 void SetProperties( const Property::Map& properties );
223 * @copydoc Dali::Handle::RegisterProperty(std::string name, Property::Value propertyValue, Property::AccessMode accessMode)
225 Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode );
228 * @brief Implementing method for this override
230 Property::Index RegisterProperty( const std::string& name, Property::Index key, const Property::Value& propertyValue, Property::AccessMode accessMode );
233 * @brief returns true if the custom property exists on this object.
235 * @note The property may be defined for a type within the type registry, but it isn't explicity
236 * defined in every consequent instantiation. It can be automatically added, e.g. parenting an actor
237 * automatically registers it's parent container's child properties.
239 * @param[in] handle The handle of the object to test
240 * @param[in] index The property index to look for.
241 * @return true if the property exists on the object, false otherwise.
243 bool DoesCustomPropertyExist( Property::Index index );
246 * @copydoc Dali::Handle::AddPropertyNotification()
248 Dali::PropertyNotification AddPropertyNotification( Property::Index index,
249 int32_t componentIndex,
250 const Dali::PropertyCondition& condition );
253 * @copydoc Dali::Handle::RemovePropertyNotification()
255 void RemovePropertyNotification( Dali::PropertyNotification propertyNotification );
258 * @copydoc Dali::Handle::RemovePropertyNotifications()
260 void RemovePropertyNotifications();
263 * Notifies that a property is being animated.
264 * @param[in] animation The animation animating the property.
265 * @param[in] index The index of the property.
266 * @param[in] value The value of the property after the animation.
267 * @param[in] animationType Whether the property value given is the target or a relative value.
269 void NotifyPropertyAnimation( Animation& animation, Property::Index index, const Property::Value& value, Animation::Type animationType );
271 /******************************** Uniform Mappings ********************************/
274 * Adds uniform mapping for given property
275 * @param propertyIndex index of the property
276 * @param uniformName name of the uniform (same as property name)
278 void AddUniformMapping( Property::Index propertyIndex, const std::string& uniformName ) const;
281 * Removes uniform mapping for given property
282 * @param uniformName name of the uniform (same as property name)
284 void RemoveUniformMapping( const std::string& uniformName ) const;
286 /******************************** Constraints ********************************/
289 * Apply a constraint to an Object.
290 * @param[in] constraint The constraint to apply.
292 void ApplyConstraint( ConstraintBase& constraint );
295 * Remove one constraint from an Object.
296 * @param[in] constraint The constraint to remove.
298 void RemoveConstraint( ConstraintBase& constraint );
301 * Remove all constraints from a Object.
303 void RemoveConstraints();
306 * @copydoc Dali::Handle::RemoveConstraints( uint32_t )
308 void RemoveConstraints( uint32_t tag );
311 * Called by TypeInfo to set the type-info that this object-impl is created by.
312 * @param[in] typeInfo The TypeInfo that creates this object-impl.
314 void SetTypeInfo( const TypeInfo* typeInfo );
317 * @return the index from which custom properties start
319 uint32_t CustomPropertyStartIndex()
321 return PROPERTY_CUSTOM_START_INDEX;
325 * Retrieve the scene-graph object added by this object.
326 * @return reference to the scene-graph object, it will always exist
328 const SceneGraph::PropertyOwner& GetSceneObject() const;
330 /******************** Can be overridden by deriving classes ********************/
333 * Retrieve an animatable property owned by the scene-graph object.
334 * @pre -1 < index < GetPropertyCount().
335 * @param[in] index The index of the property.
336 * @return A dereferenceable pointer to a property, or NULL if a scene-object does not exist with this property.
338 virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
341 * Retrieve a constraint input-property owned by the scene-graph object.
342 * @pre -1 < index < GetPropertyCount().
343 * @param[in] index The index of the property.
344 * @return A dereferenceable pointer to an input property, or NULL if a scene-object does not exist with this property.
346 virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
349 * Query whether the property is a component of a scene-graph property.
350 * @pre -1 < index < GetPropertyCount().
351 * @param[in] index The index of the property.
352 * @return The index or Property::INVALID_COMPONENT_INDEX.
354 virtual int32_t GetPropertyComponentIndex( Property::Index index ) const;
357 * @copydoc Dali::Handle::PropertySetSignal()
359 DevelHandle::PropertySetSignalType& PropertySetSignal();
364 * Constructor. Protected so use New to construct an instance of this class
366 * @param sceneObject the scene graph property owner
368 Object( const SceneGraph::PropertyOwner* sceneObject );
371 * A reference counted object may only be deleted by calling Unreference()
376 * Called immediately by derived classes, after the scene-object has been created & passed to the scene-graph.
378 void OnSceneObjectAdd();
381 * Called by derived classes, shortly before send a message to remove the scene-object.
383 void OnSceneObjectRemove();
386 * For overriding by derived classes to return the parent of this object.
388 virtual Object* GetParentObject() const
390 // By default the Object does not have a parent
395 * For use in derived classes.
396 * This is called after a property is set.
397 * @param [in] index The index of the property.
398 * @param [in] propertyValue The value of the property.
400 virtual void OnPropertySet( Property::Index index, Property::Value propertyValue ) {}
403 * Retrieves the TypeInfo for this object. Only retrieves it from the type-registry once and then stores a pointer
404 * to it locally there-after. The type info will not change during the life-time of the application.
405 * @return The type-info for this object (Can be NULL)
407 const TypeInfo* GetTypeInfo() const;
410 * Helper to find custom property
412 * @return pointer to the property
414 CustomPropertyMetadata* FindCustomProperty( Property::Index index ) const;
417 * Helper to find animatable property
419 * @return pointer to the property
421 AnimatablePropertyMetadata* FindAnimatableProperty( Property::Index index ) const;
424 * Helper to register a scene-graph property
425 * @param [in] name The name of the property.
426 * @param [in] key The key of the property
427 * @param [in] index The index of the property
428 * @param [in] value The value of the property.
429 * @return The index of the registered property or Property::INVALID_INDEX if registration failed.
431 Property::Index RegisterSceneGraphProperty( const std::string& name, Property::Index key, Property::Index index, const Property::Value& propertyValue ) const;
434 * Registers animatable scene property
435 * @param typeInfo to check the default value
436 * @param index of the property to register
437 * @param value initial value or nullptr
439 void RegisterAnimatableProperty( const TypeInfo& typeInfo, Property::Index index, const Property::Value* value ) const;
442 * Check whether the animatable property is registered already, if not then register on.
443 * @param [in] index The index of the property
444 * @param [in] value optional value for the property
445 * @return pointer to the property metadata
447 AnimatablePropertyMetadata* GetSceneAnimatableProperty( Property::Index index, const Property::Value* value ) const;
450 * Resolve the index and name of child properties if any.
452 void ResolveChildProperties();
454 private: // Default property extensions for derived classes
457 * Set the value of a default property.
458 * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
459 * @param [in] index The index of the property.
460 * @param [in] propertyValue The new value of the property.
462 virtual void SetDefaultProperty( Property::Index index, const Property::Value& propertyValue );
465 * Retrieve a default property value.
466 * @param [in] index The index of the property.
467 * @return The property value.
469 virtual Property::Value GetDefaultProperty( Property::Index index ) const;
472 * Retrieve the latest scene-graph value of a default property.
473 * @param[in] index The index of the property.
474 * @return The latest scene-graph value of a default property.
476 virtual Property::Value GetDefaultPropertyCurrentValue( Property::Index index ) const;
479 * Notifies that a default property is being animated so the deriving class should update the cached value.
480 * @param[in] animation The animation animating the property.
481 * @param[in] index The index of the property.
482 * @param[in] value The value of the property after the animation.
483 * @param[in] animationType Whether the property value given is the target or a relative value.
485 virtual void OnNotifyDefaultPropertyAnimation( Animation& animation, Property::Index index, const Property::Value& value, Animation::Type propertyChangeType )
490 // no default, copy or assignment
492 Object(const Object& rhs) = delete;
493 Object& operator=(const Object& rhs) = delete;
496 * Enable property notifications in scene graph
498 void EnablePropertyNotifications();
501 * Enable property notifications in scene graph
503 void DisablePropertyNotifications();
506 * Get the latest value of the property on the scene-graph.
507 * @param[in] entry An entry from the property lookup container.
508 * @return The latest value of the property.
510 Property::Value GetCurrentPropertyValue( const PropertyMetadata& entry ) const;
513 * Set the value of scene graph property.
514 * @param [in] index The index of the property.
515 * @param [in] entry An entry from the property lookup container.
516 * @param [in] value The new value of the property.
518 virtual void SetSceneGraphProperty( Property::Index index, const PropertyMetadata& entry, const Property::Value& value );
522 * Get the event thread services object - used for sending messages to the scene graph
523 * Assert if called from the wrong thread.
524 * This is intentionally inline for performance reasons.
526 * @return The event thread services object
528 inline EventThreadServices& GetEventThreadServices()
530 DALI_ASSERT_DEBUG( EventThreadServices::IsCoreRunning() );
531 return mEventThreadServices;
535 * Get the event thread services object - used for sending messages to the scene graph
536 * Assert if called from the wrong thread
537 * This is intentionally inline for performance reasons.
539 * @return The event thread services object
541 inline const EventThreadServices& GetEventThreadServices() const
543 DALI_ASSERT_DEBUG( EventThreadServices::IsCoreRunning() );
544 return mEventThreadServices;
549 EventThreadServices& mEventThreadServices;
553 // mutable because it's lazy initialised and GetSceneObject has to be const so it can be called from const methods
554 // const to prevent accidentally calling setters directly from event thread
555 // protected to allow fast access from derived classes that have their own scene object (no function call overhead)
556 mutable const SceneGraph::PropertyOwner* mUpdateObject; ///< Reference to object to hold the scene graph properties
560 Dali::Vector<Observer*> mObservers;
561 mutable OwnerContainer<PropertyMetadata*> mCustomProperties; ///< Used for accessing custom Node properties
562 mutable OwnerContainer<PropertyMetadata*> mAnimatableProperties; ///< Used for accessing animatable Node properties
563 mutable const TypeInfo* mTypeInfo; ///< The type-info for this object, mutable so it can be lazy initialized from const method if it is required
565 ConstraintContainer* mConstraints; ///< Container of owned -constraints.
567 using PropertyNotificationContainer = std::vector< Dali::PropertyNotification >;
568 PropertyNotificationContainer* mPropertyNotifications; ///< Container of owned property notifications.
569 DevelHandle::PropertySetSignalType mPropertySetSignal;
573 } // namespace Internal
575 // Helpers for public-api forwarding methods
577 inline Internal::Object& GetImplementation(Dali::Handle& object)
579 DALI_ASSERT_ALWAYS( object && "Object handle is empty" );
581 BaseObject& handle = object.GetBaseObject();
583 return static_cast<Internal::Object&>(handle);
586 inline const Internal::Object& GetImplementation(const Dali::Handle& object)
588 DALI_ASSERT_ALWAYS( object && "Object handle is empty" );
590 const BaseObject& handle = object.GetBaseObject();
592 return static_cast<const Internal::Object&>(handle);
597 #endif // DALI_INTERNAL_OBJECT_H