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