Merged with Tizen Branch ( builds )
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scrollable / bouncing-effect-actor.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-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
20
21 // EXTERNAL INCLUDES
22 #include <math.h>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/shader-effects/shader-effect.h>
25 #include <dali/public-api/math/vector3.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 namespace Internal
34 {
35
36 namespace
37 {
38 // Bouncing effect is presented by stacked three layers with same color and opacity
39 const size_t NUM_LAYERS( 3 );
40 const Vector3 LAYER_HEIGHTS( 1.f, 27.f/42.f, 13.f/42.f);
41
42 // use the actor color to paint every layer
43 const char* MESH_FRAGMENT_SHADER =
44 "void main()\n"
45 "{\n"
46 "  gl_FragColor = uColor;\n"
47 "}\n";
48
49 // Constraint to move the vertices vertically
50 struct VertexPositionConstraint
51 {
52   VertexPositionConstraint( float initialY, float range )
53   : mInitialY( initialY ),
54     mRange( range )
55   {
56   }
57
58   void operator()( Vector3& current, const PropertyInputContainer& inputs )
59   {
60     current.y = mInitialY + mRange * fabsf( inputs[0]->GetFloat() );
61   }
62
63   float mInitialY;
64   float mRange;
65 };
66
67 } // namespace Anon
68
69 Actor CreateBouncingEffectActor( Property::Index& bouncePropertyIndex )
70 {
71   Dali::AnimatableMesh             mesh;
72   Dali::MeshActor                  meshActor;
73
74   Dali::AnimatableMesh::Faces faces;
75   faces.reserve( NUM_LAYERS * 6 ); // 2 triangles per layer
76   for( size_t i=0; i<NUM_LAYERS; i++ )
77   {
78     size_t j=i*4;
79     faces.push_back(j); faces.push_back(j+3); faces.push_back(j+1);
80     faces.push_back(j); faces.push_back(j+2); faces.push_back(j+3);
81   }
82
83   mesh = Dali::AnimatableMesh::New(NUM_LAYERS*4, faces); // 4 vertices per layer
84   for( size_t i=0;i<NUM_LAYERS;i++ )
85   {
86     size_t j=i*4;
87     float positionZ = 0.01f*static_cast<float>( i ); // the interval between each layer is 0.01
88     mesh[j  ].SetPosition( Vector3( -0.5f, -0.5f, positionZ ) );
89     mesh[j+1].SetPosition( Vector3( 0.5f, -0.5f, positionZ ) );
90     mesh[j+2].SetPosition( Vector3( -0.5f, -0.5f, positionZ ) );
91     mesh[j+3].SetPosition( Vector3( 0.5f, -0.5f, positionZ ) );
92   }
93
94   meshActor = Dali::MeshActor::New(mesh);
95
96   Dali::ShaderEffect shaderEffect = Dali::ShaderEffect::New( "", MESH_FRAGMENT_SHADER,
97                                                              GEOMETRY_TYPE_UNTEXTURED_MESH,
98                                                              Dali::ShaderEffect::HINT_BLENDING );
99   meshActor.SetShaderEffect(shaderEffect);
100
101   // To control the movement of all vertices with one custom property
102   bouncePropertyIndex = meshActor.RegisterProperty("BounceCoeffcient", 0.f);
103   for( size_t i=0;i<NUM_LAYERS;i++ )
104   {
105     size_t j=i*4;
106     Constraint constraint = Constraint::New<Vector3>( mesh, mesh.GetPropertyIndex(j+2, AnimatableVertex::Property::POSITION ), VertexPositionConstraint(-0.5f, LAYER_HEIGHTS[i]) );
107     constraint.AddSource( Source(meshActor, bouncePropertyIndex) );
108     constraint.Apply();
109
110     constraint = Constraint::New<Vector3>( mesh, mesh.GetPropertyIndex(j+3,  AnimatableVertex::Property::POSITION), VertexPositionConstraint(-0.5f, LAYER_HEIGHTS[i]) );
111     constraint.AddSource( Source(meshActor, bouncePropertyIndex) );
112     constraint.Apply();
113   }
114
115   return meshActor;
116 }
117
118 } // namespace Internal
119
120 } // namespace Toolkit
121
122 } // namespace Dali