From 7b35a721fdad598d9db60e95f73fc2ec45c3ed2f Mon Sep 17 00:00:00 2001 From: Ryan Cleven Date: Sun, 25 Feb 2024 17:41:21 -0800 Subject: [PATCH 1/2] Added dedicated Pad Event stream You can set an optional callback on the pad to send pad events where you want them. This can be used to reduce the need to constantly check. It can also track inputs that happen between frames. --- hxd/Pad.hx | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/hxd/Pad.hx b/hxd/Pad.hx index 0e3886c70b..58b70106d9 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]; @@ -419,6 +437,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 +449,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 ) + if( p != null && e.button > -1 && e.button < 6 ) { p._setAxis( e.button, e.value ); + if (p.onPadEvent != null) + p.onPadEvent( { kind: EPadAxis, axis: e.button, value: e.value, pad: p } ); + } default: } } From dfee58583b2d1b693aa6647691d5e1e1585bd6da Mon Sep 17 00:00:00 2001 From: Ryan Cleven Date: Sun, 25 Feb 2024 17:54:27 -0800 Subject: [PATCH 2/2] Return the normalized raw value instead of the hlsdl integer values --- hxd/Pad.hx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hxd/Pad.hx b/hxd/Pad.hx index 58b70106d9..258a5b6618 100644 --- a/hxd/Pad.hx +++ b/hxd/Pad.hx @@ -396,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); @@ -415,6 +415,8 @@ class Pad { rawRXAxis = v; else if( axisId == 3 ) rawRYAxis = v; + + return values[ axisId ]; } static function initPad( index ){ @@ -470,9 +472,9 @@ class Pad { } case GControllerAxis: if( p != null && e.button > -1 && e.button < 6 ) { - p._setAxis( e.button, e.value ); + var value = p._setAxis( e.button, e.value ); if (p.onPadEvent != null) - p.onPadEvent( { kind: EPadAxis, axis: e.button, value: e.value, pad: p } ); + p.onPadEvent( { kind: EPadAxis, axis: e.button, value: value, pad: p } ); } default: }