Make Joystick::Axis a scoped enumeration

This commit is contained in:
kimci86 2023-12-10 17:41:54 +01:00 committed by Chris Thrasher
parent df2f56fe83
commit ebf190b660
9 changed files with 146 additions and 145 deletions

View File

@ -51,7 +51,7 @@ static constexpr unsigned int AxisCount{8}; //!< Maximum number of supported
/// \brief Axes supported by SFML joysticks
///
////////////////////////////////////////////////////////////
enum Axis
enum class Axis
{
X, //!< The X axis
Y, //!< The Y axis
@ -199,13 +199,13 @@ SFML_WINDOW_API void update();
/// unsigned int buttons = sf::Joystick::getButtonCount(0);
///
/// // Does joystick #0 define a X axis?
/// bool hasX = sf::Joystick::hasAxis(0, sf::Joystick::X);
/// bool hasX = sf::Joystick::hasAxis(0, sf::Joystick::Axis::X);
///
/// // Is button #2 pressed on joystick #0?
/// bool pressed = sf::Joystick::isButtonPressed(0, 2);
///
/// // What's the current position of the Y axis on joystick #0?
/// float position = sf::Joystick::getAxisPosition(0, sf::Joystick::Y);
/// float position = sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::Y);
/// \endcode
///
/// \see sf::Keyboard, sf::Mouse

View File

@ -30,6 +30,7 @@
#include <dirent.h>
#include <fcntl.h>
#include <optional>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
@ -135,31 +136,31 @@ void updatePluggedList()
}
}
int usageToAxis(int usage)
std::optional<sf::Joystick::Axis> usageToAxis(int usage)
{
switch (usage)
{
case HUG_X:
return sf::Joystick::X;
return sf::Joystick::Axis::X;
case HUG_Y:
return sf::Joystick::Y;
return sf::Joystick::Axis::Y;
case HUG_Z:
return sf::Joystick::Z;
return sf::Joystick::Axis::Z;
case HUG_RZ:
return sf::Joystick::R;
return sf::Joystick::Axis::R;
case HUG_RX:
return sf::Joystick::U;
return sf::Joystick::Axis::U;
case HUG_RY:
return sf::Joystick::V;
return sf::Joystick::Axis::V;
default:
return -1;
return std::nullopt;
}
}
void hatValueToSfml(int value, sf::priv::JoystickState& state)
{
state.axes[sf::Joystick::PovX] = static_cast<float>(hatValueMap[value].first);
state.axes[sf::Joystick::PovY] = static_cast<float>(hatValueMap[value].second);
state.axes[static_cast<int>(sf::Joystick::Axis::PovX)] = static_cast<float>(hatValueMap[value].first);
state.axes[static_cast<int>(sf::Joystick::Axis::PovY)] = static_cast<float>(hatValueMap[value].second);
}
} // namespace
@ -268,16 +269,14 @@ JoystickCaps JoystickImpl::getCapabilities() const
}
else if (usage == HUP_GENERIC_DESKTOP)
{
int axis = usageToAxis(usage);
if (usage == HUG_HAT_SWITCH)
{
caps.axes[Joystick::PovX] = true;
caps.axes[Joystick::PovY] = true;
caps.axes[static_cast<int>(Joystick::Axis::PovX)] = true;
caps.axes[static_cast<int>(Joystick::Axis::PovY)] = true;
}
else if (axis != -1)
else if (const std::optional<Joystick::Axis> axis = usageToAxis(usage))
{
caps.axes[axis] = true;
caps.axes[static_cast<int>(*axis)] = true;
}
}
}
@ -323,19 +322,18 @@ JoystickState JoystickImpl::JoystickImpl::update()
else if (usage == HUP_GENERIC_DESKTOP)
{
int value = hid_get_data(m_buffer.data(), &item);
int axis = usageToAxis(usage);
if (usage == HUG_HAT_SWITCH)
{
hatValueToSfml(value, m_state);
}
else if (axis != -1)
else if (const std::optional<Joystick::Axis> axis = usageToAxis(usage))
{
int minimum = item.logical_minimum;
int maximum = item.logical_maximum;
value = (value - minimum) * 200 / (maximum - minimum) - 100;
m_state.axes[axis] = static_cast<float>(value);
value = (value - minimum) * 200 / (maximum - minimum) - 100;
m_state.axes[static_cast<int>(*axis)] = static_cast<float>(value);
}
}
}

View File

@ -50,7 +50,7 @@ unsigned int Joystick::getButtonCount(unsigned int joystick)
////////////////////////////////////////////////////////////
bool Joystick::hasAxis(unsigned int joystick, Axis axis)
{
return priv::JoystickManager::getInstance().getCapabilities(joystick).axes[axis];
return priv::JoystickManager::getInstance().getCapabilities(joystick).axes[static_cast<int>(axis)];
}
@ -65,7 +65,7 @@ bool Joystick::isButtonPressed(unsigned int joystick, unsigned int button)
////////////////////////////////////////////////////////////
float Joystick::getAxisPosition(unsigned int joystick, Axis axis)
{
return priv::JoystickManager::getInstance().getState(joystick).axes[axis];
return priv::JoystickManager::getInstance().getState(joystick).axes[static_cast<int>(axis)];
}

View File

@ -30,6 +30,7 @@
#include <dirent.h>
#include <fcntl.h>
#include <optional>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
@ -136,31 +137,31 @@ void updatePluggedList()
}
}
int usageToAxis(int usage)
std::optional<sf::Joystick::Axis> usageToAxis(int usage)
{
switch (usage)
{
case HUG_X:
return sf::Joystick::X;
return sf::Joystick::Axis::X;
case HUG_Y:
return sf::Joystick::Y;
return sf::Joystick::Axis::Y;
case HUG_Z:
return sf::Joystick::Z;
return sf::Joystick::Axis::Z;
case HUG_RZ:
return sf::Joystick::R;
return sf::Joystick::Axis::R;
case HUG_RX:
return sf::Joystick::U;
return sf::Joystick::Axis::U;
case HUG_RY:
return sf::Joystick::V;
return sf::Joystick::Axis::V;
default:
return -1;
return std::nullopt;
}
}
void hatValueToSfml(int value, sf::priv::JoystickState& state)
{
state.axes[sf::Joystick::PovX] = hatValueMap[value].first;
state.axes[sf::Joystick::PovY] = hatValueMap[value].second;
state.axes[static_cast<int>(sf::Joystick::Axis::PovX)] = static_cast<float>(hatValueMap[value].first);
state.axes[static_cast<int>(sf::Joystick::Axis::PovY)] = static_cast<float>(hatValueMap[value].second);
}
} // namespace
@ -273,16 +274,14 @@ JoystickCaps JoystickImpl::getCapabilities() const
}
else if (usage == HUP_GENERIC_DESKTOP)
{
int axis = usageToAxis(usage);
if (usage == HUG_HAT_SWITCH)
{
caps.axes[Joystick::PovX] = true;
caps.axes[Joystick::PovY] = true;
caps.axes[static_cast<int>(Joystick::Axis::PovX)] = true;
caps.axes[static_cast<int>(Joystick::Axis::PovY)] = true;
}
else if (axis != -1)
else if (const std::optional<Joystick::Axis> axis = usageToAxis(usage))
{
caps.axes[axis] = true;
caps.axes[static_cast<int>(*axis)] = true;
}
}
}
@ -328,19 +327,18 @@ JoystickState JoystickImpl::JoystickImpl::update()
else if (usage == HUP_GENERIC_DESKTOP)
{
int value = hid_get_data(m_buffer.data(), &item);
int axis = usageToAxis(usage);
if (usage == HUG_HAT_SWITCH)
{
hatValueToSfml(value, m_state);
}
else if (axis != -1)
else if (const std::optional<Joystick::Axis> axis = usageToAxis(usage))
{
int minimum = item.logical_minimum;
int maximum = item.logical_maximum;
value = (value - minimum) * 200 / (maximum - minimum) - 100;
m_state.axes[axis] = value;
value = (value - minimum) * 200 / (maximum - minimum) - 100;
m_state.axes[static_cast<int>(*axis)] = value;
}
}
}

View File

@ -603,17 +603,17 @@ JoystickCaps JoystickImpl::getCapabilities() const
switch (m_mapping[i])
{
// clang-format off
case ABS_X: caps.axes[Joystick::X] = true; break;
case ABS_Y: caps.axes[Joystick::Y] = true; break;
case ABS_X: caps.axes[static_cast<int>(Joystick::Axis::X)] = true; break;
case ABS_Y: caps.axes[static_cast<int>(Joystick::Axis::Y)] = true; break;
case ABS_Z:
case ABS_THROTTLE: caps.axes[Joystick::Z] = true; break;
case ABS_THROTTLE: caps.axes[static_cast<int>(Joystick::Axis::Z)] = true; break;
case ABS_RZ:
case ABS_RUDDER: caps.axes[Joystick::R] = true; break;
case ABS_RX: caps.axes[Joystick::U] = true; break;
case ABS_RY: caps.axes[Joystick::V] = true; break;
case ABS_HAT0X: caps.axes[Joystick::PovX] = true; break;
case ABS_HAT0Y: caps.axes[Joystick::PovY] = true; break;
default: break;
case ABS_RUDDER: caps.axes[static_cast<int>(Joystick::Axis::R)] = true; break;
case ABS_RX: caps.axes[static_cast<int>(Joystick::Axis::U)] = true; break;
case ABS_RY: caps.axes[static_cast<int>(Joystick::Axis::V)] = true; break;
case ABS_HAT0X: caps.axes[static_cast<int>(Joystick::Axis::PovX)] = true; break;
case ABS_HAT0Y: caps.axes[static_cast<int>(Joystick::Axis::PovY)] = true; break;
default: break;
// clang-format on
}
}
@ -655,30 +655,30 @@ JoystickState JoystickImpl::JoystickImpl::update()
switch (m_mapping[joyState.number])
{
case ABS_X:
m_state.axes[Joystick::X] = value;
m_state.axes[static_cast<int>(Joystick::Axis::X)] = value;
break;
case ABS_Y:
m_state.axes[Joystick::Y] = value;
m_state.axes[static_cast<int>(Joystick::Axis::Y)] = value;
break;
case ABS_Z:
case ABS_THROTTLE:
m_state.axes[Joystick::Z] = value;
m_state.axes[static_cast<int>(Joystick::Axis::Z)] = value;
break;
case ABS_RZ:
case ABS_RUDDER:
m_state.axes[Joystick::R] = value;
m_state.axes[static_cast<int>(Joystick::Axis::R)] = value;
break;
case ABS_RX:
m_state.axes[Joystick::U] = value;
m_state.axes[static_cast<int>(Joystick::Axis::U)] = value;
break;
case ABS_RY:
m_state.axes[Joystick::V] = value;
m_state.axes[static_cast<int>(Joystick::Axis::V)] = value;
break;
case ABS_HAT0X:
m_state.axes[Joystick::PovX] = value;
m_state.axes[static_cast<int>(Joystick::Axis::PovX)] = value;
break;
case ABS_HAT0Y:
m_state.axes[Joystick::PovY] = value;
m_state.axes[static_cast<int>(Joystick::Axis::PovY)] = value;
break;
default:
break;

View File

@ -331,14 +331,14 @@ JoystickCaps JoystickImpl::getCapabilities() const
if (caps.buttonCount > Joystick::ButtonCount)
caps.buttonCount = Joystick::ButtonCount;
caps.axes[Joystick::X] = true;
caps.axes[Joystick::Y] = true;
caps.axes[Joystick::Z] = (m_caps.wCaps & JOYCAPS_HASZ) != 0;
caps.axes[Joystick::R] = (m_caps.wCaps & JOYCAPS_HASR) != 0;
caps.axes[Joystick::U] = (m_caps.wCaps & JOYCAPS_HASU) != 0;
caps.axes[Joystick::V] = (m_caps.wCaps & JOYCAPS_HASV) != 0;
caps.axes[Joystick::PovX] = (m_caps.wCaps & JOYCAPS_HASPOV) != 0;
caps.axes[Joystick::PovY] = (m_caps.wCaps & JOYCAPS_HASPOV) != 0;
caps.axes[static_cast<int>(Joystick::Axis::X)] = true;
caps.axes[static_cast<int>(Joystick::Axis::Y)] = true;
caps.axes[static_cast<int>(Joystick::Axis::Z)] = (m_caps.wCaps & JOYCAPS_HASZ) != 0;
caps.axes[static_cast<int>(Joystick::Axis::R)] = (m_caps.wCaps & JOYCAPS_HASR) != 0;
caps.axes[static_cast<int>(Joystick::Axis::U)] = (m_caps.wCaps & JOYCAPS_HASU) != 0;
caps.axes[static_cast<int>(Joystick::Axis::V)] = (m_caps.wCaps & JOYCAPS_HASV) != 0;
caps.axes[static_cast<int>(Joystick::Axis::PovX)] = (m_caps.wCaps & JOYCAPS_HASPOV) != 0;
caps.axes[static_cast<int>(Joystick::Axis::PovY)] = (m_caps.wCaps & JOYCAPS_HASPOV) != 0;
return caps;
}
@ -379,30 +379,36 @@ JoystickState JoystickImpl::update()
state.connected = true;
// Axes
state.axes[Joystick::X] = (static_cast<float>(pos.dwXpos) - static_cast<float>(m_caps.wXmax + m_caps.wXmin) / 2.f) *
200.f / static_cast<float>(m_caps.wXmax - m_caps.wXmin);
state.axes[Joystick::Y] = (static_cast<float>(pos.dwYpos) - static_cast<float>(m_caps.wYmax + m_caps.wYmin) / 2.f) *
200.f / static_cast<float>(m_caps.wYmax - m_caps.wYmin);
state.axes[Joystick::Z] = (static_cast<float>(pos.dwZpos) - static_cast<float>(m_caps.wZmax + m_caps.wZmin) / 2.f) *
200.f / static_cast<float>(m_caps.wZmax - m_caps.wZmin);
state.axes[Joystick::R] = (static_cast<float>(pos.dwRpos) - static_cast<float>(m_caps.wRmax + m_caps.wRmin) / 2.f) *
200.f / static_cast<float>(m_caps.wRmax - m_caps.wRmin);
state.axes[Joystick::U] = (static_cast<float>(pos.dwUpos) - static_cast<float>(m_caps.wUmax + m_caps.wUmin) / 2.f) *
200.f / static_cast<float>(m_caps.wUmax - m_caps.wUmin);
state.axes[Joystick::V] = (static_cast<float>(pos.dwVpos) - static_cast<float>(m_caps.wVmax + m_caps.wVmin) / 2.f) *
200.f / static_cast<float>(m_caps.wVmax - m_caps.wVmin);
state.axes[static_cast<int>(Joystick::Axis::X)] = (static_cast<float>(pos.dwXpos) -
static_cast<float>(m_caps.wXmax + m_caps.wXmin) / 2.f) *
200.f / static_cast<float>(m_caps.wXmax - m_caps.wXmin);
state.axes[static_cast<int>(Joystick::Axis::Y)] = (static_cast<float>(pos.dwYpos) -
static_cast<float>(m_caps.wYmax + m_caps.wYmin) / 2.f) *
200.f / static_cast<float>(m_caps.wYmax - m_caps.wYmin);
state.axes[static_cast<int>(Joystick::Axis::Z)] = (static_cast<float>(pos.dwZpos) -
static_cast<float>(m_caps.wZmax + m_caps.wZmin) / 2.f) *
200.f / static_cast<float>(m_caps.wZmax - m_caps.wZmin);
state.axes[static_cast<int>(Joystick::Axis::R)] = (static_cast<float>(pos.dwRpos) -
static_cast<float>(m_caps.wRmax + m_caps.wRmin) / 2.f) *
200.f / static_cast<float>(m_caps.wRmax - m_caps.wRmin);
state.axes[static_cast<int>(Joystick::Axis::U)] = (static_cast<float>(pos.dwUpos) -
static_cast<float>(m_caps.wUmax + m_caps.wUmin) / 2.f) *
200.f / static_cast<float>(m_caps.wUmax - m_caps.wUmin);
state.axes[static_cast<int>(Joystick::Axis::V)] = (static_cast<float>(pos.dwVpos) -
static_cast<float>(m_caps.wVmax + m_caps.wVmin) / 2.f) *
200.f / static_cast<float>(m_caps.wVmax - m_caps.wVmin);
// Special case for POV, it is given as an angle
if (pos.dwPOV != 0xFFFF)
{
const float angle = static_cast<float>(pos.dwPOV) / 18000.f * 3.141592654f;
state.axes[Joystick::PovX] = std::sin(angle) * 100;
state.axes[Joystick::PovY] = std::cos(angle) * 100;
const float angle = static_cast<float>(pos.dwPOV) / 18000.f * 3.141592654f;
state.axes[static_cast<int>(Joystick::Axis::PovX)] = std::sin(angle) * 100;
state.axes[static_cast<int>(Joystick::Axis::PovY)] = std::cos(angle) * 100;
}
else
{
state.axes[Joystick::PovX] = 0;
state.axes[Joystick::PovY] = 0;
state.axes[static_cast<int>(Joystick::Axis::PovX)] = 0;
state.axes[static_cast<int>(Joystick::Axis::PovY)] = 0;
}
// Buttons
@ -925,7 +931,7 @@ JoystickState JoystickImpl::updateDInputBuffered()
{
if (m_axes[j] == static_cast<int>(events[i].dwOfs))
{
if ((j == Joystick::PovX) || (j == Joystick::PovY))
if ((j == static_cast<int>(Joystick::Axis::PovX)) || (j == static_cast<int>(Joystick::Axis::PovY)))
{
const unsigned short value = LOWORD(events[i].dwData);
@ -933,13 +939,13 @@ JoystickState JoystickImpl::updateDInputBuffered()
{
const float angle = (static_cast<float>(value)) * 3.141592654f / DI_DEGREES / 180.f;
m_state.axes[Joystick::PovX] = std::sin(angle) * 100.f;
m_state.axes[Joystick::PovY] = std::cos(angle) * 100.f;
m_state.axes[static_cast<int>(Joystick::Axis::PovX)] = std::sin(angle) * 100.f;
m_state.axes[static_cast<int>(Joystick::Axis::PovY)] = std::cos(angle) * 100.f;
}
else
{
m_state.axes[Joystick::PovX] = 0.f;
m_state.axes[Joystick::PovY] = 0.f;
m_state.axes[static_cast<int>(Joystick::Axis::PovX)] = 0.f;
m_state.axes[static_cast<int>(Joystick::Axis::PovY)] = 0.f;
}
}
else
@ -1014,7 +1020,7 @@ JoystickState JoystickImpl::updateDInputPolled()
{
if (m_axes[i] != -1)
{
if ((i == Joystick::PovX) || (i == Joystick::PovY))
if ((i == static_cast<int>(Joystick::Axis::PovX)) || (i == static_cast<int>(Joystick::Axis::PovY)))
{
const unsigned short value = LOWORD(
*reinterpret_cast<const DWORD*>(reinterpret_cast<const char*>(&joystate) + m_axes[i]));
@ -1023,13 +1029,13 @@ JoystickState JoystickImpl::updateDInputPolled()
{
const float angle = (static_cast<float>(value)) * 3.141592654f / DI_DEGREES / 180.f;
state.axes[Joystick::PovX] = std::sin(angle) * 100.f;
state.axes[Joystick::PovY] = std::cos(angle) * 100.f;
state.axes[static_cast<int>(Joystick::Axis::PovX)] = std::sin(angle) * 100.f;
state.axes[static_cast<int>(Joystick::Axis::PovY)] = std::cos(angle) * 100.f;
}
else
{
state.axes[Joystick::PovX] = 0.f;
state.axes[Joystick::PovY] = 0.f;
state.axes[static_cast<int>(Joystick::Axis::PovX)] = 0.f;
state.axes[static_cast<int>(Joystick::Axis::PovY)] = 0.f;
}
}
else
@ -1097,23 +1103,23 @@ BOOL CALLBACK JoystickImpl::deviceObjectEnumerationCallback(const DIDEVICEOBJECT
{
// Axes
if (deviceObjectInstance->guidType == guids::GUID_XAxis)
joystick.m_axes[Joystick::X] = DIJOFS_X;
joystick.m_axes[static_cast<int>(Joystick::Axis::X)] = DIJOFS_X;
else if (deviceObjectInstance->guidType == guids::GUID_YAxis)
joystick.m_axes[Joystick::Y] = DIJOFS_Y;
joystick.m_axes[static_cast<int>(Joystick::Axis::Y)] = DIJOFS_Y;
else if (deviceObjectInstance->guidType == guids::GUID_ZAxis)
joystick.m_axes[Joystick::Z] = DIJOFS_Z;
joystick.m_axes[static_cast<int>(Joystick::Axis::Z)] = DIJOFS_Z;
else if (deviceObjectInstance->guidType == guids::GUID_RzAxis)
joystick.m_axes[Joystick::R] = DIJOFS_RZ;
joystick.m_axes[static_cast<int>(Joystick::Axis::R)] = DIJOFS_RZ;
else if (deviceObjectInstance->guidType == guids::GUID_RxAxis)
joystick.m_axes[Joystick::U] = DIJOFS_RX;
joystick.m_axes[static_cast<int>(Joystick::Axis::U)] = DIJOFS_RX;
else if (deviceObjectInstance->guidType == guids::GUID_RyAxis)
joystick.m_axes[Joystick::V] = DIJOFS_RY;
joystick.m_axes[static_cast<int>(Joystick::Axis::V)] = DIJOFS_RY;
else if (deviceObjectInstance->guidType == guids::GUID_Slider)
{
if (joystick.m_axes[Joystick::U] == -1)
joystick.m_axes[Joystick::U] = DIJOFS_SLIDER(0);
if (joystick.m_axes[static_cast<int>(Joystick::Axis::U)] == -1)
joystick.m_axes[static_cast<int>(Joystick::Axis::U)] = DIJOFS_SLIDER(0);
else
joystick.m_axes[Joystick::V] = DIJOFS_SLIDER(1);
joystick.m_axes[static_cast<int>(Joystick::Axis::V)] = DIJOFS_SLIDER(1);
}
else
return DIENUM_CONTINUE;
@ -1139,10 +1145,10 @@ BOOL CALLBACK JoystickImpl::deviceObjectEnumerationCallback(const DIDEVICEOBJECT
// POVs
if (deviceObjectInstance->guidType == guids::GUID_POV)
{
if (joystick.m_axes[Joystick::PovX] == -1)
if (joystick.m_axes[static_cast<int>(Joystick::Axis::PovX)] == -1)
{
joystick.m_axes[Joystick::PovX] = DIJOFS_POV(0);
joystick.m_axes[Joystick::PovY] = DIJOFS_POV(0);
joystick.m_axes[static_cast<int>(Joystick::Axis::PovX)] = DIJOFS_POV(0);
joystick.m_axes[static_cast<int>(Joystick::Axis::PovY)] = DIJOFS_POV(0);
}
}

View File

@ -252,19 +252,18 @@ void WindowImpl::processJoystickEvents()
{
if (caps.axes[j])
{
const auto axis = static_cast<Joystick::Axis>(j);
const float prevPos = m_previousAxes[i][axis];
const float currPos = m_joystickStatesImpl->states[i].axes[axis];
const float prevPos = m_previousAxes[i][j];
const float currPos = m_joystickStatesImpl->states[i].axes[j];
if (std::abs(currPos - prevPos) >= m_joystickThreshold)
{
Event event;
event.type = Event::JoystickMoved;
event.joystickMove.joystickId = i;
event.joystickMove.axis = axis;
event.joystickMove.axis = static_cast<Joystick::Axis>(j);
event.joystickMove.position = currPos;
pushEvent(event);
m_previousAxes[i][axis] = currPos;
m_previousAxes[i][j] = currPos;
}
}
}

View File

@ -236,22 +236,22 @@ bool JoystickImpl::open(unsigned int index)
switch (IOHIDElementGetUsage(element))
{
case kHIDUsage_GD_X:
m_axis[Joystick::X] = element;
m_axis[Joystick::Axis::X] = element;
break;
case kHIDUsage_GD_Y:
m_axis[Joystick::Y] = element;
m_axis[Joystick::Axis::Y] = element;
break;
case kHIDUsage_GD_Z:
m_axis[Joystick::Z] = element;
m_axis[Joystick::Axis::Z] = element;
break;
case kHIDUsage_GD_Rx:
m_axis[Joystick::U] = element;
m_axis[Joystick::Axis::U] = element;
break;
case kHIDUsage_GD_Ry:
m_axis[Joystick::V] = element;
m_axis[Joystick::Axis::V] = element;
break;
case kHIDUsage_GD_Rz:
m_axis[Joystick::R] = element;
m_axis[Joystick::Axis::R] = element;
break;
case kHIDUsage_GD_Hatswitch:
@ -377,10 +377,10 @@ JoystickCaps JoystickImpl::getCapabilities() const
// Axis:
for (const auto& [axis, iohidElementRef] : m_axis)
caps.axes[axis] = true;
caps.axes[static_cast<int>(axis)] = true;
if (m_hat != nullptr)
caps.axes[Joystick::PovX] = caps.axes[Joystick::PovY] = true;
caps.axes[static_cast<int>(Joystick::Axis::PovX)] = caps.axes[static_cast<int>(Joystick::Axis::PovY)] = true;
return caps;
}
@ -480,7 +480,7 @@ JoystickState JoystickImpl::update()
const double physicalValue = IOHIDValueGetScaledValue(value, kIOHIDValueScaleTypePhysical);
const auto scaledValue = static_cast<float>(
(((physicalValue - physicalMin) * (scaledMax - scaledMin)) / (physicalMax - physicalMin)) + scaledMin);
state.axes[axis] = scaledValue;
state.axes[static_cast<int>(axis)] = scaledValue;
}
// Update POV/Hat state. Assuming model described in `open`, values are:
@ -508,17 +508,17 @@ JoystickState JoystickImpl::update()
case 1:
case 2:
case 3:
state.axes[Joystick::PovX] = +100;
state.axes[static_cast<int>(Joystick::Axis::PovX)] = +100;
break;
case 5:
case 6:
case 7:
state.axes[Joystick::PovX] = -100;
state.axes[static_cast<int>(Joystick::Axis::PovX)] = -100;
break;
default:
state.axes[Joystick::PovX] = 0;
state.axes[static_cast<int>(Joystick::Axis::PovX)] = 0;
break;
}
@ -528,17 +528,17 @@ JoystickState JoystickImpl::update()
case 0:
case 1:
case 7:
state.axes[Joystick::PovY] = +100;
state.axes[static_cast<int>(Joystick::Axis::PovY)] = +100;
break;
case 3:
case 4:
case 5:
state.axes[Joystick::PovY] = -100;
state.axes[static_cast<int>(Joystick::Axis::PovY)] = -100;
break;
default:
state.axes[Joystick::PovY] = 0;
state.axes[static_cast<int>(Joystick::Axis::PovY)] = 0;
break;
}
}

View File

@ -38,14 +38,14 @@ TEST_CASE("[Window] sf::Joystick")
SECTION("hasAxis()")
{
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::X));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Y));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Z));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::R));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::U));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::V));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::PovX));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::PovY));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Axis::X));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Axis::Y));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Axis::Z));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Axis::R));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Axis::U));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Axis::V));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Axis::PovX));
CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Axis::PovY));
}
SECTION("isButtonPressed()")
@ -56,14 +56,14 @@ TEST_CASE("[Window] sf::Joystick")
SECTION("getAxisPosition")
{
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::X) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Y) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Z) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::R) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::U) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::V) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::PovX) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::PovY) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Axis::X) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Axis::Y) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Axis::Z) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Axis::R) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Axis::U) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Axis::V) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Axis::PovX) == 0);
CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Axis::PovY) == 0);
}
SECTION("getIdentification()")