Merge "Clean up the code to build successfully on macOS" into devel/master
[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() = default;
70
71 bool UpdateProxy::GetPosition( uint32_t id, Vector3& position ) const
72 {
73   bool success = false;
74   const SceneGraph::Node* node = GetNodeWithId( id );
75   if( node )
76   {
77     position = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION );
78     success = true;
79   }
80   return success;
81 }
82
83 bool UpdateProxy::SetPosition( uint32_t id, const Vector3& position )
84 {
85   bool success = false;
86   SceneGraph::Node* node = GetNodeWithId( id );
87   if( node )
88   {
89     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION, position );
90     success = true;
91   }
92   return success;
93 }
94
95 bool UpdateProxy::BakePosition( uint32_t id, const Vector3& position )
96 {
97   bool success = false;
98   SceneGraph::Node* node = GetNodeWithId( id );
99   if( node )
100   {
101     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION, position );
102     success = true;
103   }
104   return success;
105 }
106
107 bool UpdateProxy::GetSize( uint32_t id, Vector3& size ) const
108 {
109   bool success = false;
110   const SceneGraph::Node* node = GetNodeWithId( id );
111   if( node )
112   {
113     size = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE );
114     success = true;
115   }
116   return success;
117 }
118
119 bool UpdateProxy::SetSize( uint32_t id, const Vector3& size )
120 {
121   bool success = false;
122   SceneGraph::Node* node = GetNodeWithId( id );
123   if( node )
124   {
125     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE, size );
126     success = true;
127   }
128   return success;
129 }
130
131 bool UpdateProxy::BakeSize( uint32_t id, const Vector3& size )
132 {
133   bool success = false;
134   SceneGraph::Node* node = GetNodeWithId( id );
135   if( node )
136   {
137     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE, size );
138     success = true;
139   }
140   return success;
141 }
142
143 bool UpdateProxy::GetPositionAndSize( uint32_t id, Vector3& position, Vector3& size ) const
144 {
145   bool success = false;
146   const SceneGraph::Node* node = GetNodeWithId( id );
147   if( node )
148   {
149     position = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION );
150     size = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE );
151     success = true;
152   }
153   return success;
154 }
155
156 bool UpdateProxy::GetScale( uint32_t id, Vector3& scale ) const
157 {
158   bool success = false;
159   const SceneGraph::Node* node = GetNodeWithId( id );
160   if( node )
161   {
162     scale = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE );
163     success = true;
164   }
165
166   return success;
167 }
168
169 bool UpdateProxy::SetScale( uint32_t id, const Vector3& scale )
170 {
171   bool success = false;
172   SceneGraph::Node* node = GetNodeWithId( id );
173   if( node )
174   {
175     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE, scale );
176     success = true;
177   }
178   return success;
179 }
180
181 bool UpdateProxy::BakeScale( uint32_t id, const Vector3& scale )
182 {
183   bool success = false;
184   SceneGraph::Node* node = GetNodeWithId( id );
185   if( node )
186   {
187     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE, scale );
188     success = true;
189   }
190   return success;
191 }
192
193 bool UpdateProxy::GetColor( uint32_t id, Vector4& color ) const
194 {
195   bool success = false;
196   SceneGraph::Node* node = GetNodeWithId( id );
197   if( node )
198   {
199     color = node->mColor.Get( mCurrentBufferIndex );
200     success = true;
201   }
202
203   return success;
204 }
205
206 bool UpdateProxy::SetColor( uint32_t id, const Vector4& color )
207 {
208   bool success = false;
209   SceneGraph::Node* node = GetNodeWithId( id );
210   if( node )
211   {
212     node->mColor.Set( mCurrentBufferIndex, color );
213     node->SetDirtyFlag( SceneGraph::NodePropertyFlags::COLOR );
214     AddResetter( *node, node->mColor );
215     success = true;
216   }
217   return success;
218 }
219
220 bool UpdateProxy::BakeColor( uint32_t id, const Vector4& color )
221 {
222   bool success = false;
223   SceneGraph::Node* node = GetNodeWithId( id );
224   if( node )
225   {
226     node->mColor.Bake( mCurrentBufferIndex, color );
227     success = true;
228   }
229   return success;
230 }
231
232 void UpdateProxy::NodeHierarchyChanged()
233 {
234   mLastCachedIdNodePair = { 0u, nullptr };
235   mNodeContainer.clear();
236   mPropertyModifier.reset();
237 }
238
239 SceneGraph::Node* UpdateProxy::GetNodeWithId( uint32_t id ) const
240 {
241   SceneGraph::Node* node = nullptr;
242
243   // Cache the last accessed node so we don't have to traverse
244   if( mLastCachedIdNodePair.node && mLastCachedIdNodePair.id == id )
245   {
246     node = mLastCachedIdNodePair.node;
247   }
248   else
249   {
250     // Find node in vector
251     for( auto&& pair : mNodeContainer )
252     {
253       if( pair.id == id )
254       {
255         node = pair.node;
256         mLastCachedIdNodePair = pair;
257         break;
258       }
259     }
260
261     if( ! node )
262     {
263       // Node not in vector, find in scene-graph
264       node = FindNodeInSceneGraph( id, mRootNode );
265       if( node )
266       {
267         mNodeContainer.push_back( { id, node } );
268         mLastCachedIdNodePair = *mNodeContainer.rbegin();
269       }
270     }
271   }
272
273   return node;
274 }
275
276 void UpdateProxy::AddResetter( SceneGraph::Node& node, SceneGraph::PropertyBase& propertyBase )
277 {
278   if( ! mPropertyModifier )
279   {
280     mPropertyModifier = PropertyModifierPtr( new PropertyModifier( mUpdateManager ) );
281   }
282   mPropertyModifier->AddResetter( node, propertyBase );
283 }
284
285 } // namespace Internal
286
287 } // namespace Dali