3df069b3b6a0454acccdf66d8c03d9dad092d96e
[platform/core/uifw/dali-core.git] / dali / internal / update / common / property-owner.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_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
23 // INTERNAL INCLUDES
24 #include <dali/public-api/common/dali-vector.h>
25 #include <dali/devel-api/common/owner-container.h>
26 #include <dali/internal/common/message.h>
27 #include <dali/internal/update/common/property-base.h>
28 #include <dali/internal/update/common/scene-graph-buffers.h>
29 #include <dali/internal/update/common/uniform-map.h>
30 #include <dali/internal/update/animation/scene-graph-constraint-declarations.h>
31
32
33 namespace Dali
34 {
35
36 namespace Internal
37 {
38
39 namespace SceneGraph
40 {
41
42 class PropertyOwner;
43
44 typedef OwnerContainer< PropertyBase* > OwnedPropertyContainer;
45 typedef OwnedPropertyContainer::Iterator  OwnedPropertyIter;
46
47 /**
48  * An update-thread object which own properties.
49  * This allows observers to track the lifetime of the object & its properties.
50  */
51 class PropertyOwner
52 {
53 public:
54
55   class Observer
56   {
57   public:
58
59     /**
60      * Called when the observable object is connected to the scene graph.
61      * @param[in] owner A reference to the connected PropertyOwner
62      */
63     virtual void PropertyOwnerConnected( PropertyOwner& owner ) = 0;
64
65     /**
66      * Called when the observable object is disconnected from the scene graph.
67      * @param[in] currentBufferIndex The buffer to reset.
68      * @param[in] owner A reference to the disconnected PropertyOwner
69      */
70     virtual void PropertyOwnerDisconnected( BufferIndex updateBufferIndex, PropertyOwner& owner ) = 0;
71
72     /**
73      * Called shortly before the observable object is destroyed.
74      *
75      * @note Cleanup should be done in both this and PropertyOwnerDisconnected as PropertyOwnerDisconnected
76      * may not be called (i.e. when shutting down).
77      */
78     virtual void PropertyOwnerDestroyed( PropertyOwner& owner ) = 0;
79   };
80
81   /**
82    * Create a property owner.
83    * @return A newly allocated object.
84    */
85   static PropertyOwner* New();
86
87   /**
88    * Virtual destructor; this is intended as a base class.
89    */
90   virtual ~PropertyOwner();
91
92   /**
93    * Add an observer.
94    * The observer is responsible for calling RemoveObserver(*this) during its own destruction.
95    * Connecting an actor-side object as an observer is not allowed, due to thread-safety issues.
96    * @param[in] observer The observer.
97    */
98   void AddObserver(Observer& observer);
99
100   /**
101    * Remove an observer.
102    * @param[in] observer The observer.
103    */
104   void RemoveObserver(Observer& observer);
105
106   /**
107    * This method can be used to determine if there is an animation or
108    * constraint that is using this property owner.
109    * @return true if there are observers.
110    */
111   bool IsObserved();
112
113   /**
114    * Called just before destruction to disconnect all observers and remove constraints.
115    * This occurs when the object is in the process of being destroyed.
116    */
117   void Destroy();
118
119   /**
120    * Notify all observers that the object has been connected
121    * This occurs when the object is connected to the scene-graph during UpdateManager::Update().
122    */
123   void ConnectToSceneGraph();
124
125   /**
126    * Notify all observers that the object has been disconnected and remove constraints.
127    * This occurs when the object is disconnected from the scene-graph during UpdateManager::Update().
128    * @param[in] currentBufferIndex The current update buffer.
129    */
130   void DisconnectFromSceneGraph( BufferIndex updateBufferIndex );
131
132   /**
133    * Install a custom property.
134    * @post The PropertyOwner takes ownership of the property.
135    * @param[in] property A pointer to a newly allocated property.
136    */
137   void InstallCustomProperty(PropertyBase* property);
138
139   /**
140    * Retrieve the custom properties owned by the object.
141    * @return A container of properties.
142    */
143   OwnedPropertyContainer& GetCustomProperties()
144   {
145     return mCustomProperties;
146   }
147
148   /**
149    * Retrieve the custom properties owned by the object.
150    * @return A container of properties.
151    */
152   const OwnedPropertyContainer& GetCustomProperties() const
153   {
154     return mCustomProperties;
155   }
156
157   /**
158    * Reset animatable properties to the corresponding base values.
159    * @param[in] currentBufferIndex The buffer to reset.
160    * @post The ResetDefaultProperties method is called, during which derived classes can reset default properties.
161    */
162   void ResetToBaseValues( BufferIndex updateBufferIndex );
163
164   // Constraints
165
166   /**
167    * Apply a constraint.
168    * @param[in] constraint The constraint to apply.
169    */
170   void ApplyConstraint( ConstraintBase* constraint );
171
172   /**
173    * Begin removal of constraints.
174    * @param[in] constraint The constraint to remove.
175    */
176   void RemoveConstraint( ConstraintBase* constraint );
177
178   /**
179    * Retrieve the constraints that are currently applied.
180    * @return A container of constraints.
181    */
182   ConstraintOwnerContainer& GetConstraints();
183
184   /**
185    * @copydoc UniformMap::Add
186    */
187   virtual void AddUniformMapping( UniformPropertyMapping* map );
188
189   /**
190    * @copydoc UniformMap::Remove
191    */
192   virtual void RemoveUniformMapping( const std::string& uniformName );
193
194   /**
195    * Get the mappings table
196    */
197   const UniformMap& GetUniformMap() const;
198
199   /**
200    * @copydoc UniformMap::AddUniformMapObserver
201    */
202   void AddUniformMapObserver( UniformMap::Observer& observer );
203
204   /**
205    * @copydoc UniformMap::RemoveUniformMapObserver
206    */
207   void RemoveUniformMapObserver( UniformMap::Observer& observer );
208
209
210 protected:
211
212   /**
213    * Protected constructor.
214    */
215   PropertyOwner();
216
217 private:
218
219   // Undefined
220   PropertyOwner(const PropertyOwner&);
221
222   // Undefined
223   PropertyOwner& operator=(const PropertyOwner& rhs);
224
225   /**
226    * Called after ResetToBaseValues; derived classes should reset any default properties.
227    * @param[in] currentBufferIndex The buffer to reset.
228    */
229   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ) {}
230
231 protected:
232
233   OwnedPropertyContainer mCustomProperties; ///< Properties provided with InstallCustomProperty()
234   UniformMap mUniformMaps; ///< Container of owned uniform maps
235
236 private:
237
238   typedef Dali::Vector<PropertyOwner::Observer*> ObserverContainer;
239   typedef ObserverContainer::Iterator ObserverIter;
240   typedef ObserverContainer::ConstIterator ConstObserverIter;
241
242   ObserverContainer mObservers; ///< Container of observer raw-pointers (not owned)
243
244   ConstraintOwnerContainer mConstraints; ///< Container of owned constraints
245 };
246
247 } // namespace SceneGraph
248
249 } // namespace Internal
250
251 } // namespace Dali
252
253 #endif // __DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_H__