Guys here’s my quick patch with AIR support for game controllers. I do know it is pretty ugly so don’t make fun of it.
package
{
import flash.events.*;
import flash.events.Event;
import flash.events.GameInputEvent;
import flash.ui.GameInput;
import flash.ui.GameInputControl;
import flash.ui.GameInputDevice;
import flash.utils.Dictionary;
import flash.utils.Timer;
import net.flashpunk.utils.Input;
/**
* FlashPunks extension to GameControllers based on http://www.adobe.com/devnet/air/articles/game-controllers-on-air.html
* this static object requires Input to provide public static method overrideInput() provided here for reference
*
* public static function overrideInput(type:String, evnt:*):void
* {
* switch (type) {
* case KeyboardEvent.KEY_DOWN:
* for each (var k:int in keys(evnt)) {
* onKeyDown(new KeyboardEvent(type, true, false, 0, k));
* break;
* }
* break;
* case KeyboardEvent.KEY_UP:
* for each (var k:int in keys(evnt)) {
* onKeyUp(new KeyboardEvent(type, true, false, 0, k));
* break;
* }
* break;
* }
* }
*
* @author rostok
*/
public class InputGamepad
{
private static var gameInput:GameInput;
private static var timer:Timer;
private static var timerStop:Timer;
private static var _device:GameInputDevice;
public static var controls:Vector.<GameInputControl> = new Vector.<GameInputControl>;
public static var definesDown:Dictionary = new Dictionary();
public static var definesUp:Dictionary = new Dictionary();
public function InputGamepad()
{
}
/**
* defines mapping of gamecontroller input to FP's Input.
* If value will reach 0 it will trigger KEY_UP/release event
* @param name a Input string for example "up"
* @param control name of gamepad control for example "AXIS_1"
* @param value value that if equal will trigger KEY_DOWN
*/
public static function define(name:String, control:String, value:Number):void
{
definesDown[control + value] = name;
var x:Number = 0;
definesUp[control + x + value] = name;
}
/**
* this needs to be run in order to init gameinput
*/
public static function initialize():void
{
gameInput = new GameInput();
gameInput.addEventListener(GameInputEvent.DEVICE_ADDED, handleDeviceAttached);
gameInput.addEventListener(GameInputEvent.DEVICE_REMOVED, handleDeviceRemoved);
}
protected static function handleDeviceRemoved(event:GameInputEvent):void
{
trace("Device is removed\n");
}
protected static function handleDeviceAttached(e:GameInputEvent):void
{
trace("Device is added\n");
for(var k:Number=0;k<GameInput.numDevices;k++){
_device = GameInput.getDeviceAt(k);
var _controls:Vector.<String> = new Vector.<String>;
_device.enabled = true;
for (var i:Number = 0; i < _device.numControls; i++) {
var control:GameInputControl = _device.getControlAt(i);
control.addEventListener(Event.CHANGE, onChange);
_controls[i] = control.id;
controls[i] = control;
}
}
}
protected static function onChange(event:Event):void
{
var control:GameInputControl = event.target as GameInputControl;
//trace("The pressed control is " +control.id + " with value " + control.value + " \n");
var d:String;
d = definesDown[control.id + control.value];
if (d)
{
Input.overrideInput(KeyboardEvent.KEY_DOWN, d);
//trace(control.id + control.value, "=>", d);
}
d = definesUp[control.id + control.value + control.minValue];
if (d)
{
Input.overrideInput(KeyboardEvent.KEY_UP, d);
//trace(control.id + control.value, "=>", d);
}
d = definesUp[control.id + control.value + control.maxValue];
if (d)
{
Input.overrideInput(KeyboardEvent.KEY_UP, d);
//trace(control.id + control.value, "=>", d);
}
}
}
}
To use it just add it anywhere in your project.Initialization is pretty straitforward:
InputGamepad.initialize();
InputGamepad.define("up", "AXIS_1", -1);
InputGamepad.define("down", "AXIS_1", 1);
InputGamepad.define("left", "AXIS_0", -1);
InputGamepad.define("right", "AXIS_0", 1);
InputGamepad.define("1", "BUTTON_9", 1);
InputGamepad.define("2", "BUTTON_10", 1);
InputGamepad.define("3", "BUTTON_11", 1);
InputGamepad.define("4", "BUTTON_12", 1);