SharpLibEar/Event.cs
author StephaneLenclud
Tue, 30 Aug 2016 21:14:18 +0200
changeset 261 e2729a990e8b
parent 258 e237c2e33545
child 262 c4749a27966d
permissions -rw-r--r--
Published v1.2.1.0
Support for Event Trigger through SharpLibDisplay.
     1 //
     2 
     3 
     4 using System;
     5 using System.Collections.Generic;
     6 using System.Diagnostics;
     7 using System.Runtime.Serialization;
     8 using System.Threading.Tasks;
     9 
    10 namespace SharpLib.Ear
    11 {
    12     [DataContract]
    13     [AttributeObject(Id = "Event", Name = "User Event", Description = "An event that can be triggered by users.")]
    14     public class Event : Object
    15     {
    16         [DataMember]
    17         [AttributeObjectProperty
    18             (
    19                 Id = "Event.Enabled",
    20                 Name = "Enabled",
    21                 Description = "When enabled an event instance can be triggered."
    22             )
    23         ]
    24         public bool Enabled { get; set; } = true;
    25 
    26         [DataMember]
    27         [AttributeObjectProperty
    28             (
    29                 Id = "Event.Name",
    30                 Name = "Name",
    31                 Description = "Given event name. Can be used to trigger it."
    32             )
    33         ]
    34         public string Name { get; set; } = "";
    35 
    36         [DataMember]
    37         public List<Action> Actions = new List<Action>();
    38 
    39 
    40         protected override void DoConstruct()
    41         {
    42             base.DoConstruct();
    43 
    44             // TODO: Construct properties too
    45             foreach (Action a in Actions)
    46             {
    47                 a.Construct();
    48             }
    49 
    50         }
    51 
    52 
    53         /// <summary>
    54         /// Allows testing from generic edit dialog.
    55         /// </summary>
    56         public async void Test()
    57         {
    58             Trace.WriteLine("Event test");
    59             await Trigger();
    60         }
    61 
    62 
    63         public async Task Trigger()
    64         {
    65             Trace.WriteLine("Event triggered: " + AttributeName);
    66             foreach (Action action in Actions)
    67             {
    68                 await action.Execute();
    69             }
    70         }
    71 
    72         //
    73         public override bool Equals(object obj)
    74         {
    75             //Default implementation assumes event are the same if types are the same
    76             bool res=  obj.GetType() == GetType();
    77             return res;
    78         }
    79     };
    80 
    81 }