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 property var _activeVehicle: QGroundControl.multiVehicleManager.activeVehicle
14 property bool _initialConnectComplete: _activeVehicle ? _activeVehicle.initialConnectComplete : false
15 property real leftYAxisValue: autoCenterThrottle ? height / 2 : height
16 property var calibration: false
17 property real uiTotalWidth: 0
18 property real uiRealX: 0
19
20 Timer {
21 interval: 40 // 25Hz, same as real joystick rate
22 running: QGroundControl.settingsManager.appSettings.virtualJoystick.value
23 repeat: true
24 onTriggered: {
25 if (_activeVehicle && _initialConnectComplete) {
26 leftHandedMode ? _activeVehicle.virtualTabletJoystickValue(leftStick.xAxis, leftStick.yAxis, rightStick.xAxis, rightStick.yAxis) : _activeVehicle.virtualTabletJoystickValue(rightStick.xAxis, rightStick.yAxis, leftStick.xAxis, leftStick.yAxis)
27 }
28 leftYAxisValue = leftStick.yAxis // We keep Y axis value from the throttle stick for using it while there is a resize
29 }
30 }
31
32 onHeightChanged: { keepYAxisWhileChanged() }
33 onWidthChanged: { keepXAxisWhileChanged() }
34 onCalibrationChanged: { calibration ? calibrateJoysticks() : undefined }
35
36 function calibrateJoysticks() {
37 if( virtualJoysticks.visible ) {
38 keepXAxisWhileChanged()
39 leftYAxisValue = leftStick.yAxisReCentered() // Keep track of the correct leftYAxisValue while the width is adjusted at first start up
40 }
41 }
42
43 function keepYAxisWhileChanged () {
44 if( virtualJoysticks.visible ) {
45 leftStick.resize( leftYAxisValue )
46 rightStick.reCenter()
47 }
48 }
49
50 function keepXAxisWhileChanged () {
51 if( virtualJoysticks.visible ) {
52 leftStick.reCenter()
53 rightStick.reCenter()
54 }
55 }
56
57 JoystickThumbPad {
58 id: leftStick
59 anchors.leftMargin: xPositionDelta
60 anchors.bottomMargin: -yPositionDelta
61 anchors.left: parent.left
62 anchors.bottom: parent.bottom
63 width: parent.height
64 height: parent.height
65 yAxisPositiveRangeOnly: _activeVehicle && !_activeVehicle.rover && !leftHandedMode
66 yAxisReCenter: autoCenterThrottle
67 }
68
69 JoystickThumbPad {
70 id: rightStick
71 anchors.rightMargin: -xPositionDelta
72 anchors.bottomMargin: -yPositionDelta
73 anchors.right: parent.right
74 anchors.bottom: parent.bottom
75 width: parent.height
76 height: parent.height
77 yAxisPositiveRangeOnly: _activeVehicle && !_activeVehicle.rover && leftHandedMode
78 yAxisReCenter: true
79 }
80}