Skip to content

Commit c12a974

Browse files
committed
Add 4-direction digital joystick function
For setting the joystick output from a set of buttons or a directional (read: digital) joystick. Uses the internal joystick function to avoid rescaling.
1 parent d5d51d2 commit c12a974

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/XInput.cpp

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

280+
void XInputController::setJoystick(XInputControl joy, boolean up, boolean down, boolean left, boolean right) {
281+
const XInputMap_Joystick * joyData = getJoyFromEnum(joy);
282+
if (joyData == nullptr) return; // Not a joystick
283+
284+
const Range & range = XInputMap_Joystick::range;
285+
286+
int16_t x = 0;
287+
int16_t y = 0;
288+
289+
// Simultaneous Opposite Cardinal Directions (SOCD) Cleaner
290+
// (Mutually exclusive. Avoids the '-1' result from adding the int16 extremes)
291+
if (left != right) {
292+
x = (right * range.max) - (left * range.min);
293+
}
294+
if (up != down) {
295+
y = (up * range.max) - (down * range.min);
296+
}
297+
298+
setJoystickDirect(joy, x, y);
299+
}
300+
280301
void XInputController::setJoystickDirect(XInputControl joy, int16_t x, int16_t y) {
281302
const XInputMap_Joystick * joyData = getJoyFromEnum(joy);
282303
if (joyData == nullptr) return; // Not a joystick

src/XInput.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class XInputController {
9191
void setTrigger(XInputControl trigger, int32_t val);
9292

9393
void setJoystick(XInputControl joy, int32_t x, int32_t y);
94+
void setJoystick(XInputControl joy, boolean up, boolean down, boolean left, boolean right);
9495

9596
void releaseAll();
9697

0 commit comments

Comments
 (0)