SharpLibEar/Manager.cs
author StephaneLenclud
Sun, 21 Aug 2016 19:31:08 +0200
changeset 251 f60cfcb98c9a
parent 237 1a1c2ae3a29c
child 260 d44943088c67
permissions -rw-r--r--
Published v0.12.2.0
Adding setting for Logitech user name.
Improved event tree view update following Harmony connection.
Removed unneeded editable fields from Harmony command action.
     1 //
     2 using System;
     3 using System.Collections.Generic;
     4 using System.ComponentModel;
     5 using System.Linq;
     6 using System.Reflection;
     7 using System.Runtime.Serialization;
     8 using SharpLib.Utils;
     9 
    10 
    11 namespace SharpLib.Ear
    12 {
    13     /// <summary>
    14     /// Event Action Router (Ear) is a generic and extensible framework allowing users to execute actions in response to events. 
    15     /// Users can implement their own events and actions.
    16     /// </summary>
    17     [DataContract]
    18     public class Manager: Object
    19     {
    20         /// <summary>
    21         /// Our events instances.
    22         /// </summary>
    23         [DataMember]
    24         public List<Event> Events;
    25 
    26 
    27         /// <summary>
    28         /// Executes after internalization took place.
    29         /// </summary>
    30         protected override void DoConstruct()
    31         {
    32             base.DoConstruct();
    33 
    34             if (Events == null)
    35             {
    36                 Events = new List<Event>();
    37             }
    38 
    39             // TODO: Object properties should be constructed too
    40             foreach (Event e in Events)
    41             {
    42                 e.Construct();
    43             }
    44             
    45         }
    46 
    47         /// <summary>
    48         /// Trigger the given event.
    49         /// </summary>
    50         /// <param name="aEventType"></param>
    51         public void TriggerEvent<T>() where T: class
    52         {
    53             //Only trigger enabled events matching the desired type
    54             foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled))
    55             {
    56                 e.Trigger();
    57             }
    58         }
    59 
    60         /// <summary>
    61         /// Trigger the given event.
    62         /// </summary>
    63         /// <param name="aEventType"></param>
    64         public void TriggerEvent<T>(T aEvent) where T : class
    65         {
    66             //Only trigger events matching the desired type
    67             foreach (Event e in Events.Where(e => e.Equals(aEvent) && e.Enabled))
    68             {
    69                 e.Trigger();
    70             }
    71         }
    72 
    73 
    74         /// <summary>
    75         /// Remove the specified action from the event it belongs too.
    76         /// </summary>
    77         /// <param name="aAction"></param>
    78         public void RemoveAction(Action aAction)
    79         {
    80             foreach (Event e in Events)
    81             {
    82                 if (e.Actions.Remove(aAction))
    83                 {
    84                     //We removed our action, we are done here.
    85                     return;
    86                 }
    87             }
    88         }
    89     }
    90 }