rokojori_action_library/Runtime/Sensors/Sensor.cs

155 lines
3.6 KiB
C#
Raw Normal View History

2025-01-03 12:09:23 +00:00
using Godot;
using System.Collections.Generic;
2025-01-03 12:09:23 +00:00
namespace Rokojori
{
[Tool]
2025-01-21 20:58:56 +00:00
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/Sensor.svg")]
2026-02-26 14:06:27 +00:00
public abstract partial class Sensor: Resource
2025-01-21 20:58:56 +00:00
{
[Export]
public LocaleText name;
[Export]
public LocaleText info;
2025-01-03 12:09:23 +00:00
[Export]
public bool continous = false;
2025-05-21 20:44:28 +00:00
protected float _value = 0;
2025-01-21 20:58:56 +00:00
2025-05-21 20:44:28 +00:00
protected bool _wasActive = false;
2025-01-21 20:58:56 +00:00
2025-05-21 20:44:28 +00:00
protected bool _active = false;
2025-01-21 20:58:56 +00:00
2025-05-21 20:44:28 +00:00
protected float _activeTreshold = 0.5f;
2025-01-03 12:09:23 +00:00
2025-07-25 08:13:35 +00:00
protected bool _consumed = false;
2025-01-03 12:09:23 +00:00
public void ProcessSensor( SensorRunner runner, float delta )
{
_wasActive = _active;
UpdateValue();
}
public bool isDown => ! _wasActive && _active;
public bool isUp => _wasActive && ! _active;
2025-01-21 20:58:56 +00:00
public bool isHold => _active;
2025-01-03 12:09:23 +00:00
public bool wasActive => _wasActive;
public bool isActive => _active;
2025-01-21 20:58:56 +00:00
public float value => _value;
2025-01-03 12:09:23 +00:00
2025-01-21 20:58:56 +00:00
public virtual int deviceID => 0;
public virtual bool isMultiDevice => false;
2025-01-03 12:09:23 +00:00
2025-01-21 20:58:56 +00:00
public virtual float GetValue( int device ) => value;
public virtual bool IsDown( int device ) => isDown;
public virtual bool IsUp( int device ) => isUp;
2025-01-03 12:09:23 +00:00
2025-01-21 20:58:56 +00:00
public virtual bool WasActive( int device ) => isUp;
public virtual bool IsActive( int device ) => isUp;
2025-01-03 12:09:23 +00:00
2026-02-26 14:06:27 +00:00
public virtual bool IsDown( InputEvent ie ){ return IsSensor( ie ) && ie.IsPressed(); }
public virtual bool IsUp( InputEvent ie ){ return IsSensor( ie ) && ie.IsReleased(); }
public abstract bool IsSensor( InputEvent ie );
public virtual bool IsRepeatDown( ref float lastActivation, float delay = 0.5f, float repeatDuration = 0.1f )
{
var now = TimeLine.osTime;
if ( isDown )
{
lastActivation = now + delay;
return true;
}
var value = isActive && ( now - lastActivation ) > repeatDuration;
if ( value )
{
lastActivation = now;
}
return value;
}
public static string GetInputEventInfo( InputEvent ie )
{
var type = ie.GetType().Name;
var isPressed = ie.IsPressed();
var isReleeased = ie.IsReleased();
var details = "";
if ( ie is InputEventKey k )
{
details = "key: " + k.Keycode + " " + k.KeyLabel;
}
else if ( ie is InputEventJoypadButton b )
{
details = "gamepad button: " + b.ButtonIndex;
}
else if ( ie is InputEventJoypadMotion a )
{
details = "gamepad axis: " + a.Axis + " " + a.AxisValue;
}
else if ( ie is InputEventMouseButton mb )
{
details = "mouse button: " + mb.ButtonIndex + " " + mb.ButtonMask;
}
else if ( ie is InputEventMouseMotion mm )
{
details = "mouse motion: " + mm.Position + " " + mm.Relative;
}
return type + " pressed:" + isPressed + " released: " + isReleeased + " " + details;
}
2025-07-25 08:13:35 +00:00
public bool consumed => _consumed;
public void Consume()
{
_consumed = true;
}
2025-01-03 12:09:23 +00:00
protected virtual void UpdateValue()
{
}
protected void SetBoolValue( bool activeState )
{
_active = activeState;
_value = activeState ? 1 : 0;
2025-07-25 08:13:35 +00:00
_consumed = false;
2025-01-03 12:09:23 +00:00
}
protected void SetFloatValue( float value )
{
_value = value;
_active = value > _activeTreshold;
2025-07-25 08:13:35 +00:00
_consumed = false;
2025-01-03 12:09:23 +00:00
}
public virtual List<InputIcon> GetInputIcons()
{
return new List<InputIcon>();
}
protected void UpdateSensorUsage( int index = 0 )
{
var sm = Unique<SensorManager>.Get();
2025-03-13 16:19:28 +00:00
if ( sm == null )
{
return;
}
sm.UpdateLastActiveDevice( this, index );
}
2025-01-03 12:09:23 +00:00
}
}