summaryrefslogtreecommitdiffstats
path: root/tools/qqem/qml/EffectPreview.qml
blob: d7139fd6aa04c65f4144b305ec59825a9f764810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

import QtQuick
import "../nodes/common"

Item {
    id: rootItem

    property real animatedTime: previewFrameTimer.elapsedTime + playbackTimeComponent.scrubTime
    property int animatedFrame: previewFrameTimer.currentFrame + playbackTimeComponent.scrubFrame
    property bool timeRunning: previewAnimationRunning
    property rect paddingRect: effectManager.effectPadding
    property alias source: source

    // Scale the content automatically to closest scale step
    // With a small minimum margin
    function autoScaleToContent() {
        const minMargin = 10;
        var areaWidth = rootItem.width;
        var areaHeight = rootItem.height - effectPreviewToolbar.height;
        var maxScaleW = areaWidth / (componentParent.width + minMargin);
        var maxScaleH = areaHeight / (componentParent.height + minMargin);
        var maxScale = Math.min(maxScaleW, maxScaleH);
        var optimalScale = Math.floor(maxScale / effectPreviewToolbar.contentScaleStep) * effectPreviewToolbar.contentScaleStep;
        effectPreviewToolbar.updateScaleSlider(optimalScale);
    }

    function renderToImage(filename) {
        componentParent.grabToImage(function(result) {
            result.saveToFile(filename);
        });
    }

    function selectLastPreviewSource() {
        effectPreviewToolbar.selectLastPreviewSource();
    }

    clip: true

    Image {
        anchors.fill: parent
        source: effectPreviewToolbar.currentBackgroundImage
        fillMode: Image.PreserveAspectCrop
    }

    Item {
        id: source
        x: -paddingRect.x
        y: -paddingRect.y
        width: sourceImage.width + paddingRect.x + paddingRect.width
        height: sourceImage.height + paddingRect.y + paddingRect.height
        layer.enabled: true
        layer.mipmap: true
        layer.smooth: effectPreviewToolbar.useSmoothContent
        visible: false
        Image {
            id: sourceImage
            x: paddingRect.x
            y: paddingRect.y
            source: effectPreviewToolbar.currentSourceImage
            fillMode: Image.PreserveAspectFit
            smooth: effectPreviewToolbar.useSmoothContent
            Behavior on source {
                SequentialAnimation {
                    NumberAnimation {
                        target: sourceImage
                        property: "opacity"
                        to: 0.0
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }
                    PropertyAction {
                        target: sourceImage
                        property: "source"
                    }
                    ScriptAction {
                        script: {
                            // When image is changed, automatically scale it
                            // And do this directly, without smoothness
                            effectPreviewToolbar.smoothScaling = false;
                            autoScaleToContent();
                            effectPreviewToolbar.smoothScaling = true;
                        }
                    }
                    NumberAnimation {
                        target: sourceImage
                        property: "opacity"
                        to: 1.0
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }
                }
            }
        }
    }

    // Mouse handling for iMouse variable
    property real _effectMouseX: 0
    property real _effectMouseY: 0
    property real _effectMouseZ: 0
    property real _effectMouseW: 0
    MouseArea {
        anchors.fill: componentParent
        scale: componentParent.scale
        onPressed: (mouse)=> {
            _effectMouseX = mouse.x
            _effectMouseY = mouse.y
            _effectMouseZ = mouse.x
            _effectMouseW = mouse.y
            clickTimer.restart();
        }
        onPositionChanged: (mouse)=> {
            _effectMouseX = mouse.x
            _effectMouseY = mouse.y
        }
        onReleased: (mouse)=> {
            _effectMouseZ = -(_effectMouseZ)
        }
        onWheel: (wheel)=> {
            if (wheel.angleDelta.y > 0)
                effectPreviewToolbar.contentZoomIn();
            else
                effectPreviewToolbar.contentZoomOut();
        }

        Timer {
            id: clickTimer
            interval: 20
            onTriggered: {
                _effectMouseW = -(_effectMouseW)
            }
        }
    }

    BlurHelper {
        id: blurHelper
        anchors.fill: parent
        property int blurMax: g_propertyData.blur_helper_max_level ? g_propertyData.blur_helper_max_level : 64
        property real blurMultiplier: g_propertyData.blurMultiplier ? g_propertyData.blurMultiplier : 0
    }

    Item {
        id: componentParent
        width: source.width
        height: source.height
        anchors.centerIn: parent
        anchors.verticalCenterOffset: effectPreviewToolbar.height / 2
        scale: effectPreviewToolbar.contentScale
        // Cache the layer. This way heavy shaders rendering doesn't
        // slow down code editing & rest of the UI.
        layer.enabled: true
        layer.smooth: effectPreviewToolbar.useSmoothContent
    }

    BorderImage {
        source: "images/item_border.png"
        anchors.fill: componentParent
        anchors.margins: -2
        border.left: 4
        border.top: 4
        border.right: 4
        border.bottom: 4
        scale: componentParent.scale
        visible: effectPreviewToolbar.showContentBorders
    }

    EffectPreviewToolbar {
        id: effectPreviewToolbar
        anchors.top: parent.top
        width: parent.width
    }

    function createNewComponent() {
        var oldComponent = componentParent.children[0];
        if (oldComponent)
            oldComponent.destroy();

        try {
            const newObject = Qt.createQmlObject(
                effectManager.qmlComponentString,
                componentParent,
                ""
            );
            effectManager.resetEffectError(0);
        } catch(error) {
            let errorString = "QML: ERROR: ";
            let errorLine = -1;
            if (error.qmlErrors.length > 0) {
                // Show the first QML error
                let e = error.qmlErrors[0];
                errorString += e.lineNumber + ": " + e.message;
                errorLine = e.lineNumber;
            }
            effectManager.setEffectError(errorString, 0, errorLine);
        }
    }

    Connections {
        target: effectManager
        function onShadersBaked() {
            mainWindow.releaseResources();
            updateTimer.restart();
        }
    }
    Connections {
        target: effectManager.uniformModel
        function onUniformsChanged() {
            mainWindow.releaseResources();
            updateTimer.restart();
        }
    }
    Connections {
        target: effectManager
        function onQmlComponentStringChanged() {
            mainWindow.releaseResources();
            updateTimer.restart();
        }
    }

    Timer {
        id: updateTimer
        interval: effectManager.effectUpdateDelay();
        onTriggered: {
            console.debug("Updating ShaderEffect!");
            effectManager.updateQmlComponent();
            createNewComponent();
        }
    }
}