QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VirtualJoystick.qml
Go to the documentation of this file.
1import QtQuick
2
3import QGroundControl
4import QGroundControl.Controls
5
6Item {
7 // The following properties must be passed in from the Loader
8 // property bool autoCenterThrottle - true: throttle will snap back to center when released
9 // property bool leftHandedMode - true: virtual joystick layout will be reversed
10
11 id: virtualJoysticks
12
13 readonly property bool stickActive: leftStick.touchActive || rightStick.touchActive
14
15 property var _activeVehicle: QGroundControl.multiVehicleManager.activeVehicle
16 property bool _initialConnectComplete: _activeVehicle ? _activeVehicle.initialConnectComplete : false
17 property real leftYAxisValue: autoCenterThrottle ? height / 2 : height
18 property var calibration: false
19 property real uiTotalWidth: 0
20 property real uiRealX: 0
21
22 Timer {
23 interval: 40 // 25Hz, same as real joystick rate
24 running: QGroundControl.settingsManager.appSettings.virtualJoystick.value
25 repeat: true
26 onTriggered: {
27 if (_activeVehicle && _initialConnectComplete) {
28 leftHandedMode ? _activeVehicle.virtualTabletJoystickValue(leftStick.xAxis, leftStick.yAxis, rightStick.xAxis, rightStick.yAxis) : _activeVehicle.virtualTabletJoystickValue(rightStick.xAxis, rightStick.yAxis, leftStick.xAxis, leftStick.yAxis)
29 }
30 leftYAxisValue = leftStick.yAxis // We keep Y axis value from the throttle stick for using it while there is a resize
31 }
32 }
33
34 onHeightChanged: { keepYAxisWhileChanged() }
35 onWidthChanged: { keepXAxisWhileChanged() }
36 onCalibrationChanged: { calibration ? calibrateJoysticks() : undefined }
37
38 function calibrateJoysticks() {
39 if( virtualJoysticks.visible ) {
40 keepXAxisWhileChanged()
41 leftYAxisValue = leftStick.yAxisReCentered() // Keep track of the correct leftYAxisValue while the width is adjusted at first start up
42 }
43 }
44
45 function keepYAxisWhileChanged () {
46 if( virtualJoysticks.visible ) {
47 leftStick.resize( leftYAxisValue )
48 rightStick.reCenter()
49 }
50 }
51
52 function keepXAxisWhileChanged () {
53 if( virtualJoysticks.visible ) {
54 leftStick.reCenter()
55 rightStick.reCenter()
56 }
57 }
58
59 JoystickThumbPad {
60 id: leftStick
61 anchors.leftMargin: xPositionDelta
62 anchors.bottomMargin: -yPositionDelta
63 anchors.left: parent.left
64 anchors.bottom: parent.bottom
65 width: parent.height
66 height: parent.height
67 yAxisPositiveRangeOnly: _activeVehicle && !_activeVehicle.rover && !leftHandedMode
68 yAxisReCenter: autoCenterThrottle
69 }
70
71 JoystickThumbPad {
72 id: rightStick
73 anchors.rightMargin: -xPositionDelta
74 anchors.bottomMargin: -yPositionDelta
75 anchors.right: parent.right
76 anchors.bottom: parent.bottom
77 width: parent.height
78 height: parent.height
79 yAxisPositiveRangeOnly: _activeVehicle && !_activeVehicle.rover && leftHandedMode
80 yAxisReCenter: true
81 }
82}