65c2a3dc6b8b5373700426dfae04cbf971f0e7ac
[profile/ivi/qtdeclarative.git] / examples / demos / tweetsearch / content / FlipBar.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 2.0
42
43 Item {
44     id: container
45     property int animDuration: 300
46     property Item front: Item {}
47     property Item back: Item {}
48     property real factor: 0.1 // amount the edges fold in for the 3D effect
49     property alias delta: effect.delta
50     property Item cur: frontShown ? front : back
51     property Item noncur: frontShown ? back : front
52
53     function swap() {
54         var tmp = front;
55         front = back;
56         back = tmp;
57         resync();
58     }
59
60     width: cur.width
61     height: cur.height
62     onFrontChanged: resync(); 
63     onBackChanged: resync();
64
65     function resync() {//TODO: Are the items ever actually visible?
66         back.parent = container;
67         front.parent = container;
68         frontShown ? back.visible = false : front.visible = false;
69     }
70
71     property bool frontShown: true
72
73     onFrontShownChanged: {
74         back.visible = !frontShown
75         front.visible = frontShown
76     }
77
78     Rectangle {
79         anchors.fill: parent
80         color: "white"
81     }
82
83     function flipUp(start) {
84         effect.visible = true;
85         effect.sourceA = effect.source1
86         effect.sourceB = effect.source2
87         if (start == undefined)
88             start = 1.0;
89         deltaAnim.from = start;
90         deltaAnim.to = 0.0
91         dAnim.start();
92         frontShown = false;
93     }
94
95     function flipDown(start) {
96         effect.visible = true;
97         effect.sourceA = effect.source1
98         effect.sourceB = effect.source2
99         if (start == undefined)
100             start = 0.0;
101         deltaAnim.from = start;
102         deltaAnim.to = 1.0
103         dAnim.start();
104         frontShown = true;
105     }
106
107     ShaderEffect {
108         id: effect
109         width: cur.width
110         height: cur.height
111         property real factor: container.factor * width
112         property real delta: 1.0
113
114         mesh: GridMesh { resolution: Qt.size(8,2) }
115
116         SequentialAnimation on delta {
117             id: dAnim
118             running: false
119             NumberAnimation {
120             id: deltaAnim
121             duration: animDuration//expose anim
122             }
123         }
124
125         property variant sourceA: source1
126         property variant sourceB: source1
127         property variant source1: ShaderEffectSource {
128             sourceItem: front
129             hideSource: effect.visible
130         }
131
132         property variant source2: ShaderEffectSource {
133             sourceItem: back
134             hideSource: effect.visible
135         }
136         
137         fragmentShader: "
138             uniform lowp float qt_Opacity;
139             uniform sampler2D sourceA;
140             uniform sampler2D sourceB;
141             uniform highp float delta;
142             varying highp vec2 qt_TexCoord0;
143             void main() {
144                 highp vec4 tex = vec4(qt_TexCoord0.x, qt_TexCoord0.y * 2.0, qt_TexCoord0.x, (qt_TexCoord0.y-0.5) * 2.0);
145                 highp float shade = clamp(delta*2.0, 0.5, 1.0);
146                 highp vec4 col;
147                 if (qt_TexCoord0.y < 0.5) {
148                     col = texture2D(sourceA, tex.xy) * (shade);
149                 } else {
150                     col = texture2D(sourceB, tex.zw) * (1.5 - shade);
151                     col.w = 1.0;
152                 }
153                 gl_FragColor = col * qt_Opacity;
154             }
155         "
156         property real h: height
157         vertexShader: "
158         uniform highp float delta;
159         uniform highp float factor;
160         uniform highp float h;
161         uniform highp mat4 qt_Matrix;
162         attribute highp vec4 qt_Vertex;
163         attribute highp vec2 qt_MultiTexCoord0;
164         varying highp vec2 qt_TexCoord0;
165         void main() {
166             highp vec4 pos = qt_Vertex;
167             if (qt_MultiTexCoord0.y == 0.0)
168                 pos.x += factor * (1. - delta) * (qt_MultiTexCoord0.x * -2.0 + 1.0);
169             else if (qt_MultiTexCoord0.y == 1.0)
170                 pos.x += factor * (delta) * (qt_MultiTexCoord0.x * -2.0 + 1.0);
171             else
172                 pos.y = delta * h;
173             gl_Position = qt_Matrix * pos;
174             qt_TexCoord0 = qt_MultiTexCoord0;
175         }"
176
177     }
178 }