SharpLibEar/ManagerEventAction.cs
author StephaneLenclud
Mon, 15 Aug 2016 12:11:26 +0200
changeset 233 2b9541e54f7d
parent 214 4961ede27e0a
permissions -rw-r--r--
Adding Harmony tab.
     1 //
     2 
     3 
     4 using System;
     5 using System.Collections.Generic;
     6 using System.ComponentModel;
     7 using System.Linq;
     8 using System.Reflection;
     9 using System.Runtime.Serialization;
    10 using SharpLib.Utils;
    11 
    12 namespace SharpLib.Ear
    13 {
    14     [TypeConverter(typeof(TypeConverterJson<ManagerEventAction>))]
    15     [DataContract]
    16     public class ManagerEventAction
    17     {
    18         public static ManagerEventAction Current = null;
    19         //public IDictionary<string, Type> ActionTypes;
    20         //public IDictionary<string, Type> EventTypes;
    21 
    22         /// <summary>
    23         /// Our events instances.
    24         /// </summary>
    25         [DataMember]
    26         public List<Event> Events;
    27 
    28 
    29 
    30 
    31         public ManagerEventAction()
    32         {
    33             Init();
    34         }
    35 
    36         /// <summary>
    37         /// Executes after internalization took place.
    38         /// </summary>
    39         public void Init()
    40         {
    41             if (Events == null)
    42             {
    43                 Events = new List<Event>();
    44             }
    45             
    46         }
    47 
    48         /// <summary>
    49         /// 
    50         /// </summary>
    51         /// <param name="aEventType"></param>
    52         public void TriggerEvent<T>() where T: class
    53         {
    54             //Only trigger enabled events matching the desired type
    55             foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled))
    56             {
    57                 e.Trigger();
    58             }
    59         }
    60 
    61 
    62         /// <summary>
    63         /// 
    64         /// </summary>
    65         /// <param name="aAction"></param>
    66         public void RemoveAction(Action aAction)
    67         {
    68             foreach (Event e in Events)
    69             {
    70                 if (e.Actions.Remove(aAction))
    71                 {
    72                     //We removed our action, we are done here.
    73                     return;
    74                 }
    75             }
    76         }
    77     }
    78 }