You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.2 KiB
76 lines
2.2 KiB
using System.Collections.Generic;
|
|
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Low_Code.Variables;
|
|
|
|
/// <summary>
|
|
/// A <see cref="Variant"/> value wrapper resource used to store state values and notify ingame scripts.
|
|
/// </summary>
|
|
[GlobalClass]
|
|
public partial class VariableResource : Resource
|
|
{
|
|
/// <summary>
|
|
/// Log into console when this event resource is adding or removing listeners, and when it's raised.
|
|
/// </summary>
|
|
[Export] private bool _showLog;
|
|
|
|
/// <summary>
|
|
/// Public property that manages the access to the payload.
|
|
/// Triggers the ValueChange-function when set to a new value.
|
|
/// </summary>
|
|
[Export]
|
|
public Variant Payload
|
|
{
|
|
get { return _payload; }
|
|
set
|
|
{
|
|
if (!_payload.Equals(value))
|
|
{
|
|
_lastPayload = _payload;
|
|
_payload = value;
|
|
ValueChangeHandler();
|
|
}
|
|
}
|
|
}
|
|
|
|
private Variant _payload;
|
|
private Variant _lastPayload;
|
|
|
|
private List<VariableListener> _varListeners = new ();
|
|
|
|
/// <summary>
|
|
/// Adds an EventListener to the calling list for this event.
|
|
/// </summary>
|
|
/// <param name="listener"></param>
|
|
public void RegisterListener(VariableListener listener)
|
|
{
|
|
if(_showLog)
|
|
GD.Print("Registering listener " + listener);
|
|
_varListeners.Add(listener);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes an Eventlistener from the calling list for this event.
|
|
/// </summary>
|
|
/// <param name="listener"></param>
|
|
public void UnregisterListener(VariableListener listener)
|
|
{
|
|
if(_showLog)
|
|
GD.Print("Unregistering listener " + listener);
|
|
_varListeners.Remove(listener);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when the Payload value changed.
|
|
/// </summary>
|
|
public void ValueChangeHandler()
|
|
{
|
|
if(_showLog)
|
|
GD.Print($"Event payload changed from {_lastPayload} to {_payload} on event resource: " + ResourcePath.GetFile().TrimSuffix(".tres"));
|
|
|
|
foreach (var eventListener in _varListeners)
|
|
{
|
|
eventListener.EventPayloadChanged(_payload, _lastPayload);
|
|
}
|
|
}
|
|
} |