SharpLibEar/Manager.cs
author StephaneLenclud
Sat, 20 Aug 2016 21:00:35 +0200
changeset 246 30a221eecc06
parent 237 1a1c2ae3a29c
child 260 d44943088c67
permissions -rw-r--r--
Generic HID event with key recognition functional.
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]
Stephane@243
    18
    public class Manager: Object
StephaneLenclud@234
    19
    {
StephaneLenclud@234
    20
        /// <summary>
StephaneLenclud@234
    21
        /// Our events instances.
StephaneLenclud@234
    22
        /// </summary>
StephaneLenclud@234
    23
        [DataMember]
StephaneLenclud@234
    24
        public List<Event> Events;
StephaneLenclud@234
    25
StephaneLenclud@234
    26
StephaneLenclud@234
    27
        /// <summary>
StephaneLenclud@234
    28
        /// Executes after internalization took place.
StephaneLenclud@234
    29
        /// </summary>
Stephane@243
    30
        protected override void DoConstruct()
StephaneLenclud@234
    31
        {
Stephane@243
    32
            base.DoConstruct();
Stephane@243
    33
StephaneLenclud@234
    34
            if (Events == null)
StephaneLenclud@234
    35
            {
StephaneLenclud@234
    36
                Events = new List<Event>();
StephaneLenclud@234
    37
            }
Stephane@243
    38
Stephane@243
    39
            // TODO: Object properties should be constructed too
Stephane@243
    40
            foreach (Event e in Events)
Stephane@243
    41
            {
Stephane@243
    42
                e.Construct();
Stephane@243
    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@234
    89
    }
StephaneLenclud@234
    90
}