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