Added Event Pipeline System
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines
|
||||
{
|
||||
public class EventBus
|
||||
{
|
||||
private Dictionary<FirebirdEvent, I_Pipeline> pipelines = new();
|
||||
|
||||
private Dictionary<FirebirdEvent, List<Action<I_Pipeline>>> pendingHooks = new();
|
||||
|
||||
public void OnPipelineExists<T>(Action<I_Pipeline> callback, FirebirdEvent type)
|
||||
{
|
||||
if (callback == null) throw new ArgumentNullException(nameof(callback));
|
||||
|
||||
if (!pendingHooks.TryGetValue(type, out var hookList))
|
||||
{
|
||||
hookList = new();
|
||||
pendingHooks[type] = hookList;
|
||||
}
|
||||
|
||||
hookList.Add(callback);
|
||||
}
|
||||
|
||||
public I_Pipeline GetOrBuildPipeline(FirebirdEvent type, Type argsType, object master)
|
||||
{
|
||||
if (pipelines.TryGetValue(type, out var pipeline)) return pipeline;
|
||||
|
||||
var genericPipelineType = typeof(Pipeline<>).MakeGenericType(argsType);
|
||||
var pipelineInstance = (I_Pipeline)Activator.CreateInstance(genericPipelineType, master)!;
|
||||
pipelines[type] = pipelineInstance;
|
||||
return pipelineInstance;
|
||||
}
|
||||
|
||||
public I_Pipeline? GetPipeline(FirebirdEvent type, Type argsType)
|
||||
{
|
||||
if (!pipelines.TryGetValue(type, out var pipeline)) return null;
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
public void DestroyPipeline(FirebirdEvent type, object master)
|
||||
{
|
||||
if (!pipelines.ContainsKey(type)) return;
|
||||
if (!pipelines[type].PipelineMaster.Equals(master)) throw new InvalidDataException("Only the pipeline master is allowed to delete this pipeline.");
|
||||
pipelines.Remove(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SFML.Net" Version="2.6.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="EventArguments\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,441 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines
|
||||
{
|
||||
public enum FirebirdEvent
|
||||
{
|
||||
A = 0,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The B key
|
||||
B = 1,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The C key
|
||||
C = 2,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The D key
|
||||
D = 3,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The E key
|
||||
E = 4,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F key
|
||||
F = 5,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The G key
|
||||
G = 6,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The H key
|
||||
H = 7,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The I key
|
||||
I = 8,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The J key
|
||||
J = 9,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The K key
|
||||
K = 10,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The L key
|
||||
L = 11,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The M key
|
||||
M = 12,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The N key
|
||||
N = 13,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The O key
|
||||
O = 14,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The P key
|
||||
P = 15,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Q key
|
||||
Q = 16,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The R key
|
||||
R = 17,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The S key
|
||||
S = 18,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The T key
|
||||
T = 19,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The U key
|
||||
U = 20,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The V key
|
||||
V = 21,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The W key
|
||||
W = 22,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The X key
|
||||
X = 23,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Y key
|
||||
Y = 24,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Z key
|
||||
Z = 25,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The 0 key
|
||||
Num0 = 26,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The 1 key
|
||||
Num1 = 27,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The 2 key
|
||||
Num2 = 28,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The 3 key
|
||||
Num3 = 29,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The 4 key
|
||||
Num4 = 30,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The 5 key
|
||||
Num5 = 31,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The 6 key
|
||||
Num6 = 32,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The 7 key
|
||||
Num7 = 33,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The 8 key
|
||||
Num8 = 34,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The 9 key
|
||||
Num9 = 35,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Escape key
|
||||
Escape = 36,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The left Control key
|
||||
LControl = 37,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The left Shift key
|
||||
LShift = 38,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The left Alt key
|
||||
LAlt = 39,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The left OS specific key: window (Windows and Linux), apple (macOS), ...
|
||||
LSystem = 40,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The right Control key
|
||||
RControl = 41,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The right Shift key
|
||||
RShift = 42,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The right Alt key
|
||||
RAlt = 43,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The right OS specific key: window (Windows and Linux), apple (macOS), ...
|
||||
RSystem = 44,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Menu key
|
||||
Menu = 45,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The [ key
|
||||
LBracket = 46,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The ] key
|
||||
RBracket = 47,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The ; key
|
||||
Semicolon = 48,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The , key
|
||||
Comma = 49,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The . key
|
||||
Period = 50,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The ' key
|
||||
Apostrophe = 51,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The / key
|
||||
Slash = 52,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The \ key
|
||||
Backslash = 53,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The ~ key
|
||||
Grave = 54,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The = key
|
||||
Equal = 55,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The - key
|
||||
Hyphen = 56,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Space key
|
||||
Space = 57,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Return key
|
||||
Enter = 58,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Backspace key
|
||||
Backspace = 59,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Tabulation key
|
||||
Tab = 60,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Page up key
|
||||
PageUp = 61,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Page down key
|
||||
PageDown = 62,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The End key
|
||||
End = 63,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Home key
|
||||
Home = 64,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Insert key
|
||||
Insert = 65,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Delete key
|
||||
Delete = 66,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The + key
|
||||
Add = 67,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The - key
|
||||
Subtract = 68,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The * key
|
||||
Multiply = 69,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The / key
|
||||
Divide = 70,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// Left arrow
|
||||
Left = 71,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// Right arrow
|
||||
Right = 72,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// Up arrow
|
||||
Up = 73,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// Down arrow
|
||||
Down = 74,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The numpad 0 key
|
||||
Numpad0 = 75,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The numpad 1 key
|
||||
Numpad1 = 76,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The numpad 2 key
|
||||
Numpad2 = 77,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The numpad 3 key
|
||||
Numpad3 = 78,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The numpad 4 key
|
||||
Numpad4 = 79,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The numpad 5 key
|
||||
Numpad5 = 80,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The numpad 6 key
|
||||
Numpad6 = 81,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The numpad 7 key
|
||||
Numpad7 = 82,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The numpad 8 key
|
||||
Numpad8 = 83,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The numpad 9 key
|
||||
Numpad9 = 84,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F1 key
|
||||
F1 = 85,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F2 key
|
||||
F2 = 86,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F3 key
|
||||
F3 = 87,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F4 key
|
||||
F4 = 88,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F5 key
|
||||
F5 = 89,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F6 key
|
||||
F6 = 90,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F7 key
|
||||
F7 = 91,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F8 key
|
||||
F8 = 92,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F9 key
|
||||
F9 = 93,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F10 key
|
||||
F10 = 94,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F11 key
|
||||
F11 = 95,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F12 key
|
||||
F12 = 96,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F13 key
|
||||
F13 = 97,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F14 key
|
||||
F14 = 98,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The F15 key
|
||||
F15 = 99,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The Pause key
|
||||
Pause = 100,
|
||||
|
||||
MouseLeft = 101,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The right mouse button
|
||||
MouseRight = 102,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The middle (wheel) mouse button
|
||||
MouseMiddle = 103,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The first extra mouse button
|
||||
MouseXButton1 = 104,
|
||||
//
|
||||
// Zusammenfassung:
|
||||
// The second extra mouse button
|
||||
MouseXButton2 = 105,
|
||||
JoystickButton,
|
||||
JoystickMove,
|
||||
Tick,
|
||||
Startup,
|
||||
ModulesLoading,
|
||||
SceneLoading,
|
||||
FullyLoaded,
|
||||
SceneLoaded,
|
||||
MouseWheel,
|
||||
MouseMove
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines
|
||||
{
|
||||
public interface I_Pipeline
|
||||
{
|
||||
object PipelineMaster { get; }
|
||||
bool IsEventEmpty { get; }
|
||||
void Subscribe(Delegate handler);
|
||||
void Unsubscribe(Delegate handler);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines
|
||||
{
|
||||
public class Pipeline <T> : I_Pipeline where T : EventArgs
|
||||
{
|
||||
public object PipelineMaster { get; }
|
||||
|
||||
private event EventHandler<T>? OnEvent;
|
||||
|
||||
public Pipeline(object pipelineMaster)
|
||||
{
|
||||
PipelineMaster = pipelineMaster;
|
||||
}
|
||||
|
||||
public bool IsEventEmpty { get { return OnEvent != null; } }
|
||||
|
||||
public void Subscribe(Delegate handler)
|
||||
{
|
||||
OnEvent += (EventHandler<T>)handler;
|
||||
}
|
||||
|
||||
public void Unsubscribe(Delegate handler)
|
||||
{
|
||||
OnEvent -= (EventHandler<T>)handler;
|
||||
}
|
||||
|
||||
public void SendEvent(object sender, T args)
|
||||
{
|
||||
if (sender == null || args == null) throw new ArgumentNullException(nameof(sender));
|
||||
if (args is not T) throw new ArgumentException($"Invalid EventArgs type. Expected {typeof(T)}");
|
||||
if (sender.Equals(PipelineMaster)) throw new InvalidDataException("Only the pipeline master is allowed to send events through this pipeline.");
|
||||
|
||||
OnEvent?.Invoke(sender, args);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user