270a73b48ee92ee9e325474bf708c375f127f08f
[platform/core/uifw/dali-core.git] / dali / internal / event / common / object-impl.h
1 #ifndef __DALI_INTERNAL_OBJECT_H__
2 #define __DALI_INTERNAL_OBJECT_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 // INTERNAL INCLUDES
22 #include <dali/public-api/animation/active-constraint.h>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/common/dali-vector.h>
25 #include <dali/public-api/common/vector-wrapper.h>
26 #include <dali/public-api/object/base-object.h>
27 #include <dali/public-api/object/constrainable.h>
28 #include <dali/public-api/object/handle.h>
29 #include <dali/public-api/object/property.h>
30 #include <dali/public-api/object/property-index.h>
31 #include <dali/public-api/object/property-input.h>
32 #include <dali/public-api/object/property-notification.h>
33 #include <dali/internal/common/owner-container.h>
34 #include <dali/internal/event/common/object-impl.h>
35 #include <dali/internal/event/common/custom-property.h>
36 #include <dali/internal/event/common/property-input-impl.h>
37 #include <dali/internal/update/common/property-base.h>
38
39
40 namespace Dali
41 {
42
43 class PropertyNotification;
44
45 namespace Internal
46 {
47 class Constraint;
48 class Handle;
49 class PropertyCondition;
50 class PropertyInputImpl;
51 class Stage;
52 class TypeInfo;
53
54 /**
55  * @brief Structure for setting up default properties and their details.
56  */
57 struct PropertyDetails
58 {
59   const char* name;         ///< The name of the property.
60   Property::Type type;      ///< The property type.
61   bool writable:1;          ///< Whether the property is writable
62   bool animatable:1;        ///< Whether the property is animatable.
63   bool constraintInput:1;   ///< Whether the property can be used as an input to a constraint.
64 };
65
66 namespace SceneGraph
67 {
68 class PropertyBase;
69 class PropertyOwner;
70 }
71
72 typedef std::vector< Dali::ActiveConstraint >     ActiveConstraintContainer;
73 typedef ActiveConstraintContainer::iterator       ActiveConstraintIter;
74 typedef ActiveConstraintContainer::const_iterator ActiveConstraintConstIter;
75
76 /**
77  * A base class for objects which optionally provide properties.
78  * The concrete derived class is responsible for implementing the property system methods.
79  * Classes may derive from Dali::BaseObject, until any properties are required.
80  *
81  * An object for a property-owning object in the scene-graph.
82  * This provides an interface for observing the addition/removal of scene-objects.
83  *
84  * The concrete derived class is responsible for:
85  *   1) Adding & removing an object from the scene-graph. The OnSceneObjectAdd() and OnSceneObjectRemove()
86  *      methods should be called by the derived class, to trigger observer callbacks.
87  *   3) Implementing the GetSceneObject() methods, used to access the scene-object.
88  *   4) Providing access to properties stored by the scene-graph object. These should match the properties
89  *      reported by the base Dali::Internal::Object methods.
90  *
91  */
92 class Object : public Dali::BaseObject
93 {
94 public:
95
96   typedef Dali::Handle::Capability Capability;
97
98   class Observer
99   {
100   public:
101
102     /**
103      * Called immediately after the object has created & passed ownership of a scene-graph object.
104      * @param[in] object The object object.
105      */
106     virtual void SceneObjectAdded(Object& object) = 0;
107
108     /**
109      * Called shortly before the object sends a message to remove its scene object.
110      * @param[in] object The object object.
111      */
112     virtual void SceneObjectRemoved(Object& object) = 0;
113
114     /**
115      * Called shortly before the object itself is destroyed; no further callbacks will be received.
116      * @param[in] object The object object.
117      */
118     virtual void ObjectDestroyed(Object& object) = 0;
119
120   protected:
121
122     /**
123      * Virtual destructor
124      */
125     virtual ~Observer(){}
126   };
127
128   /**
129    * Constructor.
130    */
131   Object();
132
133   /**
134    * Add an observer to the object.
135    * @param[in] observer The observer to add.
136    */
137   virtual void AddObserver( Observer& observer );
138
139   /**
140    * Remove an observer from the object
141    * @pre The observer has already been added.
142    * @param[in] observer The observer to remove.
143    */
144   virtual void RemoveObserver( Observer& observer );
145
146   /**
147    * @copydoc Dali::Handle::Supports()
148    */
149   virtual bool Supports( Capability capability ) const;
150
151   /**
152    * @copydoc Dali::Handle::GetPropertyCount()
153    */
154   virtual unsigned int GetPropertyCount() const;
155
156   /**
157    * @copydoc Dali::Handle::GetPropertyName()
158    */
159   virtual std::string GetPropertyName( Property::Index index ) const;
160
161   /**
162    * @copydoc Dali::Handle::GetPropertyIndex()
163    */
164   virtual Property::Index GetPropertyIndex( const std::string& name ) const;
165
166   /**
167    * @copydoc Dali::Handle::IsPropertyWritable()
168    */
169   virtual bool IsPropertyWritable( Property::Index index ) const;
170
171   /**
172    * @copydoc Dali::Handle::IsPropertyAnimatable()
173    */
174   virtual bool IsPropertyAnimatable( Property::Index index ) const;
175
176   /**
177    * @copydoc Dali::Handle::IsPropertyAConstraintInput()
178    */
179   virtual bool IsPropertyAConstraintInput( Property::Index index ) const;
180
181   /**
182    * @copydoc Dali::Handle::GetPropertyType()
183    */
184   virtual Property::Type GetPropertyType( Property::Index index ) const;
185
186   /**
187    * @copydoc Dali::Handle::SetProperty()
188    */
189   virtual void SetProperty( Property::Index index, const Property::Value& propertyValue );
190
191   /**
192    * @copydoc Dali::Handle::GetProperty()
193    */
194   virtual Property::Value GetProperty( Property::Index index ) const;
195
196   /**
197    * @copydoc Dali::Handle::GetPropertyIndices()
198    */
199   virtual void GetPropertyIndices( Property::IndexContainer& indices ) const;
200
201   /**
202    * @copydoc Dali::Handle::RegisterProperty()
203    */
204   virtual Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue );
205
206   /**
207    * @copydoc Dali::Handle::RegisterProperty(std::string name, Property::Value propertyValue, Property::AccessMode accessMode)
208    */
209   virtual Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode );
210
211   /**
212    * @copydoc Dali::Handle::AddPropertyNotification()
213    */
214   virtual Dali::PropertyNotification AddPropertyNotification( Property::Index index,
215                                                               int componentIndex,
216                                                               const Dali::PropertyCondition& condition );
217
218   /**
219    * @copydoc Dali::Handle::RemovePropertyNotification()
220    */
221   virtual void RemovePropertyNotification( Dali::PropertyNotification propertyNotification );
222
223   /**
224    * @copydoc Dali::Handle::RemovePropertyNotifications()
225    */
226   virtual void RemovePropertyNotifications();
227
228   // Constraints
229
230   /**
231    * Apply a constraint to a Object.
232    * @param[in] constraint The constraint to apply.
233    */
234   Dali::ActiveConstraint ApplyConstraint( Constraint& constraint );
235
236   /**
237    * Apply a constraint to a Object.
238    * @param[in] constraint The constraint to apply.
239    * @param[in] weightObject An object with a "weight" float property.
240    */
241   Dali::ActiveConstraint ApplyConstraint( Constraint& constraint, Dali::Constrainable weightObject );
242
243   /**
244    * Remove one constraint from a Object.
245    * @param[in] activeConstraint The active constraint to remove.
246    */
247   void RemoveConstraint( Dali::ActiveConstraint activeConstraint );
248
249   /**
250    * Remove all constraints from a Object.
251    */
252   void RemoveConstraints();
253
254   /**
255    * Remove all constraints from a Object with a matching tag
256    */
257   void RemoveConstraints( unsigned int tag );
258
259   /**
260    * Called by TypeInfo to set the type-info that this object-impl is created by.
261    * @param[in] typeInfo The TypeInfo that creates this object-impl.
262    */
263   void SetTypeInfo( const TypeInfo* typeInfo );
264
265   /**
266    * @return the index from which custom properties start
267    */
268   unsigned int CustomPropertyStartIndex()
269   {
270     return PROPERTY_CUSTOM_START_INDEX;
271   }
272
273   // To be overridden by deriving classes
274
275   /**
276    * Retrieve the scene-graph object added by this object.
277    * @return A pointer to the object, or NULL if no object has been added to the scene-graph.
278    */
279   virtual const SceneGraph::PropertyOwner* GetSceneObject() const = 0;
280
281   /**
282    * Retrieve an animatable property owned by the scene-graph object.
283    * @pre -1 < index < GetPropertyCount().
284    * @param[in] index The index of the property.
285    * @return A dereferenceable pointer to a property, or NULL if a scene-object does not exist with this property.
286    */
287   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const = 0;
288
289   /**
290    * Retrieve an constraint input-property owned by the scene-graph object.
291    * @pre -1 < index < GetPropertyCount().
292    * @param[in] index The index of the property.
293    * @return A dereferenceable pointer to an input property, or NULL if a scene-object does not exist with this property.
294    */
295   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const = 0;
296
297   /**
298    * Query whether the property is a component of a scene-graph property.
299    * @pre -1 < index < GetPropertyCount().
300    * @param[in] index The index of the property.
301    * @return The index or Property::INVALID_COMPONENT_INDEX.
302    */
303   virtual int GetPropertyComponentIndex( Property::Index index ) const;
304
305 protected:
306
307   /**
308    * A reference counted object may only be deleted by calling Unreference()
309    */
310   virtual ~Object();
311
312   /**
313    * Called immediately by derived classes, after the scene-object has been created & passed to the scene-graph.
314    */
315   void OnSceneObjectAdd();
316
317   /**
318    * Called by derived classes, shortly before send a message to remove the scene-object.
319    */
320   void OnSceneObjectRemove();
321
322   /**
323    * For use in derived classes.
324    * This is called after a non animatable custom property is set.
325    * @param [in] index The index of the property.
326    * @param [in] propertyValue The value of the property.
327    */
328   virtual void OnPropertySet( Property::Index index, Property::Value propertyValue ) {}
329
330   /**
331    * Retrieves the TypeInfo for this object. Only retrieves it from the type-registry once and then stores a pointer
332    * to it locally there-after. The type info will not change during the life-time of the application.
333    * @return The type-info for this object (Can be NULL)
334    */
335   const TypeInfo* GetTypeInfo() const;
336
337   /**
338    * Helper to find custom property
339    * @param index
340    * @return pointer to the property
341    */
342   CustomProperty* FindCustomProperty( Property::Index index ) const;
343
344 private: // Default property extensions for derived classes
345
346   /**
347    * Query how many default properties the derived class supports.
348    * @return The number of default properties.
349    */
350   virtual unsigned int GetDefaultPropertyCount() const = 0;
351
352   /**
353    * Retrieve all the indices that are associated with the default properties supported by the derived class.
354    * @return A container of default property indices.
355    * @note The deriving class must not modify the existing elements in the container.
356    */
357   virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const = 0;
358
359   /**
360    * Query how many default properties the derived class supports.
361    * @return The number of default properties.
362    */
363   virtual const char* GetDefaultPropertyName( Property::Index index ) const = 0;
364
365   /**
366    * Query the index of a default property.
367    * @param [in] name The name of the property.
368    * @return The index of the property, or Property::INVALID_INDEX if no default property exists with the given name.
369    */
370   virtual Property::Index GetDefaultPropertyIndex( const std::string& name ) const = 0;
371
372   /**
373    * Query whether a default property is writable.
374    * @param [in] index The index of the property.
375    * @return True if the property is animatable.
376    */
377   virtual bool IsDefaultPropertyWritable( Property::Index index ) const = 0;
378
379   /**
380    * Query whether a default property is animatable.
381    * This determines whether the property can be the target of an animation or constraint.
382    * @param [in] index The index of the property.
383    * @return True if the property is animatable.
384    */
385   virtual bool IsDefaultPropertyAnimatable( Property::Index index ) const = 0;
386
387   /**
388    * @brief Query whether a default property can be used as an input to a constraint.
389    *
390    * @param [in] index The index of the property.
391    * @return True if the property can be used as an input to a constraint.
392    */
393   virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const = 0;
394
395   /**
396    * Query the type of a default property.
397    * @param [in] index The index of the property.
398    * @return The type of the property.
399    */
400   virtual Property::Type GetDefaultPropertyType( Property::Index index ) const = 0;
401
402   /**
403    * Set the value of a default property.
404    * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
405    * @param [in] index The index of the property.
406    * @param [in] propertyValue The new value of the property.
407    */
408   virtual void SetDefaultProperty( Property::Index index, const Property::Value& propertyValue ) = 0;
409
410   /**
411    * Retrieve a default property value.
412    * @param [in] index The index of the property.
413    * @return The property value.
414    */
415   virtual Property::Value GetDefaultProperty( Property::Index index ) const = 0;
416
417   /**
418    * @todo this is virtual so that for now actor can override it,
419    * it needs to be removed and only have GetSceneObject but that requires changing actor and constraint logic
420    * Retrieve the scene-graph object added by this object.
421    * @return A pointer to the object, or NULL if no object has been added to the scene-graph.
422    */
423   virtual const SceneGraph::PropertyOwner* GetPropertyOwner() const
424   {
425     return GetSceneObject();
426   }
427
428   /**
429    * Notify derived class of installation of a new scene-object property.
430    * This method is called after the message is to sent to install the property
431    * @param [in] newProperty A newly allocated scene-object property. Ownership is obviously not passed.
432    * @param [in] name The name allocated to this custom property.
433    * @param [in] index The index allocated to this custom property.
434    */
435   virtual void NotifyScenePropertyInstalled( const SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index )
436   { }
437
438 private:
439
440   // Not implemented
441   Object(const Object& rhs);
442
443   // Not implemented
444   Object& operator=(const Object& rhs);
445
446   /**
447    * Enable property notifications in scene graph
448    */
449   void EnablePropertyNotifications();
450
451   /**
452    * Enable property notifications in scene graph
453    */
454   void DisablePropertyNotifications();
455
456   /**
457    * Helper for ApplyConstraint overloads.
458    * @param[in] constraint The constraint to apply.
459    * @param[in] weightObject An object with a "weight" float property, or an empty handle.
460    * @return The new active-constraint which is owned by Object.
461    */
462   ActiveConstraintBase* DoApplyConstraint( Constraint& constraint, Dali::Constrainable weightObject );
463
464   /**
465    * Helper to remove active constraints
466    */
467   void RemoveConstraint( ActiveConstraint& constraint, bool isInScenegraph );
468
469   /**
470    * Set the value of scene graph property.
471    * @param [in] index The index of the property.
472    * @param [in] entry An entry from the CustomPropertyLookup.
473    * @param [in] value The new value of the property.
474    */
475   virtual void SetSceneGraphProperty( Property::Index index, const CustomProperty& entry, const Property::Value& value );
476
477 private:
478
479   typedef OwnerContainer<CustomProperty*> CustomPropertyLookup;
480   CustomPropertyLookup mCustomProperties; ///< Used for accessing custom Node properties
481   mutable TypeInfo const *  mTypeInfo; ///< The type-info for this object, mutable so it can be lazy initialized from const method if it is required
482
483   Dali::Vector<Observer*> mObservers;
484
485   ActiveConstraintContainer* mConstraints;               ///< Container of owned active-constraints.
486
487   typedef std::vector< Dali::PropertyNotification >     PropertyNotificationContainer;
488   typedef PropertyNotificationContainer::iterator       PropertyNotificationContainerIter;
489   typedef PropertyNotificationContainer::const_iterator PropertyNotificationContainerConstIter;
490   PropertyNotificationContainer* mPropertyNotifications; ///< Container of owned property notifications.
491 };
492
493 } // namespace Internal
494
495 // Helpers for public-api forwarding methods
496
497 inline Internal::Object& GetImplementation(Dali::Handle& object)
498 {
499   DALI_ASSERT_ALWAYS( object && "Object handle is empty" );
500
501   BaseObject& handle = object.GetBaseObject();
502
503   return static_cast<Internal::Object&>(handle);
504 }
505
506 inline const Internal::Object& GetImplementation(const Dali::Handle& object)
507 {
508   DALI_ASSERT_ALWAYS( object && "Object handle is empty" );
509
510   const BaseObject& handle = object.GetBaseObject();
511
512   return static_cast<const Internal::Object&>(handle);
513 }
514
515 } // namespace Dali
516
517 #endif // __DALI_INTERNAL_OBJECT_H__
518