parent
1e004b62b8
commit
a593be8273
@ -1,10 +1,8 @@
|
|||||||
[gd_resource type="Resource" script_class="EventResource" load_steps=2 format=3 uid="uid://beed6ikqf46vk"]
|
[gd_resource type="Resource" script_class="EventResource" load_steps=2 format=3 uid="uid://bgfxakxxfmoxs"]
|
||||||
|
|
||||||
[ext_resource type="Script" uid="uid://ci3t5mvnopntg" path="res://scripts/CSharp/Events/EventResource.cs" id="1_6u8ge"]
|
[ext_resource type="Script" uid="uid://ci3t5mvnopntg" path="res://scripts/CSharp/Low Code/Events/EventResource.cs" id="1_jm77s"]
|
||||||
|
|
||||||
[resource]
|
[resource]
|
||||||
resource_name = "kafjhkj"
|
script = ExtResource("1_jm77s")
|
||||||
script = ExtResource("1_6u8ge")
|
|
||||||
Name = "kafjhkj"
|
|
||||||
_showLog = true
|
_showLog = true
|
||||||
metadata/_custom_type_script = "uid://ci3t5mvnopntg"
|
metadata/_custom_type_script = "uid://ci3t5mvnopntg"
|
||||||
@ -1,13 +0,0 @@
|
|||||||
using Godot;
|
|
||||||
|
|
||||||
namespace Babushka.scripts.CSharp.Events;
|
|
||||||
|
|
||||||
public partial class EventCaller : Node
|
|
||||||
{
|
|
||||||
[Export] EventResource eventResource;
|
|
||||||
|
|
||||||
public void RaiseEvent()
|
|
||||||
{
|
|
||||||
eventResource.Raise();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,28 +0,0 @@
|
|||||||
using Godot;
|
|
||||||
|
|
||||||
namespace Babushka.scripts.CSharp.Events;
|
|
||||||
|
|
||||||
public partial class EventListener : Node
|
|
||||||
{
|
|
||||||
[Export] private EventResource _eventResource;
|
|
||||||
[Export] private bool _showLog;
|
|
||||||
|
|
||||||
[Signal] public delegate void EventRaisedEventHandler();
|
|
||||||
|
|
||||||
public override void _EnterTree()
|
|
||||||
{
|
|
||||||
_eventResource.RegisterListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void _ExitTree()
|
|
||||||
{
|
|
||||||
_eventResource.UnregisterListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RaiseEvent()
|
|
||||||
{
|
|
||||||
if(_showLog)
|
|
||||||
GD.Print("Event Raised on: " + Name);
|
|
||||||
EmitSignal(SignalName.EventRaised);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,44 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using Godot;
|
|
||||||
|
|
||||||
namespace Babushka.scripts.CSharp.Events;
|
|
||||||
|
|
||||||
[GlobalClass] [Tool]
|
|
||||||
public partial class EventResource : Resource
|
|
||||||
{
|
|
||||||
[Export] private string Name
|
|
||||||
{
|
|
||||||
get { return ResourceName; }
|
|
||||||
set { ResourceName = value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
[Export] private bool _showLog;
|
|
||||||
[ExportToolButton("Raise")] Callable _raiseAction => Callable.From(Raise);
|
|
||||||
|
|
||||||
private List<EventListener> _eventListeners = new ();
|
|
||||||
|
|
||||||
public void RegisterListener(EventListener listener)
|
|
||||||
{
|
|
||||||
if(_showLog)
|
|
||||||
GD.Print("Registering listener " + listener);
|
|
||||||
_eventListeners.Add(listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UnregisterListener(EventListener listener)
|
|
||||||
{
|
|
||||||
if(_showLog)
|
|
||||||
GD.Print("Unregistering listener " + listener);
|
|
||||||
_eventListeners.Remove(listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Raise()
|
|
||||||
{
|
|
||||||
if(_showLog)
|
|
||||||
GD.Print("Raising event: " + Name);
|
|
||||||
|
|
||||||
foreach (var eventListener in _eventListeners)
|
|
||||||
{
|
|
||||||
eventListener.RaiseEvent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,62 @@
|
|||||||
|
using System;
|
||||||
|
using Godot;
|
||||||
|
using Godot.Collections;
|
||||||
|
|
||||||
|
namespace Babushka.scripts.CSharp.Low_Code.Events;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Listens to an <see cref="EventResource"/> as long as this node is in the tree (i.e., during playtime).
|
||||||
|
/// </summary>
|
||||||
|
public partial class EventListener : Node
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The event resource to listen to.
|
||||||
|
/// </summary>
|
||||||
|
[Export] private Array<EventResource> _eventResources;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Log to console when this event is being raised.
|
||||||
|
/// </summary>
|
||||||
|
[Export] private bool _showLog;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The signal that is triggered when this listener is called by one of the <see cref="EventListener._eventResources"/>.
|
||||||
|
/// </summary>
|
||||||
|
[Signal] public delegate void EventRaisedEventHandler();
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Subscribes to all <see cref="EventResource"/>s present in the <see cref="_eventResources"/> array.
|
||||||
|
/// <inheritdoc cref="Node._EnterTree"/>
|
||||||
|
/// </summary>
|
||||||
|
public override void _EnterTree()
|
||||||
|
{
|
||||||
|
foreach (var eventRes in _eventResources)
|
||||||
|
{
|
||||||
|
eventRes.RegisterListener(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unsubscribes from all <see cref="EventResource"/>s present in the <see cref="_eventResources"/> array.
|
||||||
|
/// <inheritdoc cref="Node._ExitTree"/>
|
||||||
|
/// </summary>
|
||||||
|
public override void _ExitTree()
|
||||||
|
{
|
||||||
|
foreach (var eventRes in _eventResources)
|
||||||
|
{
|
||||||
|
eventRes.UnregisterListener(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called by a <see cref="EventResource"/>s from the <see cref="_eventResources"/> array.
|
||||||
|
/// Propagates the event by emitting <see cref="EventRaised"/> signal.
|
||||||
|
/// </summary>
|
||||||
|
public void Invoke()
|
||||||
|
{
|
||||||
|
if(_showLog)
|
||||||
|
GD.Print("Event Raised on: " + Name);
|
||||||
|
EmitSignal(SignalName.EventRaised);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
using Godot;
|
||||||
|
using Godot.Collections;
|
||||||
|
|
||||||
|
namespace Babushka.scripts.CSharp.Low_Code.Events;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Raises one or more <see cref="EventResource"/>s from the scene.
|
||||||
|
/// </summary>
|
||||||
|
public partial class EventRaiser : Node
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The <see cref="EventResource"/>s to call
|
||||||
|
/// </summary>
|
||||||
|
[Export] Array<EventResource> _eventResources;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Raises all <see cref="EventResource"/>s present in <see cref="_eventResources"/>.
|
||||||
|
/// </summary>
|
||||||
|
public void RaiseEvents()
|
||||||
|
{
|
||||||
|
foreach (var eventRes in _eventResources)
|
||||||
|
{
|
||||||
|
eventRes.Raise();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Raises <see cref="EventResource"/> at index.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="index"></param>
|
||||||
|
public void RaiseEvent(int index)
|
||||||
|
{
|
||||||
|
_eventResources[index].Raise();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,62 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue