Otter  0.9.88
2d Game Development Framework based on SFML.Net
 All Classes Namespaces Functions Variables Enumerations Properties
EventRouter Class Reference

Class for broadcasting messages to subscribers. Clients of the EventRouter register themselves by calling Subscribe and passing in the even they are interested in along with a delegate to be called back when the event is received. Events are published via the Publish method which allows arbitrary data to be passed along with the event to be interpreted by the event receiver. Events do not have to be pre-defined. More...

Classes

class  Event
 Event data class, passed in to the subscriber whenever an event occours. More...
 

Public Member Functions

delegate void Handler (Event e)
 

Static Public Member Functions

static void Subscribe (Enum evt, Handler h, object owner=null)
 Subscribe to the event specified by evt. Pass in a delegate to be called back when the even occurs. More...
 
static void Subscribe (string id, Enum evt, Handler h, object owner=null)
 Subscribe to the event specified by evt filtered by the specified id. Pass in a delegate to be called back when the even occurs. More...
 
static void Subscribe (string id, string evt, Handler h, object owner=null)
 Subscribe to the event specified by evt. Pass in a delegate to be called back when the even occurs. More...
 
static void Subscribe (string evt, Handler h, object owner=null)
 Subscribe to the event specified by evt. Pass in a delegate to be called back when the even occurs. More...
 
static void Unsubscribe (string id, Enum evt, Handler h)
 Unsubscribe the specified delegate from the event. More...
 
static void Unsubscribe (Enum evt, Handler h)
 Unsubscribe the specified delegate from the event. More...
 
static void Unsubscribe (string evt, Handler h)
 Unsubscribe the specified delegate from the event. More...
 
static void Unsubscribe (string id, string evt, Handler h)
 Unsubscribe the specified delegate from the event. More...
 
static void Unsubscribe (object owner)
 Unsubscribe to all events that were subscribed to by a specific owner object. More...
 
static void Publish (string id, string evt, params object[] data)
 Publish the specified event with extra data. More...
 
static void Publish (string evt, params object[] data)
 Publish the specified event with extra data. More...
 
static void Publish (string id, Enum evt, params object[] data)
 Publish the specified event with extra data. More...
 
static void Publish (Enum evt, params object[] data)
 Publish the specified event with extra data. More...
 
static void Queue (string id, string evt, params object[] data)
 
static void Queue (string evt, params object[] data)
 
static void Queue (string id, Enum evt, params object[] data)
 
static void Queue (Enum evt, params object[] data)
 
static void Clear ()
 Clear all event subscribers, used primarily when switching or resetting a level. More...
 

Static Public Attributes

static bool LogEvents = false
 

Detailed Description

Class for broadcasting messages to subscribers. Clients of the EventRouter register themselves by calling Subscribe and passing in the even they are interested in along with a delegate to be called back when the event is received. Events are published via the Publish method which allows arbitrary data to be passed along with the event to be interpreted by the event receiver. Events do not have to be pre-defined.

Publish and Subscribe methods also have an optional id parameter which will filter the events
sent to just those that match the provided id.  Subscribing to an event with no id will result 
in receiving all events of the specified type.

Example of a simple publish and subscribe class, not using any id filtering.

Sender.cs

public enum SenderEvent { Test }

public class Sender { public void Send() { EventRouter.Publish(SenderEvent.Test, "Hello World"); } }

Receiver.cs

public class Receiver { public Receiver() { EventRouter.Subscribe(SenderEvent.Test, OnSenderEvent); }

void OnSenderEvent(EventRouter.Event evt) { if(evt.HasData) { Console.WriteLine("Received event: " + evt.Type + " with data: " + evt.GetData<string>(0)); } else { Console.WriteLine("Received event: " + evt.Type + " with no data"); } } }

Example of a publish and subscribe using id's to filter specific messages.

Sender.cs

public enum SenderEvent { Test }

public class Sender { public void SendA() { EventRouter.Publish("A", SenderEvent.Test); }

public void SendB() { EventRouter.Publish("B", SenderEvent.Test); } }

