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

69 lines
1.4 KiB
C#
Raw Normal View History

2025-01-03 12:09:23 +00:00
using Godot;
namespace Rokojori
{
2025-01-08 18:46:17 +00:00
[Tool]
2025-01-03 12:09:23 +00:00
[GlobalClass,Icon("res://addons/rokojori_action_library/Icons/RJSensor.svg")]
2025-01-08 18:46:17 +00:00
public partial class MouseScreenRelative : Sensor, iOnInputSensor
2025-01-03 12:09:23 +00:00
{
public enum MouseMotionType
{
Right,
Left,
Up,
Down
}
[Export]
public MouseMotionType motionType;
float _lastInput = 0;
2025-01-08 18:46:17 +00:00
protected override void UpdateValue()
2025-01-03 12:09:23 +00:00
{
2025-01-08 18:46:17 +00:00
SetFloatValue( _lastInput );
2025-01-03 12:09:23 +00:00
}
2025-01-08 18:46:17 +00:00
public void _Input( InputEvent ev )
2025-01-03 12:09:23 +00:00
{
var mouseEvent = ev as InputEventMouseMotion ;
if ( mouseEvent == null )
{
return;
}
2025-01-08 18:46:17 +00:00
var vp = Unique<SensorManager>.Get().GetViewport();
var relativePosition = mouseEvent.Position / vp.GetVisibleRect().Size * 2 - Vector2.One;
2025-01-03 12:09:23 +00:00
var position = 0f;
// RJLog.Log( mouseEvent.Position );
if ( MouseMotionType.Left == motionType)
{
position = Mathf.Max( 0, -relativePosition.X );
}
else if ( MouseMotionType.Right == motionType)
{
position = Mathf.Max( 0, relativePosition.X );
}
else if ( MouseMotionType.Up == motionType)
{
position = Mathf.Max( 0, relativePosition.Y );
}
if ( MouseMotionType.Down == motionType)
{
position = Mathf.Max( 0, -relativePosition.Y );
}
// RJLog.Log( "Motion:", motionType, motion * speedMultiply );
_lastInput = position;
}
}
}