Merge "TextSelectionPopup to show Paste in data in Clipboard" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / bouncing-effect.cpp
1 /*
2  * Copyright (c) 2015 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 #include <dali-toolkit/devel-api/shader-effects/bouncing-effect.h>
19
20 namespace Dali
21 {
22
23 namespace Toolkit
24 {
25
26 namespace
27 {
28
29 #define MAKE_STRING(A)#A
30
31 const std::string PROGRESS_RATE_PROPERTY_NAME( "uProgressRate" );
32
33 } // namespace
34
35 BouncingEffect::BouncingEffect()
36 {
37 }
38
39 BouncingEffect::BouncingEffect( ShaderEffect handle )
40 :ShaderEffect( handle )
41 {
42 }
43
44 BouncingEffect::~BouncingEffect()
45 {
46 }
47
48 BouncingEffect BouncingEffect::New( const Vector4& color )
49 {
50   std::string fragmentShader = MAKE_STRING(
51       precision mediump float;\n
52       uniform float uProgressRate;\n
53       uniform vec4 uAssignedColor;\n
54       void main()\n
55       {\n
56         float progressRate = abs(uProgressRate)*0.5;\n
57         float amplitude = 0.15 - progressRate*0.15 ;\n
58         float x1 = 7.5 * (vTexCoord.x - progressRate);\n
59         float x2 = 7.5 * (vTexCoord.x - 1.0 + progressRate);\n
60         float height1 = max(0.00001, 0.3 - amplitude * ( exp(x1) + exp(-x1) ) );\n
61         float height2 = max(0.00001, 0.3 - amplitude * ( exp(x2) + exp(-x2) ) );\n
62         float height3 = max(0.00001, 1.0 - 3.0 * amplitude * ( exp(x1*0.5) + exp(-x1*0.5) ) );\n
63         float height4 = max(0.00001, 1.0 - 3.0 * amplitude * ( exp(x2*0.5) + exp(-x2*0.5) ) );\n
64         vec4 fragColor = vec4(0.0);\n
65         float y = vTexCoord.y/(height1+height2);\n
66         float y2 = vTexCoord.y/max(height3,height4);\n
67         float coef = max(height1,height2)*5.0/( 1.0+exp(y*12.0-6.0) );\n
68         float alpha = pow( max(0.0,(1.0-y2))*(1.0-min(abs(x1),abs(x2))/5.0), 2.0);\n
69         if( vTexCoord.y < 0.075 )\n
70         {\n
71           fragColor= mix(uAssignedColor, vec4(1.0), coef);\n
72           fragColor += (vec4(1.0)-fragColor) * alpha;\n
73         }\n
74         else if (y2<1.0)\n
75         {\n
76           fragColor =vec4(1.0,1.0,1.0, alpha + (1.0-alpha)*coef);\n
77           fragColor.rgb -= ( vec3(1.0)-uAssignedColor.rgb )*min(clamp(y*1.2-0.3, 0.0, 0.3),clamp(0.9-y*1.2,0.0,0.3));\n
78         }\n
79         fragColor.a *= 10.0*min(min(vTexCoord.x, 1.0-vTexCoord.x),0.1)*min(1.0, progressRate/0.2);\n
80         gl_FragColor =  fragColor;\n
81       }
82   );
83
84   ShaderEffect shaderEffect;
85   shaderEffect = ShaderEffect::New( "", fragmentShader,
86                                     GeometryType( GEOMETRY_TYPE_IMAGE),
87                                     ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
88   BouncingEffect handle( shaderEffect );
89
90   handle.SetUniform( "uAssignedColor", color );
91   handle.SetProgressRate( 0.f );
92
93   return handle;
94 }
95
96 void BouncingEffect::SetProgressRate( float progress )
97 {
98   SetUniform( PROGRESS_RATE_PROPERTY_NAME, progress );
99 }
100
101 const std::string& BouncingEffect::GetProgressRatePropertyName() const
102 {
103   return PROGRESS_RATE_PROPERTY_NAME;
104 }
105
106 } // namespace Toolkit
107
108 } // namespace Dali