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