Merge "Follow the include-order coding conventions" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / model-actor-factory-impl.cpp
1 /*
2  * Copyright (c) 2014 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/event/actors/model-actor-factory-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/modeling/entity.h>
23 #include <dali/internal/event/actors/mesh-actor-impl.h>
24 #include <dali/internal/event/modeling/model-impl.h>
25 #include <dali/internal/event/modeling/model-data-impl.h>
26 #include <dali/internal/event/modeling/mesh-impl.h>
27 #include <dali/internal/event/animation/animator-connector.h>
28
29
30 namespace Dali
31 {
32 using Dali::Entity;
33 using Internal::MeshIter;
34 using Internal::ActorPtr;
35
36 namespace Internal
37 {
38
39 ActorPtr ModelActorFactory::BuildActorTree(Dali::Model& model, const std::string& entityName)
40 {
41   Dali::Entity entity;
42   ActorPtr actorPtr;
43
44   Internal::ModelDataPtr modelData(GetImplementation(model).GetModelData());
45
46   if (modelData)
47   {
48     Dali::Entity root = modelData->GetRootEntity();
49     if (entityName.empty())
50     {
51       entity = root;
52     }
53     else
54     {
55       entity = root.Find(entityName);
56     }
57   }
58
59   if(entity)
60   {
61     actorPtr = RecurseNew(modelData, entity);
62   }
63   else
64   {
65     DALI_LOG_INFO(Debug::Filter::gModel, Debug::General, "Entity %s not found\n", entityName.c_str());
66   }
67
68   if(actorPtr)
69   {
70     Vector4 bounds(entity.GetUpperBounds() - entity.GetLowerBounds());
71     Vector3 initialVolume(bounds.x, bounds.y, bounds.z);
72
73     actorPtr->SetInitialVolume(initialVolume);
74     actorPtr->SetTransmitGeometryScaling(true);
75
76     BindBonesToMeshActors(actorPtr, actorPtr);
77   }
78   return actorPtr;
79 }
80
81
82 ActorPtr ModelActorFactory::RecurseNew(ModelDataPtr modelData, Dali::Entity entity)
83 {
84   ActorPtr actorPtr;
85   if(entity.HasMeshes())
86   {
87     actorPtr = Internal::MeshActor::New(modelData, entity);
88   }
89   else
90   {
91     // Root with no mesh, or bone/joint actor (with no mesh)
92     actorPtr = Internal::Actor::New();
93     actorPtr->SetName(entity.GetName());
94     Matrix transform(entity.GetTransformMatrix());
95     Vector3 position;
96     Quaternion rotation;
97     Vector3 scale;
98     transform.GetTransformComponents(position, rotation, scale);
99     actorPtr->SetPosition(position);
100     actorPtr->SetRotation(rotation);
101     actorPtr->SetScale(scale);
102   }
103
104   actorPtr->SetParentOrigin(ParentOrigin::CENTER);
105   actorPtr->SetAnchorPoint(AnchorPoint::CENTER);
106
107   if (entity.HasChildren())
108   {
109     for (EntityConstIter iter = entity.GetChildren().begin(); iter != entity.GetChildren().end(); ++iter)
110     {
111       Dali::Entity childEntity = (*iter);
112       ActorPtr child = RecurseNew(modelData, childEntity);
113       actorPtr->Add(*child.Get());
114     }
115   }
116
117   return actorPtr;
118 }
119
120 void ModelActorFactory::BindBonesToMeshActors(ActorPtr rootActor, ActorPtr actorPtr)
121 {
122   MeshActor* meshActor = dynamic_cast<MeshActor*>(actorPtr.Get());
123   if(meshActor)
124   {
125     meshActor->BindBonesToMesh(rootActor);
126   }
127
128   // Descend to all child actors, not just mesh actors
129   const ActorContainer& children = actorPtr->GetChildren();
130   for ( ActorConstIter iter = children.begin(); iter != children.end(); ++iter)
131   {
132     ActorPtr childActor = const_cast<Actor*>(&GetImplementation(*iter));
133     BindBonesToMeshActors(rootActor, childActor);
134   }
135 }
136
137
138
139 AnimationPtr ModelActorFactory::BuildAnimation(
140   Model& model,
141   Actor& rootActor,
142   size_t index)
143 {
144   AnimationPtr animation;
145   Internal::ModelDataPtr modelData(model.GetModelData());
146
147   if (modelData)
148   {
149     if (index >= modelData->NumberOfAnimationMaps())
150     {
151       DALI_LOG_ERROR("Invalid animation index\n");
152     }
153     else
154     {
155       const ModelAnimationMap* animationData(modelData->GetAnimationMap(index));
156       if( animationData != NULL )
157       {
158         animation = CreateAnimation(rootActor, animationData, AlphaFunctions::Linear, animationData->duration);
159       }
160     }
161   }
162   return animation;
163 }
164
165 AnimationPtr ModelActorFactory::BuildAnimation(
166   Model& model,
167   Actor& rootActor,
168   size_t index,
169   float durationSeconds)
170 {
171   AnimationPtr animation;
172   Internal::ModelDataPtr modelData(model.GetModelData());
173
174   if (modelData)
175   {
176     if (index >= modelData->NumberOfAnimationMaps())
177     {
178       DALI_LOG_ERROR("Invalid animation index\n");
179     }
180     else
181     {
182       const ModelAnimationMap* animationData(modelData->GetAnimationMap(index));
183       if( animationData != NULL )
184       {
185         animation = CreateAnimation(rootActor, animationData, AlphaFunctions::Linear, durationSeconds);
186       }
187     }
188   }
189   return animation;
190 }
191
192 AnimationPtr ModelActorFactory::BuildAnimation(
193   Model& model,
194   Actor& rootActor,
195   size_t index,
196   AlphaFunction alpha)
197 {
198   AnimationPtr animation;
199   Internal::ModelDataPtr modelData(model.GetModelData());
200
201   if (modelData)
202   {
203     if (index >= modelData->NumberOfAnimationMaps())
204     {
205       DALI_LOG_ERROR("Invalid animation index\n");
206     }
207     else
208     {
209       const ModelAnimationMap* animationData(modelData->GetAnimationMap(index));
210       if( animationData != NULL )
211       {
212         animation = CreateAnimation(rootActor, animationData, alpha, animationData->duration);
213       }
214     }
215   }
216   return animation;
217 }
218
219
220 AnimationPtr ModelActorFactory::BuildAnimation(
221   Model& model,
222   Actor& rootActor,
223   size_t index,
224   AlphaFunction alpha,
225   float durationSeconds)
226 {
227   AnimationPtr animation;
228   Internal::ModelDataPtr modelData(model.GetModelData());
229
230   if (modelData)
231   {
232     if (index >= modelData->NumberOfAnimationMaps())
233     {
234       DALI_LOG_ERROR("Invalid animation index\n");
235     }
236     else
237     {
238       const ModelAnimationMap* animationData(modelData->GetAnimationMap(index));
239       if( animationData != NULL )
240       {
241         animation = CreateAnimation(rootActor, animationData, alpha, durationSeconds);
242       }
243     }
244   }
245   return animation;
246 }
247
248
249
250
251 AnimationPtr ModelActorFactory::CreateAnimation(
252   Actor& rootActor,
253   const ModelAnimationMap* animationData,
254   AlphaFunction alpha,
255   float durationSeconds)
256 {
257   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
258
259   AnimationPtr animation(Animation::New(durationSeconds));
260   animation->SetDefaultAlphaFunction(alpha);
261
262   for(EntityAnimatorMapIter it = animationData->animators.begin(); it != animationData->animators.end(); ++it)
263   {
264     const EntityAnimatorMap& entityAnimator(*it);
265
266     // find actor for this animator
267     ActorPtr animatedActor = rootActor.FindChildByName(entityAnimator.GetEntityName());
268     if (!animatedActor)
269     {
270       // If we can't find the actor, it may not have been instantiated, may
271       // be a sibling or parent of rootActor or may have been removed.
272       continue;
273     }
274
275     Dali::Actor targetActor(animatedActor.Get());
276
277     Dali::KeyFrames posKFHandle = entityAnimator.GetPositionKeyFrames();
278     if(posKFHandle)
279     {
280       const KeyFrames& positionKeyFrames = GetImplementation(posKFHandle);
281       if(positionKeyFrames.GetKeyFramesBase()->GetNumberOfKeyFrames() > 0)
282       {
283         animation->AnimateBetween(Property(targetActor, Dali::Actor::POSITION),
284                                   positionKeyFrames, alpha, durationSeconds);
285       }
286     }
287
288     Dali::KeyFrames scaleKFHandle = entityAnimator.GetScaleKeyFrames();
289     if(scaleKFHandle)
290     {
291       const KeyFrames& scaleKeyFrames    = GetImplementation(scaleKFHandle);
292       if(scaleKeyFrames.GetKeyFramesBase()->GetNumberOfKeyFrames() > 0)
293       {
294         animation->AnimateBetween(Property(targetActor, Dali::Actor::SCALE),    scaleKeyFrames, alpha, durationSeconds);
295       }
296     }
297
298     Dali::KeyFrames rotationKFHandle = entityAnimator.GetRotationKeyFrames();
299     if(rotationKFHandle)
300     {
301       const KeyFrames& rotationKeyFrames = GetImplementation(rotationKFHandle);
302       if(rotationKeyFrames.GetKeyFramesBase()->GetNumberOfKeyFrames() > 0)
303       {
304         animation->AnimateBetween(Property(targetActor, Dali::Actor::ROTATION), rotationKeyFrames, alpha, durationSeconds);
305       }
306     }
307   }
308   return animation;
309 }
310
311
312
313 } // namespace Internal
314 } // namespace Dali