[Tizen] (FrameCallback) All values now local & baking of the value supported
[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 Vector3 UpdateProxy::GetPosition( unsigned int id ) const
69 {
70   Vector3 position;
71   const SceneGraph::Node* node = GetNodeWithId( id );
72   if( node )
73   {
74     position = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION );
75   }
76
77   return position;
78 }
79
80 void UpdateProxy::SetPosition( unsigned int id, const Vector3& position )
81 {
82   SceneGraph::Node* node = GetNodeWithId( id );
83   if( node )
84   {
85     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION, position );
86   }
87 }
88
89 void UpdateProxy::BakePosition( unsigned int id, const Vector3& position )
90 {
91   SceneGraph::Node* node = GetNodeWithId( id );
92   if( node )
93   {
94     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION, position );
95   }
96 }
97
98 const Vector3& UpdateProxy::GetSize( unsigned int id ) const
99 {
100   const SceneGraph::Node* node = GetNodeWithId( id );
101   if( node )
102   {
103     return mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE );
104   }
105
106   return Vector3::ZERO;
107 }
108
109 void UpdateProxy::SetSize( unsigned int id, const Vector3& size )
110 {
111   SceneGraph::Node* node = GetNodeWithId( id );
112   if( node )
113   {
114     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE, size );
115   }
116 }
117
118 void UpdateProxy::BakeSize( unsigned int id, const Vector3& size )
119 {
120   SceneGraph::Node* node = GetNodeWithId( id );
121   if( node )
122   {
123     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE, size );
124   }
125 }
126
127 void UpdateProxy::GetPositionAndSize( unsigned int id, Vector3& position, Vector3& size ) const
128 {
129   const SceneGraph::Node* node = GetNodeWithId( id );
130   if( node )
131   {
132     position = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_POSITION );
133     size = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SIZE );
134   }
135 }
136
137 Vector3 UpdateProxy::GetScale( unsigned int id ) const
138 {
139   Vector3 scale;
140   const SceneGraph::Node* node = GetNodeWithId( id );
141   if( node )
142   {
143     scale = mTransformManager.GetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE );
144   }
145
146   return scale;
147 }
148
149 void UpdateProxy::SetScale( unsigned int id, const Vector3& scale )
150 {
151   SceneGraph::Node* node = GetNodeWithId( id );
152   if( node )
153   {
154     mTransformManager.SetVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE, scale );
155   }
156 }
157
158 void UpdateProxy::BakeScale( unsigned int id, const Vector3& scale )
159 {
160   SceneGraph::Node* node = GetNodeWithId( id );
161   if( node )
162   {
163     mTransformManager.BakeVector3PropertyValue( node->mTransformId, SceneGraph::TRANSFORM_PROPERTY_SCALE, scale );
164   }
165 }
166
167 Vector4 UpdateProxy::GetColor( unsigned int id ) const
168 {
169   SceneGraph::Node* node = GetNodeWithId( id );
170   if( node )
171   {
172     return node->mColor.Get( mCurrentBufferIndex );
173   }
174
175   return Vector4::ZERO;
176 }
177
178 void UpdateProxy::SetColor( unsigned int id, const Vector4& color ) const
179 {
180   SceneGraph::Node* node = GetNodeWithId( id );
181   if( node )
182   {
183     node->mColor.Set( mCurrentBufferIndex, color );
184   }
185 }
186
187 void UpdateProxy::BakeColor( unsigned int id, const Vector4& color ) const
188 {
189   SceneGraph::Node* node = GetNodeWithId( id );
190   if( node )
191   {
192     node->mColor.Bake( mCurrentBufferIndex, color );
193   }
194 }
195
196 SceneGraph::Node* UpdateProxy::GetNodeWithId( unsigned int id ) const
197 {
198   SceneGraph::Node* node = NULL;
199
200   // Cache the last accessed node so we don't have to traverse
201   if( mLastCachedIdNodePair.node && mLastCachedIdNodePair.id == id )
202   {
203     node = mLastCachedIdNodePair.node;
204   }
205   else
206   {
207     // Find node in vector
208     for( auto&& pair : mNodeContainer )
209     {
210       if( pair.id == id )
211       {
212         node = pair.node;
213         mLastCachedIdNodePair = pair;
214         break;
215       }
216     }
217
218     if( ! node )
219     {
220       // Node not in vector, find in scene-graph
221       node = FindNodeInSceneGraph( id, mRootNode );
222       if( node )
223       {
224         mNodeContainer.push_back( { id, node } );
225         mLastCachedIdNodePair = *mNodeContainer.rbegin();
226       }
227     }
228   }
229
230   return node;
231 }
232
233 } // namespace Internal
234
235 } // namespace Dali