18942357b09712c83a696bd35aa548bf039fadb2
[platform/core/uifw/dali-core.git] / dali / public-api / object / handle.h
1 #ifndef __DALI_HANDLE_H__
2 #define __DALI_HANDLE_H__
3
4 /*
5  * Copyright (c) 2018 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 <string>
23 #include <cstdint> // uint32_t
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/object/base-handle.h>
28 #include <dali/public-api/object/property-types.h>
29 #include <dali/public-api/object/property-value.h>
30 #include <dali/public-api/object/property-notification-declarations.h>
31 #include <dali/public-api/object/ref-object.h>
32
33 namespace Dali
34 {
35 /**
36  * @addtogroup dali_core_object
37  * @{
38  */
39
40 class Constraint;
41 class PropertyNotification;
42 class PropertyCondition;
43
44 namespace Internal DALI_INTERNAL
45 {
46 class Object;
47 }
48
49 /**
50  * @brief Dali::Handle is a handle to an internal property owning Dali object that can have constraints applied to it.
51  * @SINCE_1_0.0
52  */
53 class DALI_CORE_API Handle : public BaseHandle
54 {
55 public:
56
57   /**
58    * @brief Enumeration for Handle's capabilities that can be queried using Handle::Supports().
59    * @SINCE_1_0.0
60    */
61   enum Capability
62   {
63     /**
64      * @brief Some objects support dynamic property creation at run-time.
65      *
66      * New properties are registered by calling RegisterProperty() with an unused property name.
67      * @SINCE_1_0.0
68      */
69     DYNAMIC_PROPERTIES = 0x01,
70   };
71
72 public:
73
74   /**
75    * @brief This constructor is used by Dali New() methods.
76    *
77    * @SINCE_1_0.0
78    * @param[in] handle A pointer to a newly allocated Dali resource
79    */
80   Handle( Dali::Internal::Object* handle );
81
82   /**
83    * @brief This constructor provides an uninitialized Dali::Handle.
84    *
85    * This should be initialized with a Dali New() method before use.
86    * Methods called on an uninitialized Dali::Handle will assert.
87    * @code
88    * Handle handle; // uninitialized
89    * handle.SomeMethod(); // unsafe! This will assert
90    *
91    * handle = SomeClass::New(); // now initialized
92    * handle.SomeMethod(); // safe
93    * @endcode
94    * @SINCE_1_0.0
95    */
96   Handle();
97
98   /**
99    * @brief Creates a new object.
100    *
101    * @SINCE_1_0.0
102    * @return A handle to a newly allocated object
103    */
104   static Handle New();
105
106   /**
107    * @brief Dali::Handle is intended as a base class.
108    *
109    * This is non-virtual since derived Handle types must not contain data or virtual methods.
110    * @SINCE_1_0.0
111    */
112   ~Handle();
113
114   /**
115    * @brief This copy constructor is required for (smart) pointer semantics.
116    *
117    * @SINCE_1_0.0
118    * @param[in] handle A reference to the copied handle
119    */
120   Handle( const Handle& handle );
121
122   /**
123    * @brief This assignment operator is required for (smart) pointer semantics.
124    *
125    * @SINCE_1_0.0
126    * @param[in] rhs A reference to the copied handle
127    * @return A reference to this
128    */
129   Handle& operator=( const Handle& rhs );
130
131   /**
132    * @brief Downcasts to a handle.
133    *
134    * If not, the returned handle is left uninitialized.
135    * @SINCE_1_0.0
136    * @param[in] handle to An object
137    * @return handle or an uninitialized handle
138    */
139   static Handle DownCast( BaseHandle handle );
140
141   /**
142    * @brief Queries whether an handle supports a given capability.
143    *
144    * @SINCE_1_0.0
145    * @param[in] capability The queried capability
146    * @return True if the capability is supported
147    */
148   bool Supports( Capability capability ) const;
149
150   // Properties
151
152   /**
153    * @brief Queries how many properties are provided by an handle.
154    *
155    * This may vary between instances of a class, if dynamic properties are supported.
156    * @SINCE_1_0.0
157    * @return The number of properties
158    */
159   uint32_t GetPropertyCount() const;
160
161   /**
162    * @brief Queries the name of a property.
163    *
164    * @SINCE_1_0.0
165    * @param[in] index The index of the property
166    * @return The name of the property
167    */
168   std::string GetPropertyName( Property::Index index ) const;
169
170   /**
171    * @brief Queries the index of a property.
172    *
173    * Returns the first property index that matches the given name exactly.
174    *
175    * @SINCE_1_0.0
176    * @param[in] name The name of the property
177    * @return The index of the property, or Property::INVALID_INDEX if no property exists with the given name
178    */
179   Property::Index GetPropertyIndex( const std::string& name ) const;
180
181   /**
182    * @brief Queries whether a property can be set using SetProperty().
183    *
184    * @SINCE_1_0.0
185    * @param[in] index The index of the property
186    * @return True if the property is writable
187    * @pre Property::INVALID_INDEX < index.
188    */
189   bool IsPropertyWritable( Property::Index index ) const;
190
191   /**
192    * @brief Queries whether a writable property can be the target of an animation or constraint.
193    *
194    * @SINCE_1_0.0
195    * @param[in] index The index of the property
196    * @return True if the property is animatable
197    */
198   bool IsPropertyAnimatable( Property::Index index ) const;
199
200   /**
201    * @brief Queries whether a property can be used as in input to a constraint.
202    *
203    * @SINCE_1_0.0
204    * @param[in] index The index of the property
205    * @return True if the property can be used as a constraint input
206    */
207   bool IsPropertyAConstraintInput( Property::Index index ) const;
208
209   /**
210    * @brief Queries the type of a property.
211    *
212    * @SINCE_1_0.0
213    * @param[in] index The index of the property
214    * @return The type of the property
215    */
216   Property::Type GetPropertyType( Property::Index index ) const;
217
218   /**
219    * @brief Sets the value of an existing property.
220    *
221    * Property should be write-able. Setting a read-only property is a no-op.
222    * @SINCE_1_0.0
223    * @param[in] index The index of the property
224    * @param[in] propertyValue The new value of the property
225    * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
226    */
227   void SetProperty( Property::Index index, const Property::Value& propertyValue );
228
229   /**
230    * @brief Registers a new animatable property.
231    *
232    * @SINCE_1_0.0
233    * @param[in] name The name of the property
234    * @param[in] propertyValue The new value of the property
235    * @return The index of the property or Property::INVALID_INDEX if registration failed
236    * @pre The object supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
237    * Property names are expected to be unique, but this is not enforced.
238    * Property indices are unique to each registered custom property in a given object.
239    * returns Property::INVALID_INDEX if registration failed. This can happen if you try to register
240    * animatable property on an object that does not have scene graph object.
241    * @note Only the following types can be animated:
242    *       - Property::BOOLEAN
243    *       - Property::FLOAT
244    *       - Property::INTEGER
245    *       - Property::VECTOR2
246    *       - Property::VECTOR3
247    *       - Property::VECTOR4
248    *       - Property::MATRIX3
249    *       - Property::MATRIX
250    *       - Property::ROTATION
251    * @note If a property with the desired name already exists, then the value given is just set.
252    */
253   Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue );
254
255   /**
256    * @brief Registers a new property.
257    *
258    * Properties can be set as non animatable using property attributes.
259    * @SINCE_1_0.0
260    * @param[in] name The name of the property
261    * @param[in] propertyValue The new value of the property
262    * @param[in] accessMode The property access mode (writable, animatable etc)
263    * @return The index of the property
264    * @pre The handle supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
265    * @pre name is unused i.e. GetPropertyIndex(name) returns PropertyIndex::INVALID.
266    * @note Only the following types can be animated:
267    *       - Property::BOOLEAN
268    *       - Property::FLOAT
269    *       - Property::INTEGER
270    *       - Property::VECTOR2
271    *       - Property::VECTOR3
272    *       - Property::VECTOR4
273    *       - Property::MATRIX3
274    *       - Property::MATRIX
275    *       - Property::ROTATION
276    * @note If a property with the desired name already exists, then the value given is just set.
277    */
278   Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode );
279
280   /**
281    * @brief Retrieves a property value.
282    *
283    * @SINCE_1_0.0
284    * @param[in] index The index of the property
285    * @return The property value
286    */
287   Property::Value GetProperty( Property::Index index ) const;
288
289   /**
290    * @brief Convenience function for obtaining a property of a known type.
291    *
292    * @SINCE_1_0.0
293    * @param[in] index The index of the property
294    * @return The property value
295    * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
296    */
297   template <typename T>
298   T GetProperty( Property::Index index ) const
299   {
300     Property::Value value = GetProperty(index);
301
302     return T( value.Get<T>() );
303   }
304
305   /**
306    * @brief Retrieves the latest value of the property from the scene-graph.
307    *
308    * @SINCE_1_2.41
309    * @param[in] index The index of the property
310    * @return The property value
311    */
312   Property::Value GetCurrentProperty( Property::Index index ) const;
313
314   /**
315    * @brief Convenience function for obtaining the current value of a property of a known type.
316    *
317    * @SINCE_1_2.41
318    * @param[in] index The index of the property
319    * @return The property value
320    * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
321    */
322   template <typename T>
323   T GetCurrentProperty( Property::Index index ) const
324   {
325     Property::Value value = GetCurrentProperty( index );
326
327     return T( value.Get<T>() );
328   }
329
330   /**
331    * @brief Retrieves all the property indices for this object (including custom properties).
332    *
333    * @SINCE_1_0.0
334    * @param[out] indices A container of property indices for this object
335    * @note The added container is cleared.
336    */
337   void GetPropertyIndices( Property::IndexContainer& indices ) const;
338
339   /**
340    * @brief Adds a property notification to this object.
341    *
342    * @SINCE_1_0.0
343    * @param[in] index The index of the property
344    * @param[in] condition The notification will be triggered when this condition is satisfied
345    * @return A handle to the newly created PropertyNotification
346    */
347   PropertyNotification AddPropertyNotification( Property::Index index,
348                                                 const PropertyCondition& condition );
349
350   /**
351    * @brief Adds a property notification to this object.
352    *
353    * @SINCE_1_0.0
354    * @param[in] index The index of the property
355    * @param[in] componentIndex Index to the component of a complex property such as a Vector
356    * @param[in] condition The notification will be triggered when this condition is satisfied
357    * @return A handle to the newly created PropertyNotification
358    */
359   PropertyNotification AddPropertyNotification( Property::Index index,
360                                                 int componentIndex,
361                                                 const PropertyCondition& condition );
362
363   /**
364    * @brief Removes a property notification from this object.
365    *
366    * @SINCE_1_0.0
367    * @param[in] propertyNotification The propertyNotification to be removed
368    */
369   void RemovePropertyNotification( Dali::PropertyNotification propertyNotification );
370
371   /**
372    * @brief Removes all property notifications from this object.
373    * @SINCE_1_0.0
374    */
375   void RemovePropertyNotifications();
376
377   // Constraints
378
379   /**
380    * @brief Removes all constraints from an Object.
381    *
382    * @SINCE_1_0.0
383    * @pre The object has been initialized.
384    */
385   void RemoveConstraints();
386
387   /**
388    * @brief Removes all the constraint from the Object with a matching tag.
389    *
390    * @SINCE_1_0.0
391    * @param[in] tag The tag of the constraints which will be removed
392    * @pre The Object has been initialized.
393    */
394   void RemoveConstraints( uint32_t tag );
395 };
396
397 /**
398  * @brief This namespace provides a convenient function to create an object with a custom "weight" property.
399  * @SINCE_1_0.0
400  */
401 namespace WeightObject
402 {
403
404 DALI_CORE_API extern const Property::Index WEIGHT; ///< name "weight", type FLOAT
405
406 /**
407  * @brief Convenience function to create an object with a custom "weight" property.
408  *
409  * @SINCE_1_0.0
410  * @return A handle to a newly allocated object
411  */
412 DALI_CORE_API Handle New();
413
414 } // namespace WeightObject
415
416 /**
417  * @}
418  */
419 } // namespace Dali
420
421 #endif // __DALI_HANDLE_H__