Skip to content

Instantly share code, notes, and snippets.

@jgranick
Last active December 19, 2015 03:09
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgranick/5888202 to your computer and use it in GitHub Desktop.
Save jgranick/5888202 to your computer and use it in GitHub Desktop.
Adding controller support for OUYA
package;
import flash.display.Sprite;
import flash.geom.Point;
import openfl.events.JoystickEvent;
#if android
import openfl.utils.JNI;
import tv.ouya.console.api.OuyaController;
#end
class Main extends Sprite {
public function new () {
super ();
#if android
var getContext = JNI.createStaticMethod ("org.haxe.nme.GameActivity", "getContext", "()Landroid/content/Context;", true);
OuyaController.init (getContext ());
#end
stage.addEventListener (JoystickEvent.AXIS_MOVE, stage_onJoystickAxisMove);
stage.addEventListener (JoystickEvent.BUTTON_DOWN, stage_onJoystickButtonDown);
stage.addEventListener (JoystickEvent.BUTTON_UP, stage_onJoystickButtonUp);
stage.addEventListener (JoystickEvent.HAT_MOVE, stage_onJoystickHatMove);
}
// Event Handlers
private function stage_onJoystickAxisMove (event:JoystickEvent):Void {
var player = OuyaController.getPlayerNumByDeviceId (event.device);
var leftX = event.x; // event.axis[0];
var leftY = event.y; // event.axis[1];
var rightX = event.axis[3];
var rightY = event.axis[4];
}
private function stage_onJoystickButtonDown (event:JoystickEvent):Void {
var player = OuyaController.getPlayerNumByDeviceId (event.device);
var buttonCode = event.id;
}
private function stage_onJoystickButtonUp (event:JoystickEvent):Void {
var player = OuyaController.getPlayerNumByDeviceId (event.device);
var buttonCode = event.id;
}
private function stage_onJoystickHatMove (event:JoystickEvent):Void {
var player = OuyaController.getPlayerNumByDeviceId (event.device);
var left = (event.x < 0);
var right = (event.x > 0);
var up = (event.y < 0);
var down = (event.y > 0);
}
}
@jgranick
Copy link
Author

When you a target a newer Android API, OpenFL will be able to dispatch JoystickEvents on Android, similar to desktop platforms. On OUYA, it can be difficult to map the device ID to the player, so the OuyaController class is helpful.

To add it, install "openfl-ouya" from haxelib, then put <haxelib name="openfl-ouya" /> in your project file.

@Hasufel
Copy link

Hasufel commented Jun 29, 2013

The id drift is occuring even using their ODK if you have a usb controller.keyboard plugged in when using the function getPlayerNumByDeviceId. I reported the id drift two months ago to OUYA - no move yet on their end.

What we really need to implement is if its just to use the getPlayerNumByDeviceId thingy -

  1. A poll function to know what joystick devices are connected to the system at an instant T.
    in MainView.java we can put InputDevice.getDeviceIds() -> returns an array of connected devices.
    according to this array, we can get infos: device = InputDevice.getDeviceId() and trace its name with device.getName().
    implement this in a function call in OpenFL to get this array at any time,
    like extending nme.system with nme.system.Joysticks with a function of getConnectedJoysticks returning [[deviceId, StringId],[deviceId, StringId],[deviceIdN, StringIdN]]

  2. Whenever a new device is connected, it pushes something in the pipe. Extending nme.events with NewJoystickConnected returning [deviceId,StringId]

I'll try to implement this.

@Hasufel
Copy link

Hasufel commented Jun 30, 2013

I implemented the poll function and more - and simplified the overall process - we can ditch getPlayerNumByDeviceId totally. http://www.nme.io/download_file/592/8014/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment