flixel.system.input.gamepad.OUYAButtonID
package;
import flash.display.Bitmap;import flash.display.Sprite;import flash.events.Event;import flash.events.KeyboardEvent;import flash.ui.Keyboard;import openfl.Assets;import openfl.events.JoystickEvent;
class Main extends Sprite { private var Logo:Sprite; private var movingDown:Bool; private var movingLeft:Bool; private var movingRight:Bool; private var movingUp:Bool; public function new () { super (); Logo = new Sprite (); Logo.addChild (new Bitmap (Assets.getBitmapData ("assets/openfl.png"))); Logo.x = 100; Logo.y = 100; Logo.buttonMode = true; addChild (Logo); stage.addEventListener (KeyboardEvent.KEY_DOWN, stage_onKeyDown); stage.addEventListener (JoystickEvent.AXIS_MOVE, stage_onJoystickAxisMove); stage.addEventListener (KeyboardEvent.KEY_UP, stage_onKeyUp); stage.addEventListener (Event.ENTER_FRAME, this_onEnterFrame); } // Event Handlers private function stage_onKeyDown (event:KeyboardEvent):Void { switch (event.keyCode) { case Keyboard.DOWN: movingDown = true; case Keyboard.LEFT: movingLeft = true; case Keyboard.RIGHT: movingRight = true; case Keyboard.UP: movingUp = true; } } private function stage_onJoystickAxisMove(e:JoystickEvent):Void { Logo.x += e.axis[0] * 5; Logo.y += e.axis[1] * 5; } private function stage_onKeyUp (event:KeyboardEvent):Void { switch (event.keyCode) { case Keyboard.DOWN: movingDown = false; case Keyboard.LEFT: movingLeft = false; case Keyboard.RIGHT: movingRight = false; case Keyboard.UP: movingUp = false; } } private function this_onEnterFrame (event:Event):Void { if (movingDown) { Logo.y += 5; } if (movingLeft) { Logo.x -= 5; } if (movingRight) { Logo.x += 5; } if (movingUp) { Logo.y -= 5; } } }
var g:FlxGamepad = FlxG.gamepads.get(3);
if (g.anyInput()) {...}
if (g.pressed(OUYAButtonID.O)) {...}
if ((g.getAxis(OUYAButtonID.RIGHT_ANALOGUE_X)) > 0.3) {...}
I had only gotten so far as getting a thumbstick to respond, but yes it works without the openfl-ouya library, which like James mentioned, causes current openfl projects to crash in ouya.
Here is a slightly modified Xbox controller demo https://github.com/Beeblerox/OUYA_gamepad_test you'll need to use dev version of HaxeFlixel to compile it (i've added couple functions for gamepad detection and removed openfl_ouya specific code). Don't look at the graphic (there is XBox controller image), it's just for visualisation
I hope it will work on OUYA (i don't have such device, so i can't test it). Please tell me how it works on a real device and sorry for such delay between my answers
The button id's were just educated stabs in the dark. Most of my time was spent trying to figure out why I couldn't get the trigger axes working; then I discovered FlxGamepad was creating an array for them that was only 1x4. Changed the array to 6 to account for the 2 more axes on the triggers, and re-used the existing xbox example assets to show them.
Changes to FlxGamepad:
public function new(ID:Int, GlobalDeadZone:Float = 0) { buttons = new Map<Int, FlxGamepadButton>(); ball = new FlxPoint(); axis = new Array<Float>(); axis = [for (i in 0...6) 0]; hat = new FlxPoint(); id = ID;
OUYAbuttonID:
public static inline var O:Int = 0; // 96 public static inline var U:Int = 3; // 99 public static inline var Y:Int = 4; // 100 public static inline var A:Int = 1; // 97 public static inline var LB:Int = 6; // 102 public static inline var RB:Int = 7; // 103 //public static inline var BACK:Int = 5; //public static inline var START:Int = 4; public static inline var LEFT_ANALOGUE:Int = 10; // 106 public static inline var RIGHT_ANALOGUE:Int = 11; // 107 public static inline var HOME:Int = 2; // 82 public static inline var DPAD_UP:Int = 19; public static inline var DPAD_DOWN:Int = 20; public static inline var DPAD_LEFT:Int = 21; public static inline var DPAD_RIGHT:Int = 22;
public static inline var LEFT_ANALOGUE_X:Int = 0; public static inline var LEFT_ANALOGUE_Y:Int = 1; public static inline var RIGHT_ANALOGUE_X:Int = 11; public static inline var RIGHT_ANALOGUE_Y:Int = 14; public static inline var LEFT_TRIGGER:Int = 8; public static inline var RIGHT_TRIGGER:Int = 9; public static inline var LEFT_TRIGGER_ANALOG:Int = 17; public static inline var RIGHT_TRIGGER_ANALOG:Int = 18;
New Playstate of beeblerox's modified gamepad example:
package;
import flixel.FlxG;import flixel.FlxSprite;import flixel.FlxState;#if cppimport flixel.system.input.gamepad.FlxGamepad;import flixel.system.input.gamepad.XboxButtonID;import flixel.system.input.gamepad.OUYAButtonID;#endimport flixel.util.FlxColor;import flixel.util.FlxPoint;
class PlayState extends FlxState{ inline static private var STICK_MOVEMENT_RANGE:Float = 10; inline static private var ALPHA_OFF:Float = 0.5; inline static private var ALPHA_ON:Float = 1; inline static private var LB_Y:Float = 2; inline static private var RB_Y:Float = 2; static private var LEFT_STICK_POS:FlxPoint = new FlxPoint(80, 48); static private var RIGHT_STICK_POS:FlxPoint = new FlxPoint(304, 136); private var _controllerBg:FlxSprite; private var _leftStick:FlxSprite; private var _rightStick:FlxSprite; private var _dPad:FlxSprite; private var _xButton:FlxSprite; private var _yButton:FlxSprite; private var _aButton:FlxSprite; private var _bButton:FlxSprite; private var _backButton:FlxSprite; private var _startButton:FlxSprite; private var _LB:FlxSprite; private var _RB:FlxSprite; private var _LT:FlxSprite; private var _RT:FlxSprite; private var _LTA:FlxSprite; private var _RTA:FlxSprite; private var _homeButton:FlxSprite; #if cpp public static var GAMEPAD_ID:Int = -1; private var _gamePad:FlxGamepad; #end
override public function create():Void { FlxG.cameras.bgColor = FlxColor.GRAY;
#if cpp // Getting first availble gamepad _gamePad = FlxG.gamepads.get(GAMEPAD_ID); _LB = createSprite(71, LB_Y, "assets/LB.png", 0.8); _RB = createSprite(367, RB_Y, "assets/RB.png", 0.8); _LT = createSprite(10, LB_Y, "assets/LB.png", 0.8); _RT = createSprite(450, RB_Y, "assets/RB.png", 0.8); _LTA = createSprite(10, LB_Y+6, "assets/LB.png", 0.8); _RTA = createSprite(450, RB_Y+6, "assets/RB.png", 0.8); _controllerBg = createSprite(0, 0, "assets/xbox360_gamepad.png", 1); _leftStick = createSprite(LEFT_STICK_POS.x, LEFT_STICK_POS.y, "assets/Stick.png"); _rightStick = createSprite(RIGHT_STICK_POS.x, RIGHT_STICK_POS.y, "assets/Stick.png"); _dPad = new FlxSprite(144, 126); _dPad.loadGraphic("assets/DPad.png", true, false, 87, 87); _dPad.alpha = ALPHA_OFF; add(_dPad); _xButton = createSprite(357, 70, "assets/X.png"); _yButton = createSprite(395, 34, "assets/Y.png"); _aButton = createSprite(395, 109, "assets/A.png"); _bButton = createSprite(433, 70, "assets/B.png"); _homeButton = createSprite((526 / 2)-16, 70, "assets/A.png"); _backButton = createSprite(199, 79, "assets/Back.png"); _startButton = createSprite(306, 79, "assets/Start.png"); //_startButton.alpha = ALPHA_OFF; //_backButton.alpha = ALPHA_OFF; #else FlxG.log.add("GamePad is only supported on CPP Targets"); #end }
#if cpp private function createSprite(X:Float, Y:Float, Graphic:String, Alpha:Float = -1):FlxSprite { if (Alpha == -1) { Alpha = ALPHA_OFF; } var button:FlxSprite = new FlxSprite(X, Y, Graphic); button.alpha = Alpha; add(button); return button; } override public function update():Void { super.update(); if (_gamePad.pressed(OUYAButtonID.HOME)) { _homeButton.alpha = ALPHA_ON; } else { _homeButton.alpha = ALPHA_OFF; } if (_gamePad.pressed(OUYAButtonID.LEFT_TRIGGER)) { _LT.alpha = ALPHA_ON; } else { _LT.alpha = ALPHA_OFF; } if (_gamePad.pressed(OUYAButtonID.RIGHT_TRIGGER)) { _RT.alpha = ALPHA_ON; } else { _RT.alpha = ALPHA_OFF; } if (_gamePad.pressed(OUYAButtonID.RIGHT_ANALOGUE)) { _rightStick.alpha = ALPHA_ON; _rightStick.color = 0x00FF00; } else { _rightStick.alpha = ALPHA_OFF; _rightStick.color = 0xFFFFFF; } if (_gamePad.pressed(OUYAButtonID.LEFT_ANALOGUE)) { _leftStick.alpha = ALPHA_ON; _leftStick.color = 0x00FF00; } else { _leftStick.alpha = ALPHA_OFF; _leftStick.color = 0xFFFFFF; } if (_gamePad.pressed(OUYAButtonID.O)) { _aButton.alpha = ALPHA_ON; } else { _aButton.alpha = ALPHA_OFF; } if (_gamePad.pressed(OUYAButtonID.A)) { _bButton.alpha = ALPHA_ON; } else { _bButton.alpha = ALPHA_OFF; } if (_gamePad.pressed(OUYAButtonID.U)) { _xButton.alpha = ALPHA_ON; } else { _xButton.alpha = ALPHA_OFF; } if (_gamePad.pressed(OUYAButtonID.Y)) { _yButton.alpha = ALPHA_ON; } else { _yButton.alpha = ALPHA_OFF; } if (_gamePad.pressed(OUYAButtonID.LB)) { _LB.y = LB_Y + 5; } else { _LB.y = LB_Y; } if (_gamePad.pressed(OUYAButtonID.RB)) { _RB.y = RB_Y + 5; } else { _RB.y = RB_Y; } _LTA.alpha = _gamePad.getAxis(OUYAButtonID.LEFT_TRIGGER_ANALOG); _RTA.alpha = _gamePad.getAxis(OUYAButtonID.RIGHT_TRIGGER_ANALOG); var angle:Float = 0; if (Math.abs(_gamePad.getAxis(OUYAButtonID.LEFT_ANALOGUE_X)) > _gamePad.deadZone || Math.abs(_gamePad.getAxis(OUYAButtonID.LEFT_ANALOGUE_Y)) > _gamePad.deadZone) { angle = Math.atan2(_gamePad.getAxis(OUYAButtonID.LEFT_ANALOGUE_Y), _gamePad.getAxis(OUYAButtonID.LEFT_ANALOGUE_X)); _leftStick.x = LEFT_STICK_POS.x + STICK_MOVEMENT_RANGE * Math.cos(angle); _leftStick.y = LEFT_STICK_POS.y + STICK_MOVEMENT_RANGE * Math.sin(angle); _leftStick.alpha = ALPHA_ON; } else { _leftStick.x = LEFT_STICK_POS.x; _leftStick.y = LEFT_STICK_POS.y; _leftStick.alpha = ALPHA_OFF; } if (Math.abs(_gamePad.getAxis(OUYAButtonID.RIGHT_ANALOGUE_X)) > _gamePad.deadZone || Math.abs(_gamePad.getAxis(OUYAButtonID.RIGHT_ANALOGUE_Y)) > _gamePad.deadZone) { angle = Math.atan2(_gamePad.getAxis(OUYAButtonID.RIGHT_ANALOGUE_Y), _gamePad.getAxis(OUYAButtonID.RIGHT_ANALOGUE_X)); _rightStick.x = RIGHT_STICK_POS.x + STICK_MOVEMENT_RANGE * Math.cos(angle); _rightStick.y = RIGHT_STICK_POS.y + STICK_MOVEMENT_RANGE * Math.sin(angle); _rightStick.alpha = ALPHA_ON; } else { _rightStick.x = RIGHT_STICK_POS.x; _rightStick.y = RIGHT_STICK_POS.y; _rightStick.alpha = ALPHA_OFF; } if (_gamePad.hat.x != 0 || _gamePad.hat.y != 0) { if (_gamePad.hat.x > 0) { if (_gamePad.hat.y > 0) { _dPad.animation.frameIndex = 6; } else if (_gamePad.hat.y < 0) { _dPad.animation.frameIndex = 5; } else // gamePad.hat.y == 0 { _dPad.animation.frameIndex = 2; } } else if (_gamePad.hat.x < 0) { if (_gamePad.hat.y > 0) { _dPad.animation.frameIndex = 7; } else if (_gamePad.hat.y < 0) { _dPad.animation.frameIndex = 8; } else // gamePad.hat.y == 0 { _dPad.animation.frameIndex = 4; } } else // gamePad.hat.x == 0 { if (_gamePad.hat.y > 0) { _dPad.animation.frameIndex = 3; } else if (_gamePad.hat.y < 0) { _dPad.animation.frameIndex = 1; } } _dPad.alpha = ALPHA_ON; } else { _dPad.animation.frameIndex = 0; _dPad.alpha = ALPHA_OFF; } } #end}
I have Ouya controller and button graphics; going to start putting together a proper example with 4 controllers detected. If anyone figures out how to read the menu/home button, please share. Because of its special case, I can't just plug numbers til I stumble upon it I don't think.
#if android // For the OUYA home button Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onHome); #end
#if android private function onHome(e:KeyboardEvent):Void { if(e.keyCode == 16777234) { // do stuff here } }#end
There's an FlxAndroidKeyboard class. This should probably belong there..
Thanks a lot for Menu button keycode! i'll add it to Keyboard class
--
HaxeFlixel Development Community
See our github https://github.com/haxeflixel/ and our documentation http://haxeflixel.com/documentation/
---
You received this message because you are subscribed to a topic in the Google Groups "HaxeFlixel" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/haxeflixel/hoOnjaN2XnI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to haxeflixel+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/haxeflixel.
To view this discussion on the web visit https://groups.google.com/d/msgid/haxeflixel/bef6914f-e1b7-4fa7-b1ee-1f43702d8ffc%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.