[dali_1.9.27] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scrollable / bouncing-effect-actor.cpp
1 /*
2  * Copyright (c) 2016 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 <dali/public-api/math/vector3.h>
23 #include <dali/public-api/object/property-map.h>
24 #include <dali/public-api/rendering/geometry.h>
25 #include <dali/public-api/rendering/renderer.h>
26 #include <dali/public-api/rendering/shader.h>
27 #include <dali/public-api/rendering/texture-set.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40 // Bouncing effect is presented by stacked three layers with same color and opacity
41 const float LAYER_HEIGHTS[5] =
42 {
43   1.f,
44   26.f * 4.f/ 130.f,
45   26.f * 3.f / 130.f,
46   26.f * 2.f / 130.f,
47   26.f / 130.f
48 };
49
50 #define MAKE_SHADER(A)#A
51
52 // Modify the vertex position according to the bounce coefficient;
53 const char* MESH_VERTEX_SHADER = MAKE_SHADER(
54 attribute mediump vec3    aPosition1;\n
55 attribute mediump vec3    aPosition2;\n
56 uniform   mediump mat4    uMvpMatrix;\n
57 uniform   mediump vec3    uSize;
58 uniform   mediump float   uBounceCoefficient;\n
59 \n
60 void main()\n
61 {\n
62   gl_Position = uMvpMatrix * vec4(mix( aPosition1, aPosition2, abs(uBounceCoefficient) )*uSize, 1.0);\n
63 }
64 );
65
66 // use the actor color to paint every layer
67 const char* MESH_FRAGMENT_SHADER = MAKE_SHADER(
68 uniform lowp  vec4    uColor;\n
69 void main()\n
70 {\n
71   gl_FragColor = uColor;\n
72 }\n
73 );
74
75 } // namespace Anon
76
77 Actor CreateBouncingEffectActor( Property::Index& bouncePropertyIndex )
78 {
79   // Create the bouncing mesh geometry
80   struct VertexPosition
81   {
82     Vector3 position1;
83     Vector3 position2;
84   };
85   // 4 vertices 2 triangles per layer. The depth interval between each layer is 0.01
86   VertexPosition vertexData[20] = {
87     // bottom layer
88     { Vector3( -0.5f, -0.5f, 0.f ),  Vector3( -0.5f, -0.5f, 0.f )  },
89     { Vector3( 0.5f, -0.5f, 0.f ),   Vector3( 0.5f, -0.5f, 0.f )   },
90     { Vector3( -0.5f, -0.5f, 0.f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[0], 0.f ) },
91     { Vector3( 0.5f, -0.5f, 0.f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[0], 0.f )   },
92     // mid-bottom layer
93     { Vector3( -0.5f, -0.5f, 0.01f ),  Vector3( -0.5f, -0.5f, 0.01f )  },
94     { Vector3( 0.5f, -0.5f, 0.01f ),   Vector3( 0.5f, -0.5f, 0.01f )   },
95     { Vector3( -0.5f, -0.5f, 0.01f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[1], 0.01f ) },
96     { Vector3( 0.5f, -0.5f, 0.01f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[1], 0.01f )   },
97     // middle layer
98     { Vector3( -0.5f, -0.5f, 0.02f ),  Vector3( -0.5f, -0.5f, 0.02f )  },
99     { Vector3( 0.5f, -0.5f, 0.02f ),   Vector3( 0.5f, -0.5f, 0.02f )   },
100     { Vector3( -0.5f, -0.5f, 0.02f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[2], 0.02f ) },
101     { Vector3( 0.5f, -0.5f, 0.02f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[2], 0.02f )   },
102     // mid-top layer
103     { Vector3( -0.5f, -0.5f, 0.03f ),  Vector3( -0.5f, -0.5f, 0.03f )  },
104     { Vector3( 0.5f, -0.5f, 0.03f ),   Vector3( 0.5f, -0.5f, 0.03f )   },
105     { Vector3( -0.5f, -0.5f, 0.03f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[3], 0.03f ) },
106     { Vector3( 0.5f, -0.5f, 0.03f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[3], 0.03f )   },
107     // top layer
108     { Vector3( -0.5f, -0.5f, 0.04f ),  Vector3( -0.5f, -0.5f, 0.04f )  },
109     { Vector3( 0.5f, -0.5f, 0.04f ),   Vector3( 0.5f, -0.5f, 0.04f )   },
110     { Vector3( -0.5f, -0.5f, 0.04f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[4], 0.04f ) },
111     { Vector3( 0.5f, -0.5f, 0.04f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[4], 0.04f )   }
112   };
113   Property::Map vertexFormat;
114   vertexFormat["aPosition1"] = Property::VECTOR3;
115   vertexFormat["aPosition2"] = Property::VECTOR3;
116   VertexBuffer vertices = VertexBuffer::New( vertexFormat );
117   vertices.SetData( vertexData, 20u );
118
119   unsigned short indexData[30] = { 0,3,1,0,2,3,4,7,5,4,6,7,8,11,9,8,10,11,12,15,13,12,14,15,16,19,17,16,18,19};
120
121   Geometry meshGeometry = Geometry::New();
122   meshGeometry.AddVertexBuffer( vertices );
123   meshGeometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
124
125   // Create the shader
126   Shader shader = Shader::New( MESH_VERTEX_SHADER, MESH_FRAGMENT_SHADER );
127
128   // Create renderer
129   Renderer renderer = Renderer::New( meshGeometry, shader );
130
131   // Create actor
132   Actor meshActor= Actor::New();
133   meshActor.AddRenderer( renderer );
134
135   // Register property
136   bouncePropertyIndex = meshActor.RegisterProperty("uBounceCoefficient", 0.f);
137
138   return meshActor;
139 }
140
141 } // namespace Internal
142
143 } // namespace Toolkit
144
145 } // namespace Dali