More work on Gamepad support

This commit is contained in:
Joshua Granick
2015-03-18 02:58:31 -07:00
parent cfbf3f4a3f
commit ed0ca6f512
11 changed files with 116 additions and 33 deletions

View File

@@ -9,6 +9,7 @@ import lime.graphics.GLRenderContext;
import lime.graphics.RenderContext;
import lime.graphics.Renderer;
import lime.system.System;
import lime.ui.Gamepad;
import lime.ui.Window;
@:access(lime.app.Application)
@@ -127,10 +128,12 @@ class NativeApplication {
case CONNECT:
Gamepad.devices.push (gamepadEventInfo.id);
parent.window.onGamepadConnect.dispatch (gamepadEventInfo.id);
case DISCONNECT:
Gamepad.devices.remove (gamepadEventInfo.id);
parent.window.onGamepadDisconnect.dispatch (gamepadEventInfo.id);
}

View File

@@ -2,6 +2,9 @@ package lime.app;
import lime.graphics.RenderContext;
import lime.ui.Gamepad;
import lime.ui.GamepadAxis;
import lime.ui.GamepadButton;
import lime.ui.KeyCode;
import lime.ui.KeyModifier;
@@ -18,11 +21,11 @@ interface IModule {
public function init (context:RenderContext):Void;
public function onGamepadAxisMove (id:Int, axis:Int, value:Float):Void;
public function onGamepadButtonDown(id:Int, button:Int):Void;
public function onGamepadButtonUp (id:Int, button:Int):Void;
public function onGamepadConnect (id:Int):Void;
public function onGamepadDisconnect (id:Int):Void;
public function onGamepadAxisMove (gamepad:Gamepad, axis:GamepadAxis, value:Float):Void;
public function onGamepadButtonDown (gamepad:Gamepad, button:GamepadButton):Void;
public function onGamepadButtonUp (gamepad:Gamepad, button:GamepadButton):Void;
public function onGamepadConnect (gamepad:Gamepad):Void;
public function onGamepadDisconnect (gamepad:Gamepad):Void;
/**

View File

@@ -2,6 +2,9 @@ package lime.app;
import lime.graphics.RenderContext;
import lime.ui.Gamepad;
import lime.ui.GamepadAxis;
import lime.ui.GamepadButton;
import lime.ui.KeyCode;
import lime.ui.KeyModifier;
@@ -25,11 +28,11 @@ class Module implements IModule {
public function init (context:RenderContext):Void { }
public function onGamepadAxisMove (id:Int, axis:Int, value:Float):Void { }
public function onGamepadButtonDown(id:Int, button:Int):Void { }
public function onGamepadButtonUp (id:Int, button:Int):Void { }
public function onGamepadConnect (id:Int):Void { }
public function onGamepadDisconnect (id:Int):Void { }
public function onGamepadAxisMove (gamepad:Gamepad, axis:GamepadAxis, value:Float):Void { }
public function onGamepadButtonDown (gamepad:Gamepad, button:GamepadButton):Void { }
public function onGamepadButtonUp (gamepad:Gamepad, button:GamepadButton):Void { }
public function onGamepadConnect (gamepad:Gamepad):Void { }
public function onGamepadDisconnect (gamepad:Gamepad):Void { }
/**

View File

@@ -4,13 +4,37 @@ package lime.ui;
import lime.system.System;
class Gamepad {
abstract Gamepad(Int) from Int to Int {
public static function getDeviceName (id:Int):String {
public static var devices = new Array<Gamepad> ();
public var guid (get, never):String;
public var name (get, never):String;
// Get & Set Methods
@:noCompletion private inline function get_guid ():String {
#if (cpp || neko || nodejs)
return lime_gamepad_get_device_name (id);
return lime_gamepad_get_device_name (this);
#else
return null;
#end
}
@:noCompletion private inline function get_name ():String {
#if (cpp || neko || nodejs)
return lime_gamepad_get_device_name (this);
#else
return null;
#end
@@ -26,6 +50,7 @@ class Gamepad {
#if (cpp || neko || nodejs)
private static var lime_gamepad_get_device_guid = System.load ("lime", "lime_gamepad_get_device_guid", 1);
private static var lime_gamepad_get_device_name = System.load ("lime", "lime_gamepad_get_device_name", 1);
#end

13
lime/ui/GamepadAxis.hx Normal file
View File

@@ -0,0 +1,13 @@
package lime.ui;
@:enum abstract GamepadAxis(Int) from Int to Int {
var LEFT_X = 1;
var LEFT_Y = 2;
var RIGHT_X = 3;
var RIGHT_Y = 4;
var TRIGGER_LEFT = 5;
var TRIGGER_RIGHT = 6;
}

22
lime/ui/GamepadButton.hx Normal file
View File

@@ -0,0 +1,22 @@
package lime.ui;
@:enum abstract GamepadButton(Int) from Int to Int {
var A = 1;
var B = 2;
var X = 3;
var Y = 4;
var BACK = 5;
var GUIDE = 6;
var START = 7;
var LEFT_STICK = 8;
var RIGHT_STICK = 9;
var LEFT_SHOULDER = 10;
var RIGHT_SHOULDER = 11;
var DPAD_UP = 12;
var DPAD_DOWN = 13;
var DPAD_LEFT = 14;
var DPAD_RIGHT = 15;
}

View File

@@ -15,11 +15,11 @@ class Window {
public var config:Config;
public var fullscreen:Bool;
public var height:Int;
public var onGamepadAxisMove = new Event<Int->Int->Float->Void> ();
public var onGamepadButtonDown = new Event<Int->Int->Void> ();
public var onGamepadButtonUp = new Event<Int->Int->Void> ();
public var onGamepadConnect = new Event<Int->Void> ();
public var onGamepadDisconnect = new Event<Int->Void> ();
public var onGamepadAxisMove = new Event<Gamepad->GamepadAxis->Float->Void> ();
public var onGamepadButtonDown = new Event<Gamepad->GamepadButton->Void> ();
public var onGamepadButtonUp = new Event<Gamepad->GamepadButton->Void> ();
public var onGamepadConnect = new Event<Gamepad->Void> ();
public var onGamepadDisconnect = new Event<Gamepad->Void> ();
public var onKeyDown = new Event<KeyCode->KeyModifier->Void> ();
public var onKeyUp = new Event<KeyCode->KeyModifier->Void> ();
public var onMouseDown = new Event<Float->Float->Int->Void> ();

View File

@@ -9,6 +9,7 @@ namespace lime {
public:
static const char* GetDeviceGUID (int id);
static const char* GetDeviceName (int id);
};

View File

@@ -353,6 +353,13 @@ namespace lime {
}
value lime_gamepad_get_device_guid (value id) {
return alloc_string (Gamepad::GetDeviceGUID (val_int (id)));
}
value lime_gamepad_get_device_name (value id) {
return alloc_string (Gamepad::GetDeviceName (val_int (id)));
@@ -734,6 +741,7 @@ namespace lime {
DEFINE_PRIM (lime_font_render_glyphs, 3);
DEFINE_PRIM (lime_font_set_size, 2);
DEFINE_PRIM (lime_gamepad_event_manager_register, 2);
DEFINE_PRIM (lime_gamepad_get_device_guid, 1);
DEFINE_PRIM (lime_gamepad_get_device_name, 1);
DEFINE_PRIM (lime_image_encode, 3);
DEFINE_PRIM (lime_image_load, 1);

View File

@@ -251,15 +251,11 @@ namespace lime {
case SDL_CONTROLLERDEVICEREMOVED: {
if (SDLGamepad::Disconnect (event->cdevice.which)) {
gamepadEvent.type = DISCONNECT;
gamepadEvent.id = event->cdevice.which;
GamepadEvent::Dispatch (&gamepadEvent);
}
gamepadEvent.type = DISCONNECT;
gamepadEvent.id = event->cdevice.which;
GamepadEvent::Dispatch (&gamepadEvent);
SDLGamepad::Disconnect (event->cdevice.which);
break;
}

View File

@@ -50,13 +50,6 @@ namespace lime {
}
const char* Gamepad::GetDeviceName (int id) {
return SDL_GameControllerName (gameControllers[id]);
}
int SDLGamepad::GetInstanceID (int deviceID) {
return gameControllerIDs[deviceID];
@@ -64,4 +57,20 @@ namespace lime {
}
const char* Gamepad::GetDeviceGUID (int id) {
char* guid = new char[64];
SDL_JoystickGetGUIDString (SDL_JoystickGetDeviceGUID (id), guid, 64);
return guid;
}
const char* Gamepad::GetDeviceName (int id) {
return SDL_GameControllerName (gameControllers[id]);
}
}