PageTurnView cleanup
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / page-turn-view / page-turn-book-spine-effect.h
1 #ifndef __DALI_PAGE_TURN_BOOK_SPINE_EFFECT_H__
2 #define __DALI_PAGE_TURN_BOOK_SPINE_EFFECT_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/shader-effects/shader-effect.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 namespace Internal
31 {
32
33 /**
34  * @brief Creates a new PageTurnBookSpineEffect
35  * This is an assisting effect of PageTurnEffect to display a book spine on _static_ pages, and also to flip the image horizontally when needed.
36  *
37  * When the page is turned over in landscape, call
38  * SetIsBackImageVisible(true), this effect can display the back image
39  * correctly after the imageActor been rotated 180 degrees.  To
40  * display the pages visually consistent with its turning state,
41  * please set the uniforms with the same values as the PageTurnEffect.
42  *
43  * Animatable/Constrainable uniforms:
44  *  "uSpineShadowParameter" - The two parameters are the major&minor radius (in pixels) to form an ellipse shape. The top-left
45  *                            quarter of this ellipse is used to calculate spine normal for simulating shadow
46  *  "uIsBackImageVisible"   - Set whether the current page is with its backside visible. Need to pass the parameter as true for
47  *                            the page which is turned over but still visible in Landscape
48  *  "uPageWidth"            - The page width of the PageTurnBookSpineEffect
49  *
50  * @return A handle to a newly allocated ShaderEffect
51  **/
52 inline ShaderEffect CreatePageTurnBookSpineEffect()
53 {
54   std::string vertexSource = DALI_COMPOSE_SHADER(
55       precision mediump float;\n
56       void main()\n
57       {\n
58           gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n
59           vTexCoord = aTexCoord;\n
60       }\n);
61
62   // the simplified version of the fragment shader of page turn effect
63   std::string fragmentSource = DALI_COMPOSE_SHADER(
64       precision mediump float;\n
65       uniform float uIsBackImageVisible;\n
66       uniform float uPageWidth;\n
67       uniform vec2 uSpineShadowParameter;\n
68       void main()\n
69       {\n
70       // flip the image horizontally by changing the x component of the texture coordinate
71         if( uIsBackImageVisible == 1.0 )\n
72           gl_FragColor = texture2D( sTexture, vec2( sTextureRect.p+sTextureRect.s-vTexCoord.x, vTexCoord.y ) ) * uColor; \n
73         else\n
74         gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
75       // display book spine, a stripe of shadowed texture
76         float pixelPos = (vTexCoord.x-sTextureRect.s)*uPageWidth; \n
77         if(pixelPos < uSpineShadowParameter.x) \n
78         {\n
79           float x = pixelPos - uSpineShadowParameter.x;\n
80           float y = sqrt( uSpineShadowParameter.x*uSpineShadowParameter.x - x*x );\n
81           vec2 spineNormal = normalize(vec2(uSpineShadowParameter.y*x/uSpineShadowParameter.x, y));\n
82           gl_FragColor.rgb *= spineNormal.y; \n
83         }
84       } );
85
86   const Vector2 DEFAULT_SPINE_SHADOW_PARAMETER(50.0f, 20.0f);
87
88   ShaderEffect shaderEffect = ShaderEffect::New( vertexSource, fragmentSource );
89
90   shaderEffect.SetUniform( "uIsBackImageVisible", -1.f );
91   shaderEffect.SetUniform( "uSpineShadowParameter", DEFAULT_SPINE_SHADOW_PARAMETER );
92
93   float defaultPageWidth = Dali::Stage::GetCurrent().GetSize().x;
94   shaderEffect.SetUniform( "uPageWidth", defaultPageWidth );
95
96   return shaderEffect;
97 }
98
99 } //namespace Internal
100
101 } // namespace Toolkit
102
103 } // namespace Dali
104
105 #endif /* __DALI_PAGE_TURN_BOOK_SPINE_EFFECT_H__ */