1
//
2
using System;
3
using System.Collections.Generic;
4
using System.ComponentModel;
5
using System.Linq;
6
using System.Reflection;
7
using System.Runtime.Serialization;
8
using SharpLib.Utils;
9
10
namespace SharpLib.Ear
11
{
12
/// <summary>
13
/// Event Action Router (Ear) is a generic and extensible framework allowing users to execute actions in response to events.
14
/// Users can implement their own events and actions.
15
/// </summary>
16
[TypeConverter(typeof(TypeConverterJson<Manager>))]
17
[DataContract]
18
public class Manager
19
20
21
/// Access the currently installed EAR manager.
22
23
public static Manager Current = null;
24
25
26
/// Our events instances.
27
28
[DataMember]
29
public List<Event> Events;
30
31
32
/// Constructor
33
34
public Manager()
35
36
Init();
37
}
38
39
40
/// Executes after internalization took place.
41
42
public void Init()
43
44
if (Events == null)
45
46
Events = new List<Event>();
47
48
49
50
51
52
/// Trigger the given event.
53
54
/// <param name="aEventType"></param>
55
public void TriggerEvent<T>() where T: class
56
57
//Only trigger enabled events matching the desired type
58
foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled))
59
60
e.Trigger();
61
62
63
64
65
66
/// Remove the specified action from the event it belongs too.
67
68
/// <param name="aAction"></param>
69
public void RemoveAction(Action aAction)
70
71
foreach (Event e in Events)
72
73
if (e.Actions.Remove(aAction))
74
75
//We removed our action, we are done here.
76
return;
77
78
79
80
81