Skip to content

Commit

Permalink
Allow remapping to/from triggers, fix default remappings not being ap…
Browse files Browse the repository at this point in the history
…plied
  • Loading branch information
emoose committed Feb 1, 2021
1 parent cfe4288 commit 89c3fa8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Xb2XInput/Xb2XInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ std::unordered_map<std::string, int> xinput_buttons =

{ "START", XUSB_GAMEPAD_START },
{ "BACK", XUSB_GAMEPAD_BACK },

{ "WHITE", XUSB_GAMEPAD_LEFT_SHOULDER },
{ "BLACK", XUSB_GAMEPAD_RIGHT_SHOULDER }
};

std::string PrintButtonCombination(int combo)
Expand Down
Binary file modified Xb2XInput/Xb2XInput.rc
Binary file not shown.
63 changes: 54 additions & 9 deletions Xb2XInput/XboxController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ UserSettings XboxController::LoadSettings(const std::string& ini_key, const User
if (defaults.remap_enabled)
ret.button_remap = defaults.button_remap;

if (GetSettingBool("RemapEnable", defaults.remap_enabled, ini_key))
ret.remap_enabled = GetSettingBool("RemapEnable", defaults.remap_enabled, ini_key);

if (ret.remap_enabled)
{
std::string remap;

Expand Down Expand Up @@ -201,6 +203,8 @@ UserSettings XboxController::LoadSettings(const std::string& ini_key, const User
LoadMap(DpadDown);
LoadMap(DpadLeft);
LoadMap(DpadRight);
LoadMap(LT);
LoadMap(RT);

#undef LoadMap
}
Expand Down Expand Up @@ -535,19 +539,33 @@ bool XboxController::update()
gamepad_.wButtons |= input_prev_.Gamepad.bAnalogButtons[OGXINPUT_GAMEPAD_WHITE] ? XUSB_GAMEPAD_LEFT_SHOULDER : 0;
gamepad_.wButtons |= input_prev_.Gamepad.bAnalogButtons[OGXINPUT_GAMEPAD_BLACK] ? XUSB_GAMEPAD_RIGHT_SHOULDER : 0;

// Trigger Deadzone Calculations
short triggerbuf;
deadZoneCalc(&triggerbuf, NULL, input_prev_.Gamepad.bAnalogButtons[OGXINPUT_GAMEPAD_LEFT_TRIGGER], 0, settings_.deadzone.bLeftTrigger, 0xFF);
gamepad_.bLeftTrigger = triggerbuf;
deadZoneCalc(&triggerbuf, NULL, input_prev_.Gamepad.bAnalogButtons[OGXINPUT_GAMEPAD_RIGHT_TRIGGER], 0, settings_.deadzone.bRightTrigger, 0xFF);
gamepad_.bRightTrigger = triggerbuf;

if (settings_.button_remap.size())
{
auto buttons = gamepad_.wButtons;
gamepad_.wButtons = 0;

auto leftTrig = gamepad_.bLeftTrigger;
auto rightTrig = gamepad_.bRightTrigger;

// TODO: could probably change this into a loop over XUSB_BUTTON enum instead of a macro?
#define LoadMap(btn) \
if (buttons & XUSB_GAMEPAD_##btn) \
{ \
auto remap = (int)XUSB_GAMEPAD_##btn; \
if (settings_.button_remap.count(XUSB_GAMEPAD_##btn)) \
remap = settings_.button_remap[XUSB_GAMEPAD_##btn]; \
gamepad_.wButtons |= remap; \
gamepad_.wButtons |= (remap & ~(XUSB_GAMEPAD_LT | XUSB_GAMEPAD_RT)); \
if (remap & XUSB_GAMEPAD_LT) \
gamepad_.bLeftTrigger = 255; \
if (remap & XUSB_GAMEPAD_RT) \
gamepad_.bRightTrigger = 255; \
}

LoadMap(A);
Expand All @@ -565,6 +583,40 @@ bool XboxController::update()
LoadMap(DpadLeft);
LoadMap(DpadRight);
#undef LoadMap

if (leftTrig >= 8)
{
if (settings_.button_remap.count(XUSB_GAMEPAD_LT))
{
gamepad_.bLeftTrigger = 0;

auto remap = settings_.button_remap[XUSB_GAMEPAD_LT];
gamepad_.wButtons |= (remap & ~(XUSB_GAMEPAD_LT | XUSB_GAMEPAD_RT));

if (remap & XUSB_GAMEPAD_LT)
gamepad_.bLeftTrigger = leftTrig;

if (remap & XUSB_GAMEPAD_RT)
gamepad_.bRightTrigger = leftTrig;
}
}

if (rightTrig >= 8)
{
if (settings_.button_remap.count(XUSB_GAMEPAD_RT))
{
gamepad_.bRightTrigger = 0;

auto remap = settings_.button_remap[XUSB_GAMEPAD_RT];
gamepad_.wButtons |= (remap & ~(XUSB_GAMEPAD_LT | XUSB_GAMEPAD_RT));

if (remap & XUSB_GAMEPAD_LT)
gamepad_.bLeftTrigger = rightTrig;

if (remap & XUSB_GAMEPAD_RT)
gamepad_.bRightTrigger = rightTrig;
}
}
}

// Secret Deadzone Adjustment Combinations:
Expand Down Expand Up @@ -623,13 +675,6 @@ bool XboxController::update()
deadZoneCalc(&gamepad_.sThumbLX, &gamepad_.sThumbLY, input_prev_.Gamepad.sThumbLX, input_prev_.Gamepad.sThumbLY, settings_.deadzone.sThumbL, SHRT_MAX);
deadZoneCalc(&gamepad_.sThumbRX, &gamepad_.sThumbRY, input_prev_.Gamepad.sThumbRX, input_prev_.Gamepad.sThumbRY, settings_.deadzone.sThumbR, SHRT_MAX);

// Trigger Deadzone Calculations
short triggerbuf;
deadZoneCalc(&triggerbuf, NULL, input_prev_.Gamepad.bAnalogButtons[OGXINPUT_GAMEPAD_LEFT_TRIGGER], 0, settings_.deadzone.bLeftTrigger, 0xFF);
gamepad_.bLeftTrigger = triggerbuf;
deadZoneCalc(&triggerbuf, NULL, input_prev_.Gamepad.bAnalogButtons[OGXINPUT_GAMEPAD_RIGHT_TRIGGER], 0, settings_.deadzone.bRightTrigger, 0xFF);
gamepad_.bRightTrigger = triggerbuf;

// Create a 'digital' bitfield so we can test combinations against LT/RT
int digitalPressed = gamepad_.wButtons;
if (gamepad_.bLeftTrigger >= 0x8)
Expand Down

0 comments on commit 89c3fa8

Please sign in to comment.