SharpLibEar/Event.cs
author StephaneLenclud
Wed, 31 Aug 2016 20:20:32 +0200
changeset 265 82e87f4956ea
parent 262 c4749a27966d
permissions -rw-r--r--
Actions can now have children.
StephaneLenclud@210
     1
//
StephaneLenclud@210
     2
StephaneLenclud@210
     3
StephaneLenclud@210
     4
using System;
StephaneLenclud@210
     5
using System.Collections.Generic;
StephaneLenclud@253
     6
using System.Diagnostics;
StephaneLenclud@265
     7
using System.Linq;
StephaneLenclud@210
     8
using System.Runtime.Serialization;
StephaneLenclud@258
     9
using System.Threading.Tasks;
StephaneLenclud@210
    10
StephaneLenclud@210
    11
namespace SharpLib.Ear
StephaneLenclud@210
    12
{
StephaneLenclud@210
    13
    [DataContract]
StephaneLenclud@260
    14
    [AttributeObject(Id = "Event", Name = "User Event", Description = "An event that can be triggered by users.")]
StephaneLenclud@260
    15
    public class Event : Object
StephaneLenclud@210
    16
    {
StephaneLenclud@231
    17
        [DataMember]
StephaneLenclud@231
    18
        [AttributeObjectProperty
StephaneLenclud@231
    19
            (
StephaneLenclud@231
    20
                Id = "Event.Enabled",
StephaneLenclud@231
    21
                Name = "Enabled",
StephaneLenclud@231
    22
                Description = "When enabled an event instance can be triggered."
StephaneLenclud@231
    23
            )
StephaneLenclud@231
    24
        ]
Stephane@243
    25
        public bool Enabled { get; set; } = true;
StephaneLenclud@210
    26
StephaneLenclud@262
    27
StephaneLenclud@210
    28
StephaneLenclud@231
    29
StephaneLenclud@231
    30
        /// <summary>
StephaneLenclud@231
    31
        /// Allows testing from generic edit dialog.
StephaneLenclud@231
    32
        /// </summary>
StephaneLenclud@260
    33
        public async void Test()
StephaneLenclud@231
    34
        {
StephaneLenclud@253
    35
            Trace.WriteLine("Event test");
StephaneLenclud@260
    36
            await Trigger();
StephaneLenclud@231
    37
        }
StephaneLenclud@231
    38
StephaneLenclud@231
    39
StephaneLenclud@258
    40
        public async Task Trigger()
StephaneLenclud@210
    41
        {
StephaneLenclud@260
    42
            Trace.WriteLine("Event triggered: " + AttributeName);
StephaneLenclud@265
    43
            foreach (Action action in Objects.OfType<Action>())
StephaneLenclud@210
    44
            {
StephaneLenclud@258
    45
                await action.Execute();
StephaneLenclud@210
    46
            }
StephaneLenclud@210
    47
        }
StephaneLenclud@231
    48
StephaneLenclud@237
    49
        //
StephaneLenclud@237
    50
        public override bool Equals(object obj)
StephaneLenclud@237
    51
        {
StephaneLenclud@237
    52
            //Default implementation assumes event are the same if types are the same
StephaneLenclud@237
    53
            bool res=  obj.GetType() == GetType();
StephaneLenclud@237
    54
            return res;
StephaneLenclud@237
    55
        }
StephaneLenclud@231
    56
    };
StephaneLenclud@210
    57
StephaneLenclud@210
    58
}