rokojori_action_library/Runtime/Actions/Values/NumberValue.cs

135 lines
2.0 KiB
C#

using Godot;
using Rokojori.Extensions;
namespace Rokojori;
[Tool][GlobalClass]
public partial class NumberValue : Node
{
protected double _value = 0;
protected bool _frozen;
[Export]
public double value
{
get => _value;
set { _OnValueChange( value ); }
}
public override string ToString()
{
return GetType().Name + " { value = " + _value._DDD() + ", frozen = " + _frozen + " }";
}
protected virtual void _OnValueChange( double v )
{
if ( _frozen )
{
return;
}
var setterValue = Mathf.Clamp( v, minValue, maxValue );
if ( _value == setterValue )
{
return;
}
var before = _value;
_value = setterValue;
var belowLowTreshold = before < lowTreshold;
var aboveHighTreshold = before > highTreshold;
onChange?.Trigger();
if ( ! belowLowTreshold && _value < lowTreshold )
{
onBelowLowTreshold?.Trigger();
}
if ( ! aboveHighTreshold && _value > highTreshold )
{
onAboveHighTreshold?.Trigger();
}
if ( _value == minValue )
{
onMin?.Trigger();
if ( autoFreezeOnMin )
{
_frozen = true;
}
}
if ( _value == maxValue )
{
onMax?.Trigger();
}
}
public void FreezeValue()
{
this._frozen = true;
}
public void UnfreezeValue()
{
this._frozen = false;
}
public void ResetValue()
{
this._frozen = false;
this._value = resetValue;
}
[Export]
public double resetValue = 100;
[Export]
public double minValue = 0;
[Export]
public double maxValue = 125;
[Export]
public double lowTreshold = 25;
[Export]
public double highTreshold = 75;
[Export]
public bool autoFreezeOnMin = true;
[Export]
public Action onChange;
[Export]
public Action onIncrement;
[Export]
public Action onDecrement;
[Export]
public Action onMin;
[Export]
public Action onMax;
[Export]
public Action onBelowLowTreshold;
[Export]
public Action onAboveHighTreshold;
}