From f820eefc8e286503ebb591cb82d83305a18beced Mon Sep 17 00:00:00 2001 From: Bryant Date: Sat, 15 Jun 2013 00:31:54 -0700 Subject: [PATCH] Joystick axes can now be specified as being exclusively-positive in the range [0:1.0] for the throttle. This should really be a vehicle-specific setting. --- src/input/JoystickInput.cc | 17 ++++++++++++++++- src/input/JoystickInput.h | 11 +++++++++-- src/ui/JoystickAxis.cc | 25 ++++++++++++++++++++++++- src/ui/JoystickAxis.h | 6 +++++- src/ui/JoystickAxis.ui | 24 ++++++++++++++++++++---- src/ui/JoystickWidget.cc | 6 +++--- 6 files changed, 77 insertions(+), 12 deletions(-) diff --git a/src/input/JoystickInput.cc b/src/input/JoystickInput.cc index ef7932b4a..8ccabbf03 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 e9a994030..be9ca87c3 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 a17d63dbb..c270784e6 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 cfb7dea54..ea95116bd 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 c3cb81657..e442339a7 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 efaa82197..3c5b80994 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); } } -- 2.22.0