WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
CabinetInfo.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using DirectOutput.Cab;
10 using DirectOutput.Cab.Out;
11 using System.Reflection;
12 using DirectOutput.Cab.Toys;
13 using System.IO;
14 
15 namespace DirectOutput.Frontend
16 {
17  public partial class CabinetInfo : Form
18  {
19  #region Constructor
20  public CabinetInfo() : this(new Cabinet()) { }
21 
23  {
24 
25  InitializeComponent();
26 
27 
28  this.Cabinet = Cabinet;
29 
30  }
31  #endregion
32 
33 
34 
35  private void UpdateToys()
36  {
37  DataTable DT = new DataTable();
38  DT.Columns.Add("Name", typeof(string));
39  DT.Columns.Add("Type", typeof(string));
40 
41  foreach (IToy T in Cabinet.Toys)
42  {
43  DT.Rows.Add(T.Name, T.GetType().Name);
44  }
45  CabinetToys.ClearSelection();
46  CabinetToys.Columns.Clear();
47  CabinetToys.AutoGenerateColumns = true;
48  CabinetToys.DataSource = DT;
49  CabinetToys.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
50 
51  CabinetToys.Refresh();
52 
53  UpdateToyProperties(null);
54 
55  }
56 
57  private void UpdateToyProperties(IToy Toy)
58  {
59  DataTable DT = new DataTable();
60  DT.Columns.Add("Property", typeof(string));
61  DT.Columns.Add("Value", typeof(string));
62  if (Toy != null)
63  {
64  DT.Rows.Add("Name", Toy.Name);
65  DT.Rows.Add("Type", Toy.GetType().Name);
66 
67 
68  Type T = Toy.GetType();
69 
70  foreach (PropertyInfo PI in T.GetProperties(BindingFlags.Instance | BindingFlags.Public))
71  {
72  if (PI.Name != "Name")
73  {
74  DT.Rows.Add(PI.Name, PI.GetValue(Toy, new object[] { }).ToString());
75  }
76  }
77  }
78  CabinetToyProperties.ClearSelection();
79  CabinetToyProperties.Columns.Clear();
80  CabinetToyProperties.AutoGenerateColumns = true;
81  CabinetToyProperties.DataSource = DT;
82  CabinetToyProperties.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
83 
84  CabinetToyProperties.Refresh();
85 
86  }
87 
88 
89 
90  private void UpdateOutputControllerProperties(IOutputController OutputController)
91  {
92  DataTable DT = new DataTable();
93  DT.Columns.Add("Property", typeof(string));
94  DT.Columns.Add("Value", typeof(string));
95  if (OutputController != null)
96  {
97  DT.Rows.Add("Name", OutputController.Name);
98  DT.Rows.Add("Output Count", OutputController.Outputs.Count);
99 
100  Type T = OutputController.GetType();
101 
102  foreach (PropertyInfo PI in T.GetProperties(BindingFlags.Instance | BindingFlags.Public))
103  {
104  if (PI.Name != "Name" && PI.Name != "Outputs")
105  {
106  DT.Rows.Add(PI.Name, PI.GetValue(OutputController, new object[] { }).ToString());
107  }
108  }
109  }
110  CabinetOutputControllerProperties.ClearSelection();
111  CabinetOutputControllerProperties.Columns.Clear();
112  CabinetOutputControllerProperties.AutoGenerateColumns = true;
113 
114  CabinetOutputControllerProperties.DataSource = DT;
115  CabinetOutputControllerProperties.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
116  CabinetOutputControllerProperties.Refresh();
117 
118  }
119 
120  private void UpdateOutputs(IOutputController OutputController)
121  {
122  DataTable DT = new DataTable();
123  DT.Columns.Add("Name", typeof(string));
124  if (OutputController != null)
125  {
126  if (OutputController.Outputs.Count > 0)
127  {
128  Type T = OutputController.Outputs[0].GetType();
129 
130  foreach (PropertyInfo PI in T.GetProperties(BindingFlags.Instance | BindingFlags.Public))
131  {
132  if (PI.Name != "Name")
133  {
134  DT.Columns.Add(PI.Name, PI.PropertyType);
135  }
136  }
137 
138  foreach (IOutput O in OutputController.Outputs)
139  {
140  object[] values = new object[DT.Columns.Count];
141  values[0] = O.Name;
142  int Index = 1;
143  foreach (PropertyInfo PI in T.GetProperties(BindingFlags.Instance | BindingFlags.Public))
144  {
145  if (PI.Name != "Name")
146  {
147  values[Index] = PI.GetValue(O, new object[] { });
148  Index++;
149  }
150  }
151  DT.Rows.Add(values);
152  }
153  }
154  CabinetOutputControllerOutputs.ClearSelection();
155  CabinetOutputControllerOutputs.Columns.Clear();
156  CabinetOutputControllerOutputs.AutoGenerateColumns = true;
157  CabinetOutputControllerOutputs.DataSource = DT;
158  CabinetOutputControllerOutputs.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
159  CabinetOutputControllerOutputs.Refresh();
160  }
161 
162  }
163 
164 
165 
166 
167  private void UpdateOutputControllers()
168  {
169  DataTable OutputControllersDataTable = new DataTable();
170  OutputControllersDataTable.Columns.Add("Name", typeof(string));
171  OutputControllersDataTable.Columns.Add("Type", typeof(string));
172  OutputControllersDataTable.Columns.Add("Output Count", typeof(int));
173  OutputControllersDataTable.Rows.Clear();
175  {
176  OutputControllersDataTable.Rows.Add(OC.Name, OC.GetType().Name, OC.Outputs.Count);
177  }
178  CabinetOutputControllers.ClearSelection();
179  CabinetOutputControllers.AutoGenerateColumns = true;
180  CabinetOutputControllers.DataSource = OutputControllersDataTable;
181  CabinetOutputControllers.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
182  CabinetOutputControllers.Refresh();
183 
184  UpdateOutputs(null);
185  UpdateOutputControllerProperties(null);
186 
187  }
188 
189  private void UpdateWindowTitle()
190  {
191  string S = "Cabinet Configuration";
192  if (!Cabinet.Name.IsNullOrWhiteSpace())
193  {
194  S += " - {0}".Build(Cabinet.Name);
195  }
196  if (!Cabinet.CabinetConfigurationFilename.IsNullOrWhiteSpace())
197  {
198  S += " - {0}".Build(Cabinet.CabinetConfigurationFilename);
199  }
200  else
201  {
202  S += " - <unsaved cabinet configuration>";
203  }
204  this.Text = S;
205  }
206 
207 
208  private Cabinet _Cabinet = new Cabinet();
209 
210  public Cabinet Cabinet
211  {
212  get { return _Cabinet; }
213  set
214  {
215  _Cabinet = value;
216  UpdateOutputControllers();
217  UpdateToys();
218  UpdateWindowTitle();
219 
220 
221  }
222  }
223 
224  private void CabinetOutputControllers_SelectionChanged(object sender, EventArgs e)
225  {
226  if (CabinetOutputControllers.SelectedRows.Count > 0)
227  {
228  string N = (string)CabinetOutputControllers.SelectedRows[0].Cells[0].Value;
230  {
231  UpdateOutputs(Cabinet.OutputControllers[N]);
232  UpdateOutputControllerProperties(Cabinet.OutputControllers[N]);
233  }
234  else
235  {
236  UpdateOutputs(null);
237  UpdateOutputControllerProperties(null);
238  }
239  }
240  }
241 
242  private void CabinetToys_SelectionChanged(object sender, EventArgs e)
243  {
244  if (CabinetToys.SelectedRows.Count > 0)
245  {
246 
247  string N = (string)CabinetToys.SelectedRows[0].Cells[0].Value;
248  if (Cabinet.Toys.Contains(N))
249  {
250  UpdateToyProperties(Cabinet.Toys[N]);
251  }
252  else
253  {
254  UpdateToyProperties(null);
255  }
256  }
257  }
258 
259  private void ExportCabinetConfiguration_Click(object sender, EventArgs e)
260  {
261  if (!Cabinet.CabinetConfigurationFilename.IsNullOrWhiteSpace())
262  {
263  FileInfo FI = new FileInfo(Cabinet.CabinetConfigurationFilename);
264  SaveCabinetConfiguration.InitialDirectory = FI.Directory.FullName;
265  SaveCabinetConfiguration.FileName = FI.FullName;
266  }
267  else
268  {
269  SaveCabinetConfiguration.InitialDirectory = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
270  }
271  if (SaveCabinetConfiguration.ShowDialog() == DialogResult.OK)
272  {
273  Cabinet.CabinetConfigurationFilename = SaveCabinetConfiguration.FileName;
274  try
275  {
276  Cabinet.SaveConfigXmlFile(SaveCabinetConfiguration.FileName);
277  UpdateWindowTitle();
278  MessageBox.Show("Cabinet configuration saved to\n{0}".Build(SaveCabinetConfiguration.FileName), "Cabinet configuration saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
279  }
280  catch (Exception E)
281  {
282  MessageBox.Show("Could not save cabinet config to\n{0}\n\nThe following error occured:\n{1}".Build(SaveCabinetConfiguration.FileName, E.Message), "File save error", MessageBoxButtons.OK, MessageBoxIcon.Error);
283  }
284  }
285 
286  }
287 
288 
289 
290 
291 
292 
293 
294 
295 
296 
297 
298  }
299 }
The Cabinet object describes the parts of a pinball cabinet (toys, outputcontrollers, outputs and more).
Definition: Cabinet.cs:17
new string Name
Name of the IOutputController. This property is fully implemented in the abstract OutputControllerBas...
The namespace DirectOutput.Cab.Toys contains all toy related classes.
bool Contains(string Name)
Checks if a INamedItem object with the specified name exists in the list.
OutputList Outputs
OutputList containing the IOutput objects for a IOutputController.
void SaveConfigXmlFile(string FileName)
Serializes the cabinet configuration to a XML file.
Definition: Cabinet.cs:213
string Name
Gets or sets the name of the item. Must fire the BeforeNameChange and AfterNameChange events when th...
Definition: INamedItem.cs:17
DirectOutput.Cab.Toys.ToyList Toys
List of IToy objects describing the toys in the cabinet.
Definition: Cabinet.cs:107
string Name
Name of the Cabinet.
Definition: Cabinet.cs:89
Out.OutputControllerList OutputControllers
List of IOutputController objects representing the output controllers in the cabinet.
Definition: Cabinet.cs:179
Common interface for all toy implementations. The abstract class ToyBase implements this interface...
Definition: IToy.cs:9
The namespace DirectOutput.Cab contains all cabinet related classes like the Cabinet class itself...
Definition: Cab.cs:16
DirectOutput.Cab.Out is the namespace for all output controller related classes like different output...
Common interface for outputs of any output controller. The Output class implements this interface and...
Definition: IOutput.cs:10
Common interface for all outputcontrollers. Only classes implementing this interface can be used as o...
new string Name
Gets or sets the Name of the IToy.
Definition: IToy.cs:17
string CabinetConfigurationFilename
Gets or sets the filename from which the cabiet configuration was loaded.
Definition: Cabinet.cs:98