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.
62 lines
1.9 KiB
62 lines
1.9 KiB
using System.Collections.Generic;
|
|
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Low_Code.Events;
|
|
|
|
/// <summary>
|
|
/// Represents an event in the project space.
|
|
/// Can be called by <see cref="EventRaiser"/> and subscribed to by <see cref="EventListener"/>.
|
|
/// </summary>
|
|
[GlobalClass] [Tool]
|
|
public partial class EventResource : 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>
|
|
/// Raise-Button Call with Editor Export for easier debugging.
|
|
/// Beware: This will only work with custom event listeners that register at edit-time.
|
|
/// Standard <see cref="EventListeners"/> register and deregister during playtime.
|
|
/// </summary>
|
|
[ExportToolButton("Raise")] Callable _raiseAction => Callable.From(Raise);
|
|
|
|
private List<EventListener> _eventListeners = new ();
|
|
|
|
/// <summary>
|
|
/// Adds an EventListener to the calling list for this event.
|
|
/// </summary>
|
|
/// <param name="listener"></param>
|
|
public void RegisterListener(EventListener listener)
|
|
{
|
|
if(_showLog)
|
|
GD.Print("Registering listener " + listener);
|
|
_eventListeners.Add(listener);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes an Eventlistener from the calling list for this event.
|
|
/// </summary>
|
|
/// <param name="listener"></param>
|
|
public void UnregisterListener(EventListener listener)
|
|
{
|
|
if(_showLog)
|
|
GD.Print("Unregistering listener " + listener);
|
|
_eventListeners.Remove(listener);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises this event on all current listeners.
|
|
/// </summary>
|
|
public void Raise()
|
|
{
|
|
if(_showLog)
|
|
GD.Print("Raising event: " + ResourcePath.GetFile().TrimSuffix(".tres"));
|
|
|
|
foreach (var eventListener in _eventListeners)
|
|
{
|
|
eventListener.Invoke();
|
|
}
|
|
}
|
|
} |