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