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

93 lines
1.9 KiB
C#

using Godot;
namespace Rokojori
{
[GlobalClass,Icon("res://addons/rokojori_action_library/Icons/RJSensor.svg")]
public partial class MouseScreenRelative : RJSensor
{
public enum MouseMotionType
{
Right,
Left,
Up,
Down
}
[Export]
public MouseMotionType motionType;
bool _isActive = false;
bool _wasActive = false;
float _value = 0;
float axisActivationTreshold = 0.5f;
float _lastInput = 0;
public override void _Process( double delta )
{
UpdateValue( _lastInput );
}
public override void _Input( InputEvent ev )
{
var mouseEvent = ev as InputEventMouseMotion ;
if ( mouseEvent == null )
{
return;
}
var relativePosition = mouseEvent.Position / GetViewport().GetVisibleRect().Size * 2 - Vector2.One;
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;
}
public override bool IsActive()
{
return _isActive;
}
public override bool WasActive()
{
return _wasActive;
}
public override float GetValue()
{
return _value;
}
public override void UpdateValue( float value )
{
_value = value;
_wasActive = _isActive;
_isActive = _value > axisActivationTreshold;
}
}
}