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