2020-08-13 18:15:37 +02:00
|
|
|
import QtQuick 2.14
|
|
|
|
|
import QtQuick.Window 2.14
|
|
|
|
|
import QtSensors 5.12
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2020-08-13 18:15:37 +02:00
|
|
|
Window {
|
|
|
|
|
id: window
|
2015-11-09 17:09:42 +01:00
|
|
|
visible: true
|
2020-08-13 18:15:37 +02:00
|
|
|
property alias mainWindow: mainWindow
|
|
|
|
|
property alias bubble: bubble
|
|
|
|
|
Rectangle {
|
|
|
|
|
id: mainWindow
|
|
|
|
|
color: "#ffffff"
|
2015-11-09 17:09:42 +01:00
|
|
|
anchors.fill: parent
|
2017-11-03 15:38:54 +01:00
|
|
|
|
2020-08-13 18:15:37 +02:00
|
|
|
Bubble {
|
|
|
|
|
id: bubble
|
|
|
|
|
x: bubble.centerX - bubbleCenter
|
|
|
|
|
y: bubble.centerY - bubbleCenter
|
|
|
|
|
bubbleCenter: bubble.width /2
|
|
|
|
|
centerX: mainWindow.width /2
|
|
|
|
|
centerY: mainWindow.height /2
|
|
|
|
|
|
|
|
|
|
Behavior on y {
|
|
|
|
|
SmoothedAnimation {
|
|
|
|
|
easing.type: Easing.Linear
|
|
|
|
|
duration: 100
|
2014-03-27 17:32:48 +01:00
|
|
|
}
|
2020-08-13 18:15:37 +02:00
|
|
|
}
|
|
|
|
|
Behavior on x {
|
|
|
|
|
SmoothedAnimation {
|
|
|
|
|
easing.type: Easing.Linear
|
|
|
|
|
duration: 100
|
2014-03-27 17:32:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
2015-11-09 17:09:42 +01:00
|
|
|
}
|
2016-12-07 14:58:32 +01:00
|
|
|
}
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2016-12-07 14:58:32 +01:00
|
|
|
Accelerometer {
|
2017-11-03 15:38:54 +01:00
|
|
|
id: accel
|
|
|
|
|
dataRate: 100
|
|
|
|
|
active: true
|
2020-08-13 18:15:37 +02:00
|
|
|
readonly property double radians_to_degrees: 180 / Math.PI
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2017-11-03 15:38:54 +01:00
|
|
|
onReadingChanged: {
|
|
|
|
|
var newX = (bubble.x + calcRoll(accel.reading.x, accel.reading.y, accel.reading.z) * 0.1)
|
|
|
|
|
var newY = (bubble.y - calcPitch(accel.reading.x, accel.reading.y, accel.reading.z) * 0.1)
|
2014-04-08 12:28:47 +02:00
|
|
|
|
2017-11-03 15:38:54 +01:00
|
|
|
if (isNaN(newX) || isNaN(newY))
|
|
|
|
|
return;
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2017-11-03 15:38:54 +01:00
|
|
|
if (newX < 0)
|
|
|
|
|
newX = 0
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2020-08-13 18:15:37 +02:00
|
|
|
if (newX > mainWindow.width - bubble.width)
|
|
|
|
|
newX = mainWindow.width - bubble.width
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2017-11-03 15:38:54 +01:00
|
|
|
if (newY < 18)
|
|
|
|
|
newY = 18
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2020-08-13 18:15:37 +02:00
|
|
|
if (newY > mainWindow.height - bubble.height)
|
|
|
|
|
newY = mainWindow.height - bubble.height
|
2015-11-09 17:09:42 +01:00
|
|
|
|
2020-08-13 18:15:37 +02:00
|
|
|
bubble.x = newX
|
|
|
|
|
bubble.y = newY
|
2015-11-09 17:09:42 +01:00
|
|
|
}
|
2017-11-03 15:38:54 +01:00
|
|
|
}
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2020-08-13 18:15:37 +02:00
|
|
|
function calcPitch(x,y,z) {
|
|
|
|
|
return -Math.atan2(y, Math.hypot(x, z)) * accel.radians_to_degrees;
|
2017-11-03 15:38:54 +01:00
|
|
|
}
|
2020-08-13 18:15:37 +02:00
|
|
|
function calcRoll(x,y,z) {
|
|
|
|
|
return -Math.atan2(x, Math.hypot(y, z)) * accel.radians_to_degrees;
|
2017-11-03 15:38:54 +01:00
|
|
|
}
|
2013-10-28 11:10:55 +01:00
|
|
|
}
|