Skip to content

Commit

Permalink
input/gamepads: added documentation, cleaned up y axis inversion for …
Browse files Browse the repository at this point in the history
…flash on google chrome, added missing y axis inversion case for flash on windows standalone player
  • Loading branch information
larsiusprime committed Jul 14, 2015
1 parent 4c4c89f commit ea289b1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 53 deletions.
56 changes: 18 additions & 38 deletions flixel/input/gamepad/FlxGamepad.hx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import flash.ui.GameInputControl;
import flash.ui.GameInputDevice;
#end

#if flash
import flash.system.Capabilities;
#end

@:allow(flixel.input.gamepad)
class FlxGamepad implements IFlxDestroyable
{
Expand Down Expand Up @@ -100,17 +96,12 @@ class FlxGamepad implements IFlxDestroyable
private var manager:FlxGamepadManager;
private var _deadZone:Float = 0.15;

#if FLX_JOYSTICK_API
private var leftStick:FlxGamepadAnalogStick;
private var rightStick:FlxGamepadAnalogStick;
#elseif FLX_GAMEINPUT_API
#if FLX_GAMEINPUT_API
private var _device:GameInputDevice;
#end

#if flash
private var _isChrome:Bool = false;
#end

public function new(ID:Int, Manager:FlxGamepadManager, ?Model:FlxGamepadModel, ?Attachment:FlxGamepadModelAttachment)
{
id = ID;
Expand All @@ -133,10 +124,6 @@ class FlxGamepad implements IFlxDestroyable
buttonIndex = new FlxGamepadMapping(model, attachment);
model = Model;
detectedModel = Model;

#if flash
_isChrome = (Capabilities.manufacturer == "Google Pepper");
#end
}

public function set_model(Model:FlxGamepadModel):FlxGamepadModel
Expand All @@ -147,24 +134,18 @@ class FlxGamepad implements IFlxDestroyable
motion.isSupported = buttonIndex.supportsMotion();
pointer.isSupported = buttonIndex.supportsPointer();

#if FLX_JOYSTICK_API
leftStick = getRawAnalogStick(FlxGamepadInputID.LEFT_ANALOG_STICK);
rightStick = getRawAnalogStick(FlxGamepadInputID.RIGHT_ANALOG_STICK);
#end

return model;
}

private var t:FlxTimer = null;

public function set_attachment(Attachment:FlxGamepadModelAttachment):FlxGamepadModelAttachment
{
attachment = Attachment;
buttonIndex.attachment = Attachment;
#if FLX_JOYSTICK_API
leftStick = getRawAnalogStick(FlxGamepadInputID.LEFT_ANALOG_STICK);
rightStick = getRawAnalogStick(FlxGamepadInputID.RIGHT_ANALOG_STICK);
#end
return attachment;
}

