Skip to content

Commit

Permalink
Merge pull request #1752 from larsiusprime/gamepad_analog_dpad__
Browse files Browse the repository at this point in the history
gamepad analog dpads (squashed)
  • Loading branch information
Gama11 committed Feb 27, 2016
2 parents 0541162 + 7d17c14 commit 801ee72
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 106 deletions.
61 changes: 58 additions & 3 deletions flixel/input/gamepad/FlxGamepad.hx
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,17 @@ class FlxGamepad implements IFlxDestroyable
control = _device.getControlAt(i);

//quick absolute value for analog sticks
var value = Math.abs(control.value);
button = getButton(i);

if (isAxisForAnalogStick(i))
{
handleAxisMove(i, control.value, button.value);
}

button.value = control.value;

var value = Math.abs(control.value);

if (value < deadZone)
{
button.release();
Expand Down Expand Up @@ -264,7 +272,7 @@ class FlxGamepad implements IFlxDestroyable
hat = FlxDestroyUtil.put(hat);
ball = FlxDestroyUtil.put(ball);

hat = null;
hat = null;
ball = null;
#end
}
Expand Down Expand Up @@ -605,7 +613,7 @@ class FlxGamepad implements IFlxDestroyable
{
return getAnalogYAxisValue(Stick);
}

/**
* Whether any buttons have the specified input state.
*/
Expand Down Expand Up @@ -730,6 +738,53 @@ class FlxGamepad implements IFlxDestroyable
return 0;
}

private function handleAxisMove(axis:Int, newValue:Float, oldValue:Float)
{
newValue = applyAxisFlip(newValue, axis);
oldValue = applyAxisFlip(oldValue, axis);

//check to see if we should send digital inputs as well as analog
var stick:FlxGamepadAnalogStick = getAnalogStickByAxis(axis);
if (stick.mode == ONLY_DIGITAL || stick.mode == BOTH)
{
handleAxisMoveSub(stick, axis, newValue, oldValue, 1.0);
handleAxisMoveSub(stick, axis, newValue, oldValue, -1.0);

if (stick.mode == ONLY_DIGITAL)
{
//still haven't figured out how to suppress the analog inputs properly. Oh well.
}
}
}

private function handleAxisMoveSub(stick:FlxGamepadAnalogStick, axis:Int, value:Float, oldValue:Float, sign:Float=1.0)
{
var digitalButton = -1;

if (axis == stick.x)
{
digitalButton = (sign < 0) ? stick.rawLeft : stick.rawRight;
}
else if (axis == stick.y)
{
digitalButton = (sign < 0) ? stick.rawUp : stick.rawDown;
}

var threshold = stick.digitalThreshold;
var valueSign = value * sign;
var oldValueSign = oldValue * sign;

if (valueSign > threshold && oldValueSign <= threshold)
{
var btn = getButton(digitalButton);
if (btn != null) btn.press();
}
else if (valueSign <= threshold && oldValueSign > threshold)
{
var btn = getButton(digitalButton);
if (btn != null) btn.release();
}
}
private function createMappingForModel(model:FlxGamepadModel):FlxGamepadMapping
{
return switch (model)
Expand Down
6 changes: 3 additions & 3 deletions flixel/input/gamepad/FlxGamepadAnalogStick.hx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FlxGamepadAnalogStick
/**
* when analog inputs are received, how to process them digitally
*/
public var mode(default, null):FlxAnalogToDigitalMode = ONLY_ANALOG;
public var mode(default, null):FlxAnalogToDigitalMode = BOTH;

public function new(x:Int, y:Int, ?settings:FlxGamepadAnalogStickSettings)
{
Expand All @@ -42,12 +42,12 @@ class FlxGamepadAnalogStick
if (settings == null)
return;

mode = (settings.mode != null) ? settings.mode : ONLY_ANALOG;
mode = (settings.mode != null) ? settings.mode : BOTH;
rawUp = (settings.up != null) ? settings.up : -1;
rawDown = (settings.down != null) ? settings.down : -1;
rawLeft = (settings.left != null) ? settings.left : -1;
rawRight = (settings.right != null) ? settings.right : -1;
digitalThreshold = (settings.threshold != null) ? settings.threshold : -1;
digitalThreshold = (settings.threshold != null) ? settings.threshold : 0.5;
}

public function toString():String
Expand Down
5 changes: 5 additions & 0 deletions flixel/input/gamepad/FlxGamepadButton.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import flixel.input.FlxInput;

class FlxGamepadButton extends FlxInput<Int>
{
/**
* Optional analog value, so we can check when the value has changed from the last frame
*/
public var value:Float = 0;

#if flash
private var _pressed:Bool = false;

Expand Down
18 changes: 18 additions & 0 deletions flixel/input/gamepad/FlxGamepadInputID.hx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ abstract FlxGamepadInputID(Int) from Int to Int
/**an extra digital button that doesn't fit cleanly into the universal template**/
var EXTRA_3 = 33;

/**left analog stick as a dpad, pushed up**/
var LEFT_STICK_DIGITAL_UP = 34;
/**left analog stick as a dpad, pushed right**/
var LEFT_STICK_DIGITAL_RIGHT = 35;
/**left analog stick as a dpad, pushed down**/
var LEFT_STICK_DIGITAL_DOWN = 36;
/**left analog stick as a dpad, pushed left**/
var LEFT_STICK_DIGITAL_LEFT = 37;

/**left analog stick as a dpad, pushed up**/
var RIGHT_STICK_DIGITAL_UP = 38;
/**left analog stick as a dpad, pushed right**/
var RIGHT_STICK_DIGITAL_RIGHT = 39;
/**left analog stick as a dpad, pushed down**/
var RIGHT_STICK_DIGITAL_DOWN = 40;
/**left analog stick as a dpad, pushed left**/
var RIGHT_STICK_DIGITAL_LEFT = 41;

@:from
public static inline function fromString(s:String)
{
Expand Down
46 changes: 2 additions & 44 deletions flixel/input/gamepad/FlxGamepadManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ class FlxGamepadManager implements IFlxInputManager
{
var isForStick = gamepad.isAxisForAnalogStick(i);
var isForMotion = gamepad.mapping.isAxisForMotion(i);

if (!isForStick && !isForMotion)
{
// in legacy this returns a (-1, 1) range, but in flash/next it
Expand All @@ -461,50 +462,7 @@ class FlxGamepadManager implements IFlxInputManager
}
else if (isForStick)
{
//check to see if we should send digital inputs as well as analog
var stick:FlxGamepadAnalogStick = gamepad.getAnalogStickByAxis(i);
if (stick.mode == ONLY_DIGITAL || stick.mode == BOTH)
{
var newVal = newAxis[i];
var oldVal = oldAxis[i];

var neg = stick.digitalThreshold * -1;
var pos = stick.digitalThreshold;
var digitalButton = -1;

//pressed/released for digital LEFT/UP
if (newVal < neg && oldVal >= neg)
{
if (i == stick.x) digitalButton = stick.rawLeft;
else if (i == stick.y) digitalButton = stick.rawUp;
handleButtonDown(device, digitalButton);
}
else if (newVal >= neg && oldVal < neg)
{
if (i == stick.x) digitalButton = stick.rawLeft;
else if (i == stick.y) digitalButton = stick.rawUp;
handleButtonUp(device, digitalButton);
}

//pressed/released for digital RIGHT/DOWN
if (newVal > pos && oldVal <= pos)
{
if (i == stick.x) digitalButton = stick.rawRight;
else if (i == stick.y) digitalButton = stick.rawDown;
handleButtonDown(device, digitalButton);
}
else if (newVal <= pos && oldVal > pos)
{
if (i == stick.x) digitalButton = stick.rawRight;
else if (i == stick.y) digitalButton = stick.rawDown;
handleButtonUp(device, digitalButton);
}

if (stick.mode == ONLY_DIGITAL)
{
//still haven't figured out how to suppress the analog inputs properly. Oh well.
}
}
gamepad.handleAxisMove(i, newAxis[i], (i >= 0 && i < oldAxis.length) ? oldAxis[i] : 0);
}
}

Expand Down
9 changes: 4 additions & 5 deletions flixel/input/gamepad/id/LogitechID.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class LogitechID
//TODO: Someone needs to look this up and define it! (NOTE: not all logitech controllers have this)
public static inline var LOGITECH:Int = -1;

public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1);
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3);

#else // native and html5
public static inline var ONE:Int = 0;
public static inline var TWO:Int = 1;
Expand All @@ -55,7 +52,9 @@ class LogitechID
//TODO: Someone needs to look this up and define it! (NOTE: not all logitech controllers have this)
public static inline var LOGITECH:Int = -5;

public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1);
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3);
#end

public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1, {up:24, down:25, left:26, right:27});
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3, {up:28, down:29, left:30, right:31});

}
4 changes: 2 additions & 2 deletions flixel/input/gamepad/id/MFiID.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class MFiID
public static inline var DPAD_LEFT:Int = 19;
public static inline var DPAD_RIGHT:Int = 20;

public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1);
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3);
public static var LEFT_ANALOG_STICK (default, null) = new FlxGamepadAnalogStick(0, 1, {up:21, down:22, left:23, right:24});
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3, {up:25, down:26, left:27, right:28});

public static inline var LEFT_TRIGGER:Int = 4;
public static inline var RIGHT_TRIGGER:Int = 5;
Expand Down
20 changes: 10 additions & 10 deletions flixel/input/gamepad/id/MayflashWiiRemoteID.hx
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ class MayflashWiiRemoteID

// Yes, the WiiRemote DPAD is treated as ANALOG for some reason...so we have to pass in some "fake" ID's to get simulated digital inputs
public static var REMOTE_DPAD(default, null) = new FlxGamepadAnalogStick(0, 1, {
up: REMOTE_DPAD_UP,
down: REMOTE_DPAD_DOWN,
left: REMOTE_DPAD_LEFT,
up: REMOTE_DPAD_UP,
down: REMOTE_DPAD_DOWN,
left: REMOTE_DPAD_LEFT,
right: REMOTE_DPAD_RIGHT,
threshold: 0.5,
mode: ONLY_DIGITAL
});

public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1); //the nunchuk only has the "left" analog stick
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3); //the classic controller has both the "left" and "right" analog sticks
public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1, {up:26, down:27, left:28, right:29}); //the nunchuk only has the "left" analog stick
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3, {up:30, down:31, left:32, right:33}); //the classic controller has both the "left" and "right" analog sticks

// these aren't real axes, they're simulated when the right digital buttons are pushed
public static inline var LEFT_TRIGGER_FAKE:Int = 4;
Expand Down Expand Up @@ -166,16 +166,16 @@ class MayflashWiiRemoteID

// Yes, the WiiRemote DPAD is treated as ANALOG for some reason...so we have to pass in some "fake" ID's to get simulated digital inputs
public static var REMOTE_DPAD(default, null) = new FlxGamepadAnalogStick(0, 1, {
up: REMOTE_DPAD_UP,
down: REMOTE_DPAD_DOWN,
left: REMOTE_DPAD_LEFT,
up: REMOTE_DPAD_UP,
down: REMOTE_DPAD_DOWN,
left: REMOTE_DPAD_LEFT,
right: REMOTE_DPAD_RIGHT,
threshold: 0.5,
mode: ONLY_DIGITAL
});

public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1); //the nunchuk only has the "left" analog stick
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3); //the classic controller has both the "left" and "right" analog sticks
public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1, {up:26, down:27, left:28, right:29}); //the nunchuk only has the "left" analog stick
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3, {up:26, down:27, left:28, right:29}); //the classic controller has both the "left" and "right" analog sticks

// these aren't real axes, they're simulated when the right digital buttons are pushed
public static inline var LEFT_TRIGGER_FAKE:Int = 4;
Expand Down
4 changes: 2 additions & 2 deletions flixel/input/gamepad/id/OUYAID.hx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class OUYAID
public static inline var DPAD_UP:Int = 16;

// If TRIGGER axis returns value > 0 then LT is being pressed, and if it's < 0 then RT is being pressed
public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1);
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(11, 14);
public static var LEFT_ANALOG_STICK (default, null) = new FlxGamepadAnalogStick( 0, 1, {up:23, down:24, left:25, right:26});
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(11, 14, {up:27, down:28, left:29, right:30});

public static inline var LEFT_TRIGGER_ANALOG:Int = 17;
public static inline var RIGHT_TRIGGER_ANALOG:Int = 18;
Expand Down
12 changes: 6 additions & 6 deletions flixel/input/gamepad/id/PS4ID.hx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class PS4ID
public static inline var PS:Int = 22;
public static inline var TOUCHPAD_CLICK:Int = 23;

public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0 , 1);
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 5);
public static var LEFT_ANALOG_STICK (default, null) = new FlxGamepadAnalogStick(0, 1, {up:24, down:25, left:26, right:27});
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 5, {up:28, down:29, left:30, right:31});

public static inline var DPAD_UP:Int = 6;
public static inline var DPAD_DOWN:Int = 7;
Expand All @@ -63,8 +63,8 @@ class PS4ID

public static inline var TOUCHPAD_CLICK:Int = 21;

public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1);
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3);
public static var LEFT_ANALOG_STICK (default, null) = new FlxGamepadAnalogStick(0, 1, {up:22, down:23, left:24, right:25});
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3, {up:26, down:27, left:28, right:29});

public static inline var L2:Int = 4;
public static inline var R2:Int = 5;
Expand Down Expand Up @@ -97,8 +97,8 @@ class PS4ID
public static inline var L2:Int = 3;
public static inline var R2:Int = 4;

public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1);
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 5);
public static var LEFT_ANALOG_STICK (default, null) = new FlxGamepadAnalogStick(0, 1, {up:27, down:28, left:29, right:30});
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 5, {up:31, down:32, left:33, right:34});

//"fake" IDs, we manually watch for hat axis changes and then send events using these otherwise unused joystick button codes
public static inline var DPAD_LEFT:Int = 15;
Expand Down
4 changes: 2 additions & 2 deletions flixel/input/gamepad/id/PSVitaID.hx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ class PSVitaID
public static inline var DPAD_LEFT:Int = 19;
public static inline var DPAD_RIGHT:Int = 20;

public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1);
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3);
public static var LEFT_ANALOG_STICK (default, null) = new FlxGamepadAnalogStick(0, 1, {up:21, down:22, left:23, right:24});
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3, {up:21, down:22, left:23, right:24});
}
22 changes: 11 additions & 11 deletions flixel/input/gamepad/id/WiiRemoteID.hx
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ class WiiRemoteID
// Yes, the WiiRemote DPAD is treated as ANALOG for some reason...
// so we have to pass in some "fake" ID's to get simulated digital inputs
public static var REMOTE_DPAD(default, null) = new FlxGamepadAnalogStick(0, 1, {
up: REMOTE_DPAD_UP,
down: REMOTE_DPAD_DOWN,
left: REMOTE_DPAD_LEFT,
up: REMOTE_DPAD_UP,
down: REMOTE_DPAD_DOWN,
left: REMOTE_DPAD_LEFT,
right: REMOTE_DPAD_RIGHT,
threshold: 0.5,
mode: ONLY_DIGITAL
});

// the nunchuk only has the "left" analog stick
public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1);
public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1, {up:32,down:33,left:34,right:35});
// the classic controller has both the "left" and "right" analog sticks
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3);
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3, {up:36,down:37,left:38,right:39});

// these aren't real axes, they're simulated when the right digital buttons are pushed
public static inline var LEFT_TRIGGER_FAKE:Int = 4;
Expand Down Expand Up @@ -167,22 +167,22 @@ class WiiRemoteID

// Yes, the WiiRemote DPAD is treated as ANALOG for some reason...so we have to pass in some "fake" ID's to get simulated digital inputs
public static var REMOTE_DPAD(default, null) = new FlxGamepadAnalogStick(0, 1, {
up: REMOTE_DPAD_UP,
down: REMOTE_DPAD_DOWN,
left: REMOTE_DPAD_LEFT,
up: REMOTE_DPAD_UP,
down: REMOTE_DPAD_DOWN,
left: REMOTE_DPAD_LEFT,
right: REMOTE_DPAD_RIGHT,
threshold: 0.5,
mode: ONLY_DIGITAL
mode: ONLY_DIGITAL
});

/**
* the nunchuk only has the "left" analog stick
*/
public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1);
public static var LEFT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(0, 1, {up:28,down:29,left:30,right:31});
/**
* the classic controller has both the "left" and "right" analog sticks
*/
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3);
public static var RIGHT_ANALOG_STICK(default, null) = new FlxGamepadAnalogStick(2, 3, {up:32,down:33,left:34,right:35});

// these aren't real axes, they're simulated when the right digital buttons are pushed
public static inline var LEFT_TRIGGER_FAKE:Int = 4;
Expand Down
Loading

0 comments on commit 801ee72

Please sign in to comment.