1 #ifndef DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H
2 #define DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H
5 * Copyright (c) 2019 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/public-api/animation/constraint.h>
23 #include <dali/public-api/common/dali-common.h>
24 #include <dali/internal/common/message.h>
25 #include <dali/internal/event/common/event-thread-services.h>
26 #include <dali/internal/update/common/animatable-property.h>
27 #include <dali/internal/update/common/property-owner.h>
28 #include <dali/internal/update/common/scene-graph-buffers.h>
29 #include <dali/internal/update/animation/scene-graph-constraint-declarations.h>
37 // value types used by messages
38 template <> struct ParameterType< Dali::Constraint::RemoveAction >
39 : public BasicType< Dali::Constraint::RemoveAction > {};
43 using PropertyOwnerContainer = Dali::Vector<PropertyOwner*>;
44 using PropertyOwnerIter = PropertyOwnerContainer::Iterator;
47 * An abstract base class for Constraints.
48 * This can be used to constrain a property of a scene-object, after animations have been applied.
50 class ConstraintBase : public PropertyOwner::Observer
54 * Observer to determine when the constraint is no longer present
56 class LifecycleObserver
60 * Called shortly before the constraint is destroyed.
62 virtual void ObjectDestroyed() = 0;
66 * Virtual destructor, no deletion through this interface
68 virtual ~LifecycleObserver() = default;
72 using RemoveAction = Dali::Constraint::RemoveAction;
76 * @param ownerContainer the properties to constraint
77 * @oparam removeAction perform when removed
79 ConstraintBase( PropertyOwnerContainer& ownerContainer, RemoveAction removeAction );
84 ~ConstraintBase() override;
87 * Property resetter observes the lifecycle of this object
89 void AddLifecycleObserver( LifecycleObserver& observer )
91 mLifecycleObserver = &observer;
95 * Property resetter observers the lifecycle of this object
97 void RemoveLifecycleObserver( LifecycleObserver& observer )
99 mLifecycleObserver = nullptr;
103 * Initialize the constraint.
104 * This should by called by a scene-object, when the constraint is connected.
110 mDisconnected = false;
114 * @copydoc Dali::Constraint::SetRemoveAction()
116 void SetRemoveAction( RemoveAction action )
118 mRemoveAction = action;
122 * @copydoc Dali::Constraint::GetRemoveAction()
124 RemoveAction GetRemoveAction() const
126 return mRemoveAction;
130 * Constrain the associated scene object.
131 * @param[in] updateBufferIndex The current update buffer index.
133 virtual void Apply( BufferIndex updateBufferIndex ) = 0;
136 * Helper for internal test cases; only available for debug builds.
137 * @return The current number of Constraint instances in existence.
139 static uint32_t GetCurrentInstanceCount();
142 * Helper for internal test cases; only available for debug builds.
143 * @return The total number of Constraint instances created during the Dali core lifetime.
145 static uint32_t GetTotalInstanceCount();
150 * Helper to start observing property owners
152 void StartObservation()
154 const PropertyOwnerIter end = mObservedOwners.End();
155 for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
157 (*iter)->AddObserver( *this );
162 * Helper to stop observing property owners
164 void StopObservation()
166 const PropertyOwnerIter end = mObservedOwners.End();
167 for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
169 (*iter)->RemoveObserver( *this );
172 mObservedOwners.Clear();
176 * @copydoc PropertyOwner::Observer::PropertyOwnerConnected()
178 void PropertyOwnerConnected( PropertyOwner& owner ) override
183 * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
185 void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner ) override
187 if ( !mDisconnected )
189 // Stop observing property owners
192 // Notification for derived class
195 mDisconnected = true;
200 * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
202 void PropertyOwnerDestroyed( PropertyOwner& owner ) override
204 if ( !mDisconnected )
206 // Discard pointer to destroyed property owner. Otherwise StopObservation() would crash when trying to remove
207 //the constraint from the destroyed PropertyOwner's observers list
208 PropertyOwnerIter iter = std::find( mObservedOwners.Begin(), mObservedOwners.End(), &owner );
209 if( mObservedOwners.End() != iter )
211 mObservedOwners.Erase( iter );
214 // Stop observing the rest of property owners
217 // Notification for derived class
220 mDisconnected = true;
226 * Notify the derived class to disconnect from property owners
228 virtual void OnDisconnect() = 0;
232 RemoveAction mRemoveAction;
234 bool mFirstApply : 1;
235 bool mDisconnected : 1;
239 PropertyOwnerContainer mObservedOwners; ///< A set of pointers to each observed object. Not owned.
240 LifecycleObserver* mLifecycleObserver; ///< Resetter observers this object
243 static uint32_t mCurrentInstanceCount; ///< The current number of Constraint instances in existence.
244 static uint32_t mTotalInstanceCount; ///< The total number of Constraint instances created during the Dali core lifetime.
248 // Messages for ConstraintBase
250 inline void SetRemoveActionMessage( EventThreadServices& eventThreadServices, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction )
252 using LocalType = MessageValue1<ConstraintBase, Dali::Constraint::RemoveAction>;
254 // Reserve some memory inside the message queue
255 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
257 // Construct message in the message queue memory; note that delete should not be called on the return value
258 new (slot) LocalType( &constraint, &ConstraintBase::SetRemoveAction, removeAction );
261 } // namespace SceneGraph
263 } // namespace Internal
267 #endif // DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H