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
TableElementList.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using DirectOutput.General.Generic;
4 
5 
6 namespace DirectOutput.Table
7 {
11  public class TableElementList : ExtList<TableElement>
12  {
13  //Dictionary for fast lookups
14  private Dictionary<TableElementTypeEnum, Dictionary<int, TableElement>> _TableElementsDictionary;
15 
16 
20  public void InitAssignedEffects(Table Table)
21  {
22  foreach (TableElement TE in this)
23  {
24  TE.AssignedEffects.Init(Table);
25  }
26  }
27 
31  public void FinishAssignedEffects()
32  {
33  foreach (TableElement TE in this)
34  {
35  TE.AssignedEffects.Finish();
36  }
37  }
38 
39  #region Direct access to TableElementType Dictionaries
40 
44  public Dictionary<int, TableElement> GetTableElementDictonaryForType(TableElementTypeEnum Type)
45  {
46  return _TableElementsDictionary[Type];
47  }
48 
49 
54  public List<TableElement> GetTableElementListForType(TableElementTypeEnum Type)
55  {
56  return new List<TableElement> (_TableElementsDictionary[Type].Values);
57  }
58 
62  public Dictionary<int, TableElement> Switch
63  {
64  get
65  {
66  return _TableElementsDictionary[TableElementTypeEnum.Switch];
67  }
68  }
72  public Dictionary<int, TableElement> Solenoid
73  {
74  get
75  {
76  return _TableElementsDictionary[TableElementTypeEnum.Solenoid];
77  }
78  }
82  public Dictionary<int, TableElement> Lamp
83  {
84  get
85  {
86  return _TableElementsDictionary[TableElementTypeEnum.Lamp];
87  }
88  }
92  public Dictionary<int, TableElement> GIString
93  {
94  get
95  {
96  return _TableElementsDictionary[TableElementTypeEnum.GIString];
97  }
98  }
99 
103  public Dictionary<int, TableElement> Mech
104  {
105  get
106  {
107  return _TableElementsDictionary[TableElementTypeEnum.Mech];
108  }
109  }
110  #endregion
111 
112  #region Indexer
113 
114 
115 
116 
117 
118 
119  public TableElement this[TableElementTypeEnum TableElementType, int Number]
120  {
121  get
122  {
123  return _TableElementsDictionary[TableElementType][Number];
124  }
125  }
126 
127 
136  public TableElement this[string TableElementDescriptor]
137  {
138  get
139  {
140 
141  try
142  {
143  return _TableElementsDictionary[(TableElementTypeEnum)TableElementDescriptor[0]][TableElementDescriptor.Substring(1).ToInteger()];
144 
145  }
146  catch
147  {
148 
149  return null;
150  }
151  }
152 
153  }
154 
155 
156  #endregion
157 
158  #region UpdateState()
159 
160 
167  public void UpdateState(TableElementTypeEnum TableElementType, int Number, int State)
168  {
169 
170  if (Contains(TableElementType, Number))
171  {
172  _TableElementsDictionary[TableElementType][Number].Value = State;
173 
174  }
175  else
176  {
177  Add(TableElementType, Number, State);
178  }
179 
180  }
181 
182  #endregion
183 
184  #region Add
185 
194  public new void Add(TableElement TableElement)
195  {
196  if (TableElement==null)
197  {
198  throw new Exception("Cant add null to the list of table elements.");
199  }
200  if (Contains(TableElement))
201  {
202  throw new Exception("The TableElement {0} {1} cant be added to the list. Another entry with the same type and number does already exist.".Build(TableElement.TableElementType, TableElement.Number));
203  }
204  _TableElementsDictionary[TableElement.TableElementType].Add(TableElement.Number, TableElement);
205  base.Add(TableElement);
206  }
207 
208 
220  public void Add(TableElementTypeEnum TableElementType, int Number, int State)
221  {
222  Add(new TableElement(TableElementType, Number, State));
223  }
224 
225 
226 
227 
228 
229  #endregion
230  #region Contains
231 
232 
238  public new bool Contains(TableElement TableElement)
239  {
240  return _TableElementsDictionary[TableElement.TableElementType].ContainsKey(TableElement.Number) || base.Contains(TableElement);
241  }
242 
249  public bool Contains(TableElementTypeEnum TableElementType, int Number)
250  {
251  return _TableElementsDictionary[TableElementType].ContainsKey(Number);
252  }
253 
254 
255 
256 
257  #endregion
258  #region Remove
259 
265  public new bool Remove(TableElement TableElement)
266  {
267 
268  _TableElementsDictionary[TableElement.TableElementType].Remove(TableElement.Number);
269  return base.Remove(TableElement);
270 
271  }
272 
278  public bool Remove(TableElementTypeEnum TableElementType, int Number)
279  {
280  if (!Contains(TableElementType, Number)) return false;
281  Remove(_TableElementsDictionary[TableElementType][Number]);
282  _TableElementsDictionary[TableElementType].Remove(Number);
283 
284  return true;
285 
286  }
287  #endregion
288 
289 
290  #region Events & Event handling
291  void TableElementList_AfterSet(object sender, SetEventArgs<TableElement> e)
292  {
293  _TableElementsDictionary[e.OldItem.TableElementType].Remove(e.OldItem.Number);
294  e.OldItem.ValueChanged -= new EventHandler<TableElementValueChangedEventArgs>(Item_ValueChanged);
295  _TableElementsDictionary[e.NewItem.TableElementType].Add(e.NewItem.Number, e.NewItem);
296  e.NewItem.ValueChanged += new EventHandler<TableElementValueChangedEventArgs>(Item_ValueChanged);
297  }
298 
299  void TableElementList_BeforeSet(object sender, SetEventArgs<TableElement> e)
300  {
301  if (!Contains(e.NewItem.TableElementType, e.NewItem.Number) || (e.NewItem.TableElementType == e.OldItem.TableElementType && e.NewItem.Number == e.OldItem.Number))
302  {
303 
304  }
305  else
306  {
307  throw new Exception("Another TableElement with type {0} and number {1} does already exist in the list.".Build(e.NewItem.TableElementType, e.NewItem.Number));
308  }
309  }
310 
311 
312  void TableElementList_AfterRemove(object sender, RemoveEventArgs<TableElement> e)
313  {
314  e.Item.ValueChanged -= new EventHandler<TableElementValueChangedEventArgs>(Item_ValueChanged);
315  }
316 
317  void TableElementList_AfterInsert(object sender, InsertEventArgs<TableElement> e)
318  {
319  e.Item.ValueChanged += new EventHandler<TableElementValueChangedEventArgs>(Item_ValueChanged);
320  }
321 
322  void Item_ValueChanged(object sender, TableElementValueChangedEventArgs e)
323  {
324  OnTableElementValueChanged(e);
325  }
326 
327  #region "ableElement Value Changed Event
328 
329 
330  private void OnTableElementValueChanged(TableElementValueChangedEventArgs e)
331  {
332  if (TableElementValueChanged != null)
333  {
334  TableElementValueChanged(this, e);
335  }
336  }
337 
338 
339 
343  public event TableElementValueChangedEventHandler TableElementValueChanged;
344 
350  public delegate void TableElementValueChangedEventHandler(object sender, TableElementValueChangedEventArgs e);
351 
352 
353  #endregion
354 
355  #endregion
356 
357  #region Constructor
358 
363  {
364  //Init internal dictionary
365  _TableElementsDictionary = new Dictionary<TableElementTypeEnum, Dictionary<int, TableElement>>();
366  foreach (TableElementTypeEnum T in Enum.GetValues(typeof(TableElementTypeEnum)))
367  {
368  _TableElementsDictionary.Add(T, new Dictionary<int, TableElement>());
369  }
370  this.AfterInsert += new EventHandler<InsertEventArgs<TableElement>>(TableElementList_AfterInsert);
371  this.AfterRemove += new EventHandler<RemoveEventArgs<TableElement>>(TableElementList_AfterRemove);
372  this.BeforeSet += new EventHandler<SetEventArgs<TableElement>>(TableElementList_BeforeSet);
373  this.AfterSet += new EventHandler<SetEventArgs<TableElement>>(TableElementList_AfterSet);
374  }
375 
376 
377 
378 
379 
380  #endregion
381 
386  {
387  foreach (TableElement TE in this)
388  {
389  TE.ValueChanged -= new EventHandler<TableElementValueChangedEventArgs>(Item_ValueChanged);
390 
391  }
392 
393  }
394  }
395 }