Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / constraint-base.h
1 #ifndef __DALI_INTERNAL_ACTIVE_CONSTRAINT_BASE_H__
2 #define __DALI_INTERNAL_ACTIVE_CONSTRAINT_BASE_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/internal/common/owner-pointer.h>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/internal/event/animation/constraint-source-impl.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 class EventThreadServices;
35 class Object;
36 typedef Dali::Vector<Object*>     ObjectContainer;
37 typedef ObjectContainer::Iterator ObjectIter;
38
39 namespace SceneGraph
40 {
41 class ConstraintBase;
42
43 template <typename T>
44 class AnimatableProperty;
45 }
46
47 /**
48  * An abstract base class for active constraints.
49  */
50 class ConstraintBase : public BaseObject, public Object::Observer
51 {
52 public:
53
54   typedef Dali::Constraint::RemoveAction RemoveAction;
55
56   /**
57    * Constructor.
58    * @param[in] object The property owning object.
59    * @param[in] messageController Used to send messages to the update-thread.
60    * @param[in] targetPropertyIndex The index of the property being constrained.
61    * @param[in] sources The sources of the input properties.
62    */
63   ConstraintBase( Object& object, Property::Index targetPropertyIndex, SourceContainer& sources );
64
65   /**
66    * Clone this constraint for another object.
67    * @param[in]  object  The object to clone this constraint for
68    * @return A new constraint.
69    */
70   virtual ConstraintBase* Clone( Object& object ) = 0;
71
72   /**
73    * Virtual destructor.
74    */
75   virtual ~ConstraintBase();
76
77   /**
78    * Adds a constraint source to the constraint
79    *
80    * @param[in] source The constraint source input to add
81    */
82   void AddSource( Source source );
83
84   /**
85    * @copydoc Dali::Constraint::Apply()
86    */
87   void Apply();
88
89   /**
90    * @copydoc Dali::Constraint::Remove()
91    */
92   void Remove();
93
94   /**
95    * Called when the Constraint is removed.
96    *
97    * @note This removes the scene-object as well but then does not call back into the target-object.
98    */
99   void RemoveInternal();
100
101   /**
102    * Retrieve the parent of the constraint.
103    * @return The parent object, or NULL.
104    */
105   Object* GetParent();
106
107   /**
108    * @copydoc Dali::Constraint::GetTargetObject()
109    */
110   Dali::Handle GetTargetObject();
111
112   /**
113    * @copydoc Dali::Constraint::GetTargetProperty()
114    */
115   Property::Index GetTargetProperty();
116
117   /**
118    * @copydoc Dali::Constraint::SetRemoveAction()
119    */
120   void SetRemoveAction(RemoveAction action);
121
122   /**
123    * @copydoc Dali::Constraint::GetRemoveAction()
124    */
125   RemoveAction GetRemoveAction() const;
126
127   /**
128    * @copydoc Dali::Constraint::SetTag()
129    */
130   void SetTag(const unsigned int tag);
131
132   /**
133    * @copydoc Dali::Constraint::GetTag()
134    */
135   unsigned int GetTag() const;
136
137 private: // Object::Observer methods
138
139   /**
140    * @copydoc Object::Observer::SceneObjectAdded()
141    */
142   virtual void SceneObjectAdded( Object& object );
143
144   /**
145    * @copydoc Object::Observer::SceneObjectRemoved()
146    */
147   virtual void SceneObjectRemoved( Object& object );
148
149   /**
150    * @copydoc Object::Observer::ObjectDestroyed()
151    */
152   virtual void ObjectDestroyed( Object& object );
153
154 private:
155
156   /**
157    * Helper to observe an object, if not already observing it
158    */
159   void ObserveObject( Object& object );
160
161   /**
162    * Helper to stop observing objects
163    */
164   void StopObservation();
165
166   // To be implemented in derived classes
167
168   /**
169    * Create and connect a constraint for a scene-object.
170    */
171   virtual void ConnectConstraint() = 0;
172
173 protected:
174
175   /**
176    * Get the event thread services object - used for sending messages to the scene graph
177    * Assert if called from the wrong thread.
178    * This is intentionally inline for performance reasons.
179    *
180    * @return The event thread services object
181    */
182   inline EventThreadServices& GetEventThreadServices()
183   {
184     DALI_ASSERT_DEBUG( EventThreadServices::IsCoreRunning() );
185     return mEventThreadServices;
186   }
187
188   /**
189    * Get the event thread services object - used for sending messages to the scene graph
190    * Assert if called from the wrong thread
191    * This is intentionally inline for performance reasons.
192    *
193    * @return The event thread services object
194    */
195   inline const EventThreadServices& GetEventThreadServices() const
196   {
197     DALI_ASSERT_DEBUG( EventThreadServices::IsCoreRunning() );
198     return mEventThreadServices;
199   }
200
201 protected:
202   EventThreadServices& mEventThreadServices;
203   Object* mTargetObject; ///< The object owns the constraint.
204   const SceneGraph::ConstraintBase* mSceneGraphConstraint;
205   SourceContainer mSources;
206   ObjectContainer mObservedObjects; // We don't observe the same object twice
207   Property::Index mTargetPropertyIndex;
208   RemoveAction mRemoveAction;
209   unsigned int mTag;
210   bool mApplied:1; ///< Whether the constraint has been applied
211   bool mSourceDestroyed:1; ///< Is set to true if any of our input source objects are destroyed
212 };
213
214 } // namespace Internal
215
216 // Helpers for public-api forwarding methods
217
218 inline Internal::ConstraintBase& GetImplementation(Dali::Constraint& constraint)
219 {
220   DALI_ASSERT_ALWAYS( constraint && "Constraint handle is empty" );
221
222   BaseObject& handle = constraint.GetBaseObject();
223
224   return static_cast<Internal::ConstraintBase&>(handle);
225 }
226
227 inline const Internal::ConstraintBase& GetImplementation(const Dali::Constraint& constraint)
228 {
229   DALI_ASSERT_ALWAYS( constraint && "Constraint handle is empty" );
230
231   const BaseObject& handle = constraint.GetBaseObject();
232
233   return static_cast<const Internal::ConstraintBase&>(handle);
234 }
235
236 } // namespace Dali
237
238 #endif // __DALI_INTERNAL_ACTIVE_CONSTRAINT_BASE_H__