diff options
author | Gatis Paeglis <gatis.paeglis@digia.com> | 2013-09-23 15:37:53 +0200 |
---|---|---|
committer | Gatis Paeglis <gatis.paeglis@digia.com> | 2013-10-03 17:39:56 +0300 |
commit | dae902e9d22f89a890252633c66fd7ee26636192 (patch) | |
tree | 0fd36990d97584c4259c7147177b9480391c2925 /basicsuite/Sensors/Accelbubble.qml | |
parent | 1bf9d73eff18fde3f7cfe0e6aa7e7edc39264e77 (diff) |
Add sensor demo to basicsuite
Change-Id: I72d733ffe29aaf99fdf67e9b981ffbecb9dacc1a
Reviewed-by: Gatis Paeglis <gatis.paeglis@digia.com>
Diffstat (limited to 'basicsuite/Sensors/Accelbubble.qml')
-rw-r--r-- | basicsuite/Sensors/Accelbubble.qml | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/basicsuite/Sensors/Accelbubble.qml b/basicsuite/Sensors/Accelbubble.qml new file mode 100644 index 0000000..c5b75ad --- /dev/null +++ b/basicsuite/Sensors/Accelbubble.qml @@ -0,0 +1,68 @@ +import QtQuick 2.0 +import QtSensors 5.0 + +Item { + Rectangle { + id: field + color: "lightblue" + border.width: 1 + border.color: "darkblue" + width: parent.width + height: parent.height + Accelerometer { + id: accel + active:true + onReadingChanged: { + var newX = (bubble.x + calcRoll(accel.reading.x, accel.reading.y, accel.reading.z) * .1) + var newY = (bubble.y - calcPitch(accel.reading.x, accel.reading.y, accel.reading.z) * .1) + + if (newX < 0) + newX = 0 + if (newY < 0) + newY = 0 + + var right = field.width - bubble.width + var bottom = field.height - bubble.height + + if (newX > right) + newX = right + if (newY > bottom) + newY = bottom + + bubble.x = newX + bubble.y = newY + } + } + + Image { + id: bubble + source: "bluebubble.png" + property real centerX: parent.width / 2 + property real centerY: parent.height / 2; + property real bubbleCenter: bubble.width / 2 + x: centerX - bubbleCenter + y: centerY - bubbleCenter + smooth: true + + Behavior on y { + SmoothedAnimation { + easing.type: Easing.Linear + duration: 100 + } + } + Behavior on x { + SmoothedAnimation { + easing.type: Easing.Linear + duration: 100 + } + } + } + } + + function calcPitch(x,y,z) { + return Math.atan(y / Math.sqrt(x*x + z*z)) * 57.2957795; + } + function calcRoll(x,y,z) { + return Math.atan(x / Math.sqrt(y*y + z*z)) * 57.2957795; + } +} |