DirectOutputR1
DirectOutput framework R1 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 
128  #endregion
129 
130  #region UpdateState()
131 
132 
139  public void UpdateState(TableElementTypeEnum TableElementType, int Number, int State)
140  {
141  if (Contains(TableElementType, Number))
142  {
143  _TableElementsDictionary[TableElementType][Number].Value = State;
144 
145  }
146  else
147  {
148  Add(TableElementType, Number, State);
149  }
150 
151  }
152 
153  #endregion
154 
155  #region Add
156 
165  public new void Add(TableElement TableElement)
166  {
167  if (TableElement==null)
168  {
169  throw new Exception("Cant add null to the list of table elements.");
170  }
171  if (Contains(TableElement))
172  {
173  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));
174  }
175  _TableElementsDictionary[TableElement.TableElementType].Add(TableElement.Number, TableElement);
176  base.Add(TableElement);
177  }
178 
179 
191  public void Add(TableElementTypeEnum TableElementType, int Number, int State)
192  {
193  Add(new TableElement(TableElementType, Number, State));
194  }
195 
196 
197 
198 
199 
200  #endregion
201  #region Contains
202 
203 
209  public new bool Contains(TableElement TableElement)
210  {
211  return _TableElementsDictionary[TableElement.TableElementType].ContainsKey(TableElement.Number) || base.Contains(TableElement);
212  }
213 
220  public bool Contains(TableElementTypeEnum TableElementType, int Number)
221  {
222  return _TableElementsDictionary[TableElementType].ContainsKey(Number);
223  }
224 
225 
226 
227 
228  #endregion
229  #region Remove
230 
236  public new bool Remove(TableElement TableElement)
237  {
238 
239  _TableElementsDictionary[TableElement.TableElementType].Remove(TableElement.Number);
240  return base.Remove(TableElement);
241 
242  }
243 
249  public bool Remove(TableElementTypeEnum TableElementType, int Number)
250  {
251  if (!Contains(TableElementType, Number)) return false;
252  Remove(_TableElementsDictionary[TableElementType][Number]);
253  _TableElementsDictionary[TableElementType].Remove(Number);
254 
255  return true;
256 
257  }
258  #endregion
259 
260 
261  #region Events & Event handling
262  void TableElementList_AfterSet(object sender, SetEventArgs<TableElement> e)
263  {
264  _TableElementsDictionary[e.OldItem.TableElementType].Remove(e.OldItem.Number);
265  e.OldItem.ValueChanged -= new EventHandler<TableElementValueChangedEventArgs>(Item_ValueChanged);
266  _TableElementsDictionary[e.NewItem.TableElementType].Add(e.NewItem.Number, e.NewItem);
267  e.NewItem.ValueChanged += new EventHandler<TableElementValueChangedEventArgs>(Item_ValueChanged);
268  }
269 
270  void TableElementList_BeforeSet(object sender, SetEventArgs<TableElement> e)
271  {
272  if (!Contains(e.NewItem.TableElementType, e.NewItem.Number) || (e.NewItem.TableElementType == e.OldItem.TableElementType && e.NewItem.Number == e.OldItem.Number))
273  {
274 
275  }
276  else
277  {
278  throw new Exception("Another TableElement with type {0} and number {1} does already exist in the list.".Build(e.NewItem.TableElementType, e.NewItem.Number));
279  }
280  }
281 
282 
283  void TableElementList_AfterRemove(object sender, RemoveEventArgs<TableElement> e)
284  {
285  e.Item.ValueChanged -= new EventHandler<TableElementValueChangedEventArgs>(Item_ValueChanged);
286  }
287 
288  void TableElementList_AfterInsert(object sender, InsertEventArgs<TableElement> e)
289  {
290  e.Item.ValueChanged += new EventHandler<TableElementValueChangedEventArgs>(Item_ValueChanged);
291  }
292 
293  void Item_ValueChanged(object sender, TableElementValueChangedEventArgs e)
294  {
295  OnTableElementValueChanged(e);
296  }
297 
298  #region "ableElement Value Changed Event
299 
300 
301  private void OnTableElementValueChanged(TableElementValueChangedEventArgs e)
302  {
303  if (TableElementValueChanged != null)
304  {
305  TableElementValueChanged(this, e);
306  }
307  }
308 
309 
310 
314  public event TableElementValueChangedEventHandler TableElementValueChanged;
315 
321  public delegate void TableElementValueChangedEventHandler(object sender, TableElementValueChangedEventArgs e);
322 
323 
324  #endregion
325 
326  #endregion
327 
328  #region Constructor
329 
334  {
335  //Init internal dictionary
336  _TableElementsDictionary = new Dictionary<TableElementTypeEnum, Dictionary<int, TableElement>>();
337  foreach (TableElementTypeEnum T in Enum.GetValues(typeof(TableElementTypeEnum)))
338  {
339  _TableElementsDictionary.Add(T, new Dictionary<int, TableElement>());
340  }
341  this.AfterInsert += new EventHandler<InsertEventArgs<TableElement>>(TableElementList_AfterInsert);
342  this.AfterRemove += new EventHandler<RemoveEventArgs<TableElement>>(TableElementList_AfterRemove);
343  this.BeforeSet += new EventHandler<SetEventArgs<TableElement>>(TableElementList_BeforeSet);
344  this.AfterSet += new EventHandler<SetEventArgs<TableElement>>(TableElementList_AfterSet);
345  }
346 
347 
348 
349 
350 
351  #endregion
352 
357  {
358  foreach (TableElement TE in this)
359  {
360  TE.ValueChanged -= new EventHandler<TableElementValueChangedEventArgs>(Item_ValueChanged);
361 
362  }
363 
364  }
365  }
366 }