DInputControllerInterface: Support diagonals in hat

This commit is contained in:
Connor McLaughlin 2021-03-25 13:50:13 +10:00
parent 03905b8f2e
commit f1fb7383b7
2 changed files with 25 additions and 18 deletions

View file

@ -222,19 +222,24 @@ void DInputControllerInterface::PollEvents()
} }
} }
u32 DInputControllerInterface::GetHatDirection(DWORD hat) std::array<bool, ControllerInterface::NUM_HAT_DIRECTIONS> DInputControllerInterface::GetHatButtons(DWORD hat)
{ {
std::array<bool, NUM_HAT_DIRECTIONS> buttons = {};
const WORD hv = LOWORD(hat); const WORD hv = LOWORD(hat);
if (hv == 0xFFFF) if (hv != 0xFFFF)
return NUM_HAT_DIRECTIONS; {
else if (hv < 9000) if ((hv >= 0 && hv < 9000) || hv >= 31500)
return HAT_DIRECTION_UP; buttons[HAT_DIRECTION_UP] = true;
else if (hv < 18000) if (hv >= 4500 && hv < 18000)
return HAT_DIRECTION_RIGHT; buttons[HAT_DIRECTION_RIGHT] = true;
else if (hv < 27000) if (hv >= 13500 && hv < 27000)
return HAT_DIRECTION_DOWN; buttons[HAT_DIRECTION_DOWN] = true;
else if (hv >= 22500)
return HAT_DIRECTION_LEFT; buttons[HAT_DIRECTION_LEFT] = true;
}
return buttons;
} }
void DInputControllerInterface::CheckForStateChanges(u32 index, const DIJOYSTATE& new_state) void DInputControllerInterface::CheckForStateChanges(u32 index, const DIJOYSTATE& new_state)
@ -268,14 +273,16 @@ void DInputControllerInterface::CheckForStateChanges(u32 index, const DIJOYSTATE
{ {
if (last_state.rgdwPOV[0] != new_state.rgdwPOV[0]) if (last_state.rgdwPOV[0] != new_state.rgdwPOV[0])
{ {
Log_InfoPrintf("Hat %u", LOWORD(new_state.rgdwPOV[0]));
// map hats to the last buttons // map hats to the last buttons
const u32 old_direction = GetHatDirection(last_state.rgdwPOV[0]); const std::array<bool, NUM_HAT_DIRECTIONS> old_buttons(GetHatButtons(last_state.rgdwPOV[0]));
if (old_direction != NUM_HAT_DIRECTIONS) const std::array<bool, NUM_HAT_DIRECTIONS> new_buttons(GetHatButtons(new_state.rgdwPOV[0]));
HandleButtonEvent(index, cd.num_buttons + old_direction, false); for (u32 i = 0; i < NUM_HAT_DIRECTIONS; i++)
{
if (old_buttons[i] != new_buttons[i])
HandleButtonEvent(index, cd.num_buttons + i, new_buttons[i]);
}
const u32 new_direction = GetHatDirection(new_state.rgdwPOV[0]);
if (new_direction != NUM_HAT_DIRECTIONS)
HandleButtonEvent(index, cd.num_buttons + new_direction, true);
last_state.rgdwPOV[0] = new_state.rgdwPOV[0]; last_state.rgdwPOV[0] = new_state.rgdwPOV[0];
} }
} }

View file

@ -79,7 +79,7 @@ private:
void AddDevices(); void AddDevices();
bool AddDevice(ControllerData& cd, const char* name); bool AddDevice(ControllerData& cd, const char* name);
static u32 GetHatDirection(DWORD hat); static std::array<bool, NUM_HAT_DIRECTIONS> GetHatButtons(DWORD hat);
void CheckForStateChanges(u32 index, const DIJOYSTATE& new_state); void CheckForStateChanges(u32 index, const DIJOYSTATE& new_state);