Skip to content

Commit

Permalink
Implement sensor button assignment and load/store
Browse files Browse the repository at this point in the history
Connect the button edit dialog to sensor direction buttons.
Add XML load/store logic for sensor buttons as well as the
events and boilerplate required for setting up sensors from XML.
  • Loading branch information
mmmaisel committed Apr 15, 2022
1 parent 7482b22 commit 7437814
Show file tree
Hide file tree
Showing 16 changed files with 350 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/gamecontroller/gamecontrollerset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ void GameControllerSet::readJoystickConfig(QXmlStreamReader *xml, QHash<int, SDL
} else if ((xml->name() == "stick") && xml->isStartElement())
{
getElemFromXml("stick", xml);
} else if ((xml->name() == "sensor") && xml->isStartElement())
{
getElemFromXml("sensor", xml);
} else if ((xml->name() == "vdpad") && xml->isStartElement())
{
readConfDpad(xml, hatButtons, vdpadExists, dpadExists);
Expand Down Expand Up @@ -253,6 +256,9 @@ void GameControllerSet::readConfig(QXmlStreamReader *xml)
} else if ((xml->name() == "stick") && xml->isStartElement())
{
getElemFromXml("stick", xml);
} else if ((xml->name() == "sensor") && xml->isStartElement())
{
getElemFromXml("sensor", xml);
} else if ((xml->name() == "dpad") && xml->isStartElement())
{
getElemFromXml("dpad", xml);
Expand Down Expand Up @@ -335,6 +341,10 @@ void GameControllerSet::getElemFromXml(QString elemName, QXmlStreamReader *xml)
{
xml->skipCurrentElement();
}
} else if (elemName == "sensor")
{
JoySensor *sensor = getSensor(index);
readConf(sensor, xml);
}
}

Expand Down
63 changes: 63 additions & 0 deletions src/gamecontroller/xml/gamecontrollerxml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include "gamecontroller/gamecontrollerdpad.h"
#include "gamecontroller/gamecontrollerset.h"
#include "joybuttontypes/joycontrolstickbutton.h"
#include "joybuttontypes/joysensorbutton.h"
#include "joycontrolstick.h"
#include "joysensor.h"

#include <cmath>

Expand Down Expand Up @@ -141,6 +143,14 @@ void GameControllerXml::readJoystickConfig(QXmlStreamReader *xml)
{
m_gameController->setStickButtonName(index, buttonIndex, temp);
}
} else if ((xml->name() == "sensorbuttonname") && xml->isStartElement())
{
assignVariables(xml, index, buttonIndex, temp, false);

if ((index >= 0) && !temp.isEmpty())
{
m_gameController->setSensorButtonName(index, buttonIndex, temp);
}
} else if ((xml->name() == "dpadbuttonname") && xml->isStartElement())
{
assignVariables(xml, index, buttonIndex, temp, false);
Expand Down Expand Up @@ -234,6 +244,14 @@ void GameControllerXml::readJoystickConfig(QXmlStreamReader *xml)
{
m_gameController->setStickName(index, temp);
}
} else if ((xml->name() == "sensorname") && xml->isStartElement())
{
assignVariablesShort(xml, index, temp);

if ((index >= 0) && !temp.isEmpty())
{
m_gameController->setSensorName(index, temp);
}
} else if ((xml->name() == "dpadname") && xml->isStartElement())
{
readJoystickConfigXmlLong(hatButtons, dpadNameExists, vdpadNameExists, xml);
Expand Down Expand Up @@ -321,6 +339,9 @@ void GameControllerXml::readConfig(QXmlStreamReader *xml)
} else if ((xml->name() == "controlstickbuttonname") && xml->isStartElement())
{
readXmlNamesMiddle("controlstickbuttonname", xml);
} else if ((xml->name() == "sensorbuttonname") && xml->isStartElement())
{
readXmlNamesMiddle("sensorbuttonname", xml);
} else if ((xml->name() == "dpadbuttonname") && xml->isStartElement())
{
readXmlNamesMiddle("dpadbuttonname", xml);
Expand All @@ -330,6 +351,9 @@ void GameControllerXml::readConfig(QXmlStreamReader *xml)
} else if ((xml->name() == "controlstickname") && xml->isStartElement())
{
readXmlNamesShort("controlstickname", xml);
} else if ((xml->name() == "sensorname") && xml->isStartElement())
{
readXmlNamesShort("sensorname", xml);
} else if ((xml->name() == "dpadname") && xml->isStartElement())
{
readXmlNamesShort("dpadname", xml);
Expand Down Expand Up @@ -396,6 +420,7 @@ void GameControllerXml::writeConfig(QXmlStreamWriter *xml)
writeXmlForButtons(tempSet, xml);
writeXmlForAxes(tempSet, xml);
writeXmlForSticks(tempSet, xml);
writeXmlForSensors(tempSet, xml);
writeXmlForVDpad(xml);

xml->writeEndElement(); // </names>
Expand Down Expand Up @@ -512,6 +537,44 @@ void GameControllerXml::writeXmlForSticks(SetJoystick *tempSet, QXmlStreamWriter
}
}

