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