1 #ifndef DALI_INTERNAL_SCENEGRAPH_PROPERTY_RESETTER_H
2 #define DALI_INTERNAL_SCENEGRAPH_PROPERTY_RESETTER_H
5 * Copyright (c) 2018 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.
20 #include <dali/internal/update/animation/scene-graph-animator.h>
21 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
22 #include <dali/internal/update/animation/property-accessor.h>
23 #include <dali/internal/update/animation/property-component-accessor.h>
24 #include <dali/internal/update/common/property-owner.h>
34 * Class to reset the watched property to it's base value. Used by UpdateManager to
35 * reset animating and constrained properties. The derived classes AnimatorResetter and
36 * ConstraintResetter are designed to match the lifecycle of the SceneGraph::Animator
37 * and SceneGraph::Constraint respectively.
39 class PropertyResetterBase : public PropertyOwner::Observer
45 virtual ~PropertyResetterBase()
47 if( mPropertyOwner != nullptr )
49 mPropertyOwner->RemoveObserver(*this);
56 * Watches the property owner to track if it's disconnected or not.
60 mPropertyOwner->AddObserver(*this);
64 * Reset the property to it's base value if the property owner is still alive and on stage
65 * @param[in] updateBufferIndex the current buffer index
67 void ResetToBaseValue( BufferIndex updateBufferIndex )
69 if( mPropertyOwner != nullptr && !mDisconnected )
71 mBaseProperty->ResetToBaseValue( updateBufferIndex );
76 * Called when mPropertyOwner is connected to the scene graph.
78 * Note, this resetter object may be created after the property owner has been connected
79 * to the stage, so we track disconnection and re-connection instead of connection
81 * @param[in] owner The property owner
83 virtual void PropertyOwnerConnected( PropertyOwner& owner ) override
85 mDisconnected = false;
89 * Called when mPropertyOwner is disconnected from the scene graph.
90 * @param[in] bufferIndex the current buffer index
91 * @param[in] owner The property owner
93 virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner ) override
99 * Called shortly before the propertyOwner is destroyed
100 * @param[in] owner The property owner
102 virtual void PropertyOwnerDestroyed( PropertyOwner& owner ) override
104 mDisconnected = true;
105 mPropertyOwner = nullptr;
107 // Don't need to wait another frame as the property is being destroyed
112 * Determine if the resetter has finished.
114 * If an animation or constraint stops, then we need to reset the
115 * property in the next frame as well to ensure both property values
116 * are set appropriately.
118 * @return true if the resetter has finished.
120 virtual bool IsFinished()
122 bool finished = mRunning <= 0;
135 * @param[in] propertyOwner The property owner storing the base property
136 * @param[in] baseProperty The base property
138 PropertyResetterBase( PropertyOwner* propertyOwner,
139 PropertyBase* baseProperty )
140 : mPropertyOwner( propertyOwner ),
141 mBaseProperty( baseProperty ),
143 mDisconnected( false )
147 PropertyOwner* mPropertyOwner; ///< The property owner
148 PropertyBase* mBaseProperty; ///< The base property being animated or constrained
149 int mRunning; ///< 2 if running, 1 if aging, 0 if stopped
150 bool mDisconnected; ///< True if the property owner has been disconnected
155 * Specialization of Resetter based on templated modifier type (either Constraint or Animator)
157 template< typename ModifierType>
158 class Resetter : public PropertyResetterBase, public ModifierType::LifecycleObserver
164 * @param[in] propertyOwner The owner of the property
165 * @param[in] baseProperty The property being animated
166 * @param[in] modifier The scene-graph animator/constraint of the property
167 * @return the new property resetter
169 static PropertyResetterBase* New( const PropertyOwner& propertyOwner,
170 const PropertyBase& baseProperty,
171 const ModifierType& modifier )
173 return new Resetter<ModifierType>( const_cast<PropertyOwner*>( &propertyOwner ),
174 const_cast<PropertyBase*>( &baseProperty ),
175 const_cast<ModifierType*>( &modifier ) );
179 * Virtual destructor.
183 // Disconnect from modifier object. Although this resetter should match the lifecycle of the modifier object,
184 // there are situations where it is deleted first (e.g. if the property owner is destroyed)
187 mModifier->RemoveLifecycleObserver(*this);
194 * @param[in] propertyOwner The owner of the property
195 * @param[in] baseProperty The property being animated
196 * @param[in] modifier The scene-graph animator/constraint of the property
198 Resetter( PropertyOwner* propertyOwner,
199 PropertyBase* baseProperty,
200 ModifierType* modifier )
201 : PropertyResetterBase( propertyOwner, baseProperty ),
202 mModifier( modifier )
204 // Track the lifecycle of the modifying object
205 mModifier->AddLifecycleObserver(*this);
209 * The Animator or Constraint is destroyed
211 virtual void ObjectDestroyed() override
213 // When the modifier is destroyed, reduce the running value to ensure we stay alive for
214 // another frame to reset the other buffer.
219 ModifierType* mModifier;
223 typedef Resetter<SceneGraph::AnimatorBase> AnimatorResetter;
224 typedef Resetter<SceneGraph::ConstraintBase> ConstraintResetter;
228 } // namespace SceneGraph
230 } // namespace Internal
234 #endif //DALI_INTERNAL_SCENEGRAPH_PROPERTY_RESETTER_H