void GameControllerXml::writeXmlForSensors(SetJoystick *tempSet, QXmlStreamWriter *xml)
{
QHashIterator<int, JoySensor *> currSensor(tempSet->getSensors());

while (currSensor.hasNext())
{
currSensor.next();

if (currSensor.value() != nullptr)
{
if (!currSensor.value()->getSensorName().isEmpty())
{
xml->writeStartElement("sensorname");
xml->writeAttribute("type", QString::number(currSensor.value()->getType()));
xml->writeCharacters(currSensor.value()->getSensorName());
xml->writeEndElement();
}

QHash<JoySensorDirection, JoySensorButton *> *buttons = currSensor.value()->getButtons();
QHashIterator<JoySensorDirection, JoySensorButton *> iter(*buttons);

while (iter.hasNext())
{
JoySensorButton *button = iter.next().value();

if ((button != nullptr) && !button->getButtonName().isEmpty())
{
xml->writeStartElement("sensorbuttonname");
xml->writeAttribute("type", QString::number(currSensor.value()->getType()));
xml->writeAttribute("button", QString::number(button->getRealJoyNumber()));
xml->writeCharacters(button->getButtonName());
xml->writeEndElement();
}
}
}
}
}

void GameControllerXml::writeXmlForVDpad(QXmlStreamWriter *xml)
{
QHashIterator<int, VDPad *> currVDPad(m_gameController->getActiveSetJoystick()->getVdpads());
Expand Down
1 change: 1 addition & 0 deletions src/gamecontroller/xml/gamecontrollerxml.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class GameControllerXml : public InputDeviceXml
void writeXmlForAxes(SetJoystick *tempSet, QXmlStreamWriter *xml); // GameControllerXml class
void writeXmlAxBtn(JoyAxis *axis, JoyAxisButton *naxisbutton, QXmlStreamWriter *xml); // GameControllerXml class
void writeXmlForSticks(SetJoystick *tempSet, QXmlStreamWriter *xml); // GameControllerXml class
void writeXmlForSensors(SetJoystick *tempSet, QXmlStreamWriter *xml); // GameControllerXml class
void writeXmlForVDpad(QXmlStreamWriter *xml); // GameControllerXml class
void readXmlNamesShort(QString name, QXmlStreamReader *xml); // GameControllerXml class
void readXmlNamesMiddle(QString name, QXmlStreamReader *xml); // GameControllerXml class
Expand Down
4 changes: 4 additions & 0 deletions src/globalvariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ const QString GlobalVariables::JoyControlStickButton::xmlName = "stickbutton";

const QString GlobalVariables::JoyControlStickModifierButton::xmlName = "stickmodifierbutton";

// ---- JoySensorButton ---- //

const QString GlobalVariables::JoySensorButton::xmlName = "sensorbutton";

// ---- JoyDPadButton ---- //

const QString GlobalVariables::JoyDPadButton::xmlName = "dpadbutton";
6 changes: 6 additions & 0 deletions src/globalvariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ class JoyControlStickModifierButton
static const QString xmlName;
};

