Tizen 2.4.0 rev3 SDK Public Release
[framework/graphics/dali.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_tizen 2.4
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_tizen 2.4
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_tizen 2.4
67      */
68     DYNAMIC_PROPERTIES = 0x01,
69   };
70
71 public:
72
73   /**
74    * @brief This constructor is used by Dali New() methods.
75    *
76    * @since_tizen 2.4
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_tizen 2.4
94    */
95   Handle();
96
97   /**
98    * @brief Create a new object.
99    *
100    * @since_tizen 2.4
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_tizen 2.4
110    */
111   ~Handle();
112
113   /**
114    * @brief This copy constructor is required for (smart) pointer semantics.
115    *
116    * @since_tizen 2.4
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_tizen 2.4
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_tizen 2.4
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_tizen 2.4
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_tizen 2.4
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_tizen 2.4
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_tizen 2.4
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 whether a property can be set using SetProperty().
182    *
183    * @since_tizen 2.4
184    * @param [in] index The index of the property.
185    * @return True if the property is writable.
186    * @pre Property::INVALID_INDEX < index.
187    */
188   bool IsPropertyWritable( Property::Index index ) const;
189
190   /**
191    * @brief Query whether a writable property can be the target of an animation or constraint.
192    *
193    * @since_tizen 2.4
194    * @param [in] index The index of the property.
195    * @return True if the property is animatable.
196    */
197   bool IsPropertyAnimatable( Property::Index index ) const;
198
199   /**
200    * @brief Query whether a property can be used as in input to a constraint.
201    *
202    * @since_tizen 2.4
203    * @param [in] index The index of the property.
204    * @return True if the property can be used as a constraint input.
205    */
206   bool IsPropertyAConstraintInput( Property::Index index ) const;
207
208   /**
209    * @brief Query the type of a property.
210    *
211    * @since_tizen 2.4
212    * @param [in] index The index of the property.
213    * @return The type of the property.
214    */
215   Property::Type GetPropertyType( Property::Index index ) const;
216
217   /**
218    * @brief Set the value of an existing property.
219    *
220    * Property should be write-able. Setting a read-only property is a no-op.
221    * @since_tizen 2.4
222    * @param [in] index The index of the property.
223    * @param [in] propertyValue The new value of the property.
224    * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
225    */
226   void SetProperty( Property::Index index, const Property::Value& propertyValue );
227
228   /**
229    * @brief Register a new animatable property.
230    *
231    * @since_tizen 2.4
232    * @param [in] name The name of the property.
233    * @param [in] propertyValue The new value of the property.
234    * @return The index of the property or Property::INVALID_INDEX if registration failed
235    * @pre The object supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
236    * Property names are expected to be unique, but this is not enforced.
237    * Property indices are unique to each registered custom property in a given object.
238    * returns Property::INVALID_INDEX if registration failed. This can happen if you try to register
239    * animatable property on an object that does not have scene graph object.
240    * @note Only the following types can be animated:
241    *       - Property::BOOLEAN
242    *       - Property::FLOAT
243    *       - Property::INTEGER
244    *       - Property::VECTOR2
245    *       - Property::VECTOR3
246    *       - Property::VECTOR4
247    *       - Property::MATRIX3
248    *       - Property::MATRIX
249    *       - Property::ROTATION
250    */
251   Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue );
252
253   /**
254    * @brief Register a new property.
255    *
256    * Properties can be set as non animatable using property attributes.
257    * @since_tizen 2.4
258    * @param [in] name The name of the property.
259    * @param [in] propertyValue The new value of the property.
260    * @param [in] accessMode The property access mode (writable, animatable etc).
261    * @return The index of the property
262    * @pre The handle supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
263    * @pre name is unused i.e. GetPropertyIndex(name) returns PropertyIndex::INVALID.
264    * @note Only the following types can be animated:
265    *       - Property::BOOLEAN
266    *       - Property::FLOAT
267    *       - Property::INTEGER
268    *       - Property::VECTOR2
269    *       - Property::VECTOR3
270    *       - Property::VECTOR4
271    *       - Property::MATRIX3
272    *       - Property::MATRIX
273    *       - Property::ROTATION
274    */
275   Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode );
276
277   /**
278    * @brief Retrieve a property value.
279    *
280    * @since_tizen 2.4
281    * @param [in] index The index of the property.
282    * @return The property value.
283    */
284   Property::Value GetProperty( Property::Index index ) const;
285
286   /**
287    * @brief Convenience function for obtaining a property of a known type.
288    *
289    * @since_tizen 2.4
290    * @param [in] index The index of the property.
291    * @return The property value.
292    * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
293    */
294   template <typename T>
295   T GetProperty( Property::Index index ) const
296   {
297     Property::Value value = GetProperty(index);
298
299     return T( value.Get<T>() );
300   }
301
302   /**
303    * @brief Retrieve all the property indices for this object (including custom properties).
304    *
305    * @since_tizen 2.4
306    * @param[out] indices A container of property indices for this object.
307    * @note the added container is cleared
308    */
309   void GetPropertyIndices( Property::IndexContainer& indices ) const;
310
311   /**
312    * @brief Add a property notification to this object.
313    *
314    * @since_tizen 2.4
315    * @param [in] index The index of the property.
316    * @param [in] condition The notification will be triggered when this condition is satisfied.
317    *
318    * @return A handle to the newly created PropertyNotification
319    */
320   PropertyNotification AddPropertyNotification( Property::Index index,
321                                                 const PropertyCondition& condition );
322
323   /**
324    * @brief Add a property notification to this object.
325    *
326    * @since_tizen 2.4
327    * @param [in] index The index of the property.
328    * @param [in] componentIndex Index to the component of a complex property such as a Vector
329    * @param [in] condition The notification will be triggered when this condition is satisfied.
330    *
331    * @return A handle to the newly created PropertyNotification
332    */
333   PropertyNotification AddPropertyNotification( Property::Index index,
334                                                 int componentIndex,
335                                                 const PropertyCondition& condition );
336
337   /**
338    * @brief Remove a property notification from this object.
339    *
340    * @since_tizen 2.4
341    * @param [in] propertyNotification The propertyNotification to be removed.
342    */
343   void RemovePropertyNotification( Dali::PropertyNotification propertyNotification );
344
345   /**
346    * @brief Remove all property notifications from this object.
347    * @since_tizen 2.4
348    */
349   void RemovePropertyNotifications();
350
351   // Constraints
352
353   /**
354    * @brief Remove all constraints from an Object.
355    *
356    * @since_tizen 2.4
357    * @pre The object has been initialized.
358    */
359   void RemoveConstraints();
360
361   /**
362    * @brief Remove all the constraint from the Object with a matching tag.
363    *
364    * @since_tizen 2.4
365    * @param[in] tag The tag of the constraints which will be removed
366    * @pre The Object has been initialized.
367    */
368   void RemoveConstraints( unsigned int tag );
369
370 };
371
372 /**
373  * @brief WeightObject namespace
374  * @since_tizen 2.4
375  */
376 namespace WeightObject
377 {
378
379 DALI_IMPORT_API extern const Property::Index WEIGHT; ///< name "weight", type FLOAT
380
381 /**
382  * @brief Convenience function to create an object with a custom "weight" property.
383  *
384  * @since_tizen 2.4
385  * @return A handle to a newly allocated object.
386  */
387 DALI_IMPORT_API Handle New();
388
389 } // namespace WeightObject
390
391 /**
392  * @}
393  */
394 } // namespace Dali
395
396 #endif // __DALI_HANDLE_H__