1 #ifndef DALI_INTERNAL_ACTIVE_CONSTRAINT_BASE_H
2 #define DALI_INTERNAL_ACTIVE_CONSTRAINT_BASE_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
33 class EventThreadServices;
35 using ObjectContainer = Dali::Vector<Object*>;
36 using ObjectIter = ObjectContainer::Iterator;
43 class AnimatableProperty;
44 } // namespace SceneGraph
47 * An abstract base class for active constraints.
49 class ConstraintBase : public BaseObject, public Object::Observer
52 using RemoveAction = Dali::Constraint::RemoveAction;
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.
61 ConstraintBase(Object& object, Property::Index targetPropertyIndex, SourceContainer& sources);
64 * Clone this constraint for another object.
65 * @param[in] object The object to clone this constraint for
66 * @return A new constraint.
68 ConstraintBase* Clone(Object& object);
73 ~ConstraintBase() override;
76 * Adds a constraint source to the constraint
78 * @param[in] source The constraint source input to add
80 void AddSource(Source source);
83 * @copydoc Dali::Constraint::Apply()
85 void Apply(bool isPreConstraint = true);
88 * @copydoc Dali::Constraint::ApplyPost()
93 * @copydoc Dali::Constraint::Remove()
98 * Called when the Constraint is removed.
100 * @note This removes the scene-object as well but then does not call back into the target-object.
102 void RemoveInternal();
105 * Retrieve the parent of the constraint.
106 * @return The parent object, or NULL.
111 * @copydoc Dali::Constraint::GetTargetObject()
113 Dali::Handle GetTargetObject();
116 * @copydoc Dali::Constraint::GetTargetProperty()
118 Property::Index GetTargetProperty();
121 * @copydoc Dali::Constraint::SetRemoveAction()
123 void SetRemoveAction(RemoveAction action);
126 * @copydoc Dali::Constraint::GetRemoveAction()
128 RemoveAction GetRemoveAction() const;
131 * @copydoc Dali::Constraint::SetTag()
133 void SetTag(uint32_t tag);
136 * @copydoc Dali::Constraint::GetTag()
138 uint32_t GetTag() const;
140 private: // Object::Observer methods
142 * @copydoc Object::Observer::SceneObjectAdded()
144 void SceneObjectAdded(Object& object) override;
147 * @copydoc Object::Observer::SceneObjectRemoved()
149 void SceneObjectRemoved(Object& object) override;
152 * @copydoc Object::Observer::ObjectDestroyed()
154 void ObjectDestroyed(Object& object) override;
158 * Helper to observe an object, if not already observing it
160 void ObserveObject(Object& object);
163 * Helper to stop observing objects
165 void StopObservation();
167 // To be implemented in derived classes
170 * Clone the actual constraint
172 * @param object to clone to
173 * @return pointer to the clone
175 virtual ConstraintBase* DoClone(Object& object) = 0;
178 * Connect the constraint
180 virtual void ConnectConstraint(bool isPreConstraint = true) = 0;
184 * Helper to Add an input property to the container of property owners
185 * @param source constraint[in] source used to determine the type and locate the property on the object
186 * @param propertyOwners[out] reference to the container to add
187 * @param componentIndex[out] component index
188 * @return pointer to input property if it was found, nullptr otherwise
190 PropertyInputImpl* AddInputProperty(Source& source, SceneGraph::PropertyOwnerContainer& propertyOwners, int32_t& componentIndex);
193 * Get the event thread services object - used for sending messages to the scene graph
194 * Assert if called from the wrong thread.
195 * This is intentionally inline for performance reasons.
197 * @return The event thread services object
199 inline EventThreadServices& GetEventThreadServices()
201 DALI_ASSERT_DEBUG(EventThreadServices::IsCoreRunning());
202 return mEventThreadServices;
206 * Get the event thread services object - used for sending messages to the scene graph
207 * Assert if called from the wrong thread
208 * This is intentionally inline for performance reasons.
210 * @return The event thread services object
212 inline const EventThreadServices& GetEventThreadServices() const
214 DALI_ASSERT_DEBUG(EventThreadServices::IsCoreRunning());
215 return mEventThreadServices;
219 EventThreadServices& mEventThreadServices;
220 Object* mTargetObject; ///< The object owns the constraint.
221 const SceneGraph::ConstraintBase* mSceneGraphConstraint;
222 SourceContainer mSources;
223 ObjectContainer mObservedObjects; // We don't observe the same object twice
224 Property::Index mTargetPropertyIndex;
225 RemoveAction mRemoveAction;
227 bool mApplied : 1; ///< Whether the constraint has been applied
228 bool mSourceDestroyed : 1; ///< Is set to true if any of our input source objects are destroyed
229 bool mIsPreConstraint : 1; ///< Is set to true if this constraint is run before transform.
232 } // namespace Internal
234 // Helpers for public-api forwarding methods
236 inline Internal::ConstraintBase& GetImplementation(Dali::Constraint& constraint)
238 DALI_ASSERT_ALWAYS(constraint && "Constraint handle is empty");
240 BaseObject& handle = constraint.GetBaseObject();
242 return static_cast<Internal::ConstraintBase&>(handle);
245 inline const Internal::ConstraintBase& GetImplementation(const Dali::Constraint& constraint)
247 DALI_ASSERT_ALWAYS(constraint && "Constraint handle is empty");
249 const BaseObject& handle = constraint.GetBaseObject();
251 return static_cast<const Internal::ConstraintBase&>(handle);
256 #endif // DALI_INTERNAL_ACTIVE_CONSTRAINT_BASE_H