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