Separated events from variables and adjusted test scene

This commit is contained in:
2025-10-28 11:57:36 +01:00
parent 080ebaae47
commit c96e6da78e
16 changed files with 195 additions and 136 deletions
@@ -23,27 +23,6 @@ public partial class EventListener : Node
/// </summary>
[Signal] public delegate void EventRaisedEventHandler();
/// <summary>
/// The signal that is triggered when this listener is called by one of the <see cref="EventListener._eventResources"/>.
/// </summary>
[Signal] public delegate void EventRaisedWithPayloadEventHandler(Variant payload);
/// <summary>
/// A signal that is triggered when the payload of one of the <see cref="EventListener._eventResources"/> changed.
/// </summary>
[Signal] public delegate void PayloadChangedEventHandler(Variant payload, Variant oldPayload);
/// <summary>
/// A signal that is triggered when the payload of one of the <see cref="EventListener._eventResources"/> changed.
/// </summary>
[Signal] public delegate void NewEventPayloadEventHandler(Variant payload);
/// <summary>
/// A signal that is triggered when the payload of one of the <see cref="EventListener._eventResources"/> changed.
/// </summary>
[Signal] public delegate void OldEventPayloadEventHandler(Variant oldPayload);
/// <summary>
/// Subscribes to all <see cref="EventResource"/>s present in the <see cref="_eventResources"/> array.
/// <inheritdoc cref="Node._EnterTree"/>
@@ -72,24 +51,12 @@ public partial class EventListener : Node
/// 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 EventInvoked(Variant payload)
public void EventInvoked()
{
if(_showLog)
GD.Print("Event Raised on: " + Name);
EmitSignal(SignalName.EventRaised);
EmitSignal(SignalName.EventRaisedWithPayload, payload);
}
/// <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 EventPayloadChanged(Variant payload, Variant oldPayload)
{
if(_showLog)
GD.Print($"Calling Event Payload Changed Signals on: " + Name);
EmitSignal(SignalName.PayloadChanged, payload, oldPayload);
EmitSignal(SignalName.NewEventPayload, payload);
EmitSignal(SignalName.OldEventPayload, oldPayload);
}
}
@@ -14,24 +14,6 @@ public partial class EventResource : Resource
/// Log into console when this event resource is adding or removing listeners, and when it's raised.
/// </summary>
[Export] private bool _showLog;
[Export]
public Variant Payload
{
get { return _payload; }
set
{
if (!_payload.Equals(value))
{
_lastPayload = _payload;
_payload = value;
ValueChangeHandler();
}
}
}
private Variant _payload;
private Variant _lastPayload;
/// <summary>
/// Raise-Button Call with Editor Export for easier debugging.
@@ -74,22 +56,10 @@ public partial class EventResource : Resource
foreach (var eventListener in _eventListeners)
{
eventListener.EventInvoked(_payload);
eventListener.EventInvoked();
}
}
/// <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 _eventListeners)
{
eventListener.EventPayloadChanged(_payload, _lastPayload);
}
}
}
@@ -1,5 +1,6 @@
using System;
using Babushka.scripts.CSharp.Low_Code.Events;
using Babushka.scripts.CSharp.Low_Code.Variables;
using Godot;
@@ -13,23 +14,23 @@ public partial class VariantRandomizer : Node
/// <summary>
/// The event resource to work on.
/// </summary>
[Export] public EventResource _eventResource;
[Export] public VariableResource _variableResource;
/// <summary>
/// Sets the payload of a randomizable event resource to a random value.
/// </summary>
public void RandomizeEventResource()
{
switch (_eventResource.Payload.VariantType)
switch (_variableResource.Payload.VariantType)
{
case Variant.Type.Color:
_eventResource.Payload= GetRandomColor();
_variableResource.Payload= GetRandomColor();
break;
case Variant.Type.Int:
_eventResource.Payload= GetRandomInt();
_variableResource.Payload= GetRandomInt();
break;
case Variant.Type.Float:
_eventResource.Payload= GetRandomFloat();
_variableResource.Payload= GetRandomFloat();
break;
}
}
@@ -0,0 +1,69 @@
using Godot;
using Godot.Collections;
namespace Babushka.scripts.CSharp.Low_Code.Variables;
public partial class VariableListener : Node
{
/// <summary>
/// The event resource to listen to.
/// </summary>
[Export] private Array<VariableResource> _variableResources;
/// <summary>
/// Log to console when this event is being raised.
/// </summary>
[Export] private bool _showLog;
/// <summary>
/// A signal that is triggered when the payload of one of the <see cref="EventListener._eventResources"/> changed.
/// </summary>
[Signal] public delegate void PayloadChangedEventHandler(Variant payload, Variant oldPayload);
/// <summary>
/// A signal that is triggered when the payload of one of the <see cref="EventListener._eventResources"/> changed.
/// </summary>
[Signal] public delegate void NewEventPayloadEventHandler(Variant payload);
/// <summary>
/// A signal that is triggered when the payload of one of the <see cref="EventListener._eventResources"/> changed.
/// </summary>
[Signal] public delegate void OldEventPayloadEventHandler(Variant oldPayload);
/// <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 resource in _variableResources)
{
resource.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 variableResource in _variableResources)
{
variableResource.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 EventPayloadChanged(Variant payload, Variant oldPayload)
{
if(_showLog)
GD.Print($"Calling Event Payload Changed Signals on: " + Name);
EmitSignal(SignalName.PayloadChanged, payload, oldPayload);
EmitSignal(SignalName.NewEventPayload, payload);
EmitSignal(SignalName.OldEventPayload, oldPayload);
}
}
@@ -0,0 +1 @@
uid://pqemey80frcq
@@ -0,0 +1,69 @@
using System.Collections.Generic;
using Godot;
namespace Babushka.scripts.CSharp.Low_Code.Variables;
[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;
[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);
}
}
}
@@ -0,0 +1 @@
uid://dtvx2cakx0bey