1 #ifndef DALI_INTERNAL_OBJECT_H
2 #define DALI_INTERNAL_OBJECT_H
5 * Copyright (c) 2024 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/integration-api/ordered-set.h>
28 #include <dali/internal/common/const-string.h>
29 #include <dali/internal/event/animation/animation-impl.h>
30 #include <dali/internal/event/common/event-thread-services.h>
31 #include <dali/internal/event/common/property-input-impl.h>
32 #include <dali/internal/event/common/property-metadata.h>
33 #include <dali/internal/update/common/property-base.h>
34 #include <dali/public-api/animation/constraint.h>
35 #include <dali/public-api/common/dali-vector.h>
36 #include <dali/public-api/common/vector-wrapper.h>
37 #include <dali/public-api/object/base-object.h>
38 #include <dali/public-api/object/handle.h>
39 #include <dali/public-api/object/property-index-ranges.h>
40 #include <dali/public-api/object/property-input.h>
41 #include <dali/public-api/object/property-map.h>
42 #include <dali/public-api/object/property-notification.h>
43 #include <dali/public-api/object/property.h>
47 class PropertyNotification;
52 class EventThreadServices;
53 class PropertyCondition;
54 class PropertyInputImpl;
62 } // namespace SceneGraph
64 using ConstraintContainer = std::vector<Dali::Constraint>;
65 using ConstraintIter = ConstraintContainer::iterator;
66 using ConstraintConstIter = ConstraintContainer::const_iterator;
71 KeyRef(const Property::Key& key)
74 if(mType == Property::Key::STRING)
76 mString = ConstString(key.stringKey);
80 mIndex = key.indexKey;
83 KeyRef(ConstString str)
84 : mType(Property::Key::STRING)
88 KeyRef(Property::Index index)
89 : mType(Property::Key::INDEX)
93 Property::Key::Type mType;
94 Property::Index mIndex{Property::INVALID_INDEX};
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.
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.
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
110 class Object : public Dali::BaseObject
113 using Capability = Dali::Handle::Capability;
116 using ObserverContainer = Integration::OrderedSet<Observer, false>;
122 * Called immediately after the object has created & passed ownership of a scene-graph object.
123 * @param[in] object The object object.
125 virtual void SceneObjectAdded(Object& object) = 0;
128 * Called shortly before the object sends a message to remove its scene object.
129 * @param[in] object The object object.
131 virtual void SceneObjectRemoved(Object& object) = 0;
134 * Called shortly before the object itself is destroyed; no further callbacks will be received.
135 * @param[in] object The object object.
137 virtual void ObjectDestroyed(Object& object) = 0;
143 virtual ~Observer() = default;
147 * Creates a new object
149 * @return an smart pointer to the object
151 static IntrusivePtr<Object> New();
154 * Add an observer to the object.
155 * @param[in] observer The observer to add.
157 void AddObserver(Observer& observer);
160 * Remove an observer from the object
161 * @pre The observer has already been added.
162 * @param[in] observer The observer to remove.
164 void RemoveObserver(Observer& observer);
167 * @copydoc Dali::Handle::Supports()
169 bool Supports(Capability capability) const;
172 * @copydoc Dali::Handle::GetPropertyCount()
174 uint32_t GetPropertyCount() const;
177 * @copydoc Dali::Handle::GetPropertyName()
179 std::string_view GetPropertyName(Property::Index index) const;
182 * @copydoc Dali::Handle::GetPropertyIndex()
184 Property::Index GetPropertyIndex(KeyRef key) const;
187 * @copydoc Dali::Handle::IsPropertyWritable()
189 bool IsPropertyWritable(Property::Index index) const;
192 * @copydoc Dali::Handle::IsPropertyAnimatable()
194 bool IsPropertyAnimatable(Property::Index index) const;
197 * @copydoc Dali::Handle::IsPropertyAConstraintInput()
199 bool IsPropertyAConstraintInput(Property::Index index) const;
202 * @copydoc Dali::Handle::GetPropertyType()
204 Property::Type GetPropertyType(Property::Index index) const;
207 * @copydoc Dali::Handle::SetProperty()
209 void SetProperty(Property::Index index, Property::Value propertyValue);
212 * @copydoc Dali::Handle::GetProperty()
214 Property::Value GetProperty(Property::Index index) const;
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.
221 Property::Value GetCurrentProperty(Property::Index index) const;
224 * @copydoc Dali::Handle::GetPropertyIndices()
226 void GetPropertyIndices(Property::IndexContainer& indices) const;
229 * @copydoc Dali::DevelHandle::SetProperties()
231 void SetProperties(const Property::Map& properties);
234 * @copydoc Dali::DevelHandle::GetProperties()
236 void GetProperties(Property::Map& properties);
239 * @copydoc Dali::Handle::ReserveCustomProperties()
241 void ReserveCustomProperties(int propertyCount);
244 * @copydoc Dali::Handle::RegisterProperty()
246 Property::Index RegisterProperty(std::string_view name, Property::Value propertyValue);
249 * @copydoc Dali::Handle::RegisterProperty()
251 Property::Index RegisterProperty(std::string_view name, Property::Index key, Property::Value propertyValue, bool checkForUniqueName);
254 * @copydoc Dali::Handle::RegisterProperty(std::string name, Property::Value propertyValue, Property::AccessMode accessMode)
256 Property::Index RegisterProperty(std::string_view name, Property::Value propertyValue, Property::AccessMode accessMode);
259 * @brief Implementing method for this override
261 Property::Index RegisterProperty(std::string_view name,
263 Property::Value propertyValue,
264 Property::AccessMode accessMode,
265 bool checkForUniqueName);
268 * @brief returns true if the custom property exists on this object.
270 * @note The property may be defined for a type within the type registry, but it isn't explicity
271 * defined in every consequent instantiation. It can be automatically added, e.g. parenting an actor
272 * automatically registers it's parent container's child properties.
274 * @param[in] handle The handle of the object to test
275 * @param[in] index The property index to look for.
276 * @return true if the property exists on the object, false otherwise.
278 bool DoesCustomPropertyExist(Property::Index index);
281 * @copydoc Dali::Handle::AddPropertyNotification()
283 Dali::PropertyNotification AddPropertyNotification(Property::Index index,
284 int32_t componentIndex,
285 const Dali::PropertyCondition& condition);
288 * @copydoc Dali::Handle::RemovePropertyNotification()
290 void RemovePropertyNotification(Dali::PropertyNotification propertyNotification);
293 * @copydoc Dali::Handle::RemovePropertyNotifications()
295 void RemovePropertyNotifications();
298 * Notifies that a property is being animated.
299 * @param[in] animation The animation animating the property.
300 * @param[in] index The index of the property.
301 * @param[in] value The value of the property after the animation.
302 * @param[in] animationType Whether the property value given is the target or a relative value.
304 void NotifyPropertyAnimation(Animation& animation, Property::Index index, const Property::Value& value, Animation::Type animationType);
306 /******************************** Uniform Mappings ********************************/
309 * Adds uniform mapping for given property
310 * @param propertyIndex index of the property
311 * @param uniformName name of the uniform (same as property name)
313 void AddUniformMapping(Property::Index propertyIndex, ConstString uniformName) const;
316 * Removes uniform mapping for given property
317 * @param uniformName name of the uniform (same as property name)
319 void RemoveUniformMapping(const std::string& uniformName) const;
321 /******************************** Constraints ********************************/
324 * Apply a constraint to an Object.
325 * @param[in] constraint The constraint to apply.
327 void ApplyConstraint(ConstraintBase& constraint);
330 * Remove one constraint from an Object.
331 * @param[in] constraint The constraint to remove.
333 void RemoveConstraint(ConstraintBase& constraint);
336 * Remove all constraints from a Object.
338 void RemoveConstraints();
341 * @copydoc Dali::Handle::RemoveConstraints( uint32_t )
343 void RemoveConstraints(uint32_t tag);
346 * Called by TypeInfo to set the type-info that this object-impl is created by.
347 * @param[in] typeInfo The TypeInfo that creates this object-impl.
349 void SetTypeInfo(const TypeInfo* typeInfo);
352 * @return the index from which custom properties start
354 uint32_t CustomPropertyStartIndex()
356 return PROPERTY_CUSTOM_START_INDEX;
360 * Retrieve the scene-graph object added by this object.
361 * @return reference to the scene-graph object, it will always exist
363 const SceneGraph::PropertyOwner& GetSceneObject() const;
365 /******************** Can be overridden by deriving classes ********************/
368 * Retrieve an animatable property owned by the scene-graph object.
369 * @pre -1 < index < GetPropertyCount().
370 * @param[in] index The index of the property.
371 * @return A dereferenceable pointer to a property, or NULL if a scene-object does not exist with this property.
373 virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty(Property::Index index) const;
376 * Retrieve a constraint input-property owned by the scene-graph object.
377 * @pre -1 < index < GetPropertyCount().
378 * @param[in] index The index of the property.
379 * @return A dereferenceable pointer to an input property, or NULL if a scene-object does not exist with this property.
381 virtual const PropertyInputImpl* GetSceneObjectInputProperty(Property::Index index) const;
384 * Query whether the property is a component of a scene-graph property.
385 * @pre -1 < index < GetPropertyCount().
386 * @param[in] index The index of the property.
387 * @return The index or Property::INVALID_COMPONENT_INDEX.
389 virtual int32_t GetPropertyComponentIndex(Property::Index index) const;
392 * Query whether playing an animation is possible or not.
393 * @return true if playing an animation is possible.
395 virtual bool IsAnimationPossible() const
401 * @copydoc Dali::Handle::PropertySetSignal()
403 Handle::PropertySetSignalType& PropertySetSignal();
407 * Constructor. Protected so use New to construct an instance of this class
409 * @param sceneObject the scene graph property owner
411 Object(const SceneGraph::PropertyOwner* sceneObject);
414 * A reference counted object may only be deleted by calling Unreference()
419 * Called immediately by derived classes, after the scene-object has been created & passed to the scene-graph.
421 void OnSceneObjectAdd();
424 * Called by derived classes, shortly before send a message to remove the scene-object.
426 void OnSceneObjectRemove();
429 * For overriding by derived classes to return the parent of this object.
431 virtual Object* GetParentObject() const
433 // By default the Object does not have a parent
438 * For use in derived classes.
439 * This is called after a property is set.
440 * @param [in] index The index of the property.
441 * @param [in] propertyValue The value of the property.
443 virtual void OnPropertySet(Property::Index index, const Property::Value& propertyValue)
448 * Retrieves the TypeInfo for this object. Only retrieves it from the type-registry once and then stores a pointer
449 * to it locally there-after. The type info will not change during the life-time of the application.
450 * @return The type-info for this object (Can be NULL)
452 const TypeInfo* GetTypeInfo() const;
455 * Helper to find custom property
457 * @return pointer to the property
459 CustomPropertyMetadata* FindCustomProperty(Property::Index index) const;
462 * Helper to find animatable property
464 * @return pointer to the property
466 AnimatablePropertyMetadata* FindAnimatableProperty(Property::Index index) const;
469 * Helper to register a scene-graph property
470 * @param [in] name The name of the property.
471 * @param [in] key The key of the property
472 * @param [in] index The index of the property
473 * @param [in] value The value of the property.
474 * @return The index of the registered property or Property::INVALID_INDEX if registration failed.
476 Property::Index RegisterSceneGraphProperty(ConstString name, Property::Index key, Property::Index index, Property::Value propertyValue) const;
479 * Registers animatable scene property
480 * @param typeInfo to check the default value
481 * @param index of the property to register
482 * @param value initial value or nullptr
484 void RegisterAnimatableProperty(const TypeInfo& typeInfo, Property::Index index, const Property::Value* value) const;
487 * Check whether the animatable property is registered already, if not then register on.
488 * @param [in] index The index of the property
489 * @param [in] value optional value for the property
490 * @return pointer to the property metadata
492 AnimatablePropertyMetadata* GetSceneAnimatableProperty(Property::Index index, const Property::Value* value) const;
495 * Resolve the index and name of child properties if any.
497 void ResolveChildProperties();
499 private: // Default property extensions for derived classes
501 * Set the value of a default property.
502 * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
503 * @param [in] index The index of the property.
504 * @param [in] propertyValue The new value of the property.
506 virtual void SetDefaultProperty(Property::Index index, const Property::Value& propertyValue);
509 * Retrieve a default property value.
510 * @param [in] index The index of the property.
511 * @return The property value.
513 virtual Property::Value GetDefaultProperty(Property::Index index) const;
516 * Retrieve the latest scene-graph value of a default property.
517 * @param[in] index The index of the property.
518 * @return The latest scene-graph value of a default property.
520 virtual Property::Value GetDefaultPropertyCurrentValue(Property::Index index) const;
523 * Notifies that a default property is being animated so the deriving class should update the cached value.
524 * @param[in] animation The animation animating the property.
525 * @param[in] index The index of the property.
526 * @param[in] value The value of the property after the animation.
527 * @param[in] animationType Whether the property value given is the target or a relative value.
529 virtual void OnNotifyDefaultPropertyAnimation(Animation& animation, Property::Index index, const Property::Value& value, Animation::Type propertyChangeType)
534 // no default, copy or assignment
536 Object(const Object& rhs) = delete;
537 Object& operator=(const Object& rhs) = delete;
540 * Enable property notifications in scene graph
542 void EnablePropertyNotifications();
545 * Enable property notifications in scene graph
547 void DisablePropertyNotifications();
550 * Get the latest value of the property on the scene-graph.
551 * @param[in] entry An entry from the property lookup container.
552 * @return The latest value of the property.
554 Property::Value GetCurrentPropertyValue(const PropertyMetadata& entry) const;
557 * Set the value of scene graph property.
558 * @param [in] index The index of the property.
559 * @param [in] entry An entry from the property lookup container.
560 * @param [in] value The new value of the property.
562 virtual void SetSceneGraphProperty(Property::Index index, const PropertyMetadata& entry, const Property::Value& value);
566 * Get the event thread services object - used for sending messages to the scene graph
567 * Assert if called from the wrong thread.
568 * This is intentionally inline for performance reasons.
570 * @return The event thread services object
572 inline EventThreadServices& GetEventThreadServices()
574 DALI_ASSERT_ALWAYS(EventThreadServices::IsCoreRunning());
575 return mEventThreadServices;
579 * Get the event thread services object - used for sending messages to the scene graph
580 * Assert if called from the wrong thread
581 * This is intentionally inline for performance reasons.
583 * @return The event thread services object
585 inline const EventThreadServices& GetEventThreadServices() const
587 DALI_ASSERT_ALWAYS(EventThreadServices::IsCoreRunning());
588 return mEventThreadServices;
592 EventThreadServices& mEventThreadServices;
595 // mutable because it's lazy initialised and GetSceneObject has to be const so it can be called from const methods
596 // const to prevent accidentally calling setters directly from event thread
597 // protected to allow fast access from derived classes that have their own scene object (no function call overhead)
598 mutable const SceneGraph::PropertyOwner* mUpdateObject; ///< Reference to object to hold the scene graph properties
601 ObserverContainer mObservers;
602 mutable OwnerContainer<PropertyMetadata*> mCustomProperties; ///< Used for accessing custom Node properties
603 mutable OwnerContainer<PropertyMetadata*> mAnimatableProperties; ///< Used for accessing animatable Node properties
604 mutable const TypeInfo* mTypeInfo; ///< The type-info for this object, mutable so it can be lazy initialized from const method if it is required
606 ConstraintContainer* mConstraints; ///< Container of owned -constraints.
608 using PropertyNotificationContainer = std::vector<Dali::PropertyNotification>;
609 PropertyNotificationContainer* mPropertyNotifications; ///< Container of owned property notifications.
611 Handle::PropertySetSignalType mPropertySetSignal;
613 public: /// To be used at observer container changes only.
614 bool mObserverNotifying : 1; ///< Whether we are currently notifying observers.
615 bool mObserverRemoved : 1; ///< Whether we have removed an observer during notify.
618 } // namespace Internal
620 // Helpers for public-api forwarding methods
622 inline Internal::Object& GetImplementation(Dali::Handle& object)
624 DALI_ASSERT_ALWAYS(object && "Object handle is empty");
626 BaseObject& handle = object.GetBaseObject();
628 return static_cast<Internal::Object&>(handle);
631 inline const Internal::Object& GetImplementation(const Dali::Handle& object)
633 DALI_ASSERT_ALWAYS(object && "Object handle is empty");
635 const BaseObject& handle = object.GetBaseObject();
637 return static_cast<const Internal::Object&>(handle);
642 #endif // DALI_INTERNAL_OBJECT_H