class JoySensorButton
{
public:
static const QString xmlName;
};

class JoyDPadButton
{
public:
Expand Down
65 changes: 65 additions & 0 deletions src/inputdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
#include "common.h"
#include "globalvariables.h"
#include "joybuttontypes/joycontrolstickbutton.h"
#include "joybuttontypes/joysensorbutton.h"
#include "joybuttontypes/joydpadbutton.h"
#include "joycontrolstick.h"
#include "joysensor.h"
#include "joydpad.h"
#include "vdpad.h"

Expand Down Expand Up @@ -826,6 +828,30 @@ void InputDevice::setStickButtonName(int stickIndex, int buttonIndex, QString te
}
}

void InputDevice::setSensorButtonName(int sensorType, int buttonIndex, QString tempName)
{
QHashIterator<int, SetJoystick *> iter(getJoystick_sets());

while (iter.hasNext())
{
SetJoystick *tempSet = iter.next().value();
disconnect(tempSet, &SetJoystick::setStickButtonNameChange, this,
&InputDevice::updateSetStickButtonNames);
JoySensor *sensor = tempSet->getSensor(sensorType);

if (sensor != nullptr)
{
JoySensorButton *button = sensor->getDirectionButton(
JoySensorDirection(buttonIndex));

if (button != nullptr)
button->setButtonName(tempName);
}

connect(tempSet, &SetJoystick::setSensorButtonNameChange, this,
&InputDevice::updateSetSensorButtonNames);
}
}
void InputDevice::setDPadButtonName(int dpadIndex, int buttonIndex, QString tempName)
{
QHashIterator<int, SetJoystick *> iter(getJoystick_sets());
Expand Down Expand Up @@ -904,6 +930,23 @@ void InputDevice::setStickName(int stickIndex, QString tempName)
}
}

void InputDevice::setSensorName(int sensorType, QString tempName)
{
QHashIterator<int, SetJoystick *> iter(getJoystick_sets());

while (iter.hasNext())
{
SetJoystick *tempSet = iter.next().value();
disconnect(tempSet, &SetJoystick::setSensorNameChange, this, &InputDevice::updateSetSensorNames);
JoySensor *sensor = tempSet->getSensor(sensorType);

if (sensor != nullptr)
sensor->setSensorName(tempName);

connect(tempSet, &SetJoystick::setSensorNameChange, this, &InputDevice::updateSetSensorNames);
}
}

void InputDevice::setDPadName(int dpadIndex, QString tempName)
{
QHashIterator<int, SetJoystick *> iter(getJoystick_sets());
Expand Down Expand Up @@ -977,6 +1020,20 @@ void InputDevice::updateSetStickButtonNames(int stickIndex, int buttonIndex)
}
}

void InputDevice::updateSetSensorButtonNames(int sensorType, int buttonIndex)
{
JoySensor *sensor = getActiveSetJoystick()->getSensor(sensorType);

if (sensor != nullptr)
{
JoySensorButton *button =
sensor->getDirectionButton(JoySensorDirection(buttonIndex));

if (button != nullptr)
setSensorButtonName(sensorType, buttonIndex, button->getButtonName());
}
}

void InputDevice::updateSetDPadButtonNames(int dpadIndex, int buttonIndex)
{
JoyDPad *dpad = getActiveSetJoystick()->getJoyDPad(dpadIndex);
Expand Down Expand Up @@ -1019,6 +1076,14 @@ void InputDevice::updateSetStickNames(int stickIndex)
setStickName(stickIndex, stick->getStickName());
}

void InputDevice::updateSetSensorNames(int sensorType)
{
JoySensor *sensor = getActiveSetJoystick()->getSensor(sensorType);

if (sensor != nullptr)
setSensorName(sensorType, sensor->getSensorName());
}

