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));
|
||||
|
||||
@@ -1,22 +1,28 @@
|
||||
using Firebird2D.EventPipelines;
|
||||
using SFML.System;
|
||||
using Firebird2D.EventPipelines.EventArguments;
|
||||
using Firebird2D.EventPipelines;
|
||||
using SFML.Window;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.Json;
|
||||
|
||||
|
||||
namespace Firebird2D.KeyMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps input events (keyboard, mouse, joystick) to custom Firebird events and manages their pipelines.
|
||||
/// </summary>
|
||||
public class KeyMapper
|
||||
{
|
||||
private EventBus eventBus;
|
||||
|
||||
// Stores registered input events by name.
|
||||
private Dictionary<string, Delegate> inputEvents = [];
|
||||
|
||||
// Stores the expected argument type for each input event.
|
||||
private Dictionary<string, Type> eventTypes = [];
|
||||
|
||||
// Maps event names to FirebirdEvent identifiers.
|
||||
private Dictionary<string, FirebirdEvent> inputMapping = [];
|
||||
|
||||
// Caches active pipelines for performance.
|
||||
private Dictionary<FirebirdEvent, I_Pipeline> cachedPipelines = [];
|
||||
|
||||
public KeyMapper(EventBus eventBus)
|
||||
@@ -25,31 +31,40 @@ namespace Firebird2D.KeyMapping
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers an input event with its handler and links it to a FirebirdEvent.
|
||||
/// </summary>
|
||||
public void RegisterEvent<T>(string EventName, EventHandler<T> handler, FirebirdEvent eventNumber) where T : EventArgs
|
||||
{
|
||||
if (inputEvents.ContainsKey(EventName)) throw new ArgumentException($"Event {EventName} exist Already.");
|
||||
if (inputEvents.ContainsKey(EventName))
|
||||
throw new ArgumentException($"Event {EventName} already exists.");
|
||||
|
||||
inputEvents.Add(EventName, handler);
|
||||
|
||||
eventTypes.Add(EventName, typeof(T));
|
||||
|
||||
Map(EventName, eventNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps an existing input event to a new FirebirdEvent.
|
||||
/// </summary>
|
||||
public void Map(string EventName, FirebirdEvent eventNumber)
|
||||
{
|
||||
if (inputMapping.ContainsKey(EventName))
|
||||
{
|
||||
RemoveMapping(EventName);
|
||||
inputMapping[EventName] = eventNumber;
|
||||
AjustPipeline(EventName, eventNumber);
|
||||
}
|
||||
else
|
||||
{
|
||||
inputMapping.Add(EventName, eventNumber);
|
||||
AjustPipeline(EventName, eventNumber);
|
||||
}
|
||||
|
||||
AjustPipeline(EventName, eventNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the current mapping and unsubscribes from the pipeline.
|
||||
/// </summary>
|
||||
private void RemoveMapping(string EventName)
|
||||
{
|
||||
if (!inputMapping.TryGetValue(EventName, out var firebirdEvent)) return;
|
||||
@@ -66,9 +81,13 @@ namespace Firebird2D.KeyMapping
|
||||
cachedPipelines.Remove(firebirdEvent);
|
||||
}
|
||||
}
|
||||
|
||||
inputMapping.Remove(EventName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates or adjusts a pipeline to reflect current mappings.
|
||||
/// </summary>
|
||||
private void AjustPipeline(string EventName, FirebirdEvent eventNumber)
|
||||
{
|
||||
if (!eventTypes.TryGetValue(EventName, out var type)) return;
|
||||
@@ -77,17 +96,23 @@ namespace Firebird2D.KeyMapping
|
||||
pipeline.Subscribe(inputEvents[EventName]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes and saves the current mapping to a file.
|
||||
/// </summary>
|
||||
public void SaveMapping(string Path)
|
||||
{
|
||||
string json = JsonSerializer.Serialize(inputMapping);
|
||||
File.WriteAllText(Path, json);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads and applies an input mapping from a JSON file.
|
||||
/// </summary>
|
||||
public void LoadMapping(string Path)
|
||||
{
|
||||
string json = File.ReadAllText(Path);
|
||||
Dictionary<string, FirebirdEvent>? keyValuePairs = JsonSerializer.Deserialize<Dictionary<string, FirebirdEvent>>(json);
|
||||
if (keyValuePairs == null) throw new FileLoadException("Unable to deserialize Inputmapping");
|
||||
if (keyValuePairs == null) throw new FileLoadException("Unable to deserialize input mapping.");
|
||||
|
||||
cachedPipelines.Clear();
|
||||
|
||||
@@ -107,12 +132,17 @@ namespace Firebird2D.KeyMapping
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a pipeline from the internal cache.
|
||||
/// </summary>
|
||||
public void InvalidatePipelineCache(FirebirdEvent fbEvent)
|
||||
{
|
||||
cachedPipelines.Remove(fbEvent);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a cached pipeline or fetches and caches it.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private Pipeline<T>? GetOrCachePipeline<T>(FirebirdEvent fbEvent) where T : EventArgs
|
||||
{
|
||||
@@ -127,31 +157,45 @@ namespace Firebird2D.KeyMapping
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
|
||||
public void HandleKeyboardInput(KeyEvent key)
|
||||
/// <summary>
|
||||
/// Handles a keyboard input and dispatches the event.
|
||||
/// </summary>
|
||||
public void HandleKeyboardInput(KeyEvent key, InputEventType type)
|
||||
{
|
||||
var pipeline = GetOrCachePipeline<KeyEventArgs>((FirebirdEvent)key.Code);
|
||||
pipeline?.SendEvent(this, new KeyEventArgs(key));
|
||||
var pipeline = GetOrCachePipeline<FirebirdKeyEventArgs>((FirebirdEvent)key.Code);
|
||||
pipeline?.SendEvent(this, new FirebirdKeyEventArgs(key, type));
|
||||
}
|
||||
|
||||
public void HandleMouseInput(MouseButtonEvent button)
|
||||
/// <summary>
|
||||
/// Handles a mouse button input and dispatches the event.
|
||||
/// </summary>
|
||||
public void HandleMouseInput(MouseButtonEvent button, InputEventType type)
|
||||
{
|
||||
var pipeline = GetOrCachePipeline<MouseButtonEventArgs>((FirebirdEvent)(button.Button + 101));
|
||||
pipeline?.SendEvent(this, new MouseButtonEventArgs(button));
|
||||
var pipeline = GetOrCachePipeline<FirebirdMouseButtonEventArgs>((FirebirdEvent)(button.Button + 101));
|
||||
pipeline?.SendEvent(this, new FirebirdMouseButtonEventArgs(button, type));
|
||||
}
|
||||
|
||||
public void HandleJoystickButtonInput(JoystickButtonEvent joystickButton)
|
||||
/// <summary>
|
||||
/// Handles a joystick button press and dispatches the event.
|
||||
/// </summary>
|
||||
public void HandleJoystickButtonInput(JoystickButtonEvent joystickButton, InputEventType type)
|
||||
{
|
||||
var pipeline = GetOrCachePipeline<JoystickButtonEventArgs>(FirebirdEvent.JoystickButton);
|
||||
pipeline?.SendEvent(this, new JoystickButtonEventArgs(joystickButton));
|
||||
var pipeline = GetOrCachePipeline<FirebirdJoystickButtonEventArgs>(FirebirdEvent.JoystickButton);
|
||||
pipeline?.SendEvent(this, new FirebirdJoystickButtonEventArgs(joystickButton, type));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles mouse movement and dispatches the event.
|
||||
/// </summary>
|
||||
public void HandleMouseMovement(MouseMoveEvent mouseMove)
|
||||
{
|
||||
var pipeline = GetOrCachePipeline<MouseMoveEventArgs>(FirebirdEvent.MouseMove);
|
||||
pipeline?.SendEvent(this, new MouseMoveEventArgs(mouseMove));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles joystick movement and dispatches the event.
|
||||
/// </summary>
|
||||
public void HandleJoytickMove(JoystickMoveEvent joystickMove)
|
||||
{
|
||||
var pipeline = GetOrCachePipeline<JoystickMoveEventArgs>(FirebirdEvent.JoystickMove);
|
||||
|
||||
Reference in New Issue
Block a user