SharpLibEar/Event.cs
author StephaneLenclud
Mon, 15 Aug 2016 12:11:26 +0200
changeset 233 2b9541e54f7d
parent 229 7c631055b94b
child 237 1a1c2ae3a29c
permissions -rw-r--r--
Adding Harmony tab.
     1 //
     2 
     3 
     4 using System;
     5 using System.Collections.Generic;
     6 using System.Runtime.Serialization;
     7 
     8 namespace SharpLib.Ear
     9 {
    10     [DataContract]
    11     [KnownType("DerivedTypes")]
    12     public abstract class Event
    13     {
    14         [DataMember]
    15         [AttributeObjectProperty
    16             (
    17                 Id = "Event.Enabled",
    18                 Name = "Enabled",
    19                 Description = "When enabled an event instance can be triggered."
    20             )
    21         ]
    22         public bool Enabled { get; set; }
    23 
    24         [DataMember]
    25         public List<Action> Actions = new List<Action>();
    26 
    27         public string Name
    28         {
    29             //Get the name of this object attribute
    30             get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name; }
    31             private set { }
    32         }
    33 
    34         public string Description
    35         {
    36             //Get the description of this object attribute
    37             get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Description; }
    38             private set { }
    39         }
    40 
    41 
    42         protected Event()
    43         {
    44             Enabled = true;
    45         }
    46 
    47 
    48         /// <summary>
    49         /// Allows testing from generic edit dialog.
    50         /// </summary>
    51         public void Test()
    52         {
    53             Console.WriteLine("Event test");
    54             Trigger();
    55         }
    56 
    57 
    58         public void Trigger()
    59         {
    60             Console.WriteLine("Event triggered: " + Name);
    61             foreach (Action action in Actions)
    62             {
    63                 action.Execute();
    64             }
    65         }
    66 
    67         /// <summary>
    68         /// So that data contract knows all our types.
    69         /// </summary>
    70         /// <returns></returns>
    71         private static IEnumerable<Type> DerivedTypes()
    72         {
    73             return SharpLib.Utils.Reflection.GetDerivedTypes<Event>();
    74         }
    75     };
    76 
    77 }