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