~rschroll/+junk/level_flat

« back to all changes in this revision

Viewing changes to Main.qml

  • Committer: Robert Schroll
  • Date: 2015-04-22 19:58:31 UTC
  • Revision ID: rschroll@gmail.com-20150422195831-ve398mbepc7ow07l
Add horizontal mode

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
        property real smoothing: 0.03
29
29
        // Angle of acceleration vector in X-Y plane
30
30
        property real theta
 
31
        // Angle of acceleration out of X-Y plane
 
32
        property real phi
31
33
 
32
34
        onReadingChanged: {
33
 
            var newTheta = Math.atan2(accel.reading.y, accel.reading.x) * 180 / Math.PI
 
35
            var newTheta = Math.atan2(reading.y, reading.x) * 180 / Math.PI
34
36
            if (newTheta > 90 && theta < -90)
35
37
                theta += 360
36
38
            else if (theta > 90 && newTheta < -90)
37
39
                theta -= 360
38
40
            theta = smoothing * newTheta + (1 - smoothing) * theta
 
41
 
 
42
            var newPhi = Math.atan2(reading.z, Math.sqrt(reading.x*reading.x + reading.y*reading.y)) * 180 / Math.PI
 
43
            phi = smoothing * newPhi + (1 - smoothing) * phi
39
44
        }
40
45
    }
41
46
 
42
47
    Rectangle {
43
 
        id: container
 
48
        id: vertLevel
 
49
        visible: accel.phi > -45 && accel.phi < 45
44
50
        rotation: 90 - accel.theta
45
51
        anchors.centerIn: parent
46
52
        height: Math.sqrt((parent.height*parent.height) + (parent.width*parent.width))
62
68
            anchors.bottomMargin: units.gu(5)
63
69
            anchors.horizontalCenter: parent.horizontalCenter
64
70
            // The mod function is broken for negative numbers
65
 
            text: Math.round(container.rotation + 360) % 360 + "&deg;"
 
71
            text: Math.round(vertLevel.rotation + 360) % 360 + "&deg;"
66
72
            textFormat: Text.RichText
67
73
        }
68
74
    }
69
75
 
 
76
    Rectangle {
 
77
        id: horizLevel
 
78
        visible: !vertLevel.visible
 
79
        anchors.fill: parent
 
80
        color: "white"
 
81
 
 
82
        function distForAngle(angle) {
 
83
            return width / 2 * Math.sqrt(angle / 45)
 
84
        }
 
85
 
 
86
        Repeater {
 
87
            model: [40, 30, 20, 10, 5, 1]
 
88
            Rectangle {
 
89
                anchors.centerIn: parent
 
90
                width: 2 * horizLevel.distForAngle(modelData)
 
91
                height: width
 
92
                radius: width/2
 
93
                border.color: "black"
 
94
                border.width: units.dp(2)
 
95
            }
 
96
        }
 
97
 
 
98
        Rectangle {
 
99
            width: units.gu(3)
 
100
            height: width
 
101
            radius: width/2
 
102
            color: "red"
 
103
            x: (horizLevel.width - width) / 2 + Math.cos(accel.theta * Math.PI / 180) * horizLevel.distForAngle(90 - Math.abs(accel.phi))
 
104
            y: (horizLevel.height - height) / 2 - Math.sin(accel.theta * Math.PI / 180) * horizLevel.distForAngle(90 - Math.abs(accel.phi))
 
105
        }
 
106
    }
70
107
}
71
108