Re-worked event system

This commit is contained in:
Tristan B. Velloza Kildaire 2022-01-16 14:32:00 +02:00
parent f1a13623a6
commit f649a63a77
2 changed files with 38 additions and 14 deletions

View File

@ -1,7 +1,7 @@
module eventy.engine;
import eventy.queues : Queue;
import eventy.signal : Signal, EventHandler;
import eventy.signal : Signal;
import eventy.event : Event;
import std.container.dlist;
@ -26,10 +26,38 @@ unittest
engine.start();
/**
* Let the event engine know what typeIDs are
* allowed to be queued
*/
engine.addQueue(1);
engine.addQueue(2);
Signal j = new Signal([1,2], &runner);
/**
* Create a new Signal Handler that will handles
* event types `1` and `2` with the given `handler()`
* function
*/
class SignalHandler1 : Signal
{
this()
{
super([1,2]);
}
public override void handler(Event e)
{
import std.stdio;
writeln("Running event", e.id);
}
}
/**
* Tell the event engine that I want to register
* the following handler for its queues `1` and `2`
*/
Signal j = new SignalHandler1();
engine.addSignalHandler(j);
Event eTest = new Event(1);
@ -200,8 +228,8 @@ public final class Engine : Thread
public void worker()
{
EventHandler handler = signal.getHandler();
handler(e);
signal.handler(e);
//handler(e);
}
};

View File

@ -9,20 +9,19 @@ import eventy.event : Event;
* which means that it contains an associated function to be run
* on handling of a given Event
*/
alias EventHandler = void function(Event);
//alias EventHandler = void function(Event);
public class Signal
public abstract class Signal
{
/* TypeIDs this signal handler associates with */
private ulong[] typeIDs;
/* Signal handler */
private EventHandler handler;
//private EventHandler handler;
this(ulong[] typeIDs, EventHandler handler)
this(ulong[] typeIDs)
{
this.typeIDs = typeIDs;
this.handler = handler;
}
/**
@ -53,8 +52,5 @@ public class Signal
}
public EventHandler getHandler()
{
return handler;
}
}
public abstract void handler(Event);
}