1 #ifndef DALI_INTERNAL_OBJECT_H
2 #define DALI_INTERNAL_OBJECT_H
5 * Copyright (c) 2021 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/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>
46 class PropertyNotification;
51 class EventThreadServices;
52 class PropertyCondition;
53 class PropertyInputImpl;
61 } // namespace SceneGraph
63 using ConstraintContainer = std::vector<Dali::Constraint>;
64 using ConstraintIter = ConstraintContainer::iterator;
65 using ConstraintConstIter = ConstraintContainer::const_iterator;
70 KeyRef(const Property::Key& key)
73 if(mType == Property::Key::STRING)
75 mString = ConstString(key.stringKey);
79 mIndex = key.indexKey;
82 KeyRef(ConstString str)
83 : mType(Property::Key::STRING)
87 KeyRef(Property::Index index)
88 : mType(Property::Key::INDEX)
92 Property::Key::Type mType;
93 Property::Index mIndex{Property::INVALID_INDEX};
98 * A base class for objects which optionally provide properties.
99 * The concrete derived class is responsible for implementing the property system methods.
100 * Classes may derive from Dali::BaseObject, until any properties are required.
102 * An object for a property-owning object in the scene-graph.
103 * This provides an interface for observing the addition/removal of scene-objects.
105 * The derived class should either:
106 * a) Create their own scene-graph object and pass it to Object constructor.
107 * b) pass nullptr to Object constructor, in this case Object will create default scene object for property handling
109 class Object : public Dali::BaseObject
112 using Capability = Dali::Handle::Capability;
118 * Called immediately after the object has created & passed ownership of a scene-graph object.
119 * @param[in] object The object object.
121 virtual void SceneObjectAdded(Object& object) = 0;
124 * Called shortly before the object sends a message to remove its scene object.
125 * @param[in] object The object object.
127 virtual void SceneObjectRemoved(Object& object) = 0;
130 * Called shortly before the object itself is destroyed; no further callbacks will be received.
131 * @param[in] object The object object.
133 virtual void ObjectDestroyed(Object& object) = 0;
139 virtual ~Observer() = default;
143 * Creates a new object
145 * @return an smart pointer to the object
147 static IntrusivePtr<Object> New();
150 * Add an observer to the object.
151 * @param[in] observer The observer to add.
153 void AddObserver(Observer& observer);
156 * Remove an observer from the object
157 * @pre The observer has already been added.
158 * @param[in] observer The observer to remove.
160 void RemoveObserver(Observer& observer);
163 * @copydoc Dali::Handle::Supports()
165 bool Supports(Capability capability) const;
168 * @copydoc Dali::Handle::GetPropertyCount()
170 uint32_t GetPropertyCount() const;
173 * @copydoc Dali::Handle::GetPropertyName()
175 std::string_view GetPropertyName(Property::Index index) const;
178 * @copydoc Dali::Handle::GetPropertyIndex()
180 Property::Index GetPropertyIndex(KeyRef key) const;
183 * @copydoc Dali::Handle::IsPropertyWritable()
185 bool IsPropertyWritable(Property::Index index) const;
188 * @copydoc Dali::Handle::IsPropertyAnimatable()
190 bool IsPropertyAnimatable(Property::Index index) const;
193 * @copydoc Dali::Handle::IsPropertyAConstraintInput()
195 bool IsPropertyAConstraintInput(Property::Index index) const;
198 * @copydoc Dali::Handle::GetPropertyType()
200 Property::Type GetPropertyType(Property::Index index) const;
203 * @copydoc Dali::Handle::SetProperty()
205 void SetProperty(Property::Index index, Property::Value propertyValue);
208 * @copydoc Dali::Handle::GetProperty()
210 Property::Value GetProperty(Property::Index index) const;
213 * @brief Retrieves the latest value of the property on the scene-graph.
214 * @param[in] index The index of the property required.
215 * @return The latest value of the property on the scene-graph.
217 Property::Value GetCurrentProperty(Property::Index index) const;
220 * @copydoc Dali::Handle::GetPropertyIndices()
222 void GetPropertyIndices(Property::IndexContainer& indices) const;
225 * @copydoc Dali::Handle::RegisterProperty()
227 Property::Index RegisterProperty(std::string_view name, Property::Value propertyValue);
230 * @copydoc Dali::Handle::RegisterProperty()
232 Property::Index RegisterProperty(std::string_view name, Property::Index key, Property::Value propertyValue);
235 * @copydoc Dali::DevelHandle::SetProperties()
237 void SetProperties(const Property::Map& properties);
240 * @copydoc Dali::DevelHandle::GetProperties()
242 void GetProperties(Property::Map& properties);
245 * @copydoc Dali::Handle::RegisterProperty(std::string name, Property::Value propertyValue, Property::AccessMode accessMode)
247 Property::Index RegisterProperty(std::string_view name, Property::Value propertyValue, Property::AccessMode accessMode);
250 * @brief Implementing method for this override
252 Property::Index RegisterProperty(std::string_view name,
254 Property::Value propertyValue,
255 Property::AccessMode accessMode);
258 * @brief returns true if the custom property exists on this object.
260 * @note The property may be defined for a type within the type registry, but it isn't explicity
261 * defined in every consequent instantiation. It can be automatically added, e.g. parenting an actor
262 * automatically registers it's parent container's child properties.
264 * @param[in] handle The handle of the object to test
265 * @param[in] index The property index to look for.
266 * @return true if the property exists on the object, false otherwise.
268 bool DoesCustomPropertyExist(Property::Index index);
271 * @copydoc Dali::Handle::AddPropertyNotification()
273 Dali::PropertyNotification AddPropertyNotification(Property::Index index,
274 int32_t componentIndex,
275 const Dali::PropertyCondition& condition);
278 * @copydoc Dali::Handle::RemovePropertyNotification()
280 void RemovePropertyNotification(Dali::PropertyNotification propertyNotification);
283 * @copydoc Dali::Handle::RemovePropertyNotifications()
285 void RemovePropertyNotifications();
288 * Notifies that a property is being animated.
289 * @param[in] animation The animation animating the property.
290 * @param[in] index The index of the property.
291 * @param[in] value The value of the property after the animation.
292 * @param[in] animationType Whether the property value given is the target or a relative value.
294 void NotifyPropertyAnimation(Animation& animation, Property::Index index, const Property::Value& value, Animation::Type animationType);
296 /******************************** Uniform Mappings ********************************/
299 * Adds uniform mapping for given property
300 * @param propertyIndex index of the property
301 * @param uniformName name of the uniform (same as property name)
303 void AddUniformMapping(Property::Index propertyIndex, ConstString uniformName) const;
306 * Removes uniform mapping for given property
307 * @param uniformName name of the uniform (same as property name)
309 void RemoveUniformMapping(const std::string& uniformName) const;
311 /******************************** Constraints ********************************/
314 * Apply a constraint to an Object.
315 * @param[in] constraint The constraint to apply.
317 void ApplyConstraint(ConstraintBase& constraint);
320 * Remove one constraint from an Object.
321 * @param[in] constraint The constraint to remove.
323 void RemoveConstraint(ConstraintBase& constraint);
326 * Remove all constraints from a Object.
328 void RemoveConstraints();
331 * @copydoc Dali::Handle::RemoveConstraints( uint32_t )
333 void RemoveConstraints(uint32_t tag);
336 * Called by TypeInfo to set the type-info that this object-impl is created by.
337 * @param[in] typeInfo The TypeInfo that creates this object-impl.
339 void SetTypeInfo(const TypeInfo* typeInfo);
342 * @return the index from which custom properties start
344 uint32_t CustomPropertyStartIndex()
346 return PROPERTY_CUSTOM_START_INDEX;
350 * Retrieve the scene-graph object added by this object.
351 * @return reference to the scene-graph object, it will always exist
353 const SceneGraph::PropertyOwner& GetSceneObject() const;
355 /******************** Can be overridden by deriving classes ********************/
358 * Retrieve an animatable property owned by the scene-graph object.
359 * @pre -1 < index < GetPropertyCount().
360 * @param[in] index The index of the property.
361 * @return A dereferenceable pointer to a property, or NULL if a scene-object does not exist with this property.
363 virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty(Property::Index index) const;
366 * Retrieve a constraint input-property owned by the scene-graph object.
367 * @pre -1 < index < GetPropertyCount().
368 * @param[in] index The index of the property.
369 * @return A dereferenceable pointer to an input property, or NULL if a scene-object does not exist with this property.
371 virtual const PropertyInputImpl* GetSceneObjectInputProperty(Property::Index index) const;
374 * Query whether the property is a component of a scene-graph property.
375 * @pre -1 < index < GetPropertyCount().
376 * @param[in] index The index of the property.
377 * @return The index or Property::INVALID_COMPONENT_INDEX.
379 virtual int32_t GetPropertyComponentIndex(Property::Index index) const;
382 * Query whether playing an animation is possible or not.
383 * @return true if playing an animation is possible.
385 virtual bool IsAnimationPossible() const
391 * @copydoc Dali::Handle::PropertySetSignal()
393 Handle::PropertySetSignalType& PropertySetSignal();
397 * Constructor. Protected so use New to construct an instance of this class
399 * @param sceneObject the scene graph property owner
401 Object(const SceneGraph::PropertyOwner* sceneObject);
404 * A reference counted object may only be deleted by calling Unreference()
409 * Called immediately by derived classes, after the scene-object has been created & passed to the scene-graph.
411 void OnSceneObjectAdd();
414 * Called by derived classes, shortly before send a message to remove the scene-object.
416 void OnSceneObjectRemove();
419 * For overriding by derived classes to return the parent of this object.
421 virtual Object* GetParentObject() const
423 // By default the Object does not have a parent
428 * For use in derived classes.
429 * This is called after a property is set.
430 * @param [in] index The index of the property.
431 * @param [in] propertyValue The value of the property.
433 virtual void OnPropertySet(Property::Index index, const Property::Value& propertyValue)
438 * Retrieves the TypeInfo for this object. Only retrieves it from the type-registry once and then stores a pointer
439 * to it locally there-after. The type info will not change during the life-time of the application.
440 * @return The type-info for this object (Can be NULL)
442 const TypeInfo* GetTypeInfo() const;
445 * Helper to find custom property
447 * @return pointer to the property
449 CustomPropertyMetadata* FindCustomProperty(Property::Index index) const;
452 * Helper to find animatable property
454 * @return pointer to the property
456 AnimatablePropertyMetadata* FindAnimatableProperty(Property::Index index) const;
459 * Helper to register a scene-graph property
460 * @param [in] name The name of the property.
461 * @param [in] key The key of the property
462 * @param [in] index The index of the property
463 * @param [in] value The value of the property.
464 * @return The index of the registered property or Property::INVALID_INDEX if registration failed.
466 Property::Index RegisterSceneGraphProperty(ConstString name, Property::Index key, Property::Index index, Property::Value propertyValue) const;
469 * Registers animatable scene property
470 * @param typeInfo to check the default value
471 * @param index of the property to register
472 * @param value initial value or nullptr
474 void RegisterAnimatableProperty(const TypeInfo& typeInfo, Property::Index index, const Property::Value* value) const;
477 * Check whether the animatable property is registered already, if not then register on.
478 * @param [in] index The index of the property
479 * @param [in] value optional value for the property
480 * @return pointer to the property metadata
482 AnimatablePropertyMetadata* GetSceneAnimatableProperty(Property::Index index, const Property::Value* value) const;
485 * Resolve the index and name of child properties if any.
487 void ResolveChildProperties();
489 private: // Default property extensions for derived classes
491 * Set the value of a default property.
492 * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
493 * @param [in] index The index of the property.
494 * @param [in] propertyValue The new value of the property.
496 virtual void SetDefaultProperty(Property::Index index, const Property::Value& propertyValue);
499 * Retrieve a default property value.
500 * @param [in] index The index of the property.
501 * @return The property value.
503 virtual Property::Value GetDefaultProperty(Property::Index index) const;
506 * Retrieve the latest scene-graph value of a default property.
507 * @param[in] index The index of the property.
508 * @return The latest scene-graph value of a default property.
510 virtual Property::Value GetDefaultPropertyCurrentValue(Property::Index index) const;
513 * Notifies that a default property is being animated so the deriving class should update the cached value.
514 * @param[in] animation The animation animating the property.
515 * @param[in] index The index of the property.
516 * @param[in] value The value of the property after the animation.
517 * @param[in] animationType Whether the property value given is the target or a relative value.
519 virtual void OnNotifyDefaultPropertyAnimation(Animation& animation, Property::Index index, const Property::Value& value, Animation::Type propertyChangeType)
524 // no default, copy or assignment
526 Object(const Object& rhs) = delete;
527 Object& operator=(const Object& rhs) = delete;
530 * Enable property notifications in scene graph
532 void EnablePropertyNotifications();
535 * Enable property notifications in scene graph
537 void DisablePropertyNotifications();
540 * Get the latest value of the property on the scene-graph.
541 * @param[in] entry An entry from the property lookup container.
542 * @return The latest value of the property.
544 Property::Value GetCurrentPropertyValue(const PropertyMetadata& entry) const;
547 * Set the value of scene graph property.
548 * @param [in] index The index of the property.
549 * @param [in] entry An entry from the property lookup container.
550 * @param [in] value The new value of the property.
552 virtual void SetSceneGraphProperty(Property::Index index, const PropertyMetadata& entry, const Property::Value& value);
556 * Get the event thread services object - used for sending messages to the scene graph
557 * Assert if called from the wrong thread.
558 * This is intentionally inline for performance reasons.
560 * @return The event thread services object
562 inline EventThreadServices& GetEventThreadServices()
564 DALI_ASSERT_ALWAYS(EventThreadServices::IsCoreRunning());
565 return mEventThreadServices;
569 * Get the event thread services object - used for sending messages to the scene graph
570 * Assert if called from the wrong thread
571 * This is intentionally inline for performance reasons.
573 * @return The event thread services object
575 inline const EventThreadServices& GetEventThreadServices() const
577 DALI_ASSERT_ALWAYS(EventThreadServices::IsCoreRunning());
578 return mEventThreadServices;
582 EventThreadServices& mEventThreadServices;
585 // mutable because it's lazy initialised and GetSceneObject has to be const so it can be called from const methods
586 // const to prevent accidentally calling setters directly from event thread
587 // protected to allow fast access from derived classes that have their own scene object (no function call overhead)
588 mutable const SceneGraph::PropertyOwner* mUpdateObject; ///< Reference to object to hold the scene graph properties
591 Dali::Vector<Observer*> mObservers;
592 mutable OwnerContainer<PropertyMetadata*> mCustomProperties; ///< Used for accessing custom Node properties
593 mutable OwnerContainer<PropertyMetadata*> mAnimatableProperties; ///< Used for accessing animatable Node properties
594 mutable const TypeInfo* mTypeInfo; ///< The type-info for this object, mutable so it can be lazy initialized from const method if it is required
596 ConstraintContainer* mConstraints; ///< Container of owned -constraints.
598 using PropertyNotificationContainer = std::vector<Dali::PropertyNotification>;
599 PropertyNotificationContainer* mPropertyNotifications; ///< Container of owned property notifications.
601 Handle::PropertySetSignalType mPropertySetSignal;
604 } // namespace Internal
606 // Helpers for public-api forwarding methods
608 inline Internal::Object& GetImplementation(Dali::Handle& object)
610 DALI_ASSERT_ALWAYS(object && "Object handle is empty");
612 BaseObject& handle = object.GetBaseObject();
614 return static_cast<Internal::Object&>(handle);
617 inline const Internal::Object& GetImplementation(const Dali::Handle& object)
619 DALI_ASSERT_ALWAYS(object && "Object handle is empty");
621 const BaseObject& handle = object.GetBaseObject();
623 return static_cast<const Internal::Object&>(handle);
628 #endif // DALI_INTERNAL_OBJECT_H