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