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