Merge "(FrameCallback) All values now local & baking of the value supported" into...
[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 namespace Dali
22 {
23
24 namespace Internal
25 {
26
27 namespace
28 {
29
30 SceneGraph::Node* FindNodeInSceneGraph( unsigned int id, SceneGraph::Node& node )
31 {
32   SceneGraph::Node* matchingNode = NULL;
33
34   if( node.mId == id )
35   {
36     matchingNode = &node;
37   }
38   else
39   {
40     for( auto&& i : node.GetChildren() )
41     {
42       matchingNode = FindNodeInSceneGraph( id, *i );
43       if( matchingNode )
44       {
45         break;
46       }
47     }
48   }
49
50   return matchingNode;
51 }
52
53 } // unnamed namespace
54
55 UpdateProxy::UpdateProxy( SceneGraph::TransformManager& transformManager, SceneGraph::Node& rootNode )
56 : mNodeContainer(),
57   mLastCachedIdNodePair( { 0u, NULL } ),
58   mCurrentBufferIndex( 0u ),
59   mTransformManager( transformManager ),
60   mRootNode( rootNode )
61 {
62 }
63
64 UpdateProxy::~UpdateProxy()
65 {
66 }
67
68 bool UpdateProxy::GetPosition( unsigned int id, Vector3& position ) const
69 {
70   bool success = false;
71   const SceneGraph::Node* node = GetNodeWithId( id );
72   if( node )
73   {
74     position = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION );
75     success = true;
76   }
77   return success;
78 }
79
80 bool UpdateProxy::SetPosition( unsigned int id, const Vector3& position )
81 {
82   bool success = false;
83   SceneGraph::Node* node = GetNodeWithId( id );
84   if( node )
85   {
86     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION, position );
87     success = true;
88   }
89   return success;
90 }
91
92 bool UpdateProxy::BakePosition( unsigned int id, const Vector3& position )
93 {
94   bool success = false;
95   SceneGraph::Node* node = GetNodeWithId( id );
96   if( node )
97   {
98     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION, position );
99     success = true;
100   }
101   return success;
102 }
103
104 bool UpdateProxy::GetSize( unsigned int id, Vector3& size ) const
105 {
106   bool success = false;
107   const SceneGraph::Node* node = GetNodeWithId( id );
108   if( node )
109   {
110     size = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE );
111     success = true;
112   }
113   return success;
114 }
115
116 bool UpdateProxy::SetSize( unsigned int id, const Vector3& size )
117 {
118   bool success = false;
119   SceneGraph::Node* node = GetNodeWithId( id );
120   if( node )
121   {
122     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE, size );
123     success = true;
124   }
125   return success;
126 }
127
128 bool UpdateProxy::BakeSize( unsigned int id, const Vector3& size )
129 {
130   bool success = false;
131   SceneGraph::Node* node = GetNodeWithId( id );
132   if( node )
133   {
134     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE, size );
135     success = true;
136   }
137   return success;
138 }
139
140 bool UpdateProxy::GetPositionAndSize( unsigned int id, Vector3& position, Vector3& size ) const
141 {
142   bool success = false;
143   const SceneGraph::Node* node = GetNodeWithId( id );
144   if( node )
145   {
146     position = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION );
147     size = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE );
148     success = true;
149   }
150   return success;
151 }
152
153 bool UpdateProxy::GetScale( unsigned int id, Vector3& scale ) const
154 {
155   bool success = false;
156   const SceneGraph::Node* node = GetNodeWithId( id );
157   if( node )
158   {
159     scale = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE );
160     success = true;
161   }
162
163   return success;
164 }
165
166 bool UpdateProxy::SetScale( unsigned int id, const Vector3& scale )
167 {
168   bool success = false;
169   SceneGraph::Node* node = GetNodeWithId( id );
170   if( node )
171   {
172     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE, scale );
173     success = true;
174   }
175   return success;
176 }
177
178 bool UpdateProxy::BakeScale( unsigned int id, const Vector3& scale )
179 {
180   bool success = false;
181   SceneGraph::Node* node = GetNodeWithId( id );
182   if( node )
183   {
184     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE, scale );
185     success = true;
186   }
187   return success;
188 }
189
190 bool UpdateProxy::GetColor( unsigned int id, Vector4& color ) const
191 {
192   bool success = false;
193   SceneGraph::Node* node = GetNodeWithId( id );
194   if( node )
195   {
196     color = node->mColor.Get( mCurrentBufferIndex );
197     success = true;
198   }
199
200   return success;
201 }
202
203 bool UpdateProxy::SetColor( unsigned int id, const Vector4& color ) const
204 {
205   bool success = false;
206   SceneGraph::Node* node = GetNodeWithId( id );
207   if( node )
208   {
209     node->mColor.Set( mCurrentBufferIndex, color );
210     success = true;
211   }
212   return success;
213 }
214
215 bool UpdateProxy::BakeColor( unsigned int id, const Vector4& color ) const
216 {
217   bool success = false;
218   SceneGraph::Node* node = GetNodeWithId( id );
219   if( node )
220   {
221     node->mColor.Bake( mCurrentBufferIndex, color );
222     success = true;
223   }
224   return success;
225 }
226
227 void UpdateProxy::NodeHierarchyChanged()
228 {
229   mLastCachedIdNodePair = { 0u, NULL };
230   mNodeContainer.clear();
231 }
232
233 SceneGraph::Node* UpdateProxy::GetNodeWithId( unsigned int id ) const
234 {
235   SceneGraph::Node* node = NULL;
236
237   // Cache the last accessed node so we don't have to traverse
238   if( mLastCachedIdNodePair.node && mLastCachedIdNodePair.id == id )
239   {
240     node = mLastCachedIdNodePair.node;
241   }
242   else
243   {
244     // Find node in vector
245     for( auto&& pair : mNodeContainer )
246     {
247       if( pair.id == id )
248       {
249         node = pair.node;
250         mLastCachedIdNodePair = pair;
251         break;
252       }
253     }
254
255     if( ! node )
256     {
257       // Node not in vector, find in scene-graph
258       node = FindNodeInSceneGraph( id, mRootNode );
259       if( node )
260       {
261         mNodeContainer.push_back( { id, node } );
262         mLastCachedIdNodePair = *mNodeContainer.rbegin();
263       }
264     }
265   }
266
267   return node;
268 }
269
270 } // namespace Internal
271
272 } // namespace Dali