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