SharpLibEar/Event.cs
author StephaneLenclud
Wed, 31 Aug 2016 16:06:47 +0200
changeset 262 c4749a27966d
parent 260 d44943088c67
child 265 82e87f4956ea
permissions -rw-r--r--
Fix crash when trying to select Harmony command without configuration.
Consolidate Named event Trigger.
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@210
     7
using System.Runtime.Serialization;
StephaneLenclud@258
     8
using System.Threading.Tasks;
StephaneLenclud@210
     9
StephaneLenclud@210
    10
namespace SharpLib.Ear
StephaneLenclud@210
    11
{
StephaneLenclud@210
    12
    [DataContract]
StephaneLenclud@260
    13
    [AttributeObject(Id = "Event", Name = "User Event", Description = "An event that can be triggered by users.")]
StephaneLenclud@260
    14
    public class Event : Object
StephaneLenclud@210
    15
    {
StephaneLenclud@231
    16
        [DataMember]
StephaneLenclud@231
    17
        [AttributeObjectProperty
StephaneLenclud@231
    18
            (
StephaneLenclud@231
    19
                Id = "Event.Enabled",
StephaneLenclud@231
    20
                Name = "Enabled",
StephaneLenclud@231
    21
                Description = "When enabled an event instance can be triggered."
StephaneLenclud@231
    22
            )
StephaneLenclud@231
    23
        ]
Stephane@243
    24
        public bool Enabled { get; set; } = true;
StephaneLenclud@210
    25
StephaneLenclud@262
    26
StephaneLenclud@262
    27
        /// <summary>
StephaneLenclud@262
    28
        /// TODO: Should the name property be moved to our EAR Object?
StephaneLenclud@262
    29
        /// </summary>
Stephane@212
    30
        [DataMember]
StephaneLenclud@260
    31
        [AttributeObjectProperty
StephaneLenclud@260
    32
            (
StephaneLenclud@260
    33
                Id = "Event.Name",
StephaneLenclud@260
    34
                Name = "Name",
StephaneLenclud@260
    35
                Description = "Given event name. Can be used to trigger it."
StephaneLenclud@260
    36
            )
StephaneLenclud@260
    37
        ]
StephaneLenclud@260
    38
        public string Name { get; set; } = "";
StephaneLenclud@260
    39
StephaneLenclud@262
    40
StephaneLenclud@260
    41
        [DataMember]
Stephane@212
    42
        public List<Action> Actions = new List<Action>();
StephaneLenclud@210
    43
StephaneLenclud@231
    44
Stephane@243
    45
        protected override void DoConstruct()
Stephane@243
    46
        {
Stephane@243
    47
            base.DoConstruct();
StephaneLenclud@231
    48
StephaneLenclud@262
    49
            //Make sure our name is not null
StephaneLenclud@262
    50
            if (Name == null)
StephaneLenclud@262
    51
            {
StephaneLenclud@262
    52
                Name = "";
StephaneLenclud@262
    53
            }
StephaneLenclud@262
    54
Stephane@243
    55
            // TODO: Construct properties too
Stephane@243
    56
            foreach (Action a in Actions)
Stephane@243
    57
            {
Stephane@243
    58
                a.Construct();
Stephane@243
    59
            }
Stephane@243
    60
StephaneLenclud@210
    61
        }
StephaneLenclud@210
    62
StephaneLenclud@231
    63
StephaneLenclud@231
    64
        /// <summary>
StephaneLenclud@231
    65
        /// Allows testing from generic edit dialog.
StephaneLenclud@231
    66
        /// </summary>
StephaneLenclud@260
    67
        public async void Test()
StephaneLenclud@231
    68
        {
StephaneLenclud@253
    69
            Trace.WriteLine("Event test");
StephaneLenclud@260
    70
            await Trigger();
StephaneLenclud@231
    71
        }
StephaneLenclud@231
    72
StephaneLenclud@231
    73
StephaneLenclud@258
    74
        public async Task Trigger()
StephaneLenclud@210
    75
        {
StephaneLenclud@260
    76
            Trace.WriteLine("Event triggered: " + AttributeName);
StephaneLenclud@210
    77
            foreach (Action action in Actions)
StephaneLenclud@210
    78
            {
StephaneLenclud@258
    79
                await action.Execute();
StephaneLenclud@210
    80
            }
StephaneLenclud@210
    81
        }
StephaneLenclud@231
    82
StephaneLenclud@237
    83
        //
StephaneLenclud@237
    84
        public override bool Equals(object obj)
StephaneLenclud@237
    85
        {
StephaneLenclud@237
    86
            //Default implementation assumes event are the same if types are the same
StephaneLenclud@237
    87
            bool res=  obj.GetType() == GetType();
StephaneLenclud@237
    88
            return res;
StephaneLenclud@237
    89
        }
StephaneLenclud@231
    90
    };
StephaneLenclud@210
    91
StephaneLenclud@210
    92
}