SharpLibEar/Manager.cs
author StephaneLenclud
Mon, 02 Jan 2017 18:43:45 +0100
changeset 273 e5f85a895a62
parent 265 82e87f4956ea
permissions -rw-r--r--
Draft audio spectrum visualizer.
     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 async void TriggerEvents<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                 await e.Trigger();
    57             }
    58         }
    59 
    60         /// <summary>
    61         /// Trigger the given event.
    62         /// </summary>
    63         /// <param name="aEventType"></param>
    64         public async void TriggerEvents<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                 await e.Trigger();
    70             }
    71         }
    72 
    73         /// <summary>
    74         /// 
    75         /// </summary>
    76         /// <typeparam name="T"></typeparam>
    77         /// <param name="aEvent"></param>
    78         public async void TriggerEventsByName(string aName)
    79         {
    80             if (string.IsNullOrEmpty(aName))
    81             {
    82                 //Just don't do that, that would be silly
    83                 return;
    84             }
    85             // Only trigger events matching the desired type
    86             // Doing some safety checks as well to prevent crashing if name was left null for some reason
    87             // This was the case when loading existing settings after event Name was introduced
    88             foreach (Event e in Events.Where(e => !string.IsNullOrEmpty(e.Name) && e.Enabled && aName.Equals(e.Name)))
    89             {
    90                 await e.Trigger();
    91             }
    92         }
    93 
    94 
    95 
    96 
    97         /// <summary>
    98         /// Remove the specified action from the event it belongs too.
    99         /// </summary>
   100         /// <param name="aAction"></param>
   101         public void RemoveAction(Action aAction)
   102         {
   103             foreach (Event e in Events)
   104             {
   105                 if (RemoveObject(e,aAction))
   106                 {
   107                     //We removed our action, we are done here.
   108                     return;
   109                 }
   110             }
   111         }
   112 
   113         /// <summary>
   114         /// Remove the specified action from the event it belongs too.
   115         /// </summary>
   116         /// <param name="aAction"></param>
   117         private static bool RemoveObject(Object aCurrent, Object aToRemove)
   118         {
   119             //Exit condition
   120             if (aCurrent.Objects.Remove(aToRemove))
   121             {
   122                 //We removed our action, we are done here.
   123                 return true;
   124             }
   125 
   126             foreach (Object o in aCurrent.Objects)
   127             {
   128                 bool done = RemoveObject(o, aToRemove);
   129                 if (done)
   130                 {
   131                     return true;
   132                 } 
   133             }
   134 
   135             return false;
   136         }
   137 
   138     }
   139 }