Merge "Text - Initialize the mUpdateCursorHookPosition member in the constructor...
[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) 2016 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 // INTERNAL INCLUDES
22 #include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
23
24 #define DALI_COMPOSE_SHADER(STR) #STR
25
26 namespace Dali
27 {
28
29 namespace Toolkit
30 {
31
32 namespace Internal
33 {
34
35 /**
36  * @brief Creates a new PageTurnBookSpineEffect
37  * This is an assisting effect of PageTurnEffect to display a book spine on _static_ pages, and also to flip the image horizontally when needed.
38  *
39  * When the page is turned over in landscape, call
40  * SetIsBackImageVisible(true), this effect can display the back image
41  * correctly after the page been rotated 180 degrees.  To
42  * display the pages visually consistent with its turning state,
43  * please set the uniforms with the same values as the PageTurnEffect.
44  *
45  * Animatable/Constrainable uniforms:
46  *  "uSpineShadowParameter" - The two parameters are the major&minor radius (in pixels) to form an ellipse shape. The top-left
47  *                            quarter of this ellipse is used to calculate spine normal for simulating shadow *
48  *  "uTextureWidth" - 1.0 for single sided page,
49  *                    2.0 for double sided image which has left half part as page front side and right half part as page back side.
50  *
51  * @return The newly created Property::Map with the page turn book spine effect
52  **/
53 inline Property::Map CreatePageTurnBookSpineEffect()
54 {
55   const char* vertexSource = DALI_COMPOSE_SHADER(
56       precision mediump float;\n
57       attribute mediump vec2 aPosition;\n
58       uniform mediump mat4 uMvpMatrix;\n
59       uniform vec3 uSize;\n
60       uniform float uTextureWidth;\n
61       varying vec2 vTexCoord;\n
62       void main()\n
63       {\n
64         mediump vec4 vertexPosition = vec4(aPosition*uSize.xy, 0.0, 1.0);\n
65         gl_Position = uMvpMatrix * vertexPosition;\n
66         vTexCoord = aPosition + vec2(0.5);\n
67         vTexCoord.x /= uTextureWidth;
68       }\n);
69
70   // the simplified version of the fragment shader of page turn effect
71   const char* fragmentSource = DALI_COMPOSE_SHADER(
72       precision mediump float;\n
73       varying mediump vec2 vTexCoord;\n
74       uniform vec3 uSize;\n
75       uniform vec2 uSpineShadowParameter;\n
76       uniform sampler2D sTexture;\n
77       uniform lowp vec4 uColor;\n
78
79       void main()\n
80       {\n
81         if( gl_FrontFacing )\n // display front side
82         {\n
83           gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
84         }\n
85         else\n // display back side, flip the image horizontally by changing the x component of the texture coordinate
86         {\n
87           gl_FragColor = texture2D( sTexture, vec2( 1.0 - vTexCoord.x, vTexCoord.y ) ) * uColor;\n
88         }\n
89         // display book spine, a stripe of shadowed texture
90         float pixelPos = vTexCoord.x * uSize.x;\n
91         if( pixelPos < uSpineShadowParameter.x )\n
92         {\n
93           float x = pixelPos - uSpineShadowParameter.x;\n
94           float y = sqrt( uSpineShadowParameter.x*uSpineShadowParameter.x - x*x );\n
95           vec2 spineNormal = normalize(vec2(uSpineShadowParameter.y*x/uSpineShadowParameter.x, y));\n
96           gl_FragColor.rgb *= spineNormal.y; \n
97         }\n
98       } );
99
100   Property::Map map;
101
102   Property::Map customShader;
103
104   customShader[ Toolkit::Visual::Shader::Property::VERTEX_SHADER ] = vertexSource;
105   customShader[ Toolkit::Visual::Shader::Property::FRAGMENT_SHADER ] = fragmentSource;
106
107   map[ Toolkit::VisualProperty::SHADER ] = customShader;
108   return map;
109 }
110
111 } //namespace Internal
112
113 } // namespace Toolkit
114
115 } // namespace Dali
116
117 #endif // DALI_PAGE_TURN_BOOK_SPINE_EFFECT_H