rj-action-library/Runtime/Sensors/InputSensor.cs

76 lines
1.5 KiB
C#

using Godot;
namespace Rokojori
{
[GlobalClass,Icon("res://addons/rokojori_action_library/Icons/RJSensor.svg")]
public partial class InputSensor : RJSensor
{
[Export]
public string inputActionName = "";
[Export]
public bool pollAsButton = false;
[Export]
public bool pollAsReleasedOnly = false;
[Export]
public float buttonSmoothingFilterCoefficient = 1;
[Export]
public float axisActivationTreshold = 0.75f;
float _value = 0;
bool _wasActive = false;
bool _isActive = false;
public override float GetValue()
{
return _value;
}
public override bool IsActive()
{
return _isActive;
}
public override bool WasActive()
{
return _wasActive;
}
public override void UpdateValue( float value )
{
_value = value;
_wasActive = _isActive;
_isActive = _value > axisActivationTreshold;
}
public override void _Process( double delta )
{
if ( pollAsButton )
{
if ( pollAsReleasedOnly )
{
bool state = Input.IsActionJustReleased( inputActionName );
UpdateValue( state ? 1 : 0 );
}
else
{
bool state = Input.IsActionPressed( inputActionName );
UpdateValue( state ? 1 : 0 );
}
}
else
{
float value = Input.GetActionRawStrength( inputActionName );
UpdateValue( value );
}
}
}
}