aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick3d/xr_touch/ScreenContent.qml
blob: 056b4e230b29a8d121253fe3312a2abd98ba2970 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQuick
import QtQuick.Layouts
import QtQuick.Controls

Rectangle {
    width: 640
    height: 400
    color: "white"
    ColumnLayout {
        anchors.fill: parent
        Text {
            Layout.alignment: Qt.AlignHCenter
            text: "XR Touch Example"
        }
        RowLayout {
            ColumnLayout {
                Button {
                    text: "Button 1"
                    Layout.margins: 15
                }
                Button {
                    text: "Button 2"
                    Layout.margins: 15
                }
                Button {
                    text: "Button 3"
                    Layout.margins: 15
                }
                Slider {}
            }
            Rectangle {
                id: field
                Layout.fillHeight: true
                Layout.fillWidth: true
                color: "#ffeedd"
                MultiPointTouchArea {
                    anchors.fill: parent
                    mouseEnabled: true // For debug purposes
                    touchPoints: [
                        TouchPoint { id: point1 },
                        TouchPoint { id: point2 }
                    ]
                }
                Rectangle {
                    x: point1.x - 15
                    y: point1.y - 15
                    visible: point1.pressed
                    width: 30; height: 30
                    radius: 15
                    color: point1.pointId === 0 ? "green" : "red"
                }

                Rectangle {
                    x: point2.x - 15
                    y: point2.y - 15
                    visible: point2.pressed
                    width: 30; height: 30
                    radius: 15
                    color: point2.pointId === 0 ? "green" : "red"
                }


                Rectangle {
                    id: ball
                    width: 10; height: 10
                    radius: 5
                    color: "black"
                    property vector2d pos: Qt.vector2d(field.width / 2, field.height / 2)
                    property vector2d speed: Qt.vector2d(1, 1)
                    x: pos.x - radius / 2
                    y: pos.y - radius / 2
                    FrameAnimation {
                        running: true
                        onTriggered: {
                            // Touch points attract
                            const scale = 1.0 / Math.max(field.width, field.height)
                            let relativeAccel = Qt.vector2d(0, 0)
                            for (const point of [point1, point2]) {
                                if (point.pressed) {
                                    const vector = ball.pos.minus(Qt.vector2d(point.x, point.y)).times(scale)
                                    const dist = Math.max(100, vector.length() * 200)
                                    const a = vector.normalized().times(1 / (dist * dist))
                                    relativeAccel = relativeAccel.plus(a)
                                }
                            }
                            const accel = relativeAccel.times(1/scale)
                            ball.speed = ball.speed.minus(accel)

                            // Bounce off the edges with dampening
                            const nextX = ball.pos.x + ball.speed.x
                            if (nextX < 0 || nextX > field.width)
                                ball.speed.x = -ball.speed.x * 0.8
                            const nextY = ball.pos.y + ball.speed.y
                            if (nextY < 0 || nextY > field.height)
                                ball.speed.y = -ball.speed.y * 0.8
                            ball.pos = ball.pos.plus(ball.speed)
                        }
                    }
                }

                Text {
                    text: "Touch area"
                    anchors.centerIn: parent
                }
            }
        }
    }
}