void InputDevice::updateSetDPadNames(int dpadIndex)
{
JoyDPad *dpad = getActiveSetJoystick()->getJoyDPad(dpadIndex);
Expand Down
4 changes: 4 additions & 0 deletions src/inputdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ class InputDevice : public QObject
void setButtonName(int index, QString tempName); // InputDeviceXml class
void setAxisButtonName(int axisIndex, int buttonIndex, QString tempName); // InputDeviceXml class
void setStickButtonName(int stickIndex, int buttonIndex, QString tempName); // InputDeviceXml class
void setSensorButtonName(int sensorType, int buttonIndex, QString tempName);
void setDPadButtonName(int dpadIndex, int buttonIndex, QString tempName); // InputDeviceXml class
void setVDPadButtonName(int vdpadIndex, int buttonIndex, QString tempName); // InputDeviceXml class

void setAxisName(int axisIndex, QString tempName); // InputDeviceAxis class
void setStickName(int stickIndex, QString tempName); // InputDeviceStick class
void setSensorName(int sensorType, QString tempName);
void setDPadName(int dpadIndex, QString tempName); // InputDeviceHat class
void setVDPadName(int vdpadIndex, QString tempName); // InputDeviceVDPad class

Expand Down Expand Up @@ -219,11 +221,13 @@ class InputDevice : public QObject
void updateSetButtonNames(int index); // InputDeviceButton class
void updateSetAxisButtonNames(int axisIndex, int buttonIndex); // InputDeviceAxis class
void updateSetStickButtonNames(int stickIndex, int buttonIndex); // InputDeviceStick class
void updateSetSensorButtonNames(int stickIndex, int buttonIndex);
void updateSetDPadButtonNames(int dpadIndex, int buttonIndex); // InputDeviceHat class
void updateSetVDPadButtonNames(int vdpadIndex, int buttonIndex); // InputDeviceVDPad class

void updateSetAxisNames(int axisIndex); // InputDeviceAxis class
void updateSetStickNames(int stickIndex); // InputDeviceStick class
void updateSetSensorNames(int sensorType);
void updateSetDPadNames(int dpadIndex); // InputDeviceHat class
void updateSetVDPadNames(int vdpadIndex); // InputDeviceVDPad class

Expand Down
8 changes: 8 additions & 0 deletions src/joybuttontypes/joysensorbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ JoySensorButton::JoySensorButton(JoySensor *sensor,
{
}

/**
* @brief Get a 0 indexed number of button
* @return 0 indexed button index number
*/
int JoySensorButton::getRealJoyNumber() const { return m_index; }

QString JoySensorButton::getPartialName(bool forceFullFormat, bool displayNames) const
{
QString temp = m_sensor->getPartialName(forceFullFormat, displayNames);
Expand All @@ -59,6 +65,8 @@ QString JoySensorButton::getPartialName(bool forceFullFormat, bool displayNames)
return temp;
}

QString JoySensorButton::getXmlName() { return GlobalVariables::JoySensorButton::xmlName; }

/**
* @brief Check if button should be considered a part of a real controller
* axis. Needed for some dialogs so the program won't have to resort to
Expand Down
11 changes: 8 additions & 3 deletions src/joybuttontypes/joysensorbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@ class JoySensorButton : public JoyGradientButton
Q_OBJECT

public:
explicit JoySensorButton(JoySensor *sensor, int index, int originset, SetJoystick *parentSet, QObject *parent);
explicit JoySensorButton(JoySensor *sensor, int index, int originset,
SetJoystick *parentSet, QObject *parent);

virtual QString getPartialName(bool forceFullFormat = false, bool displayNames = false) const;
virtual int getRealJoyNumber() const;
virtual QString getPartialName(
bool forceFullFormat = false, bool displayNames = false) const;
virtual QString getXmlName();

virtual bool isPartRealAxis();

JoySensor *getSensor() const;
QString getDirectionName() const;

signals:
void setAssignmentChanged(int current_button, int axis_index, int associated_set, int mode);
void setAssignmentChanged(
int current_button, int axis_index, int associated_set, int mode);

private:
JoySensor *m_sensor;
Expand Down
Loading

0 comments on commit 7437814

Please sign in to comment.