Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dedicated Pad Event stream #1195

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions hxd/Pad.hx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ typedef PadConfig = {
names : Array<String>,
}

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
Expand Down Expand Up @@ -230,6 +247,7 @@ class Pad {

public dynamic function onDisconnect(){
}
public var onPadEvent : (event : PadEvent) -> Void;

public function isDown( button : Int ) {
return buttons[button];
Expand Down Expand Up @@ -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);
Expand All @@ -397,6 +415,8 @@ class Pad {
rawRXAxis = v;
else if( axisId == 3 )
rawRYAxis = v;

return values[ axisId ];
}

static function initPad( index ){
Expand All @@ -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 ){
Expand All @@ -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:
}
}
Expand Down