Add a node resetter that resets the animatable properties for two frames after node...
[platform/core/uifw/dali-core.git] / dali / internal / update / common / node-resetter.h
1 #ifndef DALI_INTERNAL_SCENEGRAPH_NODE_RESETTER_H
2 #define DALI_INTERNAL_SCENEGRAPH_NODE_RESETTER_H
3
4 /*
5  * Copyright (c) 2021 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 // EXTERNAL INCLDUES
21 #include <cstdint> // int8_t
22
23 #include <dali/internal/update/animation/property-accessor.h>
24 #include <dali/internal/update/animation/property-component-accessor.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/nodes/node.h>
27
28 namespace Dali
29 {
30 namespace Internal
31 {
32 namespace SceneGraph
33 {
34 /**
35  * Class to reset the node's properties to their base values. Used by UpdateManager
36  * to reset the node properties after the node is connected to the scene graph.
37  */
38 class NodeResetter : public PropertyOwner::Observer
39 {
40 public:
41   /**
42    * New method.
43    * @param[in] node  The node
44    * @return the new node resetter
45    */
46   static NodeResetter* New(const Node& node)
47   {
48     return new NodeResetter(const_cast<Node*>(&node));
49   }
50
51   /**
52    * Virtual Destructor
53    */
54   ~NodeResetter() override
55   {
56     if(mNode != nullptr)
57     {
58       mNode->RemoveObserver(*this);
59     }
60   }
61
62   /**
63    * Initialize.
64    *
65    * Watches the node to track if it's disconnected or not.
66    */
67   void Initialize()
68   {
69     mNode->AddObserver(*this);
70   }
71
72   /**
73    * Reset the node properties to their base values if the node is still alive and on stage
74    * @param[in] updateBufferIndex the current buffer index
75    */
76   void ResetToBaseValue(BufferIndex updateBufferIndex)
77   {
78     if(mNode != nullptr && mActive)
79     {
80       // Start aging the node properties.
81       // We need to reset the node properties for two frames to ensure both
82       // property values are set appropriately.
83       //      if(mDisconnected)
84       {
85         --mActive;
86       }
87
88       mNode->mVisible.ResetToBaseValue(updateBufferIndex);
89       mNode->mCulled.ResetToBaseValue(updateBufferIndex);
90       mNode->mColor.ResetToBaseValue(updateBufferIndex);
91       mNode->mUpdateSizeHint.ResetToBaseValue(updateBufferIndex);
92     }
93   };
94
95   /**
96    * Called when the node is connected to the scene graph.
97    *
98    * Note, this resetter object may be created after the node has been connected
99    * to the scene graph, so we track disconnection and re-connection instead of connection
100    *
101    * @param[in] owner The property owner
102    */
103   void PropertyOwnerConnected(PropertyOwner& owner) override
104   {
105     mDisconnected = false;
106     mActive       = ACTIVE;
107   }
108
109   /**
110    * Called when mPropertyOwner is disconnected from the scene graph.
111    * @param[in] bufferIndex the current buffer index
112    * @param[in] owner The property owner
113    */
114   void PropertyOwnerDisconnected(BufferIndex bufferIndex, PropertyOwner& owner) override
115   {
116     mDisconnected = true;
117   }
118
119   /**
120    * Called shortly before the propertyOwner is destroyed
121    * @param[in] owner The property owner
122    */
123   void PropertyOwnerDestroyed(PropertyOwner& owner) override
124   {
125     mDisconnected = true;
126     mNode         = nullptr;
127
128     // Don't need to wait another frame as the properties are being destroyed
129     mActive = STOPPED;
130   }
131
132   /**
133    * Determine if the node resetter has finished.
134    *
135    * @return true if the node resetter has finished.
136    */
137   virtual bool IsFinished()
138   {
139     return mActive <= STOPPED;
140   }
141
142 protected:
143   enum
144   {
145     STOPPED = 0,
146     AGING   = 1,
147     ACTIVE  = 2,
148   };
149
150   /**
151    * Constructor
152    *
153    * @param[in] node The node storing the base properties
154    */
155   NodeResetter(Node* node)
156   : mNode(node),
157     mActive(ACTIVE),
158     mDisconnected(false)
159   {
160   }
161
162   Node*  mNode;         ///< The node that owns the properties
163   int8_t mActive;       ///< 2 if active, 1 if aging, 0 if stopped
164   bool   mDisconnected; ///< True if the node has been disconnected
165 };
166
167 } // namespace SceneGraph
168
169 } // namespace Internal
170
171 } // namespace Dali
172
173 #endif //DALI_INTERNAL_SCENEGRAPH_NODE_RESETTER_H