diff --git a/hxd/Pad.hx b/hxd/Pad.hx index 0e3886c70b..258a5b6618 100644 --- a/hxd/Pad.hx +++ b/hxd/Pad.hx @@ -51,6 +51,23 @@ typedef PadConfig = { names : Array, } +enum abstract PadEventKind(Int) from Int to Int { + var EPadConnect; + var EPadDisconnect; + var EPadPush; + var EPadRelease; + var EPadAxis; +} + +@:structInit +class PadEvent { + public var kind : PadEventKind; + public var button : Int = 0; + public var value : Float = 0.0; + public var axis : Int = 0; + public var pad : Pad; +} + class Pad { #if hlsdl @@ -230,6 +247,7 @@ class Pad { public dynamic function onDisconnect(){ } + public var onPadEvent : (event : PadEvent) -> Void; public function isDown( button : Int ) { return buttons[button]; @@ -378,7 +396,7 @@ class Pad { #if hlsdl - inline function _setAxis( axisId : Int, value : Int ){ + inline function _setAxis( axisId : Int, value : Int ) : Float{ var v = value / 0x7FFF; _detectAnalogButton(axisId, v); @@ -397,6 +415,8 @@ class Pad { rawRXAxis = v; else if( axisId == 3 ) rawRYAxis = v; + + return values[ axisId ]; } static function initPad( index ){ @@ -419,6 +439,8 @@ class Pad { for( button in 0...15 ) p._setButton( button + 6, sp.getButton(button) ); waitPad( p ); + if (p.onPadEvent != null) + p.onPadEvent( { kind: EPadConnect, pad:p } ); } static function onEvent( e : Event ){ @@ -429,20 +451,31 @@ class Pad { initPad(e.controller); case GControllerRemoved: if( p != null ){ + if (p.onPadEvent != null) + p.onPadEvent( { kind: EPadDisconnect, pad:p } ); pads.remove( p.index ); p.d.close(); p.connected = false; p.onDisconnect(); } case GControllerDown: - if( p != null && e.button > -1 ) + if( p != null && e.button > -1 ) { p._setButton( e.button + 6, true ); + if (p.onPadEvent != null) + p.onPadEvent( { kind: EPadPush, button: e.button + 6, value: 1, pad: p } ); + } case GControllerUp: - if( p != null && e.button > -1 ) + if( p != null && e.button > -1 ) { p._setButton( e.button + 6, false ); + if (p.onPadEvent != null) + p.onPadEvent( { kind: EPadRelease, button: e.button + 6, value: 0, pad: p } ); + } case GControllerAxis: - if( p != null && e.button > -1 && e.button < 6 ) - p._setAxis( e.button, e.value ); + if( p != null && e.button > -1 && e.button < 6 ) { + var value = p._setAxis( e.button, e.value ); + if (p.onPadEvent != null) + p.onPadEvent( { kind: EPadAxis, axis: e.button, value: value, pad: p } ); + } default: } }