mirror of https://github.com/qt/qtdoc.git
Dice: Improve shake detection
This change enhances the readability of the code and improves the behavior of triggering a roll. It adds a small delay to prevent multiple rolls from occurring in short intervals. Pick-to: 6.8 6.9 6.9.0 Change-Id: I657450be50742d9b6236e2c29cf365b04349f191 Reviewed-by: Nicholas Bennett <nicholas.bennett@qt.io>
This commit is contained in:
parent
6b5a26d537
commit
a7c2e00368
|
@ -18,23 +18,23 @@ ApplicationWindow {
|
|||
id: accelerometer
|
||||
dataRate: 25
|
||||
active: true
|
||||
property real previousForce: 0
|
||||
property real multiplier: 100
|
||||
onReadingChanged: {
|
||||
const accelerometerReading = reading as AccelerometerReading
|
||||
let force = Qt.vector3d(accelerometerReading.x, accelerometerReading.y, accelerometerReading.z - 9.81)
|
||||
if (isShake(force, previousForce)) {
|
||||
tapLabel.opacity = 0
|
||||
force.x *= multiplier
|
||||
force.y *= multiplier
|
||||
force.z *= multiplier
|
||||
scene.rollForce = force
|
||||
scene.spawnDice()
|
||||
}
|
||||
previousForce = force.length()
|
||||
readonly property vector3d force: Qt.vector3d(reading.x, reading.y, reading.z)
|
||||
readonly property bool highForce : Math.abs(force.length() - 9.81) > 16
|
||||
|
||||
onHighForceChanged: {
|
||||
if (!highForce)
|
||||
shakeTimeout.restart()
|
||||
}
|
||||
function isShake(force, previousForce) {
|
||||
return ((force.length() > 16) && (force.length() < previousForce))
|
||||
}
|
||||
|
||||
Timer {
|
||||
id: shakeTimeout
|
||||
interval: 200
|
||||
running: false
|
||||
repeat: false;
|
||||
onTriggered: {
|
||||
scene.rollForce = accelerometer.force.times(100.0)
|
||||
scene.spawnDice()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue