Added Comments
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
using SFML.Window;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines.EventArguments
|
||||
{
|
||||
public class FirebirdJoystickButtonEventArgs : JoystickButtonEventArgs
|
||||
{
|
||||
public InputEventType InputEventType { get; private set; }
|
||||
|
||||
public FirebirdJoystickButtonEventArgs(JoystickButtonEvent e, InputEventType t) : base(e)
|
||||
{
|
||||
InputEventType = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using SFML.Window;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines.EventArguments
|
||||
{
|
||||
public class FirebirdKeyEventArgs : KeyEventArgs
|
||||
{
|
||||
public InputEventType InputEventType { get; private set; }
|
||||
|
||||
public FirebirdKeyEventArgs(KeyEvent e, InputEventType t) : base(e)
|
||||
{
|
||||
InputEventType = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using SFML.Window;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines.EventArguments
|
||||
{
|
||||
public class FirebirdMouseButtonEventArgs : MouseButtonEventArgs
|
||||
{
|
||||
public InputEventType InputEventType { get; private set; }
|
||||
|
||||
public FirebirdMouseButtonEventArgs(MouseButtonEvent e,InputEventType t) : base(e)
|
||||
{
|
||||
InputEventType = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines.EventArguments
|
||||
{
|
||||
public enum InputEventType
|
||||
{
|
||||
Pressed = 0,
|
||||
Released = 1
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,26 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines
|
||||
{
|
||||
/// <summary>
|
||||
/// EventBus manages event pipelines and allows registering callbacks (hooks) if a pipeline is not Build Yet, as well as creating, retrieving, and destroying pipelines.
|
||||
/// </summary>
|
||||
public class EventBus
|
||||
{
|
||||
// Dictionary that stores various pipelines for specific FirebirdEvents.
|
||||
// The key is the event, and the value is the corresponding pipeline.
|
||||
private Dictionary<FirebirdEvent, I_Pipeline> pipelines = new();
|
||||
|
||||
// Dictionary that stores a list of callbacks for each FirebirdEvent.
|
||||
// These callbacks will be invoked when the event's pipeline is build.
|
||||
private Dictionary<FirebirdEvent, List<Action<I_Pipeline>>> pendingHooks = new();
|
||||
|
||||
public void OnPipelineExists<T>(Action<I_Pipeline> callback, FirebirdEvent type)
|
||||
/// <summary>
|
||||
/// Registers a callback (hook) for a specific event. The callback will be invoked when the pipeline for this event is built.
|
||||
/// </summary>
|
||||
/// <param name="callback">The function to be called when the pipeline for the event is built.</param>
|
||||
/// <param name="type">The event for which the callback should be registered.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown when the callback is null.</exception>
|
||||
public void OnPipelineIsBuild(Action<I_Pipeline> callback, FirebirdEvent type)
|
||||
{
|
||||
if (callback == null) throw new ArgumentNullException(nameof(callback));
|
||||
|
||||
@@ -25,6 +38,15 @@ namespace Firebird2D.EventPipelines
|
||||
hookList.Add(callback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to create or retrieve a pipeline for the given event.
|
||||
/// If the pipeline already exists, it is returned.
|
||||
/// Otherwise, a new pipeline is created and stored.
|
||||
/// </summary>
|
||||
/// <param name="type">The event for which the pipeline should be created or retrieved.</param>
|
||||
/// <param name="argsType">The type of the arguments to be passed to the pipeline.</param>
|
||||
/// <param name="master">The master object used when creating the pipeline.</param>
|
||||
/// <returns>The pipeline for the given event.</returns>
|
||||
public I_Pipeline GetOrBuildPipeline(FirebirdEvent type, Type argsType, object master)
|
||||
{
|
||||
if (pipelines.TryGetValue(type, out var pipeline)) return pipeline;
|
||||
@@ -35,12 +57,24 @@ namespace Firebird2D.EventPipelines
|
||||
return pipelineInstance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the pipeline for an event if it exists; otherwise, returns null.
|
||||
/// </summary>
|
||||
/// <param name="type">The event for which the pipeline should be retrieved.</param>
|
||||
/// <param name="argsType">The type of the arguments for the pipeline.</param>
|
||||
/// <returns>The pipeline for the event, or null if it does not exist.</returns>
|
||||
public I_Pipeline? GetPipeline(FirebirdEvent type, Type argsType)
|
||||
{
|
||||
if (!pipelines.TryGetValue(type, out var pipeline)) return null;
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destroys the pipeline for an event. Only the "master" of the pipeline is allowed to destroy it.
|
||||
/// </summary>
|
||||
/// <param name="type">The event for which the pipeline should be destroyed.</param>
|
||||
/// <param name="master">The master object that has permission to destroy the pipeline.</param>
|
||||
/// <exception cref="InvalidDataException">Thrown when a non-authorized user attempts to destroy the pipeline.</exception>
|
||||
public void DestroyPipeline(FirebirdEvent type, object master)
|
||||
{
|
||||
if (!pipelines.ContainsKey(type)) return;
|
||||
|
||||
@@ -10,8 +10,4 @@
|
||||
<PackageReference Include="SFML.Net" Version="2.6.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="EventArguments\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -6,11 +6,29 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for the Firebird event Pipeline
|
||||
/// </summary>
|
||||
public interface I_Pipeline
|
||||
{
|
||||
/// <summary>
|
||||
/// Class witch owns the Pipeline and has the right to send events or delete the pipeline
|
||||
/// </summary>
|
||||
object PipelineMaster { get; }
|
||||
/// <summary>
|
||||
/// Check if no one subscribed to the event
|
||||
/// </summary>
|
||||
bool IsEventEmpty { get; }
|
||||
/// <summary>
|
||||
/// Subscribe to this event Pipeline
|
||||
/// </summary>
|
||||
/// <param name="handler">Handler witch should be Invoked when the event is send</param>
|
||||
void Subscribe(Delegate handler);
|
||||
|
||||
/// <summary>
|
||||
/// Unsubscribe from this event Pipeline
|
||||
/// </summary>
|
||||
/// <param name="handler">Handler witch should be removed from the Pipeline</param>
|
||||
void Unsubscribe(Delegate handler);
|
||||
|
||||
}
|
||||
|
||||
@@ -8,29 +8,60 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines
|
||||
{
|
||||
/// <summary>
|
||||
/// Eventpypeline which transports an event to every Subscyber
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Event Arguments for the Pipeline event</typeparam>
|
||||
public class Pipeline <T> : I_Pipeline where T : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Class witch owns the Pipeline and has the right to send events or delete the pipeline
|
||||
/// </summary>
|
||||
public object PipelineMaster { get; }
|
||||
|
||||
//EventHandler to which the subscribers subribe.
|
||||
private event EventHandler<T>? OnEvent;
|
||||
|
||||
/// <summary>
|
||||
/// Pipeline Constructor
|
||||
/// </summary>
|
||||
/// <param name="pipelineMaster">Every pipeline needs a Master which is able to send events or Destroy the pipeline</param>
|
||||
public Pipeline(object pipelineMaster)
|
||||
{
|
||||
PipelineMaster = pipelineMaster;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if no one subscribed to the event
|
||||
/// </summary>
|
||||
public bool IsEventEmpty { get { return OnEvent != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Subscribe to this event Pipeline
|
||||
/// </summary>
|
||||
/// <param name="handler">Handler witch should be Invoked when the event is send</param>
|
||||
public void Subscribe(Delegate handler)
|
||||
{
|
||||
OnEvent += (EventHandler<T>)handler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unsubscribe from this event Pipeline
|
||||
/// </summary>
|
||||
/// <param name="handler">Handler witch should be removed from the Pipeline</param>
|
||||
public void Unsubscribe(Delegate handler)
|
||||
{
|
||||
OnEvent -= (EventHandler<T>)handler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method to send an event through the pipeline
|
||||
/// </summary>
|
||||
/// <param name="sender">Sneder object must be Pipelinemaster or the Pipeline throws an exception</param>
|
||||
/// <param name="args">Arguments that should be sent</param>
|
||||
/// <exception cref="ArgumentNullException">Exception when sender or arguments ar null</exception>
|
||||
/// <exception cref="ArgumentException">Exception wehn Arguments are not equal the Pipeline type</exception>
|
||||
/// <exception cref="InvalidDataException">Exception when sender is not Pipeline master</exception>
|
||||
public void SendEvent(object sender, T args)
|
||||
{
|
||||
if (sender == null || args == null) throw new ArgumentNullException(nameof(sender));
|
||||
|
||||
Reference in New Issue
Block a user