Receiver.cs

public class Receiver { public Receiver() { EventRouter.Subscribe("A", SenderEvent.Test, OnSenderEventA); EventRouter.Subscribe("B", SenderEvent.Test, OnSenderEventB); }

void OnSenderEventA(EventRouter.Event evt) { Console.WriteLine("Received SenderEvent.Test with id 'A'"); }

void OnSenderEventB(EventRouter.Event evt) { Console.WriteLine("Received SenderEvent.Test with id 'B'"); } }

Member Function Documentation

static void EventRouter.Clear ( )
static

Clear all event subscribers, used primarily when switching or resetting a level.

static void EventRouter.Publish ( string  id,
string  evt,
params object[]  data 
)
static

Publish the specified event with extra data.

Parameters
idThe string representing the object.
Parameters
evtThe string representing the event.
dataAn arbitrary params array of objects to be interpreted by the receiver of the event.
static void EventRouter.Publish ( string  evt,
params object[]  data 
)
static

Publish the specified event with extra data.

Parameters
evtThe string representing the event.
dataAn arbitrary params array of objects to be interpreted by the receiver of the event.
static void EventRouter.Publish ( string  id,
Enum  evt,
params object[]  data 
)
static

Publish the specified event with extra data.

Parameters
idThe string representing the object.
Parameters
evtThe event enumeration value.
dataAn arbitrary params array of objects to be interpreted by the receiver of the event.
static void EventRouter.Publish ( Enum  evt,
params object[]  data 
)
static

Publish the specified event with extra data.

Parameters
evtThe event enumeration value.
dataAn arbitrary params array of objects to be interpreted by the receiver of the event.
static void EventRouter.Subscribe ( Enum  evt,
Handler  h,
object  owner = null 
)
static

Subscribe to the event specified by evt. Pass in a delegate to be called back when the even occurs.

Parameters
evtThe event enumeration value.
hThe delegate to be called when the even occurs.
static void EventRouter.Subscribe ( string  id,
Enum  evt,
Handler  h,
object  owner = null 
)
static

Subscribe to the event specified by evt filtered by the specified id. Pass in a delegate to be called back when the even occurs.

Parameters
idThe id of the event
evtThe event enumeration value.
hThe delegate to be called when the even occurs.
static void EventRouter.Subscribe ( string  id,
string  evt,
Handler  h,
object  owner = null 
)
static

Subscribe to the event specified by evt. Pass in a delegate to be called back when the even occurs.

Parameters
idThe string representing the id.
evtThe string representing the event.
hThe delegate to be called when the even occurs.
static void EventRouter.Subscribe ( string  evt,
Handler  h,
object  owner = null 
)
static

Subscribe to the event specified by evt. Pass in a delegate to be called back when the even occurs.

Parameters
evtThe string representing the event.
hThe delegate to be called when the even occurs.
static void EventRouter.Unsubscribe ( string  id,
Enum  evt,
Handler  h 
)
static

Unsubscribe the specified delegate from the event.

Parameters
idThe id of the event
evtThe event enumeration value.
hThe delegate to be removed from the event handlers.
static void EventRouter.Unsubscribe ( Enum  evt,
Handler  h 
)
static

Unsubscribe the specified delegate from the event.

Parameters
evtThe event enumeration value.
hThe delegate to be removed from the event handlers.
static void EventRouter.Unsubscribe ( string  evt,
Handler  h 
)
static

Unsubscribe the specified delegate from the event.

Parameters
evtThe string representing the event.
hThe delegate to be removed from the event handlers.
static void EventRouter.Unsubscribe ( string  id,
string  evt,
Handler  h 
)
static

Unsubscribe the specified delegate from the event.

Parameters
idThe string representing the id.
evtThe string representing the event.
hThe delegate to be removed from the event handlers.
static void EventRouter.Unsubscribe ( object  owner)
static

Unsubscribe to all events that were subscribed to by a specific owner object.

Parameters
ownerThe object that subscribed to the object.

The documentation for this class was generated from the following file: