aboutsummaryrefslogtreecommitdiffstats
path: root/examples/universalinput/mousegrab/main.qml
blob: 37c9a060083fea401eae6c86fac2eb056f19aa1d (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQuick
import QtQuick.Controls
import QtUniversalInput

ApplicationWindow {
    id: mainWindow
    visible: true
    width: 800
    height: 400
    title: "Mousegrab"

    Rectangle {
        id: gameArea
        width: parent.width
        height: parent.height
        color: "gray"

        Rectangle {
            id: player
            width: 10
            height: 10
            color: "red"

            Component.onCompleted: {
                resetPosition()
            }

            function resetPosition() {
                x = parent.width / 2 - width / 2
                y = parent.height / 2 - height / 2
            }

            function move(amount) {
                x += amount.x
                y += amount.y
            }
        }

        Keys.onPressed: event => {
                            if (event.key === Qt.Key_Escape) {
                                gameArea.focus = false
                            }
                        }

        onFocusChanged: {
            universalInput.mouseDisabled = focus
            if (focus) {
                player.resetPosition()
                universalInput.firstFramesAfterFocus = true
            }

        }

        MouseArea {
            anchors.fill: parent
            onPressed: {
                gameArea.focus = true
            }
        }

        Label {
            x: 10
            y: 10
            text: "Press ESC to release mouse"
        }

        Label {
            x: 10
            y: 30
            text: "Click to grab mouse"
        }

        Label {
            id: deltaLabel
            x: 10
            y: 50
            text: "Mouse deltas: (0,0)"
        }
    }

    UniversalInput {
        id: universalInput
        mouseDisabled: gameArea.focus

        property bool firstFramesAfterFocus: false
        property int frameCount: 0

        onMouseDeltaChanged: delta => {
                                 deltaLabel.text = "Mouse deltas: (" + delta.x + "," + delta.y + ")"

                                if (mouseDisabled) {
                                     // Big delta after focus is received, so delay the action
                                     if (firstFramesAfterFocus) {
                                         if (frameCount < 1) {
                                             ++frameCount
                                             return
                                         }
                                         frameCount = 0
                                         firstFramesAfterFocus = false
                                         return
                                     }
                                     player.move(delta)
                                 }
                             }
    }
}