[4.0] Changed Update to reset only target properties each frame
[platform/core/uifw/dali-core.git] / dali / internal / update / common / property-resetter.h
1 #ifndef DALI_INTERNAL_SCENEGRAPH_PROPERTY_RESETTER_H
2 #define DALI_INTERNAL_SCENEGRAPH_PROPERTY_RESETTER_H
3
4 /*
5  * Copyright (c) 2018 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 #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>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 namespace SceneGraph
31 {
32
33 /**
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.
38  */
39 class PropertyResetterBase : public PropertyOwner::Observer
40 {
41 public:
42   /**
43    * Virtual Destructor
44    */
45   virtual ~PropertyResetterBase()
46   {
47     if( mPropertyOwner != nullptr )
48     {
49       mPropertyOwner->RemoveObserver(*this);
50     }
51   }
52
53   /**
54    * Initialize.
55    *
56    * Watches the property owner to track if it's disconnected or not.
57    */
58   void Initialize()
59   {
60     mPropertyOwner->AddObserver(*this);
61   }
62
63   /**
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
66    */
67   void ResetToBaseValue( BufferIndex updateBufferIndex )
68   {
69     if( mPropertyOwner != nullptr && !mDisconnected )
70     {
71       mBaseProperty->ResetToBaseValue( updateBufferIndex );
72     }
73   };
74
75   /**
76    * Called when mPropertyOwner is connected to the scene graph.
77    *
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
80    *
81    * @param[in] owner The property owner
82    */
83   virtual void PropertyOwnerConnected( PropertyOwner& owner ) override
84   {
85     mDisconnected = false;
86   }
87
88   /**
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
92    */
93   virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner ) override
94   {
95     mDisconnected = true;
96   }
97
98   /**
99    * Called shortly before the propertyOwner is destroyed
100    * @param[in] owner The property owner
101    */
102   virtual void PropertyOwnerDestroyed( PropertyOwner& owner ) override
103   {
104     mDisconnected = true;
105     mPropertyOwner = nullptr;
106
107     // Don't need to wait another frame as the property is being destroyed
108     mRunning = 0;
109   }
110
111   /**
112    * Determine if the resetter has finished.
113    *
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.
117    *
118    * @return true if the resetter has finished.
119    */
120   virtual bool IsFinished()
121   {
122     bool finished = mRunning <= 0;
123     if( mRunning == 1 )
124     {
125       mRunning = 0;
126     }
127     return finished;
128   }
129
130 protected:
131
132   /**
133    * Constructor
134    *
135    * @param[in] propertyOwner The property owner storing the base property
136    * @param[in] baseProperty The base property
137    */
138   PropertyResetterBase( PropertyOwner* propertyOwner,
139                         PropertyBase* baseProperty )
140   : mPropertyOwner( propertyOwner ),
141     mBaseProperty( baseProperty ),
142     mRunning( 2 ),
143     mDisconnected( false )
144   {
145   }
146
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
151 };
152
153
154 /**
155  * Specialization of Resetter based on templated modifier type (either Constraint or Animator)
156  */
157 template< typename ModifierType>
158 class Resetter : public PropertyResetterBase, public ModifierType::LifecycleObserver
159 {
160 public:
161
162   /**
163    * New method.
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
168    */
169   static PropertyResetterBase* New( const PropertyOwner& propertyOwner,
170                                     const PropertyBase& baseProperty,
171                                     const ModifierType& modifier )
172   {
173     return new Resetter<ModifierType>( const_cast<PropertyOwner*>( &propertyOwner ),
174                                        const_cast<PropertyBase*>( &baseProperty ),
175                                        const_cast<ModifierType*>( &modifier ) );
176   }
177
178   /**
179    * Virtual destructor.
180    */
181   virtual ~Resetter()
182   {
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)
185     if( mModifier )
186     {
187       mModifier->RemoveLifecycleObserver(*this);
188     }
189   }
190
191 private:
192   /**
193    * Constructor
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
197    */
198   Resetter( PropertyOwner* propertyOwner,
199             PropertyBase* baseProperty,
200             ModifierType* modifier )
201   : PropertyResetterBase( propertyOwner, baseProperty ),
202     mModifier( modifier )
203   {
204     // Track the lifecycle of the modifying object
205     mModifier->AddLifecycleObserver(*this);
206   }
207
208   /**
209    * The Animator or Constraint is destroyed
210    */
211   virtual void ObjectDestroyed() override
212   {
213     // When the modifier is destroyed, reduce the running value to ensure we stay alive for
214     // another frame to reset the other buffer.
215     --mRunning;
216     mModifier = nullptr;
217   }
218
219   ModifierType* mModifier;
220 };
221
222
223 typedef Resetter<SceneGraph::AnimatorBase> AnimatorResetter;
224 typedef Resetter<SceneGraph::ConstraintBase> ConstraintResetter;
225
226
227
228 } // namespace SceneGraph
229
230 } // namespace Internal
231
232 } // namespace Dali
233
234 #endif //DALI_INTERNAL_SCENEGRAPH_PROPERTY_RESETTER_H