Skip to content
JoystickThumbPad.qml 6.9 KiB
Newer Older
import QtQuick                  2.3
import QtQuick.Controls         1.2
Don Gagne's avatar
Don Gagne committed

import QGroundControl.Palette       1.0
import QGroundControl.ScreenTools   1.0

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

Don Gagne's avatar
Don Gagne committed
    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
    property real   xPositionDelta: 0                   ///< Amount to move the control on x axis
    property real   yPositionDelta: 0                   ///< Amount to move the control on y axis
    property bool   throttle:       false
Don Gagne's avatar
Don Gagne committed

Don Gagne's avatar
Don Gagne committed
    property real   _centerXY:              width / 2
    property bool   _processTouchPoints:    false
    property bool   _stickCenteredOnce:     false
    property real   stickPositionX:         _centerXY
    property real   stickPositionY:         yAxisThrottle ? height : _centerXY
Don Gagne's avatar
Don Gagne committed

    QGCMapPalette { id: mapPal }

Don Gagne's avatar
Don Gagne committed
    onStickPositionXChanged: {
        var xAxisTemp = stickPositionX / width
Don Gagne's avatar
Don Gagne committed
        xAxisTemp *= 2.0
        xAxisTemp -= 1.0
        xAxis = xAxisTemp
Don Gagne's avatar
Don Gagne committed
    }
Don Gagne's avatar
Don Gagne committed

Don Gagne's avatar
Don Gagne committed
    onStickPositionYChanged: {
        var yAxisTemp = stickPositionY / height
Don Gagne's avatar
Don Gagne committed
        yAxisTemp *= 2.0
        yAxisTemp -= 1.0
        if (yAxisThrottle) {
            yAxisTemp = ((yAxisTemp * -1.0) / 2.0) + 0.5
        }
        yAxis = yAxisTemp
    }

    function reCenter()
    {
Don Gagne's avatar
Don Gagne committed
        _processTouchPoints = false
Don Gagne's avatar
Don Gagne committed
        // Move control back to original position
        xPositionDelta = 0
        yPositionDelta = 0
Don Gagne's avatar
Don Gagne committed
        // Center sticks
        stickPositionX = _centerXY
        if (!yAxisThrottle) {
            stickPositionY = _centerXY
        }
Don Gagne's avatar
Don Gagne committed
    }

    function thumbDown(touchPoints)
    {
        // Position the control around the initial thumb position
Don Gagne's avatar
Don Gagne committed
        xPositionDelta = touchPoints[0].x - _centerXY
        if (yAxisThrottle) {
            yPositionDelta = touchPoints[0].y - stickPositionY
        } else {
            yPositionDelta = touchPoints[0].y - _centerXY
        }

Don Gagne's avatar
Don Gagne committed
        // 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
    /*
Don Gagne's avatar
Don Gagne committed
    // Keep in for debugging
Don Gagne's avatar
Don Gagne committed
    Column {
        QGCLabel { text: xAxis }
        QGCLabel { text: yAxis }
    }
Don Gagne's avatar
Don Gagne committed
    */
Don Gagne's avatar
Don Gagne committed

dogmaphobic's avatar
dogmaphobic committed
    Image {
        anchors.fill:       parent
        source:             lightColors ? "/res/JoystickBezel.png" : "/res/JoystickBezelLight.png"
        mipmap:             true
        smooth:             true
    QGCColoredImage {
        color:                      lightColors ? "white" : "black"
        visible:                    throttle
        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"
        visible:                    throttle
        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"
        visible:                    throttle
        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"
        visible:                    throttle
        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
    Rectangle {
        anchors.margins:    parent.width / 4
        anchors.fill:       parent
        radius:             width / 2
        border.color:       mapPal.thumbJoystick
        border.width:       2
        color:              Qt.rgba(0,0,0,0)
    Rectangle {
        anchors.fill:       parent
        radius:             width / 2
        border.color:       mapPal.thumbJoystick
        border.width:       2
        color:              Qt.rgba(0,0,0,0)
Don Gagne's avatar
Don Gagne committed
    Rectangle {
        width:  hatWidth
        height: hatWidth
        radius: hatWidthHalf
        border.color: lightColors ? "white" : "black"
        border.width: 1
Don Gagne's avatar
Don Gagne committed
        color:  mapPal.thumbJoystick
Don Gagne's avatar
Don Gagne committed
        x:      stickPositionX - hatWidthHalf
        y:      stickPositionY - hatWidthHalf
Don Gagne's avatar
Don Gagne committed

        readonly property real hatWidth:        ScreenTools.defaultFontPixelHeight
        readonly property real hatWidthHalf:    ScreenTools.defaultFontPixelHeight / 2
    }
Don Gagne's avatar
Don Gagne committed

    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 } ]

        onPressed:  _joyRoot.thumbDown(touchPoints)
        onReleased: _joyRoot.reCenter()
    }
Don Gagne's avatar
Don Gagne committed
}