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

91 lines
1.8 KiB
C#
Raw Normal View History

2024-12-01 17:07:41 +00:00
using Godot;
namespace Rokojori
{
2025-01-08 18:46:17 +00:00
[Tool]
2024-12-01 17:07:41 +00:00
[GlobalClass,Icon("res://addons/rokojori_action_library/Icons/RJSensor.svg")]
2025-01-08 18:46:17 +00:00
public partial class KeySensor : Sensor, iOnInputSensor
2024-12-01 17:07:41 +00:00
{
[Export]
public Key key;
[ExportGroup( "Modifiers")]
2025-01-08 18:46:17 +00:00
[Export]
2024-12-01 17:07:41 +00:00
public Trillean ctrlHold = Trillean.Any;
[Export]
public Trillean altHold = Trillean.Any;
[Export]
public Trillean shiftHold = Trillean.Any;
2025-01-03 12:09:23 +00:00
public bool modifiersEnabled => ! TrilleanLogic.AllAny( ctrlHold, altHold, shiftHold );
2024-12-01 17:07:41 +00:00
public enum ModifiersMode
{
Hold_Modifiers_Only_On_Down,
Hold_Modifiers_All_The_Time
}
[Export]
public ModifiersMode modifiersMode;
float _lastInput = 0;
2025-01-08 18:46:17 +00:00
protected override void UpdateValue()
2024-12-01 17:07:41 +00:00
{
2025-01-08 18:46:17 +00:00
SetFloatValue( _lastInput );
2024-12-01 17:07:41 +00:00
}
2025-01-08 18:46:17 +00:00
public void _Input( InputEvent ev )
2024-12-01 17:07:41 +00:00
{
var keyEvent = ev as InputEventKey;
if ( keyEvent == null )
{
return;
}
2025-01-03 12:09:23 +00:00
if ( keyEvent.Keycode != key )
2024-12-01 17:07:41 +00:00
{
return;
}
var checkModifiers = modifiersEnabled &&
(
ModifiersMode.Hold_Modifiers_All_The_Time == modifiersMode ||
_lastInput == 0 && ModifiersMode.Hold_Modifiers_Only_On_Down == modifiersMode
);
if ( checkModifiers )
{
2025-01-03 12:09:23 +00:00
2024-12-01 17:07:41 +00:00
if ( ! TrilleanLogic.Matches( ctrlHold, keyEvent.CtrlPressed ) )
{
2025-01-03 12:09:23 +00:00
_lastInput = 0;
2024-12-01 17:07:41 +00:00
return;
}
if ( ! TrilleanLogic.Matches( altHold, keyEvent.AltPressed ) )
{
_lastInput = 0;
return;
}
if ( ! TrilleanLogic.Matches( shiftHold, keyEvent.ShiftPressed ) )
{
_lastInput = 0;
return;
}
}
_lastInput = keyEvent.IsPressed() ? 1 : 0;
}
}
}