From c8647cc07bdac21f7e6c0a0de1bef759067a9bf0 Mon Sep 17 00:00:00 2001 From: Bryant Date: Thu, 13 Jun 2013 12:57:09 -0700 Subject: [PATCH] The hat readings from the joystick now works correctly. Also moved some signals over to using an enum from an int type. --- src/input/JoystickInput.cc | 17 ++++++++++++----- src/ui/JoystickAxis.cc | 3 ++- src/ui/JoystickAxis.h | 3 ++- src/ui/JoystickWidget.cc | 17 ++++++++--------- src/ui/JoystickWidget.h | 23 ++++++++++++++--------- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/input/JoystickInput.cc b/src/input/JoystickInput.cc index 5c8f48135..81e0ce3c8 100644 --- a/src/input/JoystickInput.cc +++ b/src/input/JoystickInput.cc @@ -205,11 +205,18 @@ void JoystickInput::run() // Build up vectors describing the hat position int hatPosition = SDL_JoystickGetHat(joystick, 0); - if ((SDL_HAT_UP & hatPosition) > 0) yHat = 1; - if ((SDL_HAT_DOWN & hatPosition) > 0) yHat = -1; - if ((SDL_HAT_LEFT & hatPosition) > 0) xHat = -1; - if ((SDL_HAT_RIGHT & hatPosition) > 0) xHat = 1; - emit hatDirectionChanged(xHat, yHat); + int newYHat = 0; + if ((SDL_HAT_UP & hatPosition) > 0) newYHat = 1; + if ((SDL_HAT_DOWN & hatPosition) > 0) newYHat = -1; + int newXHat = 0; + if ((SDL_HAT_LEFT & hatPosition) > 0) newXHat = -1; + if ((SDL_HAT_RIGHT & hatPosition) > 0) newXHat = 1; + if (newYHat != yHat || newXHat != xHat) + { + xHat = newXHat; + yHat = newYHat; + } + emit hatDirectionChanged(newXHat, newYHat); // Emit signals for each button individually for (int i = 0; i < joystickNumButtons; i++) diff --git a/src/ui/JoystickAxis.cc b/src/ui/JoystickAxis.cc index 5f16f4964..ec730e92c 100644 --- a/src/ui/JoystickAxis.cc +++ b/src/ui/JoystickAxis.cc @@ -1,4 +1,5 @@ #include "JoystickAxis.h" +#include "JoystickInput.h" #include "ui_JoystickAxis.h" #include @@ -24,5 +25,5 @@ void JoystickAxis::setValue(float value) void JoystickAxis::mappingComboBoxChanged(int newMapping) { - emit mappingChanged(id, newMapping); + emit mappingChanged(id, (JoystickInput::JOYSTICK_INPUT_MAPPING)newMapping); } diff --git a/src/ui/JoystickAxis.h b/src/ui/JoystickAxis.h index 22482d9c6..25aaa4598 100644 --- a/src/ui/JoystickAxis.h +++ b/src/ui/JoystickAxis.h @@ -2,6 +2,7 @@ #define JOYSTICKAXIS_H #include +#include "JoystickInput.h" namespace Ui { class JoystickAxis; @@ -17,7 +18,7 @@ public: signals: /** @brief Signal a change in this axis' yaw/pitch/roll mapping */ - void mappingChanged(int id, int newMapping); + void mappingChanged(int id, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping); public slots: /** @brief Update the displayed value of the included progressbar. diff --git a/src/ui/JoystickWidget.cc b/src/ui/JoystickWidget.cc index 6edea3def..6bd29ad5c 100644 --- a/src/ui/JoystickWidget.cc +++ b/src/ui/JoystickWidget.cc @@ -21,10 +21,11 @@ JoystickWidget::JoystickWidget(JoystickInput* joystick, QWidget *parent) : // Initialize the UI based on the current joystick initUI(); - // Watch for button and hat input events from the joystick. + // Watch for button, axis, and hat input events from the joystick. connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(joystickButtonPressed(int))); connect(this->joystick, SIGNAL(buttonReleased(int)), this, SLOT(joystickButtonReleased(int))); connect(this->joystick, SIGNAL(axisValueChanged(int,float)), this, SLOT(updateAxisValue(int,float))); + connect(this->joystick, SIGNAL(hatDirectionChanged(int,int)), this, SLOT(setHat(int,int))); // Update the UI if the joystick changes. connect(m_ui->joystickNameComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUIForJoystick(int))); @@ -131,7 +132,7 @@ void JoystickWidget::updateUIForJoystick(int id) { JoystickAxis* axis = new JoystickAxis(i, m_ui->axesBox); axis->setValue(joystick->getCurrentValueForAxis(i)); - connect(axis, SIGNAL(mappingChanged(int,int)), this, SLOT(setMappingAxis(int,int))); + connect(axis, SIGNAL(mappingChanged(int,JoystickInput::JOYSTICK_INPUT_MAPPING)), this, SLOT(setMappingAxis(int,JoystickInput::JOYSTICK_INPUT_MAPPING))); // And make sure we insert BEFORE the vertical spacer. m_ui->axesLayout->insertWidget(i, axis); axes.append(axis); @@ -146,9 +147,9 @@ void JoystickWidget::updateAxisValue(int axis, float value) } } -void JoystickWidget::setHat(float x, float y) +void JoystickWidget::setHat(int x, int y) { - updateStatus(tr("Hat position: x: %1, y: %2").arg(x).arg(y)); + m_ui->statusLabel->setText(tr("Hat position: x: %1, y: %2").arg(x).arg(y)); } void JoystickWidget::setMappingAxis(int axisID, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping) @@ -167,6 +168,9 @@ void JoystickWidget::setMappingAxis(int axisID, JoystickInput::JOYSTICK_INPUT_MA case JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE: joystick->setMappingThrottleAxis(axisID); break; + case JoystickInput::JOYSTICK_INPUT_MAPPING_NONE: + default: + break; } } @@ -180,8 +184,3 @@ void JoystickWidget::joystickButtonReleased(int key) { buttons.at(key)->setStyleSheet(""); } - -void JoystickWidget::updateStatus(const QString& status) -{ - m_ui->statusLabel->setText(status); -} diff --git a/src/ui/JoystickWidget.h b/src/ui/JoystickWidget.h index 87b8d448a..de57a0a84 100644 --- a/src/ui/JoystickWidget.h +++ b/src/ui/JoystickWidget.h @@ -53,14 +53,25 @@ public: virtual ~JoystickWidget(); public slots: + /** @brief Update the UI for a new joystick based on SDL ID. */ + void updateUIForJoystick(int id); + /** @brief Change the stored mapping for a given axis. */ + void setMappingAxis(int axisID, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping); + /** + * @brief Update a given axis with a new value + * @param axis The index of the axis to update. + * @param value The new value for the axis, [-1.0:1.0]. + * @see JoystickInput:axisValueChanged + */ void updateAxisValue(int axis, float value); - void setHat(float x, float y); + /** @brief Update the UI with new values for the hat. + * @see JoystickInput::hatDirectionChanged + */ + void setHat(int x, int y); /** @brief Trigger a UI change based on a button being pressed */ void joystickButtonPressed(int key); /** @brief Trigger a UI change based on a button being released */ void joystickButtonReleased(int key); - /** @brief Update status string */ - void updateStatus(const QString& status); /** @brief Update the UI color scheme when the MainWindow theme changes. */ void styleChanged(MainWindow::QGC_MAINWINDOW_STYLE); @@ -77,12 +88,6 @@ protected: /** @brief The color to use for button labels when their corresponding button is pressed */ QColor buttonLabelColor; -protected slots: - /** @brief Update the UI for a new joystick based on SDL ID. */ - void updateUIForJoystick(int id); - /** @brief Change the stored mapping for a given axis. */ - void setMappingAxis(int axisID, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping); - private: Ui::JoystickWidget *m_ui; /** @brief Initialize all dynamic UI elements (button list, joystick names, etc.) */ -- 2.22.0