SharpLibEar/Manager.cs
author StephaneLenclud
Thu, 18 Aug 2016 14:35:50 +0200
changeset 238 c92587ddabcd
parent 235 ba14a29944c4
child 243 cc2251d065db
permissions -rw-r--r--
Support for launch action and WMC HID events.
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@237
    60
        /// <summary>
StephaneLenclud@237
    61
        /// Trigger the given event.
StephaneLenclud@237
    62
        /// </summary>
StephaneLenclud@237
    63
        /// <param name="aEventType"></param>
StephaneLenclud@237
    64
        public void TriggerEvent<T>(T aEvent) where T : class
StephaneLenclud@237
    65
        {
StephaneLenclud@237
    66
            //Only trigger events matching the desired type
StephaneLenclud@237
    67
            foreach (Event e in Events.Where(e => e.Equals(aEvent) && e.Enabled))
StephaneLenclud@237
    68
            {
StephaneLenclud@237
    69
                e.Trigger();
StephaneLenclud@237
    70
            }
StephaneLenclud@237
    71
        }
StephaneLenclud@237
    72
StephaneLenclud@234
    73
StephaneLenclud@234
    74
        /// <summary>
StephaneLenclud@234
    75
        /// Remove the specified action from the event it belongs too.
StephaneLenclud@234
    76
        /// </summary>
StephaneLenclud@234
    77
        /// <param name="aAction"></param>
StephaneLenclud@234
    78
        public void RemoveAction(Action aAction)
StephaneLenclud@234
    79
        {
StephaneLenclud@234
    80
            foreach (Event e in Events)
StephaneLenclud@234
    81
            {
StephaneLenclud@234
    82
                if (e.Actions.Remove(aAction))
StephaneLenclud@234
    83
                {
StephaneLenclud@234
    84
                    //We removed our action, we are done here.
StephaneLenclud@234
    85
                    return;
StephaneLenclud@234
    86
                }
StephaneLenclud@234
    87
            }
StephaneLenclud@234
    88
        }
StephaneLenclud@235
    89
StephaneLenclud@235
    90
        /// <summary>
StephaneLenclud@235
    91
        /// Allow extending our data contract.
StephaneLenclud@235
    92
        /// See KnownType above.
StephaneLenclud@235
    93
        /// </summary>
StephaneLenclud@235
    94
        /// <returns></returns>
StephaneLenclud@235
    95
        private static IEnumerable<Type> DerivedTypes()
StephaneLenclud@235
    96
        {
StephaneLenclud@235
    97
            return SharpLib.Utils.Reflection.GetDerivedTypes<Manager>();
StephaneLenclud@235
    98
        }
StephaneLenclud@234
    99
    }
StephaneLenclud@234
   100
}