rokojori_action_library/Runtime/Sensors/GamePadAxisSensor.cs

94 lines
2.0 KiB
C#
Raw Normal View History

2025-01-21 20:58:56 +00:00
using Godot;
using System.Collections.Generic;
2025-01-21 20:58:56 +00:00
namespace Rokojori
{
[Tool]
[GlobalClass,Icon("res://addons/rokojori_action_library/Icons/AxisSensor.svg")]
public partial class GamePadAxisSensor : Sensor, iOnInputSensor
{
[Export]
public JoyAxis axis;
public override bool isMultiDevice => true;
[Export]
public GamePadAxisType type;
2025-01-21 20:58:56 +00:00
public override string ToString()
{
return RJLog.GetInfo( this, axis, type );
}
2025-01-21 20:58:56 +00:00
float _lastInput = 0;
protected override void UpdateValue()
{
SetFloatValue( _lastInput );
}
2026-02-26 14:06:27 +00:00
public override bool IsSensor( InputEvent ev )
{
// this.LogInfo( "Checking axis sensor:", Sensor.GetInputEventInfo( ev ) );
var joypadAxisEvent = ev as InputEventJoypadMotion;
if ( joypadAxisEvent == null )
{
return false;
}
if ( joypadAxisEvent.Axis != axis )
{
return false;
}
if ( type == GamePadAxisType.Positive )
{
// this.LogInfo( "Is positive axis sensor:", Sensor.GetInputEventInfo( ev ) );
return joypadAxisEvent.AxisValue > 0;
}
else if ( type == GamePadAxisType.Negative )
{
// this.LogInfo( "Is negative axis sensor:", Sensor.GetInputEventInfo( ev ) );
return joypadAxisEvent.AxisValue < 0;
}
return false;
}
2025-01-21 20:58:56 +00:00
public void _Input( InputEvent ev )
{
var joypadAxisEvent = ev as InputEventJoypadMotion;
if ( joypadAxisEvent == null )
{
return;
}
if ( joypadAxisEvent.Axis != axis )
{
return;
}
_lastInput = Mathf.Max( (GamePadAxisType.Negative == type ? -1 : 1 ) * joypadAxisEvent.AxisValue, 0 );
UpdateSensorUsage( joypadAxisEvent.Device );
2025-01-21 20:58:56 +00:00
}
public override List<InputIcon> GetInputIcons()
{
var icon = new GamePadAxisIcon();
icon.axis = axis;
icon.type = type;
return new List<InputIcon>(){ icon };
}
2025-01-21 20:58:56 +00:00
}
}