Skip to content

Commit

Permalink
Add sensor buttons to JoyTabWidget
Browse files Browse the repository at this point in the history
If a controller has sensors, add configuration buttons similar to a
control stick to the main view. The layout is based on a isometric 3D view
with the regular XY axes and a diagonal Z axis.

Add a JoySensor button for every direction to the JoySensor object
and connect them to the "flashing" events. Add dummy methods for required
JoySensor interfaces.

Make all the QT connections in JoyTabWidgets as for a JoyControlStick.
But I'm not sure for what checkSensorDisplay and checkSensorEmptyDisplay are
really used.

The new buttons have no function yet. Even though it is possible to map
buttons to the sensor directions, but no events will be generated yet.
  • Loading branch information
mmmaisel committed May 5, 2022
1 parent 91c0f6e commit 7c79206
Show file tree
Hide file tree
Showing 15 changed files with 690 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ set(antimicrox_SOURCES
src/joycontrolstickstatusbox.cpp
src/joydpad.cpp
src/joysensor.cpp
src/joysensorbuttonpushbutton.cpp
src/joysensorpushbutton.cpp
src/joystick.cpp
src/keyboard/virtualkeyboardmousewidget.cpp
src/keyboard/virtualkeypushbutton.cpp
Expand All @@ -210,6 +212,7 @@ set(antimicrox_SOURCES
src/pt1filter.cpp
src/qtkeymapperbase.cpp
src/sdleventreader.cpp
src/sensorpushbuttongroup.cpp
src/setjoystick.cpp
src/simplekeygrabberbutton.cpp
src/statisticsestimator.cpp
Expand Down Expand Up @@ -310,6 +313,8 @@ set(antimicrox_HEADERS
src/joydpad.h
src/joysensor.h
src/joysensordirection.h
src/joysensorbuttonpushbutton.h
src/joysensorpushbutton.h
src/joysensortype.h
src/joystick.h
src/keyboard/virtualkeyboardmousewidget.h
Expand All @@ -330,6 +335,7 @@ set(antimicrox_HEADERS
src/pt1filter.h
src/qtkeymapperbase.h
src/sdleventreader.h
src/sensorpushbuttongroup.h
src/setjoystick.h
src/simplekeygrabberbutton.h
src/statisticsestimator.h
Expand Down
96 changes: 96 additions & 0 deletions src/gui/joytabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
#include "joyaxiswidget.h"
#include "joybuttontypes/joycontrolstickbutton.h"
#include "joybuttontypes/joydpadbutton.h"
#include "joybuttontypes/joysensorbutton.h"
#include "joybuttonwidget.h"
#include "joycontrolstick.h"
#include "joydpad.h"
#include "joysensor.h"
#include "joystick.h"
#include "quicksetdialog.h"
#include "sensorpushbuttongroup.h"
#include "setnamesdialog.h"
#include "stickpushbuttongroup.h"
#include "vdpad.h"
Expand Down Expand Up @@ -1766,6 +1769,18 @@ void JoyTabWidget::checkStickDisplay()
}
}

void JoyTabWidget::checkSensorDisplay()
{
JoySensorButton *button = qobject_cast<JoySensorButton *>(sender());
JoySensor *sensor = button->getSensor();
if ((sensor != nullptr) && sensor->hasSlotsAssigned())
{
SetJoystick *currentSet = m_joystick->getActiveSetJoystick();
removeSetButtons(currentSet);
fillSetButtons(currentSet);
}
}

void JoyTabWidget::checkDPadButtonDisplay()
{
JoyDPadButton *button = qobject_cast<JoyDPadButton *>(sender()); // static_cast
Expand Down Expand Up @@ -1815,6 +1830,18 @@ void JoyTabWidget::checkStickEmptyDisplay()
}
}

void JoyTabWidget::checkSensorEmptyDisplay()
{
SensorPushButtonGroup *group = qobject_cast<SensorPushButtonGroup *>(sender());
JoySensor *sensor = group->getSensor();
if ((sensor != nullptr) && !sensor->hasSlotsAssigned())
{
SetJoystick *currentSet = m_joystick->getActiveSetJoystick();
removeSetButtons(currentSet);
fillSetButtons(currentSet);
}
}

void JoyTabWidget::checkDPadButtonEmptyDisplay()
{
DPadPushButtonGroup *group = qobject_cast<DPadPushButtonGroup *>(sender()); // static_cast
Expand Down Expand Up @@ -1979,6 +2006,75 @@ void JoyTabWidget::fillSetButtons(SetJoystick *set)

column = 0;

QGridLayout *sensorGrid = nullptr;
QGroupBox *sensorGroup = nullptr;
int sensorGridColumn = 0;
int sensorGridRow = 0;

for (size_t i = 0; i < SENSOR_COUNT; ++i)
{
JoySensorType type = static_cast<JoySensorType>(i);
if (!m_joystick->hasSensor(type))
continue;

JoySensor *sensor = currentSet->getSensor(type);
sensor->establishPropertyUpdatedConnection();
QHash<JoySensorDirection, JoySensorButton *> *sensorButtons = sensor->getButtons();

if (!hideEmptyButtons || sensor->hasSlotsAssigned())
{
if (sensorGroup == nullptr)
sensorGroup = new QGroupBox(tr("Sensors"), this);

if (sensorGrid == nullptr)
{
sensorGrid = new QGridLayout();
sensorGridColumn = 0;
sensorGridRow = 0;
}

QWidget *groupContainer = new QWidget(sensorGroup);
SensorPushButtonGroup *sensorButtonGroup =
new SensorPushButtonGroup(sensor, isKeypadUnlocked(), displayingNames, groupContainer);
if (hideEmptyButtons)
{
connect(sensorButtonGroup, &SensorPushButtonGroup::buttonSlotChanged, this,
&JoyTabWidget::checkSensorEmptyDisplay);
}

connect(namesPushButton, &QPushButton::clicked, sensorButtonGroup, &SensorPushButtonGroup::toggleNameDisplay);

if (sensorGridColumn > 1)
{
sensorGridColumn = 0;
sensorGridRow++;
}

groupContainer->setLayout(sensorButtonGroup);
sensorGrid->addWidget(groupContainer, sensorGridRow, sensorGridColumn);
sensorGridColumn++;
} else
{
for (auto iter = sensorButtons->cbegin(); iter != sensorButtons->cend(); ++iter)
{
JoySensorButton *button = iter.value();
button->establishPropertyUpdatedConnections();
connect(button, &JoySensorButton::slotsChanged, this, &JoyTabWidget::checkSensorDisplay);
}
}
}

if (sensorGroup != nullptr)
{
QSpacerItem *tempspacer = new QSpacerItem(10, 4, QSizePolicy::Minimum, QSizePolicy::Fixed);
QVBoxLayout *tempvbox = new QVBoxLayout;
tempvbox->addLayout(sensorGrid);
tempvbox->addItem(tempspacer);
sensorGroup->setLayout(tempvbox);
current_layout->addWidget(sensorGroup, row, column, 1, 2, Qt::AlignTop);
row++;
}

QGridLayout *hatGrid = nullptr;
QGroupBox *hatGroup = nullptr;
int hatGridColumn = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/gui/joytabwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ class JoyTabWidget : public QWidget
void checkForUnsavedProfile(int newindex = -1);

void checkStickDisplay();
void checkSensorDisplay();
void checkDPadButtonDisplay();
void checkAxisButtonDisplay();
void checkButtonDisplay();

void checkStickEmptyDisplay();
void checkSensorEmptyDisplay();
void checkDPadButtonEmptyDisplay();
void checkAxisButtonEmptyDisplay();
void checkButtonEmptyDisplay();
Expand Down
9 changes: 9 additions & 0 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "joycontrolstickbuttonpushbutton.h"
#include "joycontrolstickpushbutton.h"
#include "joydpadbuttonwidget.h"
#include "joysensorpushbutton.h"
#include "joystick.h"
#include "joystickstatuswindow.h"
#include "joytabwidget.h"
Expand Down Expand Up @@ -846,6 +847,14 @@ void MainWindow::enableFlashActions()
stickWidget->tryFlash();
}

QList<JoySensorPushButton *> sensors = ui->tabWidget->widget(i)->findChildren<JoySensorPushButton *>();
for (auto iter = sensors.cbegin(); iter != sensors.cend(); ++iter)
{
JoySensorPushButton *sensorWidget = *iter;
sensorWidget->enableFlashes();
sensorWidget->tryFlash();
}

QList<JoyDPadButtonWidget *> list4 = ui->tabWidget->widget(i)->findChildren<JoyDPadButtonWidget *>();
QListIterator<JoyDPadButtonWidget *> iter4(list4);
while (iter4.hasNext())
Expand Down
6 changes: 6 additions & 0 deletions src/inputdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,12 @@ int InputDevice::getNumberHats() { return getActiveSetJoystick()->getNumberHats(

int InputDevice::getNumberSticks() { return getActiveSetJoystick()->getNumberSticks(); }

/**
* @brief Checks if this input device has a sensor of given type
* @returns True if sensor is present, false otherwise
*/
bool InputDevice::hasSensor(JoySensorType type) { return getActiveSetJoystick()->hasSensor(type); }

int InputDevice::getNumberVDPads() { return getActiveSetJoystick()->getNumberVDPads(); }

SetJoystick *InputDevice::getSetJoystick(int index) { return getJoystick_sets().value(index); }
Expand Down
1 change: 1 addition & 0 deletions src/inputdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class InputDevice : public QObject
virtual int getNumberAxes();
virtual int getNumberHats();
virtual int getNumberSticks();
virtual bool hasSensor(JoySensorType type);
virtual int getNumberVDPads();

int getJoyNumber();
Expand Down
4 changes: 4 additions & 0 deletions src/joybutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2463,6 +2463,10 @@ void JoyButton::setChangeSetCondition(SetChangeCondition condition, bool passive

JoyButton::SetChangeCondition JoyButton::getChangeSetCondition() { return setSelectionCondition; }

/**
* @brief Checks if this button is currently active
* @returns True if the button is pressed, false otherwise
*/
bool JoyButton::getButtonState() { return isButtonPressed; }

int JoyButton::getOriginSet() { return m_originset; }
Expand Down
74 changes: 74 additions & 0 deletions src/joysensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#define _USE_MATH_DEFINES

#include "joysensor.h"
#include "inputdevice.h"
#include "joybuttontypes/joyaccelerometerbutton.h"
#include "joybuttontypes/joygyroscopebutton.h"

#include <cmath>

Expand All @@ -28,6 +31,7 @@ JoySensor::JoySensor(JoySensorType type, double rate, int originset, SetJoystick
, m_originset(originset)
, m_parent_set(parent_set)
{
populateButtons();
}

JoySensor::~JoySensor() {}
Expand Down Expand Up @@ -87,6 +91,8 @@ void JoySensor::clearPendingEvent()
m_pending_ignore_sets = false;
}

bool JoySensor::hasSlotsAssigned() const { return false; }

/**
* @brief Get the name of this sensor
* @returns Sensor name
Expand Down Expand Up @@ -119,11 +125,18 @@ QString JoySensor::getPartialName(bool forceFullFormat, bool displayNames) const
return label;
}

QString JoySensor::getSensorName() const { return m_sensor_name; }

/**
* @brief Returns the sensor type
*/
JoySensorType JoySensor::getType() const { return m_type; }

/**
* @brief Returns the current sensor direction
*/
JoySensorDirection JoySensor::getCurrentDirection() const { return m_current_direction; }

/**
* @brief Get the value for the corresponding X axis.
* @return X axis value in m/s^2 for accelerometer or °/s for gyroscope.
Expand Down Expand Up @@ -176,6 +189,12 @@ bool JoySensor::inDeadZone(float *values) const { return false; }

double JoySensor::calculateDirectionalDistance(JoySensorDirection direction) const { return 0; }

/**
* @brief Returns a QHash which maps the SensorDirection to
* the corresponding JoySensorButton.
*/
QHash<JoySensorDirection, JoySensorButton *> *JoySensor::getButtons() { return &m_buttons; }

bool JoySensor::isDefault() const { return false; }

/**
Expand All @@ -191,6 +210,61 @@ void JoySensor::setSensorName(QString tempName)
}
}

void JoySensor::establishPropertyUpdatedConnection()
{
connect(this, &JoySensor::propertyUpdated, getParentSet()->getInputDevice(), &InputDevice::profileEdited);
}

void JoySensor::setDefaultSensorName(QString tempname) { m_default_sensor_name = tempname; }

QString JoySensor::getDefaultSensorName() const { return m_default_sensor_name; }

/**
* @brief Get pointer to the set that a sensor belongs to.
* @return Pointer to the set that a sensor belongs to.
*/
SetJoystick *JoySensor::getParentSet() const { return m_parent_set; }

/**
* @brief Initializes the JoySensorButton objects for this sensor.
*/
void JoySensor::populateButtons()
{
JoySensorButton *button = nullptr;
if (m_type == ACCELEROMETER)
{
button = new JoyAccelerometerButton(this, SENSOR_LEFT, m_originset, getParentSet(), this);
m_buttons.insert(SENSOR_LEFT, button);

button = new JoyAccelerometerButton(this, SENSOR_RIGHT, m_originset, getParentSet(), this);
m_buttons.insert(SENSOR_RIGHT, button);

button = new JoyAccelerometerButton(this, SENSOR_UP, m_originset, getParentSet(), this);
m_buttons.insert(SENSOR_UP, button);

button = new JoyAccelerometerButton(this, SENSOR_DOWN, m_originset, getParentSet(), this);
m_buttons.insert(SENSOR_DOWN, button);

button = new JoyAccelerometerButton(this, SENSOR_BWD, m_originset, getParentSet(), this);
m_buttons.insert(SENSOR_BWD, button);
} else
{
button = new JoyGyroscopeButton(this, SENSOR_LEFT, m_originset, getParentSet(), this);
m_buttons.insert(SENSOR_LEFT, button);

button = new JoyGyroscopeButton(this, SENSOR_RIGHT, m_originset, getParentSet(), this);
m_buttons.insert(SENSOR_RIGHT, button);

button = new JoyGyroscopeButton(this, SENSOR_UP, m_originset, getParentSet(), this);
m_buttons.insert(SENSOR_UP, button);

button = new JoyGyroscopeButton(this, SENSOR_DOWN, m_originset, getParentSet(), this);
m_buttons.insert(SENSOR_DOWN, button);

button = new JoyGyroscopeButton(this, SENSOR_FWD, m_originset, getParentSet(), this);
m_buttons.insert(SENSOR_FWD, button);

button = new JoyGyroscopeButton(this, SENSOR_BWD, m_originset, getParentSet(), this);
m_buttons.insert(SENSOR_BWD, button);
}
}
Loading

0 comments on commit 7c79206

Please sign in to comment.