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