WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
AvailableEffectsInfo.cs
Go to the documentation of this file.
1 using System;
2 using System.Data;
3 using System.IO;
4 using System.Linq;
5 using System.Reflection;
6 using System.Windows.Forms;
7 using System.Xml.Serialization;
8 using DirectOutput.FX;
9 
10 namespace DirectOutput.Frontend
11 {
12  public partial class AvailableEffectsInfo : Form
13  {
14  private void UpdateAvailableTypes()
15  {
16  DataTable DT = new DataTable();
17  DT.Columns.Add("Effect type", typeof(string));
18  DT.Columns.Add("Source", typeof(string));
19  Assembly A=System.Reflection.Assembly.GetExecutingAssembly();
20  foreach (Type T in AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => typeof(IEffect).IsAssignableFrom(p) && !p.IsAbstract))
21  {
22  DT.Rows.Add(T.Name,(A==T.Assembly?"Built in":"Scripted"));
23  }
24 
25  AvailableEffects.ClearSelection();
26  AvailableEffects.Columns.Clear();
27  AvailableEffects.AutoGenerateColumns = true;
28  AvailableEffects.DataSource = DT;
29  AvailableEffects.Refresh();
30  AvailableEffects.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
31 
32  UpdateTypeXml();
33  }
34 
35 
36  private void UpdateTypeXml()
37  {
38  string S = "";
39  if (AvailableEffects.SelectedRows.Count > 0)
40  {
41  string N = AvailableEffects.SelectedRows[0].Cells[0].Value.ToString();
42  General.TypeList Types = new General.TypeList(AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => typeof(IEffect).IsAssignableFrom(p) && !p.IsAbstract));
43  if (Types.Contains(N))
44  {
45  Type T = Types[N];
46 
47 
48  object O = Activator.CreateInstance(T);
49 
50 
51  foreach (PropertyInfo PI in T.GetProperties(BindingFlags.Instance | BindingFlags.Public))
52  {
53  if (PI.CanWrite)
54  {
55  if (PI.PropertyType == typeof(string) && PI.Name == "Name")
56  {
57  PI.SetValue(O, "Effect name", null);
58  }
59  else if (PI.PropertyType.IsNumber())
60  {
61  // PI.SetValue(O, 0, null);
62  }
63  else if (PI.PropertyType == typeof(bool))
64  {
65  PI.SetValue(O, false, null);
66  }
67  else if (PI.PropertyType == typeof(string) && PI.Name.ToLower().Contains("output"))
68  {
69  PI.SetValue(O, "Name of a output", null);
70  }
71  else if (PI.PropertyType == typeof(string) && PI.Name.ToLower().Contains("toy"))
72  {
73  PI.SetValue(O, "Name of a toy", null);
74  }
75  else if (PI.PropertyType == typeof(string))
76  {
77  string V = (string)PI.GetValue(O, null);
78  if (V.IsNullOrWhiteSpace())
79  {
80  PI.SetValue(O, "{0} value".Build(PI.Name), null);
81  }
82  }
83  else if (PI.PropertyType == typeof(DateTime))
84  {
85  PI.SetValue(O, DateTime.MaxValue, null);
86  }
87  }
88  }
89 
90  try
91  {
92  XmlSerializerNamespaces EmptyNamepsaces = new XmlSerializerNamespaces(new[] { System.Xml.XmlQualifiedName.Empty });
93  var Serializer = new XmlSerializer(T);
94  var Settings = new System.Xml.XmlWriterSettings();
95  Settings.Indent = true;
96  Settings.OmitXmlDeclaration = true;
97 
98 
99  using (var Stream = new StringWriter())
100  using (var Writer = System.Xml.XmlWriter.Create(Stream, Settings))
101  {
102  Serializer.Serialize(Writer, O, EmptyNamepsaces);
103  S = Stream.ToString();
104  }
105 
106  }
107  catch (Exception E)
108  {
109  S = "XML Serialization failed.\nException:\n{0}".Build(E.Message);
110  }
111 
112  }
113  else
114  {
115  S = "Effect not found";
116  }
117 
118  }
119  TypeXml.Text = S;
120  }
122  {
123  InitializeComponent();
124  UpdateAvailableTypes();
125  }
126 
127 
128 
129  private void CopyToClipboard_Click(object sender, EventArgs e)
130  {
131  if (!TypeXml.Text.IsNullOrWhiteSpace())
132  {
133  Clipboard.SetText(TypeXml.Text);
134  }
135  }
136 
137  private void AvailableEffects_SelectionChanged(object sender, EventArgs e)
138  {
139  UpdateTypeXml();
140  }
141 
142 
143  }
144 }
Common interface for all effects. If a new effect is implemented it is best to inherit from the abst...
Definition: IEffect.cs:13
The namespace FX contains effect related classes. Effects can be assigned directly to a Table and wi...