SharpLibEar/Object.cs
author StephaneLenclud
Sun, 08 Jan 2017 12:19:35 +0100
changeset 278 2481c46d1f93
parent 260 d44943088c67
permissions -rw-r--r--
Reading audio output is stopped when audio visualizers are not in used.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Runtime.Serialization;
     7 using System.ComponentModel;
     8 
     9 namespace SharpLib.Ear
    10 {
    11 
    12     /// <summary>
    13     /// EAR object provides serialization support.
    14     /// It assumes most derived class is decorated with AttributeObject.
    15     /// </summary>
    16     [DataContract]
    17     [KnownType("DerivedTypes")]
    18     public abstract class Object: IComparable, INotifyPropertyChanged
    19     {
    20         private bool iConstructed = false;
    21 
    22         [DataMember]
    23         public List<Object> Objects = new List<Object>();
    24 
    25         /// <summary>
    26         /// </summary>
    27         [DataMember]
    28         [AttributeObjectProperty
    29             (
    30                 Id = "Object.Name",
    31                 Name = "Name",
    32                 Description = "Given object name."
    33             )
    34         ]
    35         public string Name { get; set; } = "";
    36 
    37 
    38         protected Object()
    39         {
    40             Construct();
    41         }
    42 
    43         /// <summary>
    44         /// Needed as our constructor is not called following internalization.
    45         /// </summary>
    46         public void Construct()
    47         {
    48             //Construct ourselves first
    49             if (!iConstructed)
    50             {
    51                 DoConstruct();
    52                 iConstructed = true;
    53             }
    54 
    55             //Then construct our children
    56             foreach (Object o in Objects)
    57             {
    58                 o.Construct();
    59             }
    60         }
    61 
    62         /// <summary>
    63         /// 
    64         /// </summary>
    65         protected virtual void DoConstruct()
    66         {
    67             //Make sure our name is not null
    68             if (Name == null)
    69             {
    70                 Name = "";
    71             }
    72 
    73             // Makes sure our objects are not null
    74             if (Objects == null)
    75             {
    76                 Objects = new List<Object>();
    77             }
    78         }
    79 
    80         public enum State
    81         {
    82             Rest=0,
    83             Edit
    84         }
    85 
    86         State iCurrentState = State.Rest;
    87         public State CurrentState { get { return iCurrentState; } set { OnStateLeave(); iCurrentState = value; OnStateEnter(); } }
    88 
    89         /// <summary>
    90         /// 
    91         /// </summary>
    92         protected virtual void OnStateLeave()
    93         {
    94 
    95         }
    96 
    97         /// <summary>
    98         /// 
    99         /// </summary>
   100         protected virtual void OnStateEnter()
   101         {
   102 
   103         }
   104 
   105         /// <summary>
   106         /// Static object name.
   107         /// </summary>
   108         public string AttributeName
   109         {
   110             //Get the name of this object attribute
   111             get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name; }
   112             private set { }
   113         }
   114 
   115         /// <summary>
   116         /// Static object description.
   117         /// </summary>
   118         public string AttributeDescription
   119         {
   120             //Get the description of this object attribute
   121             get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Description; }
   122             private set { }
   123         }
   124 
   125         /// <summary>
   126         /// Dynamic object description.
   127         /// </summary>
   128         /// <returns></returns>
   129         public virtual string Brief()
   130         {
   131             return AttributeName;
   132         }
   133 
   134         /// <summary>
   135         /// Needed to make sure our sorting makes sense
   136         /// </summary>
   137         /// <param name="obj"></param>
   138         /// <returns></returns>
   139         public int CompareTo(object obj)
   140         {
   141             //Sort by object name
   142             return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name.CompareTo(obj.GetType());
   143         }
   144 
   145         /// <summary>
   146         /// Tells whether the current object configuration is valid.
   147         /// </summary>
   148         /// <returns></returns>
   149         public virtual bool IsValid()
   150         {
   151             return true;
   152         }
   153 
   154         /// <summary>
   155         /// So that data contract knows all our types.
   156         /// </summary>
   157         /// <returns></returns>
   158         private static IEnumerable<Type> DerivedTypes()
   159         {
   160             return SharpLib.Utils.Reflection.GetDerivedTypes<Object>();
   161         }
   162 
   163         /// <summary>
   164         /// 
   165         /// </summary>
   166         public event PropertyChangedEventHandler PropertyChanged;
   167 
   168 
   169         /// <summary>
   170         /// Invoke our event.
   171         /// </summary>
   172         /// <param name="name"></param>
   173         protected void OnPropertyChanged(string name)
   174         {
   175             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
   176         }
   177 
   178 
   179     }
   180 }