JoystickThumbPad.qml 7.4 KB
Newer Older
1 2
import QtQuick                  2.3
import QtQuick.Controls         1.2
Don Gagne's avatar
Don Gagne committed
3 4 5 6

import QGroundControl.Palette       1.0
import QGroundControl.ScreenTools   1.0

dogmaphobic's avatar
dogmaphobic committed
7
Item {
8
    id:             _joyRoot
Don Gagne's avatar
Don Gagne committed
9

Don Gagne's avatar
Don Gagne committed
10 11 12 13
    property alias  lightColors:    mapPal.lightColors  ///< true: use light colors from QGCMapPalette for drawing
    property real   xAxis:          0                   ///< Value range [-1,1], negative values left stick, positive values right stick
    property real   yAxis:          0                   ///< Value range [-1,1], negative values up stick, positive values down stick
    property bool   yAxisThrottle:  false               ///< true: yAxis used for throttle, range [1,0], positive value are stick up
14
    property bool   yAxisThrottleCentered: false        ///< false: center yAxis in throttle for reverser and forward
Don Gagne's avatar
Don Gagne committed
15
    property real   xPositionDelta: 0                   ///< Amount to move the control on x axis
16
    property real   yPositionDelta: 0                   ///< Amount to move the control on y axis
Gus Grubba's avatar
Gus Grubba committed
17
    property bool   springYToCenter:true                ///< true: Spring Y to center on release
Don Gagne's avatar
Don Gagne committed
18

Don Gagne's avatar
Don Gagne committed
19 20 21
    property real   _centerXY:              width / 2
    property bool   _processTouchPoints:    false
    property real   stickPositionX:         _centerXY
22
    property real   stickPositionY:         yAxisThrottleCentered ? _centerXY : height
Don Gagne's avatar
Don Gagne committed
23 24 25

    QGCMapPalette { id: mapPal }

26 27 28 29 30
    onWidthChanged: calculateXAxis()
    onStickPositionXChanged: calculateXAxis()
    onHeightChanged: calculateYAxis()
    onStickPositionYChanged: calculateYAxis()

31 32
    function calculateXAxis() {
        if(!_joyRoot.visible) {
33 34
            return;
        }
Don Gagne's avatar
Don Gagne committed
35
        var xAxisTemp = stickPositionX / width
Don Gagne's avatar
Don Gagne committed
36 37 38
        xAxisTemp *= 2.0
        xAxisTemp -= 1.0
        xAxis = xAxisTemp
Don Gagne's avatar
Don Gagne committed
39
    }
Don Gagne's avatar
Don Gagne committed
40

41 42
    function calculateYAxis() {
        if(!_joyRoot.visible) {
43 44
            return;
        }
45
        var yAxisTemp = stickPositionY / height
Don Gagne's avatar
Don Gagne committed
46 47 48 49 50 51 52 53
        yAxisTemp *= 2.0
        yAxisTemp -= 1.0
        if (yAxisThrottle) {
            yAxisTemp = ((yAxisTemp * -1.0) / 2.0) + 0.5
        }
        yAxis = yAxisTemp
    }

54
    function reCenter() {
Don Gagne's avatar
Don Gagne committed
55
        _processTouchPoints = false
56

Don Gagne's avatar
Don Gagne committed
57 58 59
        // Move control back to original position
        xPositionDelta = 0
        yPositionDelta = 0
60

Don Gagne's avatar
Don Gagne committed
61 62
        // Center sticks
        stickPositionX = _centerXY
63
        if (yAxisThrottleCentered) {
64 65
            stickPositionY = _centerXY
        }
Don Gagne's avatar
Don Gagne committed
66 67
    }

68
    function thumbDown(touchPoints) {
69
        // Position the control around the initial thumb position
Don Gagne's avatar
Don Gagne committed
70
        xPositionDelta = touchPoints[0].x - _centerXY
71 72 73 74 75
        if (yAxisThrottle) {
            yPositionDelta = touchPoints[0].y - stickPositionY
        } else {
            yPositionDelta = touchPoints[0].y - _centerXY
        }
Don Gagne's avatar
Don Gagne committed
76 77
        // We need to wait until we move the control to the right position before we process touch points
        _processTouchPoints = true
Don Gagne's avatar
Don Gagne committed
78 79
    }

Don Gagne's avatar
Don Gagne committed
80
    /*
Don Gagne's avatar
Don Gagne committed
81
    // Keep in for debugging
Don Gagne's avatar
Don Gagne committed
82 83 84 85
    Column {
        QGCLabel { text: xAxis }
        QGCLabel { text: yAxis }
    }
Don Gagne's avatar
Don Gagne committed
86
    */
Don Gagne's avatar
Don Gagne committed
87

dogmaphobic's avatar
dogmaphobic committed
88 89 90 91 92
    Image {
        anchors.fill:       parent
        source:             lightColors ? "/res/JoystickBezel.png" : "/res/JoystickBezelLight.png"
        mipmap:             true
        smooth:             true
Don Gagne's avatar
Don Gagne committed
93 94
    }

95 96
    QGCColoredImage {
        color:                      lightColors ? "white" : "black"
97
        visible:                    yAxisThrottle
98 99 100 101 102 103 104 105 106 107 108 109 110
        height:                     ScreenTools.defaultFontPixelHeight
        width:                      height
        sourceSize.height:          height
        mipmap:                     true
        fillMode:                   Image.PreserveAspectFit
        source:                     "/res/clockwise-arrow.svg"
        anchors.right:              parent.right
        anchors.rightMargin:        ScreenTools.defaultFontPixelWidth
        anchors.verticalCenter:     parent.verticalCenter
    }

    QGCColoredImage {
        color:                      lightColors ? "white" : "black"
111
        visible:                    yAxisThrottle
112 113 114 115 116 117 118 119 120 121 122 123 124
        height:                     ScreenTools.defaultFontPixelHeight
        width:                      height
        sourceSize.height:          height
        mipmap:                     true
        fillMode:                   Image.PreserveAspectFit
        source:                     "/res/counter-clockwise-arrow.svg"
        anchors.left:               parent.left
        anchors.leftMargin:         ScreenTools.defaultFontPixelWidth
        anchors.verticalCenter:     parent.verticalCenter
    }

    QGCColoredImage {
        color:                      lightColors ? "white" : "black"
125
        visible:                    yAxisThrottle
126 127 128 129 130 131 132 133 134 135 136 137 138
        height:                     ScreenTools.defaultFontPixelHeight
        width:                      height
        sourceSize.height:          height
        mipmap:                     true
        fillMode:                   Image.PreserveAspectFit
        source:                     "/res/chevron-up.svg"
        anchors.top:                parent.top
        anchors.topMargin:          ScreenTools.defaultFontPixelWidth
        anchors.horizontalCenter:   parent.horizontalCenter
    }

    QGCColoredImage {
        color:                      lightColors ? "white" : "black"
139
        visible:                    yAxisThrottle
140 141 142 143 144 145 146 147 148 149 150
        height:                     ScreenTools.defaultFontPixelHeight
        width:                      height
        sourceSize.height:          height
        mipmap:                     true
        fillMode:                   Image.PreserveAspectFit
        source:                     "/res/chevron-down.svg"
        anchors.bottom:             parent.bottom
        anchors.bottomMargin:       ScreenTools.defaultFontPixelWidth
        anchors.horizontalCenter:   parent.horizontalCenter
    }

Don Gagne's avatar
Don Gagne committed
151 152 153 154 155 156
    Rectangle {
        anchors.margins:    parent.width / 4
        anchors.fill:       parent
        radius:             width / 2
        border.color:       mapPal.thumbJoystick
        border.width:       2
157
        color:              Qt.rgba(0,0,0,0)
Don Gagne's avatar
Don Gagne committed
158 159
    }

160 161 162 163 164
    Rectangle {
        anchors.fill:       parent
        radius:             width / 2
        border.color:       mapPal.thumbJoystick
        border.width:       2
165
        color:              Qt.rgba(0,0,0,0)
166 167
    }

Don Gagne's avatar
Don Gagne committed
168 169 170 171
    Rectangle {
        width:  hatWidth
        height: hatWidth
        radius: hatWidthHalf
172 173
        border.color: lightColors ? "white" : "black"
        border.width: 1
Don Gagne's avatar
Don Gagne committed
174
        color:  mapPal.thumbJoystick
Don Gagne's avatar
Don Gagne committed
175 176
        x:      stickPositionX - hatWidthHalf
        y:      stickPositionY - hatWidthHalf
Don Gagne's avatar
Don Gagne committed
177 178 179 180

        readonly property real hatWidth:        ScreenTools.defaultFontPixelHeight
        readonly property real hatWidthHalf:    ScreenTools.defaultFontPixelHeight / 2
    }
Don Gagne's avatar
Don Gagne committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

    Connections {
        target: touchPoint

        onXChanged: {
            if (_processTouchPoints) {
                _joyRoot.stickPositionX = Math.max(Math.min(touchPoint.x, _joyRoot.width), 0)
            }
        }
        onYChanged: {
            if (_processTouchPoints) {
                _joyRoot.stickPositionY = Math.max(Math.min(touchPoint.y, _joyRoot.height), 0)
            }
        }
    }

    MultiPointTouchArea {
        anchors.fill:       parent
        minimumTouchPoints: 1
        maximumTouchPoints: 1
        touchPoints:        [ TouchPoint { id: touchPoint } ]
Gus Grubba's avatar
Gus Grubba committed
202 203 204 205 206
        onPressed:          _joyRoot.thumbDown(touchPoints)
        onReleased: {
            if(springYToCenter)
                _joyRoot.reCenter()
        }
Don Gagne's avatar
Don Gagne committed
207
    }
Don Gagne's avatar
Don Gagne committed
208
}