Skip to content

Commit

Permalink
FlxUICursor: prevent/remove references to disconnected gamepads (#265)
Browse files Browse the repository at this point in the history
* prevent crash when disconnecting gamepads

* avoid setter calls

* remove destroyed gamepad input checkers
  • Loading branch information
Geokureli committed Jan 2, 2024
1 parent 47dc1d6 commit cb642a8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 59 deletions.
10 changes: 0 additions & 10 deletions flixel/addons/ui/FlxMultiGamepad.hx
Expand Up @@ -28,36 +28,26 @@ class FlxMultiGamepad extends FlxBaseMultiInput

private override function checkJustPressed():Bool
{
if (gamepad == null)
return false;
return gamepad.checkStatus(input, JUST_PRESSED);
}

private override function checkJustReleased():Bool
{
if (gamepad == null)
return false;
return gamepad.checkStatus(input, JUST_RELEASED);
}

private override function checkPressed():Bool
{
if (gamepad == null)
return false;
return gamepad.checkStatus(input, PRESSED);
}

private override function checkCombos(value:Bool):Bool
{
if (gamepad == null)
return false;
return gamepad.anyPressed(combos) == value;
}

private override function checkForbiddens(value:Bool):Bool
{
if (gamepad == null)
return false;
return gamepad.anyPressed(forbiddens) == value;
}
}
104 changes: 55 additions & 49 deletions flixel/addons/ui/FlxUICursor.hx
Expand Up @@ -318,13 +318,12 @@ class FlxUICursor extends FlxUISprite
public override function update(elapsed:Float):Void
{
#if FLX_GAMEPAD
if (gamepad == null)
if (gamepad == null || !gamepad.connected)
{
var g = getGamepad(false);
if (g != null)
{
final g = getGamepad(false);
// avoid calling set_gamepad unnecessarily
if (gamepad != g)
gamepad = g;
}
}
#end

Expand Down Expand Up @@ -607,12 +606,14 @@ class FlxUICursor extends FlxUISprite
_defaultCode = code;
_clearKeys();
_newKeys();

if (code.has(KEYS_TAB))
{
_addToKeys(keysRight, new FlxMultiKey(TAB, null, [SHIFT])); // Tab, (but NOT Shift+Tab!)
_addToKeys(keysLeft, new FlxMultiKey(TAB, [SHIFT])); // Shift+Tab
_addToKeys(keysClick, new FlxMultiKey(ENTER));
}

if (code.has(KEYS_ARROWS))
{
_addToKeys(keysRight, new FlxMultiKey(RIGHT));
Expand All @@ -621,6 +622,7 @@ class FlxUICursor extends FlxUISprite
_addToKeys(keysUp, new FlxMultiKey(UP));
_addToKeys(keysClick, new FlxMultiKey(ENTER));
}

if (code.has(KEYS_WASD))
{
_addToKeys(keysRight, new FlxMultiKey(D));
Expand All @@ -629,6 +631,7 @@ class FlxUICursor extends FlxUISprite
_addToKeys(keysUp, new FlxMultiKey(W));
_addToKeys(keysClick, new FlxMultiKey(ENTER));
}

if (code.has(KEYS_NUMPAD))
{
_addToKeys(keysRight, new FlxMultiKey(NUMPADSIX));
Expand All @@ -637,42 +640,46 @@ class FlxUICursor extends FlxUISprite
_addToKeys(keysUp, new FlxMultiKey(NUMPADEIGHT));
_addToKeys(keysClick, new FlxMultiKey(ENTER));
}

#if FLX_GAMEPAD
if (gamepad == null)
{
if (_gamepad == null || !_gamepad.connected)
_gamepad = getGamepad(); // set _gamepad to avoid a stack overflow loop
}

if (code.has(GAMEPAD_DPAD))
{
_addToKeys(keysLeft, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_LEFT));
_addToKeys(keysRight, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_RIGHT));
_addToKeys(keysDown, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_DOWN));
_addToKeys(keysUp, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_UP));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
}
if (code.has(GAMEPAD_SHOULDER_BUTTONS))
{
_addToKeys(keysLeft, new FlxMultiGamepad(gamepad, FlxGamepadInputID.LEFT_SHOULDER));
_addToKeys(keysRight, new FlxMultiGamepad(gamepad, FlxGamepadInputID.RIGHT_SHOULDER));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
}
if (code.has(GAMEPAD_LEFT_STICK))

if (_gamepad != null && _gamepad.connected)
{
_addToKeys(keysLeft, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: X, positive: false}));
_addToKeys(keysRight, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: X, positive: true}));
_addToKeys(keysUp, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: Y, positive: false}));
_addToKeys(keysDown, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: Y, positive: true}));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
}
if (code.has(GAMEPAD_RIGHT_STICK))
{
_addToKeys(keysLeft, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: X, positive: false}));
_addToKeys(keysRight, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: X, positive: true}));
_addToKeys(keysUp, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: Y, positive: false}));
_addToKeys(keysDown, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: Y, positive: true}));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
if (code.has(GAMEPAD_DPAD))
{
_addToKeys(keysLeft, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_LEFT));
_addToKeys(keysRight, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_RIGHT));
_addToKeys(keysDown, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_DOWN));
_addToKeys(keysUp, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_UP));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
}

if (code.has(GAMEPAD_SHOULDER_BUTTONS))
{
_addToKeys(keysLeft, new FlxMultiGamepad(gamepad, FlxGamepadInputID.LEFT_SHOULDER));
_addToKeys(keysRight, new FlxMultiGamepad(gamepad, FlxGamepadInputID.RIGHT_SHOULDER));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
}

if (code.has(GAMEPAD_LEFT_STICK))
{
_addToKeys(keysLeft, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: X, positive: false}));
_addToKeys(keysRight, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: X, positive: true}));
_addToKeys(keysUp, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: Y, positive: false}));
_addToKeys(keysDown, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: Y, positive: true}));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
}

if (code.has(GAMEPAD_RIGHT_STICK))
{
_addToKeys(keysLeft, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: X, positive: false}));
_addToKeys(keysRight, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: X, positive: true}));
_addToKeys(keysUp, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: Y, positive: false}));
_addToKeys(keysDown, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: Y, positive: true}));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
}
}
#end
}
Expand Down Expand Up @@ -700,25 +707,24 @@ class FlxUICursor extends FlxUISprite
#if FLX_GAMEPAD
private function getGamepad(exhaustive:Bool = true):FlxGamepad
{
var gamepad = switch (gamepadAutoConnect)
final gamepad = switch (gamepadAutoConnect)
{
case Never: null;
case FirstActive: FlxG.gamepads.getFirstActiveGamepad();
case LastActive: FlxG.gamepads.lastActive;
case GamepadID(i): FlxG.gamepads.getByID(i);
}
if (gamepad == null && exhaustive)

if ((gamepad != null && gamepad.connected) || !exhaustive)
return gamepad;

for (i in 0...FlxG.gamepads.numActiveGamepads)
{
for (i in 0...FlxG.gamepads.numActiveGamepads)
{
gamepad = FlxG.gamepads.getByID(i);
if (gamepad != null)
{
return gamepad;
}
}
final gamepad = FlxG.gamepads.getByID(i);
if (gamepad != null && gamepad.connected)
return gamepad;
}
return gamepad;
return null;
}
#end

Expand Down

0 comments on commit cb642a8

Please sign in to comment.