7f27624aa0b78b119a84bb64f9d03eb27124594a
[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) 2022 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       --mActive;
84
85       mNode->mVisible.ResetToBaseValue(updateBufferIndex);
86       mNode->mColor.ResetToBaseValue(updateBufferIndex);
87       mNode->mUpdateAreaHint.ResetToBaseValue(updateBufferIndex);
88     }
89   };
90
91   /**
92    * Called when the node is connected to the scene graph.
93    *
94    * Note, this resetter object may be created after the node has been connected
95    * to the scene graph, so we track disconnection and re-connection instead of connection
96    *
97    * @param[in] owner The property owner
98    */
99   void PropertyOwnerConnected(PropertyOwner& owner) override
100   {
101     mDisconnected = false;
102     mActive       = ACTIVE;
103
104     mNode->mVisible.MarkAsDirty();
105     mNode->mColor.MarkAsDirty();
106     mNode->mUpdateAreaHint.MarkAsDirty();
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     mNode->mVisible.MarkAsDirty();
161     mNode->mColor.MarkAsDirty();
162     mNode->mUpdateAreaHint.MarkAsDirty();
163   }
164
165   Node*  mNode;         ///< The node that owns the properties
166   int8_t mActive;       ///< 2 if active, 1 if aging, 0 if stopped
167   bool   mDisconnected; ///< True if the node has been disconnected
168 };
169
170 } // namespace SceneGraph
171
172 } // namespace Internal
173
174 } // namespace Dali
175
176 #endif //DALI_INTERNAL_SCENEGRAPH_NODE_RESETTER_H