2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali/internal/event/common/property-index-ranges.h>
19 #include <dali/internal/event/common/stage-impl.h>
20 #include <dali/internal/event/common/thread-local-storage.h>
21 #include <dali/internal/event/modeling/animatable-mesh-impl.h>
22 #include <dali/internal/event/modeling/mesh-impl.h>
23 #include <dali/internal/event/modeling/material-impl.h>
24 #include <dali/internal/update/manager/update-manager.h>
25 #include <dali/internal/update/modeling/scene-graph-animatable-mesh.h>
30 const Property::Index AnimatableVertex::POSITION = 0;
31 const Property::Index AnimatableVertex::COLOR = 1;
32 const Property::Index AnimatableVertex::TEXTURE_COORDS = 2;
40 const std::string DEFAULT_PROPERTY_NAMES[] =
46 const int VERTEX_PROPERTY_COUNT = sizeof( DEFAULT_PROPERTY_NAMES ) / sizeof( std::string );
48 const Property::Type DEFAULT_PROPERTY_TYPES[ VERTEX_PROPERTY_COUNT ] =
50 Property::VECTOR3, // position
51 Property::VECTOR4, // Color
52 Property::VECTOR2, // Texture Coords
57 AnimatableMesh::DefaultPropertyLookup* AnimatableMesh::mDefaultPropertyLookup = NULL;
60 AnimatableMesh::AnimatableMesh(
61 SceneGraph::UpdateManager& updateManager,
62 SceneGraph::AnimatableMesh* sceneObject,
64 int numberOfVertices )
65 : mUpdateManager( updateManager ),
66 mSceneObject( sceneObject ),
68 mNumberOfVertices( numberOfVertices ),
69 mPropertyCount( numberOfVertices * VERTEX_PROPERTY_COUNT )
71 // Transfer animatable ownership to a scene message
72 AddAnimatableMeshMessage( mUpdateManager, *mSceneObject );
76 AnimatableMeshPtr AnimatableMesh::New(
77 unsigned int numVertices,
78 const Dali::AnimatableMesh::Faces& faceIndices )
80 return New( numVertices, faceIndices, Dali::Material::New("dummy material"), true );
83 AnimatableMeshPtr AnimatableMesh::New(
84 unsigned int numVertices,
85 const Dali::AnimatableMesh::Faces& faceIndices,
86 Dali::Material material)
88 return New( numVertices, faceIndices, material, false );
92 AnimatableMeshPtr AnimatableMesh::New(
93 unsigned int numVertices,
94 const Dali::AnimatableMesh::Faces& faceIndices,
95 Dali::Material material,
98 DALI_ASSERT_ALWAYS( numVertices > 0 && "Mesh has no vertices" );
99 DALI_ASSERT_ALWAYS( ( numVertices * 3 ) < DEFAULT_PROPERTY_MAX_COUNT && "Mesh exceeds maximum supported vertices" );
100 DALI_ASSERT_ALWAYS( faceIndices.size() > 0 && "Mesh has no faces" );
101 for( Dali::AnimatableMesh::FacesConstIter faceIter=faceIndices.begin();
102 faceIter != faceIndices.end();
105 unsigned int faceIndex = *faceIter;
106 DALI_ASSERT_ALWAYS( faceIndex < numVertices && "Face index out of range" );
109 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
110 SceneGraph::UpdateManager& updateManager = tls.GetUpdateManager();
111 ResourceManager& resourceManager = tls.GetResourceManager();
113 Dali::MeshData meshData;
115 // We need a valid mesh data object to create the scene graph mesh
116 Dali::MeshData::VertexContainer vertices( numVertices );
118 meshData.SetData(vertices, faceIndices, bones, material);
119 bool useColor = false;
124 meshData.SetHasColor(useColor);
125 meshData.SetHasNormals(false);
126 meshData.SetHasTextureCoords(true);
128 MeshIPtr mesh( Mesh::New( meshData, false /* not discardable, data is updated in the update thread */, true /* Scaling is required */ ) );
130 // Create the scene object
131 SceneGraph::AnimatableMesh* sceneObject = new SceneGraph::AnimatableMesh( resourceManager, mesh->GetResourceId(), meshData.GetVertices() );
133 // Create the event object
134 AnimatableMeshPtr animatableMeshPtr( new AnimatableMesh( updateManager, sceneObject, mesh, meshData.GetVertexCount() ) );
136 return animatableMeshPtr;
140 AnimatableMesh::~AnimatableMesh()
142 DALI_ASSERT_DEBUG( mSceneObject != NULL );
144 // Guard to allow handle destruction after Core has been destroyed
145 if ( Stage::IsInstalled() )
147 RemoveAnimatableMeshMessage( mUpdateManager, *mSceneObject );
151 unsigned int AnimatableMesh::GetNumberOfVertices() const
153 return mNumberOfVertices;
156 Property::Index AnimatableMesh::GetVertexPropertyIndex(
158 Property::Index property ) const
160 DALI_ASSERT_DEBUG(property < VERTEX_PROPERTY_COUNT );
161 return ( vertex * VERTEX_PROPERTY_COUNT ) + property;
164 void AnimatableMesh::SetPosition( unsigned int vertexIndex, const Vector3& position)
166 StagePtr stage = Stage::GetCurrent();
167 BakeVertexPositionMessage( stage->GetUpdateInterface(), *mSceneObject, vertexIndex, position );
170 void AnimatableMesh::SetColor( unsigned int vertexIndex, const Vector4& color)
172 StagePtr stage = Stage::GetCurrent();
173 BakeVertexColorMessage( stage->GetUpdateInterface(), *mSceneObject, vertexIndex, color );
176 void AnimatableMesh::SetTextureCoords( unsigned int vertexIndex, const Vector2& coords)
178 StagePtr stage = Stage::GetCurrent();
179 BakeVertexTextureCoordsMessage( stage->GetUpdateInterface(), *mSceneObject, vertexIndex, coords );
182 const Vector3& AnimatableMesh::GetCurrentPosition( unsigned int vertexIndex) const
184 StagePtr stage = Stage::GetCurrent();
185 return mSceneObject->GetPosition(stage->GetEventBufferIndex(), vertexIndex);
188 const Vector4& AnimatableMesh::GetCurrentColor( unsigned int vertexIndex) const
190 StagePtr stage = Stage::GetCurrent();
191 return mSceneObject->GetColor(stage->GetEventBufferIndex(), vertexIndex);
194 const Vector2& AnimatableMesh::GetCurrentTextureCoords( unsigned int vertexIndex) const
196 StagePtr stage = Stage::GetCurrent();
197 return mSceneObject->GetTextureCoords(stage->GetEventBufferIndex(), vertexIndex);
200 MeshIPtr AnimatableMesh::GetMesh()
205 /********************************************************************************
206 ********************************************************************************
207 ******************************** PROPERTY METHODS **************************
208 ********************************************************************************
209 ********************************************************************************/
211 bool AnimatableMesh::Supports( Capability capability ) const
216 bool AnimatableMesh::IsSceneObjectRemovable() const
218 return false; // The scene object is permanently "on stage" whilst this object is alive
221 unsigned int AnimatableMesh::GetDefaultPropertyCount() const
223 return mPropertyCount;
226 void AnimatableMesh::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
228 indices.reserve( mPropertyCount );
230 for ( int i = 0; i < mPropertyCount; ++i )
232 indices.push_back( i );
236 const std::string& AnimatableMesh::GetDefaultPropertyName( Property::Index index ) const
238 if ( ( index >= 0 ) && ( index < mPropertyCount ) )
240 return DEFAULT_PROPERTY_NAMES[index % VERTEX_PROPERTY_COUNT];
244 // Index out-of-range... return empty string.
245 static const std::string INVALID_PROPERTY_NAME;
246 return INVALID_PROPERTY_NAME;
250 Property::Index AnimatableMesh::GetDefaultPropertyIndex(const std::string& name) const
252 Property::Index index = Property::INVALID_INDEX;
254 // TODO: Property names should be modified to append the vertex index
259 bool AnimatableMesh::IsDefaultPropertyWritable(Property::Index index) const
264 bool AnimatableMesh::IsDefaultPropertyAnimatable(Property::Index index) const
269 bool AnimatableMesh::IsDefaultPropertyAConstraintInput( Property::Index index ) const
274 Property::Type AnimatableMesh::GetDefaultPropertyType(Property::Index index) const
276 if ( ( index >= 0 ) && ( index < mPropertyCount ) )
278 return DEFAULT_PROPERTY_TYPES[index % VERTEX_PROPERTY_COUNT ];
282 // Index out-of-bounds
283 return Property::NONE;
287 void AnimatableMesh::SetDefaultProperty( Property::Index index, const Property::Value& property )
289 DALI_ASSERT_ALWAYS( ( index >= 0 ) && ( index < mPropertyCount ) );
291 int vertexProperty = index % VERTEX_PROPERTY_COUNT;
292 int vertexIndex = index / VERTEX_PROPERTY_COUNT;
293 switch ( vertexProperty )
295 case Dali::AnimatableVertex::POSITION:
297 SetPosition( vertexIndex, property.Get<Vector3>() );
300 case Dali::AnimatableVertex::COLOR:
302 SetColor( vertexIndex, property.Get<Vector4>() );
305 case Dali::AnimatableVertex::TEXTURE_COORDS:
307 SetTextureCoords( vertexIndex, property.Get<Vector2>() );
313 void AnimatableMesh::SetCustomProperty( Property::Index /*index*/, const CustomProperty& /*entry*/, const Property::Value& /*value*/ )
315 DALI_ASSERT_ALWAYS( 0 && "AnimatableMesh does not support custom properties");
318 Property::Value AnimatableMesh::GetDefaultProperty(Property::Index index) const
320 Property::Value value;
321 int vertexProperty = index % VERTEX_PROPERTY_COUNT;
322 int vertexIndex = index / VERTEX_PROPERTY_COUNT;
324 switch ( vertexProperty )
326 case Dali::AnimatableVertex::POSITION:
328 value = GetCurrentPosition(vertexIndex);
331 case Dali::AnimatableVertex::COLOR:
333 value = GetCurrentColor(vertexIndex);
336 case Dali::AnimatableVertex::TEXTURE_COORDS:
338 value = GetCurrentTextureCoords(vertexIndex);
346 void AnimatableMesh::InstallSceneObjectProperty( SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index )
348 DALI_ASSERT_ALWAYS( 0 && "AnimatableMesh does not support custom properties" );
351 const SceneGraph::PropertyOwner* AnimatableMesh::GetSceneObject() const
356 const SceneGraph::PropertyBase* AnimatableMesh::GetSceneObjectAnimatableProperty( Property::Index index ) const
358 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
360 const SceneGraph::PropertyBase* property( NULL );
362 // This method should only return a property which is part of the scene-graph
363 if( mSceneObject != NULL )
365 int vertexProperty = index % VERTEX_PROPERTY_COUNT;
366 int vertexIndex = index / VERTEX_PROPERTY_COUNT;
368 switch ( vertexProperty )
370 case Dali::AnimatableVertex::POSITION:
371 property = &mSceneObject->mVertices[vertexIndex].position;
373 case Dali::AnimatableVertex::COLOR:
374 property = &mSceneObject->mVertices[vertexIndex].color;
376 case Dali::AnimatableVertex::TEXTURE_COORDS:
377 property = &mSceneObject->mVertices[vertexIndex].textureCoords;
385 const PropertyInputImpl* AnimatableMesh::GetSceneObjectInputProperty( Property::Index index ) const
387 const PropertyInputImpl* property( NULL );
388 if( mSceneObject != NULL )
390 int vertexProperty = index % VERTEX_PROPERTY_COUNT;
391 int vertexIndex = index / VERTEX_PROPERTY_COUNT;
393 switch ( vertexProperty )
395 case Dali::AnimatableVertex::POSITION:
396 property = &mSceneObject->mVertices[vertexIndex].position;
398 case Dali::AnimatableVertex::COLOR:
399 property = &mSceneObject->mVertices[vertexIndex].color;
401 case Dali::AnimatableVertex::TEXTURE_COORDS:
402 property = &mSceneObject->mVertices[vertexIndex].textureCoords;