WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
Table.cs
Go to the documentation of this file.
1 using System;
2 using System.IO;
3 using System.Text;
4 using System.Xml.Serialization;
5 using DirectOutput.FX;
6 
9 
10 
15 {
16 
20  public class Table
21  {
22  #region Properties
23  public TableElementList TableElements { get; set; }
28 
35  [XmlIgnoreAttribute]
36  public Pinball Pinball { get; private set; }
37 
38 
39  private FastImageList _Bitmaps = new FastImageList();
40 
47  [XmlIgnoreAttribute]
48  public FastImageList Bitmaps
49  {
50  get { return _Bitmaps; }
51  private set { _Bitmaps = value; }
52  }
53 
54  private ShapeDefinitions _ShapeDefinitions;
55 
62  [XmlIgnore]
64  {
65  get { return _ShapeDefinitions; }
66  set { _ShapeDefinitions = value; }
67  }
68 
69 
70 
71 
72 
73 
74 
75  #region TableName
76  private string _TableName;
81  public string TableName
82  {
83  get { return _TableName; }
84  set
85  {
86  if (_TableName != value)
87  {
88  _TableName = value;
89  if (TableNameChanged != null)
90  {
91  TableNameChanged(this, new EventArgs());
92  }
93  }
94  }
95  }
99  public event EventHandler<EventArgs> TableNameChanged;
100  #endregion
101 
102  #region RomName
103  private string _RomName;
108  [XmlIgnoreAttribute]
109  public string RomName
110  {
111  get { return _RomName; }
112  set
113  {
114  if (_RomName != value)
115  {
116  _RomName = value;
117  if (RomNameChanged != null)
118  {
119  RomNameChanged(this, new EventArgs());
120  }
121  }
122  }
123  }
127  public event EventHandler<EventArgs> RomNameChanged;
128  #endregion
129 
136  [XmlIgnoreAttribute]
137  public string TableFilename { get; set; }
138 
145  [XmlIgnoreAttribute]
146  public string TableConfigurationFilename { get; set; }
147 
148 
149  private bool _AddLedControlConfig = false;
150 
157  public bool AddLedControlConfig
158  {
159  get { return _AddLedControlConfig; }
160  set { _AddLedControlConfig = value; }
161  }
162 
163 
164  private TableConfigSourceEnum _ConfigurationSource = TableConfigSourceEnum.Unknown;
165 
172  [XmlIgnoreAttribute]
173  public TableConfigSourceEnum ConfigurationSource
174  {
175  get { return _ConfigurationSource; }
176  set { _ConfigurationSource = value; }
177  }
178 
179 
180 
181 
182  private EffectList _Effects;
186  public EffectList Effects
187  {
188  get { return _Effects; }
189  set { _Effects = value; }
190  }
191 
192  private AssignedEffectList _AssignedStaticEffects;
200  public AssignedEffectList AssignedStaticEffects
201  {
202  get
203  {
204  return _AssignedStaticEffects;
205  }
206  set
207  {
208  _AssignedStaticEffects = value;
209  }
210  }
211 
212  #endregion
213  public void UpdateTableElement(TableElementData Data)
218  {
219 
220  TableElements.UpdateState(Data);
221  }
222 
223 
227  public void TriggerStaticEffects()
228  {
229  AssignedStaticEffects.Trigger(new TableElementData(TableElementTypeEnum.Unknown, 0, 1));
230  }
231 
236  public void Init(Pinball Pinball)
237  {
238  this.Pinball = Pinball;
239 
240  FileInfo ShapeDefinitionFile = Pinball.GlobalConfig.GetShapeDefinitionFile();
241  if (ShapeDefinitionFile != null && ShapeDefinitionFile.Exists)
242  {
243  Log.Write("Loading shape definition file: {0}".Build(ShapeDefinitionFile.FullName));
244  try
245  {
247  }
248  catch (Exception E)
249  {
250  Log.Exception("Loading shape definition file {0} failed.".Build(ShapeDefinitionFile.FullName), E);
251  }
252  ShapeDefinitions.BitmapFilePattern = new General.FilePattern(ShapeDefinitionFile.FullName.Substring(0, ShapeDefinitionFile.FullName.Length - ShapeDefinitionFile.Extension.Length) + ".png");
253  }
254  else
255  {
256  if (ShapeDefinitionFile == null)
257  {
258  Log.Warning("Could not determin name of shape definition file");
259  }
260  else
261  {
262  Log.Warning("Shape definition file {0} does not exist");
263  }
265  }
266 
267  Effects.Init(this);
268 
269  TableElements.InitAssignedEffects(this);
270  AssignedStaticEffects.Init(this);
271  }
272 
276  public void Finish()
277  {
278  AssignedStaticEffects.Finish();
279  TableElements.FinishAssignedEffects();
280  Effects.Finish();
281  Pinball = null;
282  }
283 
284 
285  #region Serialization
286 
291  public string GetConfigXml()
292  {
293  string Xml = "";
294  using (MemoryStream ms = new MemoryStream())
295  {
296  new XmlSerializer(typeof(Table)).Serialize(ms, this);
297  ms.Position = 0;
298 
299  using (StreamReader sr = new StreamReader(ms, Encoding.Default))
300  {
301  Xml = sr.ReadToEnd();
302 
303  }
304 
305  }
306  return Xml;
307  }
308 
309 
314  public void SaveConfigXmlFile(string FileName)
315  {
316  GetConfigXml().WriteToFile(FileName);
317  }
318 
319 
325  public static Table GetTableFromConfigXmlFile(string FileName)
326  {
327  string Xml;
328 
329  try
330  {
331  Xml = General.FileReader.ReadFileToString(FileName);
332 
333 
334 
335  }
336  catch (Exception E)
337  {
338  Log.Exception("Could not read Table Config file {0}".Build(FileName), E);
339  throw new Exception("Could not read Table Config file {0}".Build(FileName), E);
340  }
341  return GetTableFromConfigXml(Xml);
342  }
343 
349  public static Table GetTableFromConfigXmlFile(FileInfo TableConfigFile)
350  {
351  return GetTableFromConfigXmlFile(TableConfigFile.FullName);
352  }
353 
359  public static Table GetTableFromConfigXml(string ConfigXml)
360  {
361 
362  try
363  {
364  byte[] xmlBytes = Encoding.Default.GetBytes(ConfigXml);
365  using (MemoryStream ms = new MemoryStream(xmlBytes))
366  {
367  Table T = (Table)new XmlSerializer(typeof(Table)).Deserialize(ms);
368  T.ConfigurationSource = TableConfigSourceEnum.TableConfigurationFile;
369  return T;
370  }
371  }
372  catch (Exception E)
373  {
374 
375  Exception Ex = new Exception("Could not deserialize Table config from XML Data", E);
376  Ex.Data.Add("XMLData", ConfigXml);
377  Log.Exception(Ex);
378  throw Ex;
379  }
380  }
381  #endregion
382 
383 
384 
388  public Table()
389  {
390  Effects = new FX.EffectList();
391  TableElements = new TableElementList();
392  AssignedStaticEffects = new AssignedEffectList();
393  }
394 
395 
396 
397  }
398 }
FilePattern BitmapFilePattern
Gets or sets the file pattern which is used to load the bitmap file for the effect.
void Finish()
Finishes the table and the contained objects (Effects, TableElements)
Definition: Table.cs:276
EventHandler< EventArgs > RomNameChanged
Event is fired if the value of the property RomName is changed.
Definition: Table.cs:127
static Table GetTableFromConfigXmlFile(FileInfo TableConfigFile)
Instanciates a Table object from a Table configuration in a XML file.
Definition: Table.cs:349
static void Warning(string Message)
Writes a warning message to the log.
Definition: Log.cs:134
GlobalConfig GlobalConfig
Gets the global config for the Pinnball object.
Definition: Pinball.cs:85
void TriggerStaticEffects()
Triggers the static effects for the table.
Definition: Table.cs:227
TableElementTypeEnum
Enum for the different TableElement types.
Pinball is the main object of the DirectOutput framework. It holds all objects required to process P...
Definition: Pinball.cs:23
static void Write(string Message)
Writes the specified message to the logfile.
Definition: Log.cs:99
TableConfigSourceEnum ConfigurationSource
Gets or sets the configuration source.
Definition: Table.cs:174
A simple logger used to record important events and exceptions.
Definition: Log.cs:14
void Init(Pinball Pinball)
Initializes the table and the contained objects(Effects, TableElements).
Definition: Table.cs:236
TableConfigSourceEnum
Enum used to specify the source of a table configuration
static Table GetTableFromConfigXml(string ConfigXml)
Instanciates a Table object from a Table configuration in a XML string.
Definition: Table.cs:359
static ShapeDefinitions GetShapeDefinitionsFromShapeDefinitionsXmlFile(string FileName)
Instanciates a ShapeDefinitions object from a ShapeDefinitions ShapeDefinitionsuration in a XML file...
Data representing the state of a table emlement
List of TableElement objects.
string GetConfigXml()
Returns a serialized XML representation of the Table configuration.
Definition: Table.cs:291
EventHandler< EventArgs > TableNameChanged
Event is fired if the value of the property TableName is changed.
Definition: Table.cs:99
static Table GetTableFromConfigXmlFile(string FileName)
Instanciates a Table object from a Table configuration in a XML file.
Definition: Table.cs:325
Holds all table specific information and handles all TableElements
Definition: Table.cs:20
void SaveConfigXmlFile(string FileName)
Serializes the Table configuration to a XML file.
Definition: Table.cs:314
The namespace FX contains effect related classes. Effects can be assigned directly to a Table and wi...
Table()
Initializes a new instance of the Table class.
Definition: Table.cs:388
The namespace DirectOutput.General contains classes for general use.
Effects in this namespace are controlling toys which implement the IMatrix interface ...
List of effects which are assigned to some other object.
static void Exception(string Message, Exception E=null)
Writes a exception message to the log.
Definition: Log.cs:144
Collection of IEffect objects. Every object can only exist once in the list and every objects needs t...
Definition: EffectList.cs:14