DirectOuput Framework R2
DirectOutput framework R2 for virtual pinball cabinets
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
TableElement.cs
Go to the documentation of this file.
1 using System;
2 using System.ComponentModel;
3 using System.Xml.Serialization;
4 using DirectOutput.FX;
5 
6 namespace DirectOutput.Table
7 {
8 
12  public class TableElement
13  {
14 
15 
16 
17 
18  #region TableElementType
19  private TableElementTypeEnum _TableElementType = TableElementTypeEnum.Lamp;
23  public TableElementTypeEnum TableElementType
24  {
25  get { return _TableElementType; }
26  set
27  {
28  if (_TableElementType != value)
29  {
30  _TableElementType = value;
31 
32  }
33  }
34  }
35  #endregion
36 
37  #region Number
38  private int _Number = 0;
42  public int Number
43  {
44  get { return _Number; }
45  set
46  {
47  if (_Number != value)
48  {
49 
50  _Number = value;
51 
52  }
53  }
54  }
55 
56  #endregion
57 
58  #region Name
59  private string _Name = "";
64  public string Name
65  {
66  get { return _Name; }
67  set
68  {
69  if (_Name != value)
70  {
71  _Name = value;
72  if (NameChanged != null)
73  {
74  NameChanged(this, new EventArgs());
75 
76  }
77  }
78  }
79  }
83  public event EventHandler<EventArgs> NameChanged;
84  #endregion
85 
86  #region Value
87  private int _Value = int.MinValue;
92  [XmlIgnoreAttribute]
93  public int Value
94  {
95  get { return _Value; }
96  set
97  {
98  if (_Value != value)
99  {
100  _Value = value;
101  StorePastValue(value);
102 
103  if (ValueChanged != null)
104  {
105  ValueChanged(this, new TableElementValueChangedEventArgs(this));
106 
107  }
108  }
109  }
110  }
111 
112  const int PastValuesCount=100;
113  private int[] PastValues =new int[PastValuesCount];
114  private DateTime[] PastValueTimestamp = new DateTime[PastValuesCount];
115  private int PastValuesPosition = 0;
116 
117  private void StorePastValue(int Value)
118  {
119  PastValuesPosition++;
120  if (PastValuesPosition >= PastValuesCount) { PastValuesPosition = 0; }
121 
122  PastValues[PastValuesPosition] = Value;
123  PastValueTimestamp[PastValuesPosition] = DateTime.Now;
124 
125  }
126 
133  public bool ValueHasBeen(int Value, int DuringLastMilliseconds)
134  {
135  DateTime EarliestTime = DateTime.Now.AddMilliseconds(-DuringLastMilliseconds);
136 
137  int P = PastValuesPosition;
138  int Cnt = 0;
139 
140  while (PastValueTimestamp[P] > EarliestTime && Cnt < PastValuesCount)
141  {
142  if (PastValues[P] == Value) { return true; }
143  P--;
144  if (P < 0) { P = PastValuesCount - 1; }
145  Cnt++;
146  }
147  return false;
148  }
149 
155  public bool ValueHasChanged(int DuringLastMilliseconds)
156  {
157  DateTime EarliestTime = DateTime.Now.AddMilliseconds(-DuringLastMilliseconds);
158 
159  if (PastValueTimestamp[PastValuesPosition] >= EarliestTime) return true;
160 
161  return false;
162 
163  }
164 
168  public event EventHandler<TableElementValueChangedEventArgs> ValueChanged;
169 
170 
171  void TableElement_ValueChanged(object sender, TableElementValueChangedEventArgs e)
172  {
173 
174  AssignedEffects.Trigger(GetTableElementData());
175  }
176  #endregion
177 
178 
179  private AssignedEffectList _TableElementEffectList;
180 
181 
185  public AssignedEffectList AssignedEffects
186  {
187  get { return _TableElementEffectList; }
188  set
189  {
190  _TableElementEffectList = value;
191 
192  }
193  }
194 
199  public TableElementData GetTableElementData()
200  {
201  return new TableElementData(this);
202  }
203 
204 
205  #region Constructor
206 
207 
208 
209  public TableElement()
210  {
211  AssignedEffects = new AssignedEffectList();
212  ValueChanged += new EventHandler<TableElementValueChangedEventArgs>(TableElement_ValueChanged);
213  }
214 
215 
222  public TableElement(TableElementTypeEnum TableElementType, int Number, int Value)
223  : this()
224  {
225  this.TableElementType = TableElementType;
226  this.Number = Number;
227  this.Value = Value;
228 
229  }
230  #endregion
231 
232  }
233 }