Merge "TextSelectionPopup to show Paste in data in Clipboard" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / square-dissolve-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/square-dissolve-effect.h>
19
20 namespace Dali
21 {
22
23 namespace Toolkit
24 {
25
26 namespace
27 {
28
29 const std::string STEP_PROPERTY_NAME( "uStep" );
30 const std::string ROWS_PROPERTY_NAME( "uRows" );
31 const std::string COLUMNS_PROPERTY_NAME( "uColumns" );
32 const std::string TEXSIZE_PROPERTY_NAME( "texSize" );
33
34 } // namespace
35
36 SquareDissolveEffect::SquareDissolveEffect()
37 {
38 }
39
40 //Call the Parent copy constructor to add reference to the implementation for this object
41 SquareDissolveEffect::SquareDissolveEffect(ShaderEffect handle)
42 :ShaderEffect(handle)
43 {
44 }
45
46 SquareDissolveEffect::~SquareDissolveEffect()
47 {
48 }
49
50 SquareDissolveEffect SquareDissolveEffect::New()
51 {
52   // variable "uStep" range scope : [0.0, 1.0]
53   std::string fragmentShader(
54       "uniform  mediump vec2   texSize;\n"
55       "uniform  mediump float  uStep;\n"
56       "uniform  mediump float  uRows;\n"
57       "uniform  mediump float  uColumns;\n"
58       "void main()\n"
59       "{\n"
60       "  mediump vec2 mosaicSize = vec2(1.0 / uRows, 1.0 / uColumns);                               \n"
61       "  mediump vec2 intXY = vec2(vTexCoord.x * texSize.x, vTexCoord.y * texSize.y);               \n"
62       "  mediump vec2 XYMosaic = vec2(floor(intXY.x / mosaicSize.x) * mosaicSize.x, floor(intXY.y / mosaicSize.y) * mosaicSize.y); \n"
63       "  mediump vec2 UVMosaic = vec2(XYMosaic.x /texSize.x, XYMosaic.y / texSize.y);               \n"
64       "  mediump vec4 noiseVec = texture2D(sEffect, UVMosaic);                                      \n"
65       "  mediump float intensity = (noiseVec[0] + noiseVec[1] + noiseVec[2] + noiseVec[3]) / 4.0;   \n"
66       "  if(intensity < uStep)                                                              \n"
67       "    gl_FragColor = vec4(0.1, 0.1, 0.1, 1.0);                                         \n"
68       "  else                                                                               \n"
69       "    gl_FragColor = texture2D(sTexture, vTexCoord);                                   \n"
70       "  gl_FragColor *= uColor;                                                            \n"
71       "}                                                                                    \n" );
72
73   // Create the implementation, temporarily owned on stack
74   Dali::ShaderEffect shaderEffectCustom =  Dali::ShaderEffect::New(
75       "",
76       fragmentShader,
77       GeometryType( GEOMETRY_TYPE_IMAGE ),
78       ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID ));
79
80   /* Pass ownership to SquareDissolveEffect through overloaded constructor, So that it now has access to the
81      Dali::ShaderEffect implementation */
82   Dali::Toolkit::SquareDissolveEffect handle( shaderEffectCustom );
83
84   handle.SetUniform( TEXSIZE_PROPERTY_NAME, Vector2(1.0f, 1.0f) );//COORDINATE_TYPE_DEFAULT
85   handle.SetUniform( STEP_PROPERTY_NAME, 0.1f);
86   handle.SetUniform( ROWS_PROPERTY_NAME, 25.0f);
87   handle.SetUniform( COLUMNS_PROPERTY_NAME, 25.0f);
88   return handle;
89 }
90
91 void SquareDissolveEffect::SetStep( float step )
92 {
93   SetUniform( STEP_PROPERTY_NAME, step );
94 }
95
96 void SquareDissolveEffect::SetRows(float rows)
97 {
98   SetUniform(ROWS_PROPERTY_NAME, rows);
99 }
100
101 void SquareDissolveEffect::SetColumns(float columns)
102 {
103   SetUniform(COLUMNS_PROPERTY_NAME, columns);
104 }
105
106 void SquareDissolveEffect::SetTextureSize(const Vector2& textureSize)
107 {
108   SetUniform(TEXSIZE_PROPERTY_NAME, textureSize);
109 }
110
111 const std::string& SquareDissolveEffect::GetStepPropertyName() const
112 {
113   return STEP_PROPERTY_NAME;
114 }
115
116 const std::string& SquareDissolveEffect::GetRowsPropertyName() const
117 {
118   return ROWS_PROPERTY_NAME;
119 }
120
121 const std::string& SquareDissolveEffect::GetColumnsPropertyName() const
122 {
123   return COLUMNS_PROPERTY_NAME;
124 }
125
126 const std::string& SquareDissolveEffect::GetTexSizePropertyName() const
127 {
128   return TEXSIZE_PROPERTY_NAME;
129 }
130
131 } // namespace Toolkit
132
133 } // namespace Dali