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

123 lines
2.6 KiB
C#
Raw Normal View History

2024-12-01 17:07:41 +00:00
using Godot;
using System.Collections.Generic;
2024-12-01 17:07:41 +00:00
namespace Rokojori
{
2025-01-08 18:46:17 +00:00
[Tool]
2025-01-21 20:58:56 +00:00
[GlobalClass,Icon("res://addons/rokojori_action_library/Icons/KeySensor.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;
[Export]
public KeyLocation keyLocation = KeyLocation.Unspecified;
2024-12-01 17:07:41 +00:00
[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;
public override string ToString()
{
return RJLog.GetInfo( this, key, keyLocation, "Ctrl/Alt/Shift:" + ctrlHold + "/" + altHold + "/" + shiftHold );
}
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;
2024-12-01 17:07:41 +00:00
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;
}
if ( keyLocation != KeyLocation.Unspecified && keyEvent.Location != KeyLocation.Unspecified )
{
if ( keyEvent.Location != keyLocation )
{
return;
}
}
2024-12-01 17:07:41 +00:00
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;
UpdateSensorUsage();
2024-12-01 17:07:41 +00:00
}
public override List<InputIcon> GetInputIcons()
{
var icon = new KeyIcon();
icon.key = key;
return new List<InputIcon>(){ icon };
}
public static bool IsModifierKey( Key key )
{
return Lists.IsOneOf( key, Key.Ctrl, Key.Alt, Key.Shift, Key.Meta );
}
2024-12-01 17:07:41 +00:00
}
}