SharpLibEar/Event.cs
author StephaneLenclud
Wed, 17 Aug 2016 16:39:36 +0200
changeset 237 1a1c2ae3a29c
parent 231 4c706feaf706
child 238 c92587ddabcd
permissions -rw-r--r--
Adding HID consumer control event.
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@237
    41
        public virtual string Brief()
StephaneLenclud@237
    42
        {
StephaneLenclud@237
    43
            return Name;
StephaneLenclud@237
    44
        }
StephaneLenclud@231
    45
StephaneLenclud@210
    46
        protected Event()
StephaneLenclud@210
    47
        {
StephaneLenclud@231
    48
            Enabled = true;
StephaneLenclud@210
    49
        }
StephaneLenclud@210
    50
StephaneLenclud@231
    51
StephaneLenclud@231
    52
        /// <summary>
StephaneLenclud@231
    53
        /// Allows testing from generic edit dialog.
StephaneLenclud@231
    54
        /// </summary>
StephaneLenclud@231
    55
        public void Test()
StephaneLenclud@231
    56
        {
StephaneLenclud@231
    57
            Console.WriteLine("Event test");
StephaneLenclud@231
    58
            Trigger();
StephaneLenclud@231
    59
        }
StephaneLenclud@231
    60
StephaneLenclud@231
    61
StephaneLenclud@231
    62
        public void Trigger()
StephaneLenclud@210
    63
        {
StephaneLenclud@229
    64
            Console.WriteLine("Event triggered: " + Name);
StephaneLenclud@210
    65
            foreach (Action action in Actions)
StephaneLenclud@210
    66
            {
StephaneLenclud@210
    67
                action.Execute();
StephaneLenclud@210
    68
            }
StephaneLenclud@210
    69
        }
StephaneLenclud@231
    70
StephaneLenclud@231
    71
        /// <summary>
StephaneLenclud@231
    72
        /// So that data contract knows all our types.
StephaneLenclud@231
    73
        /// </summary>
StephaneLenclud@231
    74
        /// <returns></returns>
StephaneLenclud@231
    75
        private static IEnumerable<Type> DerivedTypes()
StephaneLenclud@231
    76
        {
StephaneLenclud@231
    77
            return SharpLib.Utils.Reflection.GetDerivedTypes<Event>();
StephaneLenclud@231
    78
        }
StephaneLenclud@237
    79
StephaneLenclud@237
    80
        //
StephaneLenclud@237
    81
        public override bool Equals(object obj)
StephaneLenclud@237
    82
        {
StephaneLenclud@237
    83
            //Default implementation assumes event are the same if types are the same
StephaneLenclud@237
    84
            bool res=  obj.GetType() == GetType();
StephaneLenclud@237
    85
            return res;
StephaneLenclud@237
    86
        }
StephaneLenclud@231
    87
    };
StephaneLenclud@210
    88
StephaneLenclud@210
    89
}