SharpLibEar/Manager.cs
author StephaneLenclud
Tue, 16 Aug 2016 12:59:32 +0200
changeset 235 ba14a29944c4
parent 234 0c75dec19d39
child 237 1a1c2ae3a29c
permissions -rw-r--r--
Removing JSON type converter from ear manager.
Using bindings for some application settings.
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@235
    10
StephaneLenclud@234
    11
namespace SharpLib.Ear
StephaneLenclud@234
    12
{
StephaneLenclud@234
    13
    /// <summary>
StephaneLenclud@234
    14
    /// Event Action Router (Ear) is a generic and extensible framework allowing users to execute actions in response to events. 
StephaneLenclud@234
    15
    /// Users can implement their own events and actions.
StephaneLenclud@234
    16
    /// </summary>
StephaneLenclud@234
    17
    [DataContract]
StephaneLenclud@235
    18
    [KnownType("DerivedTypes")]
StephaneLenclud@234
    19
    public class Manager
StephaneLenclud@234
    20
    {
StephaneLenclud@234
    21
        /// <summary>
StephaneLenclud@234
    22
        /// Our events instances.
StephaneLenclud@234
    23
        /// </summary>
StephaneLenclud@234
    24
        [DataMember]
StephaneLenclud@234
    25
        public List<Event> Events;
StephaneLenclud@234
    26
StephaneLenclud@234
    27
        /// <summary>
StephaneLenclud@234
    28
        /// Constructor
StephaneLenclud@234
    29
        /// </summary>
StephaneLenclud@234
    30
        public Manager()
StephaneLenclud@234
    31
        {
StephaneLenclud@234
    32
            Init();
StephaneLenclud@234
    33
        }
StephaneLenclud@234
    34
StephaneLenclud@234
    35
        /// <summary>
StephaneLenclud@234
    36
        /// Executes after internalization took place.
StephaneLenclud@234
    37
        /// </summary>
StephaneLenclud@234
    38
        public void Init()
StephaneLenclud@234
    39
        {
StephaneLenclud@234
    40
            if (Events == null)
StephaneLenclud@234
    41
            {
StephaneLenclud@234
    42
                Events = new List<Event>();
StephaneLenclud@234
    43
            }
StephaneLenclud@234
    44
            
StephaneLenclud@234
    45
        }
StephaneLenclud@234
    46
StephaneLenclud@234
    47
        /// <summary>
StephaneLenclud@234
    48
        /// Trigger the given event.
StephaneLenclud@234
    49
        /// </summary>
StephaneLenclud@234
    50
        /// <param name="aEventType"></param>
StephaneLenclud@234
    51
        public void TriggerEvent<T>() where T: class
StephaneLenclud@234
    52
        {
StephaneLenclud@234
    53
            //Only trigger enabled events matching the desired type
StephaneLenclud@234
    54
            foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled))
StephaneLenclud@234
    55
            {
StephaneLenclud@234
    56
                e.Trigger();
StephaneLenclud@234
    57
            }
StephaneLenclud@234
    58
        }
StephaneLenclud@234
    59
StephaneLenclud@234
    60
StephaneLenclud@234
    61
        /// <summary>
StephaneLenclud@234
    62
        /// Remove the specified action from the event it belongs too.
StephaneLenclud@234
    63
        /// </summary>
StephaneLenclud@234
    64
        /// <param name="aAction"></param>
StephaneLenclud@234
    65
        public void RemoveAction(Action aAction)
StephaneLenclud@234
    66
        {
StephaneLenclud@234
    67
            foreach (Event e in Events)
StephaneLenclud@234
    68
            {
StephaneLenclud@234
    69
                if (e.Actions.Remove(aAction))
StephaneLenclud@234
    70
                {
StephaneLenclud@234
    71
                    //We removed our action, we are done here.
StephaneLenclud@234
    72
                    return;
StephaneLenclud@234
    73
                }
StephaneLenclud@234
    74
            }
StephaneLenclud@234
    75
        }
StephaneLenclud@235
    76
StephaneLenclud@235
    77
        /// <summary>
StephaneLenclud@235
    78
        /// Allow extending our data contract.
StephaneLenclud@235
    79
        /// See KnownType above.
StephaneLenclud@235
    80
        /// </summary>
StephaneLenclud@235
    81
        /// <returns></returns>
StephaneLenclud@235
    82
        private static IEnumerable<Type> DerivedTypes()
StephaneLenclud@235
    83
        {
StephaneLenclud@235
    84
            return SharpLib.Utils.Reflection.GetDerivedTypes<Manager>();
StephaneLenclud@235
    85
        }
StephaneLenclud@234
    86
    }
StephaneLenclud@234
    87
}