I fixed it! Yessssssssss!
Now I'll explain what I changed to get it to work. Let's start with the old function:
public static function getButtonPressure(control:String):Float
{
#if desktop
if(_controlButtonMap.exists(control))
{
var buttons = _controlButtonMap.get(control);
var highestPressure:Float = 0;
if(check(control))
return 1;
for(b in buttons)
{
if(!_joyControllerReady[b.a[0]])
continue;
switch(b.a[JoystickButton.TYPE])
{
case JoystickButton.AXIS:
if(_joyAxisState.get(b.a[0])[b.a[2]] == b.a[3])
highestPressure = Math.max(highestPressure, Math.abs(_joyAxisPressure.get(b.a[0])[b.a[2]]));
case JoystickButton.HAT:
if(_joyHatState.get(b.a[0])[b.a[2]] == b.a[3])
return 1;
case JoystickButton.BUTTON:
if(_joyButtonState.get(b.a[0])[b.a[2]])
return 1;
}
}
return highestPressure;
}
else
return check(control) ? 1 : 0;
#else
return check(control) ? 1 : 0;
#end
}
While experimenting, there were two problems that I found.
Problem 1:if(check(control))
return 1;
Before checking the actual pressure of the axis, it would check to see if the corresponding control was down. Of course, this means it would always return 1, as long as the pressure is more than the analog sensitivity. This can be fixed by simply removing this bit of code.
Problem 2:case JoystickButton.AXIS:
if(_joyAxisState.get(b.a[0])[b.a[2]] == b.a[3])
highestPressure = Math.max(highestPressure, Math.abs(_joyAxisPressure.get(b.a[0])[b.a[2]]));
b.a[3] is always equal to 1 or -1, so this code (namely the second line) only detects that the pressure is valid if it is equal to 1 or -1. This can be fixed by first checking the sign, then correspondingly checking the value:
case JoystickButton.AXIS:
if(b.a[3] == 1 && _joyAxisState.get(b.a[0])[b.a[2]] > joySensitivity)
highestPressure = Math.max(highestPressure, Math.abs(_joyAxisPressure.get(b.a[0])[b.a[2]]));
else if(b.a[3] == -1 && _joyAxisState.get(b.a[0])[b.a[2]] < -joySensitivity)
highestPressure = Math.max(highestPressure, Math.abs(_joyAxisPressure.get(b.a[0])[b.a[2]]));
All in all, these fixes seem to work perfectly! I've tested this successfully with joysticks, triggers, hat buttons and regular buttons. The code may still need some improvements, but I currently see no need.
Here's the final code:
public static function getButtonPressure(control:String):Float
{
#if desktop
if(_controlButtonMap.exists(control))
{
var buttons = _controlButtonMap.get(control);
var highestPressure:Float = 0;
for(b in buttons)
{
if(!_joyControllerReady[b.a[0]])
continue;
switch(b.a[JoystickButton.TYPE])
{
case JoystickButton.AXIS:
if(b.a[3] == 1 && _joyAxisState.get(b.a[0])[b.a[2]] > joySensitivity)
highestPressure = Math.max(highestPressure, Math.abs(_joyAxisPressure.get(b.a[0])[b.a[2]]));
else if(b.a[3] == -1 && _joyAxisState.get(b.a[0])[b.a[2]] < -joySensitivity)
highestPressure = Math.max(highestPressure, Math.abs(_joyAxisPressure.get(b.a[0])[b.a[2]]));
case JoystickButton.HAT:
if(_joyHatState.get(b.a[0])[b.a[2]] == b.a[3])
return 1;
case JoystickButton.BUTTON:
if(_joyButtonState.get(b.a[0])[b.a[2]])
return 1;
}
}
return highestPressure;
}
else
return check(control) ? 1 : 0;
#else
return check(control) ? 1 : 0;
#end
}