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