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