use modern construct 'nullptr' instead of 'NULL' or '0'
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / update-proxy-impl.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/manager/update-proxy-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/update/manager/update-proxy-property-modifier.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 namespace
31 {
32
33 SceneGraph::Node* FindNodeInSceneGraph( uint32_t id, SceneGraph::Node& node )
34 {
35   SceneGraph::Node* matchingNode = nullptr;
36
37   if( node.mId == id )
38   {
39     matchingNode = &node;
40   }
41   else
42   {
43     for( auto&& i : node.GetChildren() )
44     {
45       matchingNode = FindNodeInSceneGraph( id, *i );
46       if( matchingNode )
47       {
48         break;
49       }
50     }
51   }
52
53   return matchingNode;
54 }
55
56 } // unnamed namespace
57
58 UpdateProxy::UpdateProxy( SceneGraph::UpdateManager& updateManager, SceneGraph::TransformManager& transformManager, SceneGraph::Node& rootNode )
59 : mNodeContainer(),
60   mLastCachedIdNodePair( { 0u, nullptr } ),
61   mCurrentBufferIndex( 0u ),
62   mUpdateManager( updateManager ),
63   mTransformManager( transformManager ),
64   mRootNode( rootNode ),
65   mPropertyModifier( nullptr )
66 {
67 }
68
69 UpdateProxy::~UpdateProxy()
70 {
71 }
72
73 bool UpdateProxy::GetPosition( uint32_t id, Vector3& position ) const
74 {
75   bool success = false;
76   const SceneGraph::Node* node = GetNodeWithId( id );
77   if( node )
78   {
79     position = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION );
80     success = true;
81   }
82   return success;
83 }
84
85 bool UpdateProxy::SetPosition( uint32_t id, const Vector3& position )
86 {
87   bool success = false;
88   SceneGraph::Node* node = GetNodeWithId( id );
89   if( node )
90   {
91     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION, position );
92     success = true;
93   }
94   return success;
95 }
96
97 bool UpdateProxy::BakePosition( uint32_t id, const Vector3& position )
98 {
99   bool success = false;
100   SceneGraph::Node* node = GetNodeWithId( id );
101   if( node )
102   {
103     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION, position );
104     success = true;
105   }
106   return success;
107 }
108
109 bool UpdateProxy::GetSize( uint32_t id, Vector3& size ) const
110 {
111   bool success = false;
112   const SceneGraph::Node* node = GetNodeWithId( id );
113   if( node )
114   {
115     size = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE );
116     success = true;
117   }
118   return success;
119 }
120
121 bool UpdateProxy::SetSize( uint32_t id, const Vector3& size )
122 {
123   bool success = false;
124   SceneGraph::Node* node = GetNodeWithId( id );
125   if( node )
126   {
127     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE, size );
128     success = true;
129   }
130   return success;
131 }
132
133 bool UpdateProxy::BakeSize( uint32_t id, const Vector3& size )
134 {
135   bool success = false;
136   SceneGraph::Node* node = GetNodeWithId( id );
137   if( node )
138   {
139     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE, size );
140     success = true;
141   }
142   return success;
143 }
144
145 bool UpdateProxy::GetPositionAndSize( uint32_t id, Vector3& position, Vector3& size ) const
146 {
147   bool success = false;
148   const SceneGraph::Node* node = GetNodeWithId( id );
149   if( node )
150   {
151     position = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION );
152     size = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE );
153     success = true;
154   }
155   return success;
156 }
157
158 bool UpdateProxy::GetScale( uint32_t id, Vector3& scale ) const
159 {
160   bool success = false;
161   const SceneGraph::Node* node = GetNodeWithId( id );
162   if( node )
163   {
164     scale = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE );
165     success = true;
166   }
167
168   return success;
169 }
170
171 bool UpdateProxy::SetScale( uint32_t id, const Vector3& scale )
172 {
173   bool success = false;
174   SceneGraph::Node* node = GetNodeWithId( id );
175   if( node )
176   {
177     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE, scale );
178     success = true;
179   }
180   return success;
181 }
182
183 bool UpdateProxy::BakeScale( uint32_t id, const Vector3& scale )
184 {
185   bool success = false;
186   SceneGraph::Node* node = GetNodeWithId( id );
187   if( node )
188   {
189     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE, scale );
190     success = true;
191   }
192   return success;
193 }
194
195 bool UpdateProxy::GetColor( uint32_t id, Vector4& color ) const
196 {
197   bool success = false;
198   SceneGraph::Node* node = GetNodeWithId( id );
199   if( node )
200   {
201     color = node->mColor.Get( mCurrentBufferIndex );
202     success = true;
203   }
204
205   return success;
206 }
207
208 bool UpdateProxy::SetColor( uint32_t id, const Vector4& color )
209 {
210   bool success = false;
211   SceneGraph::Node* node = GetNodeWithId( id );
212   if( node )
213   {
214     node->mColor.Set( mCurrentBufferIndex, color );
215     node->SetDirtyFlag( SceneGraph::NodePropertyFlags::COLOR );
216     AddResetter( *node, node->mColor );
217     success = true;
218   }
219   return success;
220 }
221
222 bool UpdateProxy::BakeColor( uint32_t id, const Vector4& color )
223 {
224   bool success = false;
225   SceneGraph::Node* node = GetNodeWithId( id );
226   if( node )
227   {
228     node->mColor.Bake( mCurrentBufferIndex, color );
229     success = true;
230   }
231   return success;
232 }
233
234 void UpdateProxy::NodeHierarchyChanged()
235 {
236   mLastCachedIdNodePair = { 0u, nullptr };
237   mNodeContainer.clear();
238   mPropertyModifier.reset();
239 }
240
241 SceneGraph::Node* UpdateProxy::GetNodeWithId( uint32_t id ) const
242 {
243   SceneGraph::Node* node = nullptr;
244
245   // Cache the last accessed node so we don't have to traverse
246   if( mLastCachedIdNodePair.node && mLastCachedIdNodePair.id == id )
247   {
248     node = mLastCachedIdNodePair.node;
249   }
250   else
251   {
252     // Find node in vector
253     for( auto&& pair : mNodeContainer )
254     {
255       if( pair.id == id )
256       {
257         node = pair.node;
258         mLastCachedIdNodePair = pair;
259         break;
260       }
261     }
262
263     if( ! node )
264     {
265       // Node not in vector, find in scene-graph
266       node = FindNodeInSceneGraph( id, mRootNode );
267       if( node )
268       {
269         mNodeContainer.push_back( { id, node } );
270         mLastCachedIdNodePair = *mNodeContainer.rbegin();
271       }
272     }
273   }
274
275   return node;
276 }
277
278 void UpdateProxy::AddResetter( SceneGraph::Node& node, SceneGraph::PropertyBase& propertyBase )
279 {
280   if( ! mPropertyModifier )
281   {
282     mPropertyModifier = PropertyModifierPtr( new PropertyModifier( mUpdateManager ) );
283   }
284   mPropertyModifier->AddResetter( node, propertyBase );
285 }
286
287 } // namespace Internal
288
289 } // namespace Dali