Skip to content

Commit 60fe179

Browse files
authored
Merge pull request #18 from dmadison/joysticks
Joystick Single Axis Functions
2 parents 7503b14 + a14162c commit 60fe179

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ setButton KEYWORD2
3333
setDpad KEYWORD2
3434
setTrigger KEYWORD2
3535
setJoystick KEYWORD2
36+
setJoystickX KEYWORD2
37+
setJoystickY KEYWORD2
3638

3739
releaseAll KEYWORD2
3840

src/XInput.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,36 @@ 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) {
281+
const XInputMap_Joystick * joyData = getJoyFromEnum(joy);
282+
if (joyData == nullptr) return; // Not a joystick
283+
284+
x = rescaleInput(x, *getRangeFromEnum(joy), XInputMap_Joystick::range);
285+
286+
if (getJoystickX(joy) == x) return; // Axis hasn't changed
287+
288+
tx[joyData->x_low] = lowByte(x);
289+
tx[joyData->x_high] = highByte(x);
290+
291+
newData = true;
292+
autosend();
293+
}
294+
295+
void XInputController::setJoystickY(XInputControl joy, int32_t y) {
296+
const XInputMap_Joystick * joyData = getJoyFromEnum(joy);
297+
if (joyData == nullptr) return; // Not a joystick
298+
299+
y = rescaleInput(y, *getRangeFromEnum(joy), XInputMap_Joystick::range);
300+
301+
if (getJoystickY(joy) == y) return; // Axis hasn't changed
302+
303+
tx[joyData->y_low] = lowByte(y);
304+
tx[joyData->y_high] = highByte(y);
305+
306+
newData = true;
307+
autosend();
308+
}
309+
280310
void XInputController::setJoystick(XInputControl joy, boolean up, boolean down, boolean left, boolean right, boolean useSOCD) {
281311
const XInputMap_Joystick * joyData = getJoyFromEnum(joy);
282312
if (joyData == nullptr) return; // Not a joystick
@@ -296,10 +326,12 @@ void XInputController::setJoystick(XInputControl joy, boolean up, boolean down,
296326
// output from '0' if the per-axis inputs are different, in order to
297327
// avoid the '-1' result from adding the int16 extremes
298328
if (left != right) {
299-
x = (right * range.max) - (left * range.min);
329+
if (left == true) { x = range.min; }
330+
else if (right == true) { x = range.max; }
300331
}
301332
if (up != down) {
302-
y = (up * range.max) - (down * range.min);
333+
if (up == true) { y = range.max; }
334+
else if (down == true) { y = range.min; }
303335
}
304336

305337
setJoystickDirect(joy, x, y);

src/XInput.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class XInputController {
9292

9393
void setJoystick(XInputControl joy, int32_t x, int32_t y);
9494
void setJoystick(XInputControl joy, boolean up, boolean down, boolean left, boolean right, boolean useSOCD = true);
95+
void setJoystickX(XInputControl joy, int32_t x);
96+
void setJoystickY(XInputControl joy, int32_t y);
9597

9698
void releaseAll();
9799

0 commit comments

Comments
 (0)