Merge branch 'devel/master (1.1.16)' into tizen
[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  */
51 class DALI_IMPORT_API Handle : public BaseHandle
52 {
53 public:
54
55   /**
56    * @brief An Handle's capabilities can be queried using Handle::Supports()
57    */
58   enum Capability
59   {
60     /**
61      * @brief Some objects support dynamic property creation at run-time.
62      *
63      * New properties are registered by calling RegisterProperty() with an unused property name.
64      */
65     DYNAMIC_PROPERTIES = 0x01,
66   };
67
68 public:
69
70   /**
71    * @brief This constructor is used by Dali New() methods.
72    *
73    * @param [in] handle A pointer to a newly allocated Dali resource
74    */
75   Handle( Dali::Internal::Object* handle );
76
77   /**
78    * @brief This constructor provides an uninitialized Dali::Handle.
79    *
80    * This should be initialized with a Dali New() method before use.
81    * Methods called on an uninitialized Dali::Handle will assert.
82    * @code
83    * Handle handle; // uninitialized
84    * handle.SomeMethod(); // unsafe! This will assert
85    *
86    * handle = SomeClass::New(); // now initialized
87    * handle.SomeMethod(); // safe
88    * @endcode
89    */
90   Handle();
91
92   /**
93    * @brief Create a new object.
94    *
95    * @return A handle to a newly allocated object.
96    */
97   static Handle New();
98
99   /**
100    * @brief Dali::Handle is intended as a base class
101    *
102    * This is non-virtual since derived Handle types must not contain data or virtual methods.
103    */
104   ~Handle();
105
106   /**
107    * @brief This copy constructor is required for (smart) pointer semantics.
108    *
109    * @param [in] handle A reference to the copied handle
110    */
111   Handle( const Handle& handle );
112
113   /**
114    * @brief This assignment operator is required for (smart) pointer semantics.
115    *
116    * @param [in] rhs  A reference to the copied handle
117    * @return A reference to this
118    */
119   Handle& operator=( const Handle& rhs );
120
121   /**
122    * @brief Downcast to a handle.
123    *
124    * If not the returned handle is left uninitialized.
125    * @param[in] handle to An object
126    * @return handle or an uninitialized handle
127    */
128   static Handle DownCast( BaseHandle handle );
129
130   /**
131    * @brief Query whether an handle supports a given capability.
132    *
133    * @param[in] capability The queried capability.
134    * @return True if the capability is supported.
135    */
136   bool Supports( Capability capability ) const;
137
138   // Properties
139
140   /**
141    * @brief Query how many properties are provided by an handle.
142    *
143    * This may vary between instances of a class, if dynamic properties are supported.
144    * @return The number of properties.
145    */
146   unsigned int GetPropertyCount() const;
147
148   /**
149    * @brief Query the name of a property.
150    *
151    * @param [in] index The index of the property.
152    * @return The name of the property.
153    */
154   std::string GetPropertyName( Property::Index index ) const;
155
156   /**
157    * @brief Query the index of a property.
158    * Returns the first property index that matches the given name exactly.
159    *
160    * @param [in] name The name of the property.
161    * @return The index of the property, or Property::INVALID_INDEX if no property exists with the given name.
162    */
163   Property::Index GetPropertyIndex( const std::string& name ) const;
164
165   /**
166    * @brief Query whether a property can be set using SetProperty().
167    *
168    * @pre Property::INVALID_INDEX < index.
169    * @param [in] index The index of the property.
170    * @return True if the property is writable.
171    */
172   bool IsPropertyWritable( Property::Index index ) const;
173
174   /**
175    * @brief Query whether a writable property can be the target of an animation or constraint.
176    *
177    * @param [in] index The index of the property.
178    * @return True if the property is animatable.
179    */
180   bool IsPropertyAnimatable( Property::Index index ) const;
181
182   /**
183    * @brief Query whether a property can be used as in input to a constraint.
184    *
185    * @param [in] index The index of the property.
186    * @return True if the property can be used as a constraint input.
187    */
188   bool IsPropertyAConstraintInput( Property::Index index ) const;
189
190   /**
191    * @brief Query the type of a property.
192    *
193    * @param [in] index The index of the property.
194    * @return The type of the property.
195    */
196   Property::Type GetPropertyType( Property::Index index ) const;
197
198   /**
199    * @brief Set the value of an existing property.
200    *
201    * Property should be write-able. Setting a read-only property is a no-op.
202    * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
203    * @param [in] index The index of the property.
204    * @param [in] propertyValue The new value of the property.
205    */
206   void SetProperty( Property::Index index, const Property::Value& propertyValue );
207
208   /**
209    * @brief Register a new animatable property.
210    *
211    * @pre The object supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
212    * Property names are expected to be unique, but this is not enforced.
213    * Property indices are unique to each registered custom property in a given object.
214    * returns Property::INVALID_INDEX if registration failed. This can happen if you try to register
215    * animatable property on an object that does not have scene graph object.
216    * @note Only the following types can be animated:
217    *       - Property::BOOLEAN
218    *       - Property::FLOAT
219    *       - Property::INTEGER
220    *       - Property::VECTOR2
221    *       - Property::VECTOR3
222    *       - Property::VECTOR4
223    *       - Property::MATRIX3
224    *       - Property::MATRIX
225    *       - Property::ROTATION
226    * @param [in] name The name of the property.
227    * @param [in] propertyValue The new value of the property.
228    * @return The index of the property or Property::INVALID_INDEX if registration failed
229    * @note If a property with the desired name already exists, then the value given is just set.
230    */
231   Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue );
232
233   /**
234    * @brief Register a new property.
235    *
236    * Properties can be set as non animatable using property attributes.
237    * @pre The handle supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
238    * @pre name is unused i.e. GetPropertyIndex(name) returns PropertyIndex::INVALID.
239    * @note Only the following types can be animated:
240    *       - Property::BOOLEAN
241    *       - Property::FLOAT
242    *       - Property::INTEGER
243    *       - Property::VECTOR2
244    *       - Property::VECTOR3
245    *       - Property::VECTOR4
246    *       - Property::MATRIX3
247    *       - Property::MATRIX
248    *       - Property::ROTATION
249    * @param [in] name The name of the property.
250    * @param [in] propertyValue The new value of the property.
251    * @param [in] accessMode The property access mode (writable, animatable etc).
252    * @return The index of the property
253    * @note If a property with the desired name already exists, then the value given is just set.
254    */
255   Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode );
256
257   /**
258    * @brief Retrieve a property value.
259    *
260    * @param [in] index The index of the property.
261    * @return The property value.
262    */
263   Property::Value GetProperty( Property::Index index ) const;
264
265   /**
266    * @brief Convenience function for obtaining a property of a known type.
267    *
268    * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
269    * @param [in] index The index of the property.
270    * @return The property value.
271    */
272   template <typename T>
273   T GetProperty( Property::Index index ) const
274   {
275     Property::Value value = GetProperty(index);
276
277     return T( value.Get<T>() );
278   }
279
280   /**
281    * @brief Retrieve all the property indices for this object (including custom properties).
282    *
283    * @param[out] indices A container of property indices for this object.
284    * @note the added container is cleared
285    */
286   void GetPropertyIndices( Property::IndexContainer& indices ) const;
287
288   /**
289    * @brief Add a property notification to this object.
290    *
291    * @param [in] index The index of the property.
292    * @param [in] condition The notification will be triggered when this condition is satisfied.
293    *
294    * @return A handle to the newly created PropertyNotification
295    */
296   PropertyNotification AddPropertyNotification( Property::Index index,
297                                                 const PropertyCondition& condition );
298
299   /**
300    * @brief Add a property notification to this object.
301    *
302    * @param [in] index The index of the property.
303    * @param [in] componentIndex Index to the component of a complex property such as a Vector
304    * @param [in] condition The notification will be triggered when this condition is satisfied.
305    *
306    * @return A handle to the newly created PropertyNotification
307    */
308   PropertyNotification AddPropertyNotification( Property::Index index,
309                                                 int componentIndex,
310                                                 const PropertyCondition& condition );
311
312   /**
313    * @brief Remove a property notification from this object.
314    *
315    * @param [in] propertyNotification The propertyNotification to be removed.
316    */
317   void RemovePropertyNotification( Dali::PropertyNotification propertyNotification );
318
319   /**
320    * @brief Remove all property notifications from this object.
321    */
322   void RemovePropertyNotifications();
323
324   // Constraints
325
326   /**
327    * @brief Remove all constraints from an Object.
328    *
329    * @pre The object has been initialized.
330    */
331   void RemoveConstraints();
332
333   /**
334    * @brief Remove all the constraint from the Object with a matching tag.
335    *
336    * @pre The Object has been initialized.
337    * @param[in] tag The tag of the constraints which will be removed
338    */
339   void RemoveConstraints( unsigned int tag );
340
341 };
342
343 namespace WeightObject
344 {
345
346 DALI_IMPORT_API extern const Property::Index WEIGHT; ///< name "weight", type FLOAT
347
348 /**
349  * @brief Convenience function to create an object with a custom "weight" property.
350  *
351  * @return A handle to a newly allocated object.
352  */
353 DALI_IMPORT_API Handle New();
354
355 } // namespace WeightObject
356
357 /**
358  * @}
359  */
360 } // namespace Dali
361
362 #endif // __DALI_HANDLE_H__