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

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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

    property alias  lightColors:            mapPal.lightColors  /// true: use light colors from QGCMapPalette for drawing
    property var    stickPosition:          Qt.point(0, 0)
    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 bool   _stickCenteredOnce: false

    QGCMapPalette { id: mapPal }

    onWidthChanged: {
        if (!_stickCenteredOnce && width != 0) {
            reCenter()
        }
    }

    onStickPositionChanged: {
        var xAxisTemp = stickPosition.x / width
        xAxisTemp *= 2.0
        xAxisTemp -= 1.0
        xAxis = xAxisTemp

        var yAxisTemp = stickPosition.y / width
        yAxisTemp *= 2.0
        yAxisTemp -= 1.0
        if (yAxisThrottle) {
            yAxisTemp = ((yAxisTemp * -1.0) / 2.0) + 0.5
        }
        yAxis = yAxisTemp
    }

    function reCenter()
    {
        stickPosition = Qt.point(width / 2, width / 2)
    }

Don Gagne's avatar
Don Gagne committed
46 47
    /*
      Keep in for debugging
Don Gagne's avatar
Don Gagne committed
48 49 50 51
    Column {
        QGCLabel { text: xAxis }
        QGCLabel { text: yAxis }
    }
Don Gagne's avatar
Don Gagne committed
52
    */
Don Gagne's avatar
Don Gagne committed
53

dogmaphobic's avatar
dogmaphobic committed
54 55 56 57 58
    Image {
        anchors.fill:       parent
        source:             lightColors ? "/res/JoystickBezel.png" : "/res/JoystickBezelLight.png"
        mipmap:             true
        smooth:             true
Don Gagne's avatar
Don Gagne committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    }

    Rectangle {
        anchors.margins:    parent.width / 4
        anchors.fill:       parent
        radius:             width / 2
        border.color:       mapPal.thumbJoystick
        border.width:       2
        color:              "transparent"
    }

    Rectangle {
        width:  hatWidth
        height: hatWidth
        radius: hatWidthHalf
        color:  mapPal.thumbJoystick
        x:      stickPosition.x - hatWidthHalf
        y:      stickPosition.y - hatWidthHalf

        readonly property real hatWidth:        ScreenTools.defaultFontPixelHeight
        readonly property real hatWidthHalf:    ScreenTools.defaultFontPixelHeight / 2
    }
}