1/****************************************************************************
2**
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
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 Digia Plc and its Subsidiary(-ies) nor the names
21**     of its contributors may be used to endorse or promote products derived
22**     from this software without specific prior written permission.
23**
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
41import QtQuick 2.0
42import io.thp.pyotherside 1.5
43
44
45Item {
46    width: 400
47    height: 400
48
49    // The checkers background
50    ShaderEffect {
51        id: tileBackground
52        anchors.fill: parent
53
54        property real tileSize: 16
55        property color color1: Qt.rgba(0.9, 0.9, 0.9, 1);
56        property color color2: Qt.rgba(0.85, 0.85, 0.85, 1);
57
58        property size pixelSize: Qt.size(width / tileSize, height / tileSize);
59
60        fragmentShader:
61            "
62            uniform lowp vec4 color1;
63            uniform lowp vec4 color2;
64            uniform highp vec2 pixelSize;
65            varying highp vec2 qt_TexCoord0;
66            void main() {
67                highp vec2 tc = sign(sin(3.14152 * qt_TexCoord0 * pixelSize));
68                if (tc.x != tc.y)
69                    gl_FragColor = color1;
70                else
71                    gl_FragColor = color2;
72            }
73            "
74    }
75
76    PyFBO {
77        id: fbo
78        anchors.fill: parent
79        anchors.margins: 10
80        property var t: 0
81
82        SequentialAnimation on t {
83            NumberAnimation { to: 1; duration: 2500; easing.type: Easing.InQuad }
84            NumberAnimation { to: 0; duration: 2500; easing.type: Easing.OutQuad }
85            loops: Animation.Infinite
86            running: true
87        }
88
89        // The transform is just to show something interesting..
90        transform: [
91            Rotation { id: rotation; axis.x: 0; axis.z: 0; axis.y: 1; angle: 0; origin.x: fbo.width / 2; origin.y: fbo.height / 2; },
92            Translate { id: txOut; x: -fbo.width / 2; y: -fbo.height / 2 },
93            Scale { id: scale; },
94            Translate { id: txIn; x: fbo.width / 2; y: fbo.height / 2 }
95        ]
96
97        onTChanged: {
98            if (renderer) {
99                py.call(py.getattr(renderer, 'set_t'), [t], update);
100            }
101        }
102    }
103
104    // Just to show something interesting
105    SequentialAnimation {
106        PauseAnimation { duration: 5000 }
107        ParallelAnimation {
108            NumberAnimation { target: scale; property: "xScale"; to: 0.6; duration: 1000; easing.type: Easing.InOutBack }
109            NumberAnimation { target: scale; property: "yScale"; to: 0.6; duration: 1000; easing.type: Easing.InOutBack }
110        }
111        NumberAnimation { target: rotation; property: "angle"; to: 80; duration: 1000; easing.type: Easing.InOutCubic }
112        NumberAnimation { target: rotation; property: "angle"; to: -80; duration: 1000; easing.type: Easing.InOutCubic }
113        NumberAnimation { target: rotation; property: "angle"; to: 0; duration: 1000; easing.type: Easing.InOutCubic }
114        NumberAnimation { target: fbo; property: "opacity"; to: 0.5; duration: 1000; easing.type: Easing.InOutCubic }
115        PauseAnimation { duration: 1000 }
116        NumberAnimation { target: fbo; property: "opacity"; to: 0.8; duration: 1000; easing.type: Easing.InOutCubic }
117        ParallelAnimation {
118            NumberAnimation { target: scale; property: "xScale"; to: 1; duration: 1000; easing.type: Easing.InOutBack }
119            NumberAnimation { target: scale; property: "yScale"; to: 1; duration: 1000; easing.type: Easing.InOutBack }
120        }
121        running: true
122        loops: Animation.Infinite
123    }
124
125    Rectangle {
126        id: labelFrame
127        anchors.margins: -10
128        radius: 5
129        color: "white"
130        border.color: "black"
131        opacity: 0.8
132        anchors.fill: label
133    }
134
135    Text {
136        id: label
137        anchors.bottom: fbo.bottom
138        anchors.left: fbo.left
139        anchors.right: fbo.right
140        anchors.margins: 20
141        wrapMode: Text.WordWrap
142        text: "The squircle is an FBO, rendered by the application on the scene graph rendering thread. The FBO is managed and displayed using a PyFBO item."
143    }
144
145    Python {
146        id: py
147
148        Component.onCompleted: {
149            addImportPath(Qt.resolvedUrl('.'));
150            importModule('renderer', function () {
151                call('renderer.Renderer', [], function (renderer) {
152                    fbo.renderer = renderer;
153                });
154            });
155        }
156
157        onError: console.log(traceback);
158    }
159
160}
161