Add function to retrieve all available joystick buttons

This commit is contained in:
Roland Rossgotterer
2019-02-14 16:08:40 +01:00
committed by Mat Sutcliffe
parent 2503a55dae
commit e5c435f281
9 changed files with 97 additions and 30 deletions

View File

@@ -81,6 +81,16 @@ namespace BlackInput
return true;
}
CJoystickButtonList CJoystickDevice::getDeviceButtons() const
{
CJoystickButtonList buttons;
for (const CJoystickDeviceInput &deviceInput : m_joystickDeviceInputs)
{
buttons.push_back(deviceInput.m_button);
}
return buttons;
}
void CJoystickDevice::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event);
@@ -123,8 +133,8 @@ namespace BlackInput
const qint32 buttonIndex = input.m_offset - DIJOFS_BUTTON0;
bool isPressed = state.rgbButtons[buttonIndex] & 0x80;
if (isPressed) { emit buttonChanged(m_deviceName, buttonIndex, true); }
else { emit buttonChanged(m_deviceName, buttonIndex, false); }
if (isPressed) { emit buttonChanged(input.m_button, true); }
else { emit buttonChanged(input.m_button, false); }
}
return hr;
@@ -138,12 +148,11 @@ namespace BlackInput
if (dev->guidType != GUID_Button) return DIENUM_CONTINUE;
CJoystickDeviceInput deviceInput;
deviceInput.m_number = joystickDevice->m_joystickDeviceInputs.size();
deviceInput.m_offset = DIJOFS_BUTTON(deviceInput.m_number);
deviceInput.m_name = QString::fromWCharArray(dev->tszName);
int number = joystickDevice->m_joystickDeviceInputs.size();
deviceInput.m_offset = DIJOFS_BUTTON(number);
deviceInput.m_button = CJoystickButton(joystickDevice->m_deviceName, DIJOFS_BUTTON(number) - DIJOFS_BUTTON0);
joystickDevice->m_joystickDeviceInputs.append(deviceInput);
CLogMessage(static_cast<CJoystickWindows *>(nullptr)).debug() << "Found joystick button" << QString::fromWCharArray(dev->tszName);
return DIENUM_CONTINUE;
@@ -196,6 +205,16 @@ namespace BlackInput
destroyHelperWindow();
}
CJoystickButtonList CJoystickWindows::getAllAvailableJoystickButtons() const
{
CJoystickButtonList availableButtons;
for (const CJoystickDevice *device : as_const(m_joystickDevices))
{
availableButtons.push_back(device->getDeviceButtons());
}
return availableButtons;
}
void ReleaseDirectInput(IDirectInput8 *obj)
{
if (obj) { obj->Release(); }
@@ -304,11 +323,11 @@ namespace BlackInput
}
}
void CJoystickWindows::joystickButtonChanged(const QString &name, int index, bool isPressed)
void CJoystickWindows::joystickButtonChanged(const CJoystickButton &joystickButton, bool isPressed)
{
BlackMisc::Input::CHotkeyCombination oldCombination(m_buttonCombination);
if (isPressed) { m_buttonCombination.addJoystickButton({name, index}); }
else { m_buttonCombination.removeJoystickButton({name, index}); }
if (isPressed) { m_buttonCombination.addJoystickButton(joystickButton); }
else { m_buttonCombination.removeJoystickButton(joystickButton); }
if (oldCombination != m_buttonCombination)
{
@@ -338,8 +357,7 @@ namespace BlackInput
bool operator == (CJoystickDeviceInput const &lhs, CJoystickDeviceInput const &rhs)
{
return lhs.m_number == rhs.m_number &&
lhs.m_offset == rhs.m_offset &&
lhs.m_name == rhs.m_name;
return lhs.m_offset == rhs.m_offset &&
lhs.m_button == rhs.m_button;
}
} // ns

View File

@@ -33,9 +33,8 @@ namespace BlackInput
//! Joystick device input/button
struct CJoystickDeviceInput
{
int m_number; //!< Input number
int m_offset; //!< Input offset
QString m_name; //!< Input name
int m_offset; //!< Input offset
BlackMisc::Input::CJoystickButton m_button; //!< Joystick button
};
//! Joystick device
@@ -50,9 +49,12 @@ namespace BlackInput
//! Initialize DirectInput device
bool init(HWND helperWindow);
//! Get all available device buttons
BlackMisc::Input::CJoystickButtonList getDeviceButtons() const;
signals:
//! Joystick button changed
void buttonChanged(const QString &name, int index, bool isPressed);
void buttonChanged(const BlackMisc::Input::CJoystickButton &joystickButton, bool isPressed);
protected:
//! Timer based updates
@@ -108,6 +110,9 @@ namespace BlackInput
//! \brief Destructor
virtual ~CJoystickWindows() override;
//! \copydoc BlackInput::IJoystick::getAllAvailableJoystickButtons()
virtual BlackMisc::Input::CJoystickButtonList getAllAvailableJoystickButtons() const override;
private:
friend class IJoystick;
@@ -129,7 +134,7 @@ namespace BlackInput
//! Add new joystick device
void addJoystickDevice(const DIDEVICEINSTANCE *pdidInstance);
void joystickButtonChanged(const QString &name, int index, bool isPressed);
void joystickButtonChanged(const BlackMisc::Input::CJoystickButton &joystickButton, bool isPressed);
//! Joystick enumeration callback
static BOOL CALLBACK enumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, VOID *pContext);