SharpLibEar/Manager.cs
author StephaneLenclud
Tue, 30 Aug 2016 21:14:18 +0200
changeset 261 e2729a990e8b
parent 243 cc2251d065db
child 262 c4749a27966d
permissions -rw-r--r--
Published v1.2.1.0
Support for Event Trigger through SharpLibDisplay.
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@260
    51
        public async void TriggerEvents<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@260
    56
                await 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@260
    64
        public async void TriggerEvents<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@260
    69
                await e.Trigger();
StephaneLenclud@237
    70
            }
StephaneLenclud@237
    71
        }
StephaneLenclud@237
    72
StephaneLenclud@260
    73
        /// <summary>
StephaneLenclud@260
    74
        /// 
StephaneLenclud@260
    75
        /// </summary>
StephaneLenclud@260
    76
        /// <typeparam name="T"></typeparam>
StephaneLenclud@260
    77
        /// <param name="aEvent"></param>
StephaneLenclud@260
    78
        public async void TriggerEventsByName(string aName)
StephaneLenclud@260
    79
        {
StephaneLenclud@260
    80
            if (string.IsNullOrEmpty(aName))
StephaneLenclud@260
    81
            {
StephaneLenclud@260
    82
                //Just don't do that that would be silly
StephaneLenclud@260
    83
                return;
StephaneLenclud@260
    84
            }
StephaneLenclud@260
    85
            //Only trigger events matching the desired type
StephaneLenclud@260
    86
            foreach (Event e in Events.Where(e => e.Name.Equals(aName)))
StephaneLenclud@260
    87
            {
StephaneLenclud@260
    88
                await e.Trigger();
StephaneLenclud@260
    89
            }
StephaneLenclud@260
    90
        }
StephaneLenclud@260
    91
StephaneLenclud@260
    92
StephaneLenclud@260
    93
StephaneLenclud@234
    94
StephaneLenclud@234
    95
        /// <summary>
StephaneLenclud@234
    96
        /// Remove the specified action from the event it belongs too.
StephaneLenclud@234
    97
        /// </summary>
StephaneLenclud@234
    98
        /// <param name="aAction"></param>
StephaneLenclud@234
    99
        public void RemoveAction(Action aAction)
StephaneLenclud@234
   100
        {
StephaneLenclud@234
   101
            foreach (Event e in Events)
StephaneLenclud@234
   102
            {
StephaneLenclud@234
   103
                if (e.Actions.Remove(aAction))
StephaneLenclud@234
   104
                {
StephaneLenclud@234
   105
                    //We removed our action, we are done here.
StephaneLenclud@234
   106
                    return;
StephaneLenclud@234
   107
                }
StephaneLenclud@234
   108
            }
StephaneLenclud@234
   109
        }
StephaneLenclud@234
   110
    }
StephaneLenclud@234
   111
}