diff --git a/src/input/JoystickInput.cc b/src/input/JoystickInput.cc index ef7932b4a43a84295ddda72c98b374a032fa0c71..8ccabbf03d6e58357bbf3b0e15de0aa724bc6294 100644 --- a/src/input/JoystickInput.cc +++ b/src/input/JoystickInput.cc @@ -198,7 +198,12 @@ void JoystickInput::run() axisValue = (axisValue - calibrationPositive[i]) / (calibrationNegative[i] - calibrationPositive[i]); } axisValue = 1.0f - axisValue; - axisValue = axisValue * 2.0f - 1.0f; + + // If the joystick isn't limited to [0:1.0], map it into [-1.0:1.0]. + if (!joystickAxesRangeLimited[i]) + { + axisValue = axisValue * 2.0f - 1.0f; + } // Bound rounding errors if (axisValue > 1.0f) axisValue = 1.0f; @@ -276,6 +281,7 @@ void JoystickInput::setActiveJoystick(int id) // Update cached joystick values joystickAxes.clear(); joystickAxesInverted.clear(); + joystickAxesRangeLimited.clear(); for (int i = 0; i < joystickNumAxes; i++) { int axisValue = SDL_JoystickGetAxis(joystick, i); @@ -283,6 +289,7 @@ void JoystickInput::setActiveJoystick(int id) emit axisValueChanged(i, axisValue); joystickAxesInverted.append(false); + joystickAxesRangeLimited.append(false); } joystickButtons = 0; for (int i = 0; i < joystickNumButtons; i++) @@ -348,6 +355,14 @@ void JoystickInput::setAxisInversion(int axis, bool inverted) } } +void JoystickInput::setAxisRangeLimit(int axis, bool rangeLimited) +{ + if (axis < joystickAxesRangeLimited.size()) + { + joystickAxesRangeLimited[axis] = rangeLimited; + } +} + float JoystickInput::getCurrentValueForAxis(int axis) { if (axis < joystickAxes.size()) diff --git a/src/input/JoystickInput.h b/src/input/JoystickInput.h index e9a9940302e506b9b8f9f6d47252a817ba20bc09..be9ca87c3b48876db7dd5a5540707cd7a0796d1f 100644 --- a/src/input/JoystickInput.h +++ b/src/input/JoystickInput.h @@ -156,8 +156,9 @@ protected: int joystickNumAxes; int joystickNumButtons; - QList joystickAxes; ///< The values of every axes during the last sample - QList joystickAxesInverted; ///< Whether each axis should be used inverted from what was reported + QList joystickAxes; ///< The values of every axes during the last sample. + QList joystickAxesInverted; ///< Whether each axis should be used inverted from what was reported. + QList joystickAxesRangeLimited; ///< Whether each axis should be scaled into [0:1.0] instead of [-1.0:1.0]. quint16 joystickButtons; ///< The state of every button. Bitfield supporting 16 buttons with 1s indicating that the button is down. int xHat, yHat; ///< The horizontal/vertical hat directions. Values are -1, 0, 1, with (-1,-1) indicating bottom-left. @@ -232,6 +233,12 @@ public slots: * @param inverted True indicates inverted from normal. Varies by controller. */ void setAxisInversion(int axis, bool inverted); + /** + * @brief Specifies whether an axis should have its range limited to only positive values. + * @param axis The index of the axis to limit + * @param rangeLimited True if the axis should be limited, false otherwise. + */ + void setAxisRangeLimit(int axis, bool rangeLimited); }; #endif // _JOYSTICKINPUT_H_ diff --git a/src/ui/JoystickAxis.cc b/src/ui/JoystickAxis.cc index a17d63dbbf1fdd6ce2eaee728c6f2af1c637fa34..c270784e615972153521ac6f663f579c8b52ad8d 100644 --- a/src/ui/JoystickAxis.cc +++ b/src/ui/JoystickAxis.cc @@ -9,9 +9,11 @@ JoystickAxis::JoystickAxis(int id, QWidget *parent) : ui(new Ui::JoystickAxis) { ui->setupUi(this); + ui->limitRangeCheckBox->hide(); // Hide the range checkbox by default. It's only activated by switching to the Throttle axis option. ui->label->setText(QString::number(id)); connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(mappingComboBoxChanged(int))); - connect(ui->checkBox, SIGNAL(clicked(bool)), this, SLOT(inversionCheckBoxChanged(bool))); + connect(ui->invertedCheckBox, SIGNAL(clicked(bool)), this, SLOT(inversionCheckBoxChanged(bool))); + connect(ui->limitRangeCheckBox, SIGNAL(clicked(bool)), this, SLOT(limitRangeCheckBoxChanged(bool))); } JoystickAxis::~JoystickAxis() @@ -26,6 +28,14 @@ void JoystickAxis::setValue(float value) void JoystickAxis::mappingComboBoxChanged(int newMapping) { + if (newMapping == JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE) + { + ui->limitRangeCheckBox->show(); + } + else + { + ui->limitRangeCheckBox->hide(); + } emit mappingChanged(id, (JoystickInput::JOYSTICK_INPUT_MAPPING)newMapping); } @@ -33,3 +43,16 @@ void JoystickAxis::inversionCheckBoxChanged(bool inverted) { emit inversionChanged(id, inverted); } + +void JoystickAxis::limitRangeCheckBoxChanged(bool limited) +{ + if (limited) + { + ui->progressBar->setRange(0, 100); + } + else + { + ui->progressBar->setRange(-100, 100); + } + emit rangeLimitChanged(id, limited); +} diff --git a/src/ui/JoystickAxis.h b/src/ui/JoystickAxis.h index cfb7dea54ec4ffc80fefdae0c32eea98e2142a2c..ea95116bd9c8e942ffa3831198cf116cb754e747 100644 --- a/src/ui/JoystickAxis.h +++ b/src/ui/JoystickAxis.h @@ -21,6 +21,8 @@ signals: void mappingChanged(int id, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping); /** @brief Signal a change in this axis' inversion status */ void inversionChanged(int id, bool); + /** @brief Signal a change in this axis' range limit */ + void rangeLimitChanged(int id, bool); public slots: /** @brief Update the displayed value of the included progressbar. @@ -35,8 +37,10 @@ private: private slots: /** @brief Handle changes to the mapping dropdown bar. */ void mappingComboBoxChanged(int newMapping); - /** @brief Handle changes to the inversion checkbox. */ + /** @brief Emit signal when the inversion checkbox is changed. */ void inversionCheckBoxChanged(bool inverted); + /** @brief Emit signal when the limit range checkbox is changed. */ + void limitRangeCheckBoxChanged(bool limited); }; #endif // JOYSTICKAXIS_H diff --git a/src/ui/JoystickAxis.ui b/src/ui/JoystickAxis.ui index c3cb816572d4dc99e720f6c48f0a80186ee67c4d..e442339a747e986e21d804a92266fe5474ad3ab5 100644 --- a/src/ui/JoystickAxis.ui +++ b/src/ui/JoystickAxis.ui @@ -86,10 +86,17 @@ + + + + Limit range + + + - false + true @@ -113,21 +120,30 @@ Qt::AlignCenter - false + true Qt::Vertical + + QProgressBar::TopToBottom + - + - + 0 0 + + + 0 + 25 + + Inverted diff --git a/src/ui/JoystickWidget.cc b/src/ui/JoystickWidget.cc index efaa82197e7b22517adbb328d26a8f450cd190af..3c5b80994389ac67d1092ed85034fcd3ccb0d4ed 100644 --- a/src/ui/JoystickWidget.cc +++ b/src/ui/JoystickWidget.cc @@ -126,7 +126,7 @@ void JoystickWidget::updateUIForJoystick(int id) { JoystickButton* button = new JoystickButton(i, m_ui->buttonBox); // And make sure we insert BEFORE the vertical spacer. - m_ui->buttonLayout->insertWidget(i, button); + m_ui->buttonLayout->addWidget(button); buttons.append(button); } } @@ -145,8 +145,8 @@ void JoystickWidget::updateUIForJoystick(int id) axis->setValue(joystick->getCurrentValueForAxis(i)); connect(axis, SIGNAL(mappingChanged(int,JoystickInput::JOYSTICK_INPUT_MAPPING)), this->joystick, SLOT(setAxisMapping(int,JoystickInput::JOYSTICK_INPUT_MAPPING))); connect(axis, SIGNAL(inversionChanged(int,bool)), this->joystick, SLOT(setAxisInversion(int,bool))); - // And make sure we insert BEFORE the vertical spacer. - m_ui->axesLayout->insertWidget(i, axis); + connect(axis, SIGNAL(rangeLimitChanged(int,bool)), this->joystick, SLOT(setAxisRangeLimit(int,bool))); + m_ui->axesLayout->addWidget(axis); axes.append(axis); } }