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