Expand Down Expand Up @@ -225,6 +206,7 @@ class FlxGamepad implements IFlxDestroyable
for (i in 0..._device.numControls)
{
control = _device.getControlAt(i);

var value = control.value;
value = Math.abs(value); //quick absolute value for analog sticks
button = getButton(i);
Expand Down Expand Up @@ -596,6 +578,7 @@ class FlxGamepad implements IFlxDestroyable
{
return buttonIndex.checkForFakeAxis(ID);
}
#end

public function isAxisForMotion(AxisIndex:Int):Bool
{
Expand All @@ -621,7 +604,6 @@ class FlxGamepad implements IFlxDestroyable
if (rightStick != null && AxisIndex == rightStick.x || AxisIndex == rightStick.y) return rightStick;
return null;
}
#end

/**
* Given a ButtonID for an analog stick, gets the value of its x axis
Expand Down Expand Up @@ -655,17 +637,7 @@ class FlxGamepad implements IFlxDestroyable
*/
public function getYAxisRaw(Stick:FlxGamepadAnalogStick):Float
{
var axisValue = getAnalogYAxisValue(Stick);

// the y axis is inverted on the Xbox gamepad in flash for some reason - but not in Chrome!
#if flash
if (model == XBox360 && !_isChrome)
{
axisValue = -axisValue;
}
#end

return axisValue;
return getAnalogYAxisValue(Stick);
}

/**
Expand Down Expand Up @@ -738,6 +710,11 @@ class FlxGamepad implements IFlxDestroyable
axisValue = axis[AxisID];
#end

if (isAxisForAnalogStick(AxisID))
{
axisValue *= getFlipAxis(AxisID);
}

return axisValue;
}

Expand Down Expand Up @@ -822,16 +799,19 @@ class FlxGamepadAnalogStick
public var x(default, null):Int;
public var y(default, null):Int;

//these values let the analog stick to send digital inputs to, say, the dpad
/**a raw button input ID, for sending a digital event for "up" alongside the analog event**/
public var rawUp(default, null):Int = -1;
/**a raw button input ID, for sending a digital event for "down" alongside the analog event**/
public var rawDown(default, null):Int = -1;
/**a raw button input ID, for sending a digital event for "left" alongside the analog event**/
public var rawLeft(default, null):Int = -1;
/**a raw button input ID, for sending a digital event for "right" alongside the analog event**/
public var rawRight(default, null):Int = -1;

//the value the dpad must be above before digital inputs are sent
/**the absolute value the dpad must be greater than before digital inputs are sent**/
public var digitalThreshold(default, null):Float = 0.5;

//when analog inputs are received, how to process them digitally
/**when analog inputs are received, how to process them digitally**/
public var mode(default, null):FlxAnalogToDigitalMode = OnlyAnalog;

public function new(x:Int, y:Int, ?settings:FlxGamepadAnalogStickSettings)
Expand Down Expand Up @@ -874,9 +854,9 @@ typedef FlxGamepadAnalogStickSettings = {

enum FlxAnalogToDigitalMode
{
Both;
OnlyDigital;
OnlyAnalog;
Both; //Send both digital and analog events

This comment has been minimized.

Copy link
@Gama11

Gama11 Jul 14, 2015

Member

If you want to add docs for these, do it like on FlxGamepadDeadZoneMode with javadoc comments (for completion and api docs, these // comments are usually ignored there).

OnlyDigital; //Send only digital events
OnlyAnalog; //Send only analog events
}

enum FlxGamepadModel
Expand Down
49 changes: 35 additions & 14 deletions flixel/input/gamepad/FlxGamepadMapping.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import flixel.input.gamepad.id.WiiRemoteID;
import flixel.input.gamepad.id.XBox360ID;
import flixel.input.gamepad.id.XInputID;

#if flash
import openfl.system.Capabilities;
#end

/**
* ...
* @author larsiusprime
Expand All @@ -24,11 +28,21 @@ class FlxGamepadMapping
@:allow(flixel.input.gamepad.FlxGamepad)
public var attachment(default, null):FlxGamepadModelAttachment = None;

private var _manufacturer:Manufacturer = Unknown;

public function new(Model:FlxGamepadModel, attachment:FlxGamepadModelAttachment=null)
{
model = Model;
#if flash
_manufacturer = switch(Capabilities.manufacturer)
{
case "Google Pepper": GooglePepper;
case "Adobe Windows": AdobeWindows;
default: Unknown;
}
#end
}

/**
* Given a ID, return the raw hardware code
* @param ID the "universal" ID
Expand Down Expand Up @@ -161,7 +175,7 @@ class FlxGamepadMapping
case Logitech: LogitechID.getFlipAxis(AxisID);
case OUYA: OUYAID.getFlipAxis(AxisID);
case PS4: PS4ID.getFlipAxis(AxisID);
case XBox360: XBox360ID.getFlipAxis(AxisID);
case XBox360: XBox360ID.getFlipAxis(AxisID, _manufacturer);
case XInput: XInputID.getFlipAxis(AxisID);
case MayflashWiiRemote: MayflashWiiRemoteID.getFlipAxis(AxisID, attachment);
case WiiRemote: WiiRemoteID.getFlipAxis(AxisID, attachment);
Expand Down Expand Up @@ -189,6 +203,18 @@ class FlxGamepadMapping
}
}

public function checkForFakeAxis(ID:FlxGamepadInputID):Int
{
return switch (model)
{
case MayflashWiiRemote: MayflashWiiRemoteID.checkForFakeAxis(ID, attachment);
case WiiRemote: WiiRemoteID.checkForFakeAxis(ID, attachment);
default: -1;
}
}

#end

public function isAxisForMotion(ID:FlxGamepadInputID):Bool
{
return switch (model)
Expand All @@ -204,18 +230,6 @@ class FlxGamepadMapping
}
}

public function checkForFakeAxis(ID:FlxGamepadInputID):Int
{
return switch (model)
{
case MayflashWiiRemote: MayflashWiiRemoteID.checkForFakeAxis(ID, attachment);
case WiiRemote: WiiRemoteID.checkForFakeAxis(ID, attachment);
default: -1;
}
}

#end

public function supportsMotion():Bool
{
return switch (model)
Expand Down Expand Up @@ -767,4 +781,11 @@ class FlxGamepadMapping
default: NONE;
}
}
}

enum Manufacturer

This comment has been minimized.

Copy link
@Gama11

Gama11 Jul 14, 2015

Member

This is internal API right? Should be private enum.

This comment has been minimized.

Copy link
@larsiusprime

larsiusprime Jul 14, 2015

Author Member

how does XBox360ID.hx get access to it then?

This comment has been minimized.

Copy link
@Gama11

Gama11 Jul 14, 2015

Member

@:access(flixel.input.gamepad.id)?

This comment has been minimized.

Copy link
@larsiusprime

larsiusprime Jul 14, 2015

Author Member

doesn't work. "you can't import a private type"

This comment has been minimized.

Copy link
@Gama11

Gama11 Jul 15, 2015

Member

Hm... nevermind then.

{
GooglePepper;
AdobeWindows;
Unknown;
}
18 changes: 17 additions & 1 deletion flixel/input/gamepad/id/XBox360ID.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flixel.input.gamepad.id;

import flixel.input.gamepad.FlxGamepad;
import flixel.input.gamepad.FlxGamepadMapping.Manufacturer;

/**
* IDs for Xbox 360 controllers
Expand All @@ -9,8 +10,23 @@ class XBox360ID
{
public static inline var SUPPORTS_MOTION = false;
public static inline var SUPPORTS_POINTER = false;

#if !flash
public static inline function getFlipAxis(AxisID:Int, manufacturer:Manufacturer):Int { return 1; }
#else

This comment has been minimized.

Copy link
@Gama11

Gama11 Jul 14, 2015

Member

I'd prefer if you put the conditionals inside of the function body so we don't have two function headers (looks weird in FD's outline panel and also causes some other issues I don't remember, probably completion related. Either way, FD doesn't like it).

public static function getFlipAxis(AxisID:Int, manufacturer:Manufacturer):Int
{
if (manufacturer == GooglePepper || manufacturer == AdobeWindows)
{
if (AxisID == LEFT_ANALOG_STICK.y || AxisID == RIGHT_ANALOG_STICK.y)
{
return -1;
}
}
return 1;
}
#end

public static inline function getFlipAxis(AxisID:Int):Int { return 1; }
public static function isAxisForMotion(ID:FlxGamepadInputID):Bool { return false; }

#if flash
Expand Down

0 comments on commit ea289b1

Please sign in to comment.