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