Skip to content

Commit 8c943ad

Browse files
committed
Add inversion option for joystick axes
Note that the 'invert' helper function returns int16_t instead of int32_t because it's used *after* the long value rescaling, and the max value used by the output data is int16
1 parent 867a1eb commit 8c943ad

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

src/XInput.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,12 @@ void XInputController::setJoystick(XInputControl joy, int32_t x, int32_t y) {
277277
setJoystickDirect(joy, x, y);
278278
}
279279

280-
void XInputController::setJoystickX(XInputControl joy, int32_t x) {
280+
void XInputController::setJoystickX(XInputControl joy, int32_t x, boolean invert) {
281281
const XInputMap_Joystick * joyData = getJoyFromEnum(joy);
282282
if (joyData == nullptr) return; // Not a joystick
283283

284284
x = rescaleInput(x, *getRangeFromEnum(joy), XInputMap_Joystick::range);
285+
if (invert) x = invertInput(x, XInputMap_Joystick::range);
285286

286287
if (getJoystickX(joy) == x) return; // Axis hasn't changed
287288

@@ -292,11 +293,12 @@ void XInputController::setJoystickX(XInputControl joy, int32_t x) {
292293
autosend();
293294
}
294295

295-
void XInputController::setJoystickY(XInputControl joy, int32_t y) {
296+
void XInputController::setJoystickY(XInputControl joy, int32_t y, boolean invert) {
296297
const XInputMap_Joystick * joyData = getJoyFromEnum(joy);
297298
if (joyData == nullptr) return; // Not a joystick
298299

299300
y = rescaleInput(y, *getRangeFromEnum(joy), XInputMap_Joystick::range);
301+
if (invert) y = invertInput(y, XInputMap_Joystick::range);
300302

301303
if (getJoystickY(joy) == y) return; // Axis hasn't changed
302304

@@ -521,6 +523,10 @@ int32_t XInputController::rescaleInput(int32_t val, const Range& in, const Range
521523
return map(val, in.min, in.max, out.min, out.max);
522524
}
523525

526+
int16_t XInputController::invertInput(int16_t val, const Range& range) {
527+
return range.max - val + range.min;
528+
}
529+
524530
void XInputController::setTriggerRange(int32_t rangeMin, int32_t rangeMax) {
525531
setRange(TRIGGER_LEFT, rangeMin, rangeMax);
526532
setRange(TRIGGER_RIGHT, rangeMin, rangeMax);

src/XInput.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ class XInputController {
9191
void setTrigger(XInputControl trigger, int32_t val);
9292

9393
void setJoystick(XInputControl joy, int32_t x, int32_t y);
94-
void setJoystickX(XInputControl joy, int32_t x);
95-
void setJoystickY(XInputControl joy, int32_t y);
94+
void setJoystickX(XInputControl joy, int32_t x, boolean invert=false);
95+
void setJoystickY(XInputControl joy, int32_t y, boolean invert=false);
9696
void setJoystick(XInputControl joy, boolean up, boolean down, boolean left, boolean right, boolean useSOCD=true);
9797

9898
void releaseAll();
@@ -162,6 +162,7 @@ class XInputController {
162162
Range rangeTrigLeft, rangeTrigRight, rangeJoyLeft, rangeJoyRight;
163163
Range * getRangeFromEnum(XInputControl ctrl);
164164
static int32_t rescaleInput(int32_t val, const Range& in, const Range &out);
165+
static int16_t invertInput(int16_t val, const Range& range);
165166
};
166167

167168
extern XInputController XInput;

0 commit comments

Comments
 (0)