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