5 * Copyright (c) 2020 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
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/object/base-handle.h>
28 #include <dali/public-api/object/indirect-value.h>
29 #include <dali/public-api/object/property-key.h>
30 #include <dali/public-api/object/property-notification-declarations.h>
31 #include <dali/public-api/object/property-types.h>
32 #include <dali/public-api/object/property-value.h>
33 #include <dali/public-api/object/ref-object.h>
34 #include <dali/public-api/signals/dali-signal.h>
39 * @addtogroup dali_core_object
44 class PropertyNotification;
45 class PropertyCondition;
47 namespace Internal DALI_INTERNAL
53 * @brief Dali::Handle is a handle to an internal property owning Dali object that can have constraints applied to it.
56 class DALI_CORE_API Handle : public BaseHandle
60 * @brief Enumeration for Handle's capabilities that can be queried using Handle::Supports().
66 * @brief Some objects support dynamic property creation at run-time.
68 * New properties are registered by calling RegisterProperty() with an unused property name.
71 DYNAMIC_PROPERTIES = 0x01,
75 * @brief PropertySetSignal function prototype for signal handler. Called when a property is set on this object.
77 using PropertySetSignalType = Signal<void(Handle& handle, Property::Index index, const Property::Value& value)>;
81 * @brief This constructor is used by Dali New() methods.
84 * @param[in] handle A pointer to a newly allocated Dali resource
86 Handle(Dali::Internal::Object* handle);
89 * @brief This constructor provides an uninitialized Dali::Handle.
91 * This should be initialized with a Dali New() method before use.
92 * Methods called on an uninitialized Dali::Handle will assert.
94 * Handle handle; // uninitialized
95 * handle.SomeMethod(); // unsafe! This will assert
97 * handle = SomeClass::New(); // now initialized
98 * handle.SomeMethod(); // safe
105 * @brief Creates a new object.
108 * @return A handle to a newly allocated object
113 * @brief Template to create a derived handle and set properties on it.
115 * Marked as DALI_NO_EXPORT_API to prevent internal usage exporting symbols.
117 * @tparam T The derived class to create
118 * @param[in] properties The properties to set
120 template<typename Type>
121 static DALI_NO_EXPORT_API Type New(const Property::Map& properties)
123 Type handle = Type::New();
124 handle.SetProperties(properties);
129 * @brief Dali::Handle is intended as a base class.
131 * This is non-virtual since derived Handle types must not contain data or virtual methods.
137 * @brief This copy constructor is required for (smart) pointer semantics.
140 * @param[in] handle A reference to the copied handle
142 Handle(const Handle& handle);
145 * @brief This assignment operator is required for (smart) pointer semantics.
148 * @param[in] rhs A reference to the copied handle
149 * @return A reference to this
151 Handle& operator=(const Handle& rhs);
154 * @brief Move constructor.
157 * @param[in] rhs A reference to the moved handle
159 Handle(Handle&& rhs);
162 * @brief Move assignment operator.
165 * @param[in] rhs A reference to the moved handle
166 * @return A reference to this handle
168 Handle& operator=(Handle&& rhs);
171 * @brief Downcasts to a handle.
173 * If not, the returned handle is left uninitialized.
175 * @param[in] handle to An object
176 * @return handle or an uninitialized handle
178 static Handle DownCast(BaseHandle handle);
181 * @brief Queries whether an handle supports a given capability.
184 * @param[in] capability The queried capability
185 * @return True if the capability is supported
187 bool Supports(Capability capability) const;
192 * @brief Queries how many properties are provided by an handle.
194 * This may vary between instances of a class, if dynamic properties are supported.
196 * @return The number of properties
198 uint32_t GetPropertyCount() const;
201 * @brief Queries the name of a property.
204 * @param[in] index The index of the property
205 * @return The name of the property
207 std::string GetPropertyName(Property::Index index) const;
210 * @brief Query the index of a property using the given key.
213 * @param[in] key The key of the property to search for. (The name or integer key provided to
214 * RegisterProperty()).
215 * @return the matching property index of the key, or Property::INVALID_INDEX if no
216 * property matches the given key.
218 Property::Index GetPropertyIndex(Property::Key key) const;
221 * @brief Queries whether a property can be set using SetProperty().
224 * @param[in] index The index of the property
225 * @return True if the property is writable
226 * @pre Property::INVALID_INDEX < index.
228 bool IsPropertyWritable(Property::Index index) const;
231 * @brief Queries whether a writable property can be the target of an animation or constraint.
234 * @param[in] index The index of the property
235 * @return True if the property is animatable
237 bool IsPropertyAnimatable(Property::Index index) const;
240 * @brief Queries whether a property can be used as in input to a constraint.
243 * @param[in] index The index of the property
244 * @return True if the property can be used as a constraint input
246 bool IsPropertyAConstraintInput(Property::Index index) const;
249 * @brief Queries the type of a property.
252 * @param[in] index The index of the property
253 * @return The type of the property
255 Property::Type GetPropertyType(Property::Index index) const;
258 * @brief Sets the value of an existing property.
260 * Property should be write-able. Setting a read-only property is a no-op.
262 * @param[in] index The index of the property
263 * @param[in] propertyValue The new value of the property
264 * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
266 void SetProperty(Property::Index index, Property::Value propertyValue);
269 * @brief Registers a new animatable property.
272 * @param[in] name The name of the property
273 * @param[in] propertyValue The new value of the property
274 * @return The index of the property or Property::INVALID_INDEX if registration failed
275 * @pre The object supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
276 * Property names are expected to be unique, but this is not enforced.
277 * Property indices are unique to each registered custom property in a given object.
278 * returns Property::INVALID_INDEX if registration failed. This can happen if you try to register
279 * animatable property on an object that does not have scene graph object.
280 * @note Only the following types can be animated:
281 * - Property::BOOLEAN
283 * - Property::INTEGER
284 * - Property::VECTOR2
285 * - Property::VECTOR3
286 * - Property::VECTOR4
287 * - Property::MATRIX3
289 * - Property::ROTATION
290 * @note If a property with the desired name already exists, then the value given is just set.
292 Property::Index RegisterProperty(std::string name, Property::Value propertyValue);
295 * @brief Register a new animatable property with an integer key.
298 * @param[in] key The integer key of the property.
299 * @param[in] name The text key of the property.
300 * @param[in] propertyValue The new value of the property.
302 * @return The index of the property or Property::INVALID_INDEX if registration failed
304 * @pre The object supports dynamic properties
305 * i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true. Property names and keys
306 * are expected to be unique, but this is not enforced. Property indices are unique
307 * to each registered custom property in a given object.
309 * @note Returns Property::INVALID_INDEX if registration failed. This can happen if
310 * you try to register animatable property on an object that does not have scene graph
313 * @note The returned property index is not the same as the integer key (though it
316 * This version of RegisterProperty associates both an integer key and the text key
317 * with the property, allowing for lookup of the property index by either key or name
318 * ( which is useful when other classes know the key but not the name )
320 * @note Only the following types can be animated:
321 * - Property::BOOLEAN
323 * - Property::INTEGER
324 * - Property::VECTOR2
325 * - Property::VECTOR3
326 * - Property::VECTOR4
327 * - Property::MATRIX3
329 * - Property::ROTATION
330 * @note If a property with the desired name already exists, then the value given is just set.
332 Property::Index RegisterProperty(Property::Index key,
334 Property::Value propertyValue);
337 * @brief Registers a new property.
339 * Properties can be set as non animatable using property attributes.
341 * @param[in] name The name of the property
342 * @param[in] propertyValue The new value of the property
343 * @param[in] accessMode The property access mode (writable, animatable etc)
344 * @return The index of the property
345 * @pre The handle supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
346 * @pre name is unused i.e. GetPropertyIndex(name) returns PropertyIndex::INVALID.
347 * @note Only the following types can be animated:
348 * - Property::BOOLEAN
350 * - Property::INTEGER
351 * - Property::VECTOR2
352 * - Property::VECTOR3
353 * - Property::VECTOR4
354 * - Property::MATRIX3
356 * - Property::ROTATION
357 * @note If a property with the desired name already exists, then the value given is just set.
359 Property::Index RegisterProperty(std::string name, Property::Value propertyValue, Property::AccessMode accessMode);
362 * @brief Retrieves a property value.
365 * @param[in] index The index of the property
366 * @return The property value
368 Property::Value GetProperty(Property::Index index) const;
371 * @brief Convenience function for obtaining a property of a known type.
374 * @param[in] index The index of the property
375 * @return The property value
376 * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
379 T GetProperty(Property::Index index) const
381 Property::Value value = GetProperty(index);
383 return T(value.Get<T>());
387 * @brief Retrieves the latest value of the property from the scene-graph.
390 * @param[in] index The index of the property
391 * @return The property value
393 Property::Value GetCurrentProperty(Property::Index index) const;
396 * @brief Convenience function for obtaining the current value of a property of a known type.
399 * @param[in] index The index of the property
400 * @return The property value
401 * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
404 T GetCurrentProperty(Property::Index index) const
406 Property::Value value = GetCurrentProperty(index);
408 return T(value.Get<T>());
412 * @brief Sets all the properties in the given property map.
415 * @param[in] properties The properties to set
417 void SetProperties(const Property::Map& properties);
420 * @brief Retrieves all the properties and the values for this object
423 * @param[out] properties A map which is populated with the index-value pairs
425 * @note The properties map will be cleared by this method first.
427 void GetProperties(Property::Map& properties);
430 * @brief Retrieves all the property indices for this object (including custom properties).
433 * @param[out] indices A container of property indices for this object
434 * @note The added container is cleared.
436 void GetPropertyIndices(Property::IndexContainer& indices) const;
439 * @brief Determine if the custom property index exists on this object without throwing a Dali::Exception.
442 * @note This does not check default properties.
443 * @param[in] index The index of the property to test for
445 bool DoesCustomPropertyExist(Property::Index index);
448 * @brief Adds a property notification to this object.
451 * @param[in] index The index of the property
452 * @param[in] condition The notification will be triggered when this condition is satisfied
453 * @return A handle to the newly created PropertyNotification
455 PropertyNotification AddPropertyNotification(Property::Index index,
456 const PropertyCondition& condition);
459 * @brief Adds a property notification to this object.
462 * @param[in] index The index of the property
463 * @param[in] componentIndex Index to the component of a complex property such as a Vector
464 * @param[in] condition The notification will be triggered when this condition is satisfied
465 * @return A handle to the newly created PropertyNotification
467 PropertyNotification AddPropertyNotification(Property::Index index,
469 const PropertyCondition& condition);
472 * @brief Removes a property notification from this object.
475 * @param[in] propertyNotification The propertyNotification to be removed
477 void RemovePropertyNotification(Dali::PropertyNotification propertyNotification);
480 * @brief Removes all property notifications from this object.
483 void RemovePropertyNotifications();
488 * @brief Removes all constraints from an Object.
491 * @pre The object has been initialized.
493 void RemoveConstraints();
496 * @brief Removes all the constraint from the Object with a matching tag.
499 * @param[in] tag The tag of the constraints which will be removed
500 * @pre The Object has been initialized.
502 void RemoveConstraints(uint32_t tag);
505 * @brief Index operator, using integer lookup.
507 * Returns an object that can be assigned to or cast from, enabling
508 * the indexed property to be either read or written.
510 * @param[in] index The index of the property to access.
511 * @return indirect value. Should have shorter scope than the handle
513 IndirectValue operator[](Property::Index index);
516 * @brief Index operator, using name lookup.
518 * Returns an object that can be assigned to or cast from, enabling
519 * the named property to be either read or written.
521 * @param[in] name The name of the property to access.
522 * @return indirect value. Should have shorter scope than the handle
524 IndirectValue operator[](const std::string& name);
528 * @brief Get a signal when a property is set on this object through the API (i.e. not when animating)
531 * @return The signal to attach a connection to.
533 PropertySetSignalType& PropertySetSignal();
537 * @brief This namespace provides a convenient function to create an object with a custom "weight" property.
540 namespace WeightObject
542 DALI_CORE_API extern const Property::Index WEIGHT; ///< name "weight", type FLOAT
545 * @brief Convenience function to create an object with a custom "weight" property.
548 * @return A handle to a newly allocated object
550 DALI_CORE_API Handle New();
552 } // namespace WeightObject
559 #endif // DALI_HANDLE_H