Add GetNaturalSize to Actor and deriving classes.
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / mesh-actor-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/mesh-actor-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/internal/event/modeling/model-impl.h>
24 #include <dali/internal/event/modeling/animatable-mesh-impl.h>
25 #include <dali/internal/event/modeling/mesh-impl.h>
26
27 namespace Dali
28 {
29
30 using Internal::MeshIter;
31 using Internal::MeshIPtr;
32 using Internal::MaterialIPtr;
33
34 namespace Internal
35 {
36
37
38 namespace
39 {
40
41 BaseHandle Create()
42 {
43   return Dali::MeshActor::New();
44 }
45
46 TypeRegistration mType( typeid(Dali::MeshActor), typeid(Dali::RenderableActor), Create );
47
48 }
49
50 MeshActorPtr MeshActor::New()
51 {
52   MeshActorPtr actor(new MeshActor());
53
54   // Second-phase construction
55   actor->Initialize();
56
57   // Create the attachment
58   actor->mMeshAttachment = MeshAttachment::New( *actor->mNode );
59   actor->Attach(*actor->mMeshAttachment);
60   actor->SetCullFace( Dali::CullBack );
61
62   return actor;
63 }
64
65 MeshActorPtr MeshActor::New(Dali::Mesh mesh)
66 {
67   MeshActorPtr actor = MeshActor::New();
68
69   if(actor)
70   {
71     actor->SetMesh(mesh);
72   }
73
74   return actor;
75 }
76
77 MeshActorPtr MeshActor::New(Dali::AnimatableMesh mesh)
78 {
79   MeshActorPtr actor = MeshActor::New();
80
81   if(actor)
82   {
83     actor->SetMesh(mesh);
84   }
85
86   return actor;
87 }
88
89
90 MeshActorPtr MeshActor::New(ModelDataPtr modelData, Dali::Entity entity)
91 {
92   MeshActorPtr actor(new MeshActor());
93   // Second-phase construction
94   actor->Initialize();
95
96   // Create the attachment
97   actor->mMeshAttachment = MeshAttachment::New( *actor->mNode );
98   actor->Attach(*actor->mMeshAttachment);
99   actor->SetName(entity.GetName());
100   actor->SetCullFace( Dali::CullBack );
101
102   DALI_ASSERT_ALWAYS(entity.NumberOfMeshes() == 1 && "Dali does not support multiple meshes per node in the model");
103
104   actor->SetMesh(modelData, entity.GetMeshByIndex(0)); // only use the first mesh
105
106   Matrix transform(entity.GetTransformMatrix());
107   Vector3 position;
108   Quaternion rotation;
109   Vector3 scale;
110   transform.GetTransformComponents(position, rotation, scale);
111   actor->SetPosition(position);
112   actor->SetRotation(rotation);
113   actor->SetScale(scale);
114
115   return actor;
116 }
117
118 MeshActor::MeshActor()
119 : RenderableActor()
120 {
121 }
122
123 MeshActor::~MeshActor()
124 {
125 }
126
127 void MeshActor::SetMesh(Dali::Mesh mesh)
128 {
129   SetMesh( &GetImplementation(mesh) );
130 }
131
132 void MeshActor::SetMesh(Dali::AnimatableMesh mesh)
133 {
134   mAnimatableMeshHandle = mesh;
135   SetMesh( GetImplementation(mesh).GetMesh() );
136
137   SetInitialVolume(Vector3(1.0f, 1.0f, 1.0f));
138 }
139
140 void MeshActor::SetMesh( MeshIPtr meshPtr )
141 {
142   mMeshAttachment->SetMesh( meshPtr, meshPtr->GetResourceId(), meshPtr->GetBones(),  meshPtr->GetMaterial() );
143 }
144
145 // Used by Internal::ModelActorFactory
146 void MeshActor::SetMesh(ModelDataPtr modelData, unsigned int meshIndex)
147 {
148   ResourceTicketPtr meshTicket = modelData->GetMeshTicket(meshIndex);
149   const Dali::MeshData& meshData = modelData->GetMesh(meshIndex);
150   Dali::Material material = meshData.GetMaterial();
151   DALI_ASSERT_ALWAYS( material && "No material found" );
152   MaterialIPtr matPtr = &GetImplementation(material);
153
154   mMeshAttachment->SetMesh(meshTicket, meshData.GetBones(), matPtr);
155 }
156
157 void MeshActor::SetMaterial(const Dali::Material material)
158 {
159   MaterialIPtr materialPtr = const_cast<Internal::Material*>(&GetImplementation(material));
160   mMeshAttachment->SetMaterial(materialPtr);
161 }
162
163 Dali::Material MeshActor::GetMaterial() const
164 {
165   Dali::Material material;
166
167   Internal::MaterialIPtr internalPtr = mMeshAttachment->GetMaterial( );
168
169   if(internalPtr)
170   {
171     material = Dali::Material(internalPtr.Get());
172   }
173
174   return material;
175 }
176
177 void MeshActor::SetAffectedByLighting(bool affectedByLighting)
178 {
179   mMeshAttachment->SetAffectedByLighting( affectedByLighting );
180 }
181
182 bool MeshActor::IsAffectedByLighting()
183 {
184   return mMeshAttachment->IsAffectedByLighting();
185 }
186
187 void MeshActor::BindBonesToMesh(Internal::ActorPtr rootActor)
188 {
189   mMeshAttachment->BindBonesToMesh(rootActor);
190 }
191
192 } // namespace Internal
193
194 } // namespace Dali