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 > {};
44 typedef Dali::Vector<PropertyOwner*> PropertyOwnerContainer;
45 typedef PropertyOwnerContainer::Iterator PropertyOwnerIter;
48 * An abstract base class for Constraints.
49 * This can be used to constrain a property of a scene-object, after animations have been applied.
51 class ConstraintBase : public PropertyOwner::Observer
55 * Observer to determine when the constraint is no longer present
57 class LifecycleObserver
61 * Called shortly before the constraint is destroyed.
63 virtual void ObjectDestroyed() = 0;
67 * Virtual destructor, no deletion through this interface
69 virtual ~LifecycleObserver() = default;
74 typedef Dali::Constraint::RemoveAction RemoveAction;
78 * @param ownerContainer the properties to constraint
79 * @oparam removeAction perform when removed
81 ConstraintBase( PropertyOwnerContainer& ownerContainer, RemoveAction removeAction );
86 virtual ~ConstraintBase();
89 * Property resetter observes the lifecycle of this object
91 void AddLifecycleObserver( LifecycleObserver& observer )
93 mLifecycleObserver = &observer;
97 * Property resetter observers the lifecycle of this object
99 void RemoveLifecycleObserver( LifecycleObserver& observer )
101 mLifecycleObserver = nullptr;
105 * Initialize the constraint.
106 * This should by called by a scene-object, when the constraint is connected.
112 mDisconnected = false;
116 * @copydoc Dali::Constraint::SetRemoveAction()
118 void SetRemoveAction( RemoveAction action )
120 mRemoveAction = action;
124 * @copydoc Dali::Constraint::GetRemoveAction()
126 RemoveAction GetRemoveAction() const
128 return mRemoveAction;
132 * Constrain the associated scene object.
133 * @param[in] updateBufferIndex The current update buffer index.
135 virtual void Apply( BufferIndex updateBufferIndex ) = 0;
138 * Helper for internal test cases; only available for debug builds.
139 * @return The current number of Constraint instances in existence.
141 static uint32_t GetCurrentInstanceCount();
144 * Helper for internal test cases; only available for debug builds.
145 * @return The total number of Constraint instances created during the Dali core lifetime.
147 static uint32_t GetTotalInstanceCount();
152 * Helper to start observing property owners
154 void StartObservation()
156 const PropertyOwnerIter end = mObservedOwners.End();
157 for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
159 (*iter)->AddObserver( *this );
164 * Helper to stop observing property owners
166 void StopObservation()
168 const PropertyOwnerIter end = mObservedOwners.End();
169 for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
171 (*iter)->RemoveObserver( *this );
174 mObservedOwners.Clear();
178 * @copydoc PropertyOwner::Observer::PropertyOwnerConnected()
180 virtual void PropertyOwnerConnected( PropertyOwner& owner )
185 * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
187 virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
189 if ( !mDisconnected )
191 // Stop observing property owners
194 // Notification for derived class
197 mDisconnected = true;
202 * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
204 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
206 if ( !mDisconnected )
208 // Discard pointer to destroyed property owner. Otherwise StopObservation() would crash when trying to remove
209 //the constraint from the destroyed PropertyOwner's observers list
210 PropertyOwnerIter iter = std::find( mObservedOwners.Begin(), mObservedOwners.End(), &owner );
211 if( mObservedOwners.End() != iter )
213 mObservedOwners.Erase( iter );
216 // Stop observing the rest of property owners
219 // Notification for derived class
222 mDisconnected = true;
228 * Notify the derived class to disconnect from property owners
230 virtual void OnDisconnect() = 0;
234 RemoveAction mRemoveAction;
236 bool mFirstApply : 1;
237 bool mDisconnected : 1;
241 PropertyOwnerContainer mObservedOwners; ///< A set of pointers to each observed object. Not owned.
242 LifecycleObserver* mLifecycleObserver; ///< Resetter observers this object
245 static uint32_t mCurrentInstanceCount; ///< The current number of Constraint instances in existence.
246 static uint32_t mTotalInstanceCount; ///< The total number of Constraint instances created during the Dali core lifetime.
250 // Messages for ConstraintBase
252 inline void SetRemoveActionMessage( EventThreadServices& eventThreadServices, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction )
254 typedef MessageValue1< ConstraintBase, Dali::Constraint::RemoveAction > LocalType;
256 // Reserve some memory inside the message queue
257 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
259 // Construct message in the message queue memory; note that delete should not be called on the return value
260 new (slot) LocalType( &constraint, &ConstraintBase::SetRemoveAction, removeAction );
263 } // namespace SceneGraph
265 } // namespace Internal
269 #endif // DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H