diff options
author | Nicholas Bennett <nicholas.bennett@qt.io> | 2023-08-17 06:49:16 +0300 |
---|---|---|
committer | Nicholas Bennett <nicholas.bennett@qt.io> | 2023-09-02 03:40:04 +0000 |
commit | 62b33327c03d04a18d4f23eb8decfda41cbfeb49 (patch) | |
tree | 3386eaad3f89712c18d5acc375082cf85454d02b /examples/demos/dice/main.qml | |
parent | 0aa54b0c3401a3aadb9f5246c14379c1f4aa92dd (diff) |
Dice Demo: Add sound effects and shake to roll
Added the features in line with the spike outcome.
In addition, I modified the spawnDice function to
estimate if all dice are at rest before enabling spawning.
This prevents duplicate shake events and double tap
fumbles.
Task-number: QTBUG-116491
Pick-to: 6.6
Change-Id: Ia0dc59e469e008da5fc3afe2a7342381eadc1008
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Diffstat (limited to 'examples/demos/dice/main.qml')
-rw-r--r-- | examples/demos/dice/main.qml | 50 |
1 files changed, 36 insertions, 14 deletions
diff --git a/examples/demos/dice/main.qml b/examples/demos/dice/main.qml index 1b36d9c7b..8495f6b9d 100644 --- a/examples/demos/dice/main.qml +++ b/examples/demos/dice/main.qml @@ -1,12 +1,11 @@ // Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - import QtQuick -import QtQml import QtQuick.Window import QtQuick.Controls import QtQuick.Controls.Material import QtQuick.Layouts +import QtSensors ApplicationWindow { id: root @@ -15,6 +14,28 @@ ApplicationWindow { visible: true property real currDrawerWidth: drawer.width * drawer.position + Accelerometer { + id: accelerometer + dataRate: 25 + active: true + property real previousForce: 0 + property real multiplier: 100 + onReadingChanged: { + let force = Qt.vector3d(reading.x, reading.y, reading.z - 9.81) + if (isShake(force, previousForce)) { + tapLabel.hide() + force.x *= multiplier + force.y *= multiplier + force.z *= multiplier + scene.spawnDice(diceSlider.value, force) + } + previousForce = force.length() + } + function isShake(force, previousForce) { + return ((force.length() > 16) && (force.length() < previousForce)) + } + } + Scene { id: scene x: currDrawerWidth - 10 // pad 10px for rounded corners @@ -31,7 +52,7 @@ ApplicationWindow { centerIn: parent } - text: qsTr("Tap/click to throw dice") + text: qsTr("Tap, click or shake to throw dice") font.pixelSize: 32 font.bold: true style: Text.Raised @@ -47,7 +68,7 @@ ApplicationWindow { function hide() { if (tapLabel.opacity >= 1) { - tapLabelAnimation.running = true; + tapLabelAnimation.running = true } } } @@ -57,23 +78,23 @@ ApplicationWindow { fill: parent } onClicked: { - tapLabel.hide(); - scene.spawnDice(diceSlider.value); + tapLabel.hide() + scene.spawnDice(diceSlider.value, Qt.vector3d(0, 0, 0)) } } function updateGravity() { if (gravityZero.checked) { - scene.settingGravity = 0; + scene.settingGravity = 0 } if (gravityMoon.checked) { - scene.settingGravity = 162; + scene.settingGravity = 162 } if (gravityMars.checked) { - scene.settingGravity = 371; + scene.settingGravity = 371 } if (gravityEarth.checked) { - scene.settingGravity = 980.7; + scene.settingGravity = 980.7 } } } @@ -185,7 +206,7 @@ ApplicationWindow { to: 10 value: 5 stepSize: 1 - onValueChanged: scene.spawnDice(value) + onValueChanged: scene.spawnDice(value, Qt.vector3d(0, 0, 0)) } // Throw dice @@ -193,7 +214,8 @@ ApplicationWindow { id: throwButton Layout.alignment: Qt.AlignHCenter text: qsTr("Throw dice") - onClicked: scene.spawnDice(diceSlider.value) + onClicked: scene.spawnDice(diceSlider.value, + Qt.vector3d(0, 0, 0)) } } } @@ -203,8 +225,8 @@ ApplicationWindow { text: qsTr("\u2630") x: currDrawerWidth onClicked: { - tapLabel.hide(); - drawer.open(); + tapLabel.hide() + drawer.open() } } } |