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.
19 #include <dali-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
23 #include <dali/public-api/actors/mesh-actor.h>
24 #include <dali/public-api/animation/constraint.h>
25 #include <dali/public-api/geometry/animatable-mesh.h>
26 #include <dali/public-api/shader-effects/shader-effect.h>
27 #include <dali/public-api/math/vector3.h>
40 // Bouncing effect is presented by stacked three layers with same color and opacity
41 const size_t NUM_LAYERS( 3 );
42 const Vector3 LAYER_HEIGHTS( 1.f, 27.f/42.f, 13.f/42.f);
44 // use the actor color to paint every layer
45 const char* MESH_FRAGMENT_SHADER =
48 " gl_FragColor = uColor;\n"
51 // Constraint to move the vertices vertically
52 struct VertexPositionConstraint
54 VertexPositionConstraint( float initialY, float range )
55 : mInitialY( initialY ),
60 void operator()( Vector3& current, const PropertyInputContainer& inputs )
62 current.y = mInitialY + mRange * fabsf( inputs[0]->GetFloat() );
71 Actor CreateBouncingEffectActor( Property::Index& bouncePropertyIndex )
73 Dali::AnimatableMesh mesh;
74 Dali::MeshActor meshActor;
76 Dali::AnimatableMesh::Faces faces;
77 faces.reserve( NUM_LAYERS * 6 ); // 2 triangles per layer
78 for( size_t i=0; i<NUM_LAYERS; i++ )
81 faces.push_back(j); faces.push_back(j+3); faces.push_back(j+1);
82 faces.push_back(j); faces.push_back(j+2); faces.push_back(j+3);
85 mesh = Dali::AnimatableMesh::New(NUM_LAYERS*4, faces); // 4 vertices per layer
86 for( size_t i=0;i<NUM_LAYERS;i++ )
89 float positionZ = 0.01f*static_cast<float>( i ); // the interval between each layer is 0.01
90 mesh[j ].SetPosition( Vector3( -0.5f, -0.5f, positionZ ) );
91 mesh[j+1].SetPosition( Vector3( 0.5f, -0.5f, positionZ ) );
92 mesh[j+2].SetPosition( Vector3( -0.5f, -0.5f, positionZ ) );
93 mesh[j+3].SetPosition( Vector3( 0.5f, -0.5f, positionZ ) );
96 meshActor = Dali::MeshActor::New(mesh);
98 Dali::ShaderEffect shaderEffect = Dali::ShaderEffect::New( "", MESH_FRAGMENT_SHADER,
99 GEOMETRY_TYPE_UNTEXTURED_MESH,
100 Dali::ShaderEffect::HINT_BLENDING );
101 meshActor.SetShaderEffect(shaderEffect);
103 // To control the movement of all vertices with one custom property
104 bouncePropertyIndex = meshActor.RegisterProperty("BounceCoeffcient", 0.f);
105 for( size_t i=0;i<NUM_LAYERS;i++ )
108 Constraint constraint = Constraint::New<Vector3>( mesh, mesh.GetPropertyIndex(j+2, AnimatableVertex::Property::POSITION ), VertexPositionConstraint(-0.5f, LAYER_HEIGHTS[i]) );
109 constraint.AddSource( Source(meshActor, bouncePropertyIndex) );
112 constraint = Constraint::New<Vector3>( mesh, mesh.GetPropertyIndex(j+3, AnimatableVertex::Property::POSITION), VertexPositionConstraint(-0.5f, LAYER_HEIGHTS[i]) );
113 constraint.AddSource( Source(meshActor, bouncePropertyIndex) );
120 } // namespace Internal
122 } // namespace Toolkit