Commit 64d0741e authored by Bryant's avatar Bryant

Joystick axes can now be specified as being exclusively-positive in the range...

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.
parent 40404552
...@@ -198,7 +198,12 @@ void JoystickInput::run() ...@@ -198,7 +198,12 @@ void JoystickInput::run()
axisValue = (axisValue - calibrationPositive[i]) / (calibrationNegative[i] - calibrationPositive[i]); axisValue = (axisValue - calibrationPositive[i]) / (calibrationNegative[i] - calibrationPositive[i]);
} }
axisValue = 1.0f - axisValue; 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 // Bound rounding errors
if (axisValue > 1.0f) axisValue = 1.0f; if (axisValue > 1.0f) axisValue = 1.0f;
...@@ -276,6 +281,7 @@ void JoystickInput::setActiveJoystick(int id) ...@@ -276,6 +281,7 @@ void JoystickInput::setActiveJoystick(int id)
// Update cached joystick values // Update cached joystick values
joystickAxes.clear(); joystickAxes.clear();
joystickAxesInverted.clear(); joystickAxesInverted.clear();
joystickAxesRangeLimited.clear();
for (int i = 0; i < joystickNumAxes; i++) for (int i = 0; i < joystickNumAxes; i++)
{ {
int axisValue = SDL_JoystickGetAxis(joystick, i); int axisValue = SDL_JoystickGetAxis(joystick, i);
...@@ -283,6 +289,7 @@ void JoystickInput::setActiveJoystick(int id) ...@@ -283,6 +289,7 @@ void JoystickInput::setActiveJoystick(int id)
emit axisValueChanged(i, axisValue); emit axisValueChanged(i, axisValue);
joystickAxesInverted.append(false); joystickAxesInverted.append(false);
joystickAxesRangeLimited.append(false);
} }
joystickButtons = 0; joystickButtons = 0;
for (int i = 0; i < joystickNumButtons; i++) for (int i = 0; i < joystickNumButtons; i++)
...@@ -348,6 +355,14 @@ void JoystickInput::setAxisInversion(int axis, bool inverted) ...@@ -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) float JoystickInput::getCurrentValueForAxis(int axis)
{ {
if (axis < joystickAxes.size()) if (axis < joystickAxes.size())
......
...@@ -156,8 +156,9 @@ protected: ...@@ -156,8 +156,9 @@ protected:
int joystickNumAxes; int joystickNumAxes;
int joystickNumButtons; int joystickNumButtons;
QList<float> joystickAxes; ///< The values of every axes during the last sample QList<float> joystickAxes; ///< The values of every axes during the last sample.
QList<bool> joystickAxesInverted; ///< Whether each axis should be used inverted from what was reported QList<bool> joystickAxesInverted; ///< Whether each axis should be used inverted from what was reported.
QList<bool> 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. 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. 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: ...@@ -232,6 +233,12 @@ public slots:
* @param inverted True indicates inverted from normal. Varies by controller. * @param inverted True indicates inverted from normal. Varies by controller.
*/ */
void setAxisInversion(int axis, bool inverted); 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_ #endif // _JOYSTICKINPUT_H_
...@@ -9,9 +9,11 @@ JoystickAxis::JoystickAxis(int id, QWidget *parent) : ...@@ -9,9 +9,11 @@ JoystickAxis::JoystickAxis(int id, QWidget *parent) :
ui(new Ui::JoystickAxis) ui(new Ui::JoystickAxis)
{ {
ui->setupUi(this); 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)); ui->label->setText(QString::number(id));
connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(mappingComboBoxChanged(int))); 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() JoystickAxis::~JoystickAxis()
...@@ -26,6 +28,14 @@ void JoystickAxis::setValue(float value) ...@@ -26,6 +28,14 @@ void JoystickAxis::setValue(float value)
void JoystickAxis::mappingComboBoxChanged(int newMapping) 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); emit mappingChanged(id, (JoystickInput::JOYSTICK_INPUT_MAPPING)newMapping);
} }
...@@ -33,3 +43,16 @@ void JoystickAxis::inversionCheckBoxChanged(bool inverted) ...@@ -33,3 +43,16 @@ void JoystickAxis::inversionCheckBoxChanged(bool inverted)
{ {
emit inversionChanged(id, 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);
}
...@@ -21,6 +21,8 @@ signals: ...@@ -21,6 +21,8 @@ signals:
void mappingChanged(int id, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping); void mappingChanged(int id, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping);
/** @brief Signal a change in this axis' inversion status */ /** @brief Signal a change in this axis' inversion status */
void inversionChanged(int id, bool); void inversionChanged(int id, bool);
/** @brief Signal a change in this axis' range limit */
void rangeLimitChanged(int id, bool);
public slots: public slots:
/** @brief Update the displayed value of the included progressbar. /** @brief Update the displayed value of the included progressbar.
...@@ -35,8 +37,10 @@ private: ...@@ -35,8 +37,10 @@ private:
private slots: private slots:
/** @brief Handle changes to the mapping dropdown bar. */ /** @brief Handle changes to the mapping dropdown bar. */
void mappingComboBoxChanged(int newMapping); void mappingComboBoxChanged(int newMapping);
/** @brief Handle changes to the inversion checkbox. */ /** @brief Emit signal when the inversion checkbox is changed. */
void inversionCheckBoxChanged(bool inverted); void inversionCheckBoxChanged(bool inverted);
/** @brief Emit signal when the limit range checkbox is changed. */
void limitRangeCheckBoxChanged(bool limited);
}; };
#endif // JOYSTICKAXIS_H #endif // JOYSTICKAXIS_H
...@@ -86,10 +86,17 @@ ...@@ -86,10 +86,17 @@
</item> </item>
</widget> </widget>
</item> </item>
<item>
<widget class="QCheckBox" name="limitRangeCheckBox">
<property name="text">
<string>Limit range</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QProgressBar" name="progressBar"> <widget class="QProgressBar" name="progressBar">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>true</bool>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding"> <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
...@@ -113,21 +120,30 @@ ...@@ -113,21 +120,30 @@
<set>Qt::AlignCenter</set> <set>Qt::AlignCenter</set>
</property> </property>
<property name="textVisible"> <property name="textVisible">
<bool>false</bool> <bool>true</bool>
</property> </property>
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>
<property name="textDirection">
<enum>QProgressBar::TopToBottom</enum>
</property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QCheckBox" name="checkBox"> <widget class="QCheckBox" name="invertedCheckBox">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed"> <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch> <horstretch>0</horstretch>
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="text"> <property name="text">
<string>Inverted</string> <string>Inverted</string>
</property> </property>
......
...@@ -126,7 +126,7 @@ void JoystickWidget::updateUIForJoystick(int id) ...@@ -126,7 +126,7 @@ void JoystickWidget::updateUIForJoystick(int id)
{ {
JoystickButton* button = new JoystickButton(i, m_ui->buttonBox); JoystickButton* button = new JoystickButton(i, m_ui->buttonBox);
// And make sure we insert BEFORE the vertical spacer. // And make sure we insert BEFORE the vertical spacer.
m_ui->buttonLayout->insertWidget(i, button); m_ui->buttonLayout->addWidget(button);
buttons.append(button); buttons.append(button);
} }
} }
...@@ -145,8 +145,8 @@ void JoystickWidget::updateUIForJoystick(int id) ...@@ -145,8 +145,8 @@ void JoystickWidget::updateUIForJoystick(int id)
axis->setValue(joystick->getCurrentValueForAxis(i)); 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(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))); connect(axis, SIGNAL(inversionChanged(int,bool)), this->joystick, SLOT(setAxisInversion(int,bool)));
// And make sure we insert BEFORE the vertical spacer. connect(axis, SIGNAL(rangeLimitChanged(int,bool)), this->joystick, SLOT(setAxisRangeLimit(int,bool)));
m_ui->axesLayout->insertWidget(i, axis); m_ui->axesLayout->addWidget(axis);
axes.append(axis); axes.append(axis);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment