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
Configurator.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using DirectOutput.Cab;
5 using DirectOutput.Cab.Out;
6 using DirectOutput.Cab.Toys;
7 using DirectOutput.Cab.Toys.Layer;
8 using DirectOutput.Cab.Toys.LWEquivalent;
9 using DirectOutput.FX;
10 using DirectOutput.FX.AnalogToyFX;
11 using DirectOutput.FX.ConditionFX;
12 using DirectOutput.FX.MatrixFX;
13 using DirectOutput.FX.RGBAFX;
14 using DirectOutput.FX.TimmedFX;
15 using DirectOutput.FX.ValueFX;
16 using DirectOutput.General;
17 using DirectOutput.General.Analog;
18 using DirectOutput.General.Color;
19 using DirectOutput.LedControl.Loader;
20 
21 namespace DirectOutput.LedControl.Setup
22 {
26  public class Configurator
27  {
31  public int EffectMinDurationMs = 60;
35  public int EffectRGBMinDurationMs = 120;
36 
44  public void Setup(LedControlConfigList LedControlConfigList, DirectOutput.Table.Table Table, Cabinet Cabinet, string RomName)
45  {
46  Dictionary<int, TableConfig> TableConfigDict = LedControlConfigList.GetTableConfigDictonary(RomName);
47 
48  string IniFilePath = "";
49  if (LedControlConfigList.Count > 0)
50  {
51  IniFilePath = LedControlConfigList[0].LedControlIniFile.Directory.FullName;
52  }
53 
54  Dictionary<int, Dictionary<int, IToy>> ToyAssignments = SetupCabinet(TableConfigDict, Cabinet);
55 
56 
57 
58  SetupTable(Table, TableConfigDict, ToyAssignments, IniFilePath);
59 
60 
61  }
62 
63  private void SetupTable(Table.Table Table, Dictionary<int, TableConfig> TableConfigDict, Dictionary<int, Dictionary<int, IToy>> ToyAssignments, string IniFilePath)
64  {
65  foreach (KeyValuePair<int, TableConfig> KV in TableConfigDict)
66  {
67  int LedWizNr = KV.Key;
68  if (ToyAssignments.ContainsKey(LedWizNr))
69  {
70  TableConfig TC = KV.Value;
71 
72 
73  foreach (TableConfigColumn TCC in TC.Columns)
74  {
75 
76  if (ToyAssignments[LedWizNr].ContainsKey(TCC.Number))
77  {
78  IToy Toy = ToyAssignments[LedWizNr][TCC.Number];
79 
80  int SettingNumber = 0;
81  foreach (TableConfigSetting TCS in TCC)
82  {
83  SettingNumber++;
84  IEffect Effect = null;
85 
86  int Layer = (TCS.Layer.HasValue ? TCS.Layer.Value : SettingNumber);
87 
88  if (Toy is IMatrixToy<RGBAColor> || Toy is IMatrixToy<AnalogAlpha>)
89  {
90 
91  if (TCS.IsBitmap)
92  {
93  FilePattern P = new FilePattern("{0}\\{1}.*".Build(IniFilePath, TC.ShortRomName));
94 
95  if (TCS.AreaBitmapAnimationStepCount > 1)
96  {
97  //it is a animation
98  if (Toy is IMatrixToy<RGBAColor>)
99  {
100  Effect = new RGBAMatrixBitmapAnimationEffect() { BitmapFilePattern = P, BitmapLeft = TCS.AreaBitmapLeft, BitmapTop = TCS.AreaBitmapTop, BitmapHeight = TCS.AreaBitmapHeight, BitmapWidth = TCS.AreaBitmapWidth, BitmapFrameNumber = TCS.AreaBitmapFrame, AnimationStepDirection = TCS.AreaBitmapAnimationDirection, AnimationFrameDurationMs = TCS.AreaBitmapAnimationFrameDuration, AnimationFrameCount = TCS.AreaBitmapAnimationStepCount, AnimationStepSize = TCS.AreaBitmapAnimationStepSize, AnimationBehaviour=TCS.AreaBitmapAnimationBehaviour, Height = TCS.AreaHeight, Width = TCS.AreaWidth, Top = TCS.AreaTop, Left = TCS.AreaLeft, LayerNr = Layer, ToyName = Toy.Name };
101  }
102  else
103  {
104  Effect = new AnalogAlphaMatrixBitmapAnimationEffect() { BitmapFilePattern = P, BitmapLeft = TCS.AreaBitmapLeft, BitmapTop = TCS.AreaBitmapTop, BitmapHeight = TCS.AreaBitmapHeight, BitmapWidth = TCS.AreaBitmapWidth, BitmapFrameNumber = TCS.AreaBitmapFrame, AnimationStepDirection = TCS.AreaBitmapAnimationDirection, AnimationFrameDurationMs = TCS.AreaBitmapAnimationFrameDuration, AnimationFrameCount = TCS.AreaBitmapAnimationStepCount, AnimationStepSize = TCS.AreaBitmapAnimationStepSize, AnimationBehaviour = TCS.AreaBitmapAnimationBehaviour, Height = TCS.AreaHeight, Width = TCS.AreaWidth, Top = TCS.AreaTop, Left = TCS.AreaLeft, LayerNr = Layer, ToyName = Toy.Name };
105  }
106  }
107  else
108  {
109  //its a static bitmap
110  if (Toy is IMatrixToy<RGBAColor>)
111  {
112  Effect = new RGBAMatrixBitmapEffect() { BitmapFilePattern = P, BitmapLeft = TCS.AreaBitmapLeft, BitmapTop = TCS.AreaBitmapTop, BitmapHeight = TCS.AreaBitmapHeight, BitmapWidth = TCS.AreaBitmapWidth, BitmapFrameNumber = TCS.AreaBitmapFrame, Height = TCS.AreaHeight, Width = TCS.AreaWidth, Top = TCS.AreaTop, Left = TCS.AreaLeft, LayerNr = Layer, ToyName = Toy.Name };
113  }
114  else
115  {
116  Effect = new AnalogAlphaMatrixBitmapEffect() { BitmapFilePattern = P, BitmapLeft = TCS.AreaBitmapLeft, BitmapTop = TCS.AreaBitmapTop, BitmapHeight = TCS.AreaBitmapHeight, BitmapWidth = TCS.AreaBitmapWidth, BitmapFrameNumber = TCS.AreaBitmapFrame, Height = TCS.AreaHeight, Width = TCS.AreaWidth, Top = TCS.AreaTop, Left = TCS.AreaLeft, LayerNr = Layer, ToyName = Toy.Name };
117  }
118  }
119  }
120  else
121  {
122  //Non bitmap area effects
123  if (Toy is IMatrixToy<RGBAColor>)
124  {
125  //RGBAMatrix toy
126 
127  RGBAColor ActiveColor = null;
128  if (TCS.ColorConfig != null)
129  {
130  ActiveColor = TCS.ColorConfig.GetCabinetColor().GetRGBAColor();
131  }
132  else
133  {
134  if (!TCS.ColorName.IsNullOrWhiteSpace())
135  {
136  if (TCS.ColorName.StartsWith("#"))
137  {
138  ActiveColor = new RGBAColor();
139  if (!ActiveColor.SetColor(TCS.ColorName))
140  {
141  ActiveColor = null;
142  }
143  }
144  }
145  }
146 
147  if (ActiveColor != null)
148  {
149  RGBAColor InactiveColor = ActiveColor.Clone();
150  InactiveColor.Alpha = 0;
151  if (TCS.AreaDirection.HasValue)
152  {
153  //shift effect
154  Effect = new RGBAMatrixShiftEffect() { ShiftDirection = TCS.AreaDirection.Value, ShiftAcceleration = TCS.AreaAcceleration, ActiveColor = ActiveColor, InactiveColor = InactiveColor, Height = TCS.AreaHeight, Width = TCS.AreaWidth, Top = TCS.AreaTop, Left = TCS.AreaLeft, LayerNr = Layer, ToyName = Toy.Name };
155  if (TCS.AreaSpeed > 0)
156  {
157  ((RGBAMatrixShiftEffect)Effect).ShiftSpeed = TCS.AreaSpeed;
158  }
159 
160  }
161  else if (TCS.AreaFlickerDensity > 0)
162  {
163  //flicker effect
164  Effect = new RGBAMatrixFlickerEffect() { Density = TCS.AreaFlickerDensity.Limit(1, 99), ActiveColor = ActiveColor, InactiveColor = InactiveColor, Height = TCS.AreaHeight, Width = TCS.AreaWidth, Top = TCS.AreaTop, Left = TCS.AreaLeft, LayerNr = Layer, ToyName = Toy.Name };
165  if (TCS.AreaFlickerMinDurationMs > 0)
166  {
167  ((RGBAMatrixFlickerEffect)Effect).MinFlickerDurationMs = TCS.AreaFlickerMinDurationMs;
168  }
169  if (TCS.AreaFlickerMaxDurationMs > 0)
170  {
171  ((RGBAMatrixFlickerEffect)Effect).MaxFlickerDurationMs = TCS.AreaFlickerMaxDurationMs;
172  }
173 
174  }
175  else
176  {
177  //Color effect
178  Effect = new RGBAMatrixColorEffect() { ActiveColor = ActiveColor, InactiveColor = InactiveColor, Height = TCS.AreaHeight, Width = TCS.AreaWidth, Top = TCS.AreaTop, Left = TCS.AreaLeft, LayerNr = Layer, ToyName = Toy.Name };
179  }
180 
181  }
182  else
183  {
184  Log.Warning("No color valid color definition found for area effect. Skipped setting {0} in column {1} for LedWizEqivalent number {2}.".Build(SettingNumber, TCC.Number, LedWizNr));
185 
186  }
187  }
188  else if (Toy is IMatrixToy<AnalogAlpha>)
189  {
190  AnalogAlpha ActiveValue = new AnalogAlpha(TCS.Intensity.Limit(0, 255), 255);
191  AnalogAlpha InactiveValue = ActiveValue.Clone();
192  InactiveValue.Alpha = 0;
193 
194 
195  if (TCS.AreaDirection.HasValue)
196  {
197  //shift effect
198  Effect = new AnalogAlphaMatrixShiftEffect() { ShiftDirection = TCS.AreaDirection.Value, ShiftAcceleration = TCS.AreaAcceleration, ActiveValue = ActiveValue, InactiveValue = InactiveValue, Height = TCS.AreaHeight, Width = TCS.AreaWidth, Top = TCS.AreaTop, Left = TCS.AreaLeft, LayerNr = Layer, ToyName = Toy.Name };
199  if (TCS.AreaSpeed > 0)
200  {
201  ((AnalogAlphaMatrixShiftEffect)Effect).ShiftSpeed = TCS.AreaSpeed;
202  }
203 
204  }
205  else if (TCS.AreaFlickerDensity > 0)
206  {
207  //flicker effect
208  Effect = new AnalogAlphaMatrixFlickerEffect() { Density = TCS.AreaFlickerDensity.Limit(1, 99), ActiveValue = ActiveValue, InactiveValue = InactiveValue, Height = TCS.AreaHeight, Width = TCS.AreaWidth, Top = TCS.AreaTop, Left = TCS.AreaLeft, LayerNr = Layer, ToyName = Toy.Name };
209  if (TCS.AreaFlickerMinDurationMs > 0)
210  {
211  ((AnalogAlphaMatrixFlickerEffect)Effect).MinFlickerDurationMs = TCS.AreaFlickerMinDurationMs;
212  }
213  if (TCS.AreaFlickerMaxDurationMs > 0)
214  {
215  ((AnalogAlphaMatrixFlickerEffect)Effect).MaxFlickerDurationMs = TCS.AreaFlickerMaxDurationMs;
216  }
217 
218  }
219  else
220  {
221  //Color effect
222  Effect = new AnalogAlphaMatrixValueEffect() { ActiveValue = ActiveValue, InactiveValue = InactiveValue, Height = TCS.AreaHeight, Width = TCS.AreaWidth, Top = TCS.AreaTop, Left = TCS.AreaLeft, LayerNr = Layer, ToyName = Toy.Name };
223  }
224 
225  }
226  }
227  }
228  else if (Toy is IRGBAToy)
229  {
230  RGBAColor ActiveColor = null;
231  if (TCS.ColorConfig != null)
232  {
233  ActiveColor = TCS.ColorConfig.GetCabinetColor().GetRGBAColor();
234  }
235  else
236  {
237  if (!TCS.ColorName.IsNullOrWhiteSpace())
238  {
239  if (TCS.ColorName.StartsWith("#"))
240  {
241  ActiveColor = new RGBAColor();
242  if (!ActiveColor.SetColor(TCS.ColorName))
243  {
244  ActiveColor = null;
245  Log.Warning("Skipped setting {0} in column {1} for LedWizEqivalent number {2} since {3} is not a valid color specification.".Build(new object[] { SettingNumber, TCC.Number, LedWizNr, TCS.ColorName }));
246  }
247  }
248  else
249  {
250  Log.Warning("Skipped setting {0} in column {1} for LedWizEqivalent number {2} since {3} is not a valid color specification.".Build(new object[] { SettingNumber, TCC.Number, LedWizNr, TCS.ColorName }));
251  }
252  }
253  else
254  {
255  Log.Warning("Skipped setting {0} in column {1} for LedWizEqivalent number {2} since it does not contain a color specification.".Build(SettingNumber, TCC.Number, LedWizNr));
256  }
257  }
258  if (ActiveColor != null)
259  {
260  RGBAColor InactiveColor = ActiveColor.Clone();
261  InactiveColor.Alpha = 0;
262  Effect = new RGBAColorEffect() { ToyName = Toy.Name, LayerNr = Layer, ActiveColor = ActiveColor, InactiveColor = InactiveColor };
263  }
264 
265  }
266  else if (Toy is IAnalogAlphaToy)
267  {
268  AnalogAlpha ActiveValue = new AnalogAlpha(TCS.Intensity.Limit(0, 255), 255);
269  AnalogAlpha InactiveValue = ActiveValue.Clone();
270  InactiveValue.Alpha = 0;
271  Effect = new AnalogToyValueEffect() { ToyName = Toy.Name, LayerNr = Layer, ActiveValue = ActiveValue, InactiveValue = InactiveValue };
272 
273  }
274  if (Effect != null)
275  {
276  Effect.Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} {3}".Build(new object[] { LedWizNr, TCC.Number, SettingNumber, Effect.GetType().Name });
277  MakeEffectNameUnique(Effect, Table);
278 
279  Table.Effects.Add(Effect);
280 
281  if (TCS.FadingUpDurationMs > 0 || TCS.FadingDownDurationMs > 0)
282  {
283  Effect = new FadeEffect() { Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} FadeEffect".Build(LedWizNr, TCC.Number, SettingNumber), TargetEffectName = Effect.Name, FadeDownDuration = TCS.FadingDownDurationMs, FadeUpDuration = TCS.FadingUpDurationMs };
284  MakeEffectNameUnique(Effect, Table);
285  Table.Effects.Add(Effect);
286  }
287  if (TCS.Blink != 0 && TCS.BlinkIntervalMsNested > 0)
288  {
289  Effect = new BlinkEffect() { Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} BlinkEffect Inner".Build(LedWizNr, TCC.Number, SettingNumber), TargetEffectName = Effect.Name, LowValue=TCS.BlinkLow, DurationActiveMs = (int)((double)TCS.BlinkIntervalMsNested * (double)TCS.BlinkPulseWidthNested / 100), DurationInactiveMs = (int)((double)TCS.BlinkIntervalMsNested * (100 - (double)TCS.BlinkPulseWidthNested) / 100) };
290  MakeEffectNameUnique(Effect, Table);
291  Table.Effects.Add(Effect);
292  }
293 
294 
295  if (TCS.Blink != 0)
296  {
297  Effect = new BlinkEffect() { Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} BlinkEffect".Build(LedWizNr, TCC.Number, SettingNumber), TargetEffectName = Effect.Name, DurationActiveMs = (int)((double)TCS.BlinkIntervalMs * (double)TCS.BlinkPulseWidth / 100), DurationInactiveMs = (int)((double)TCS.BlinkIntervalMs * (100 - (double)TCS.BlinkPulseWidth) / 100) };
298  if (TCS.BlinkIntervalMsNested == 0)
299  {
300  ((BlinkEffect)Effect).LowValue = TCS.BlinkLow;
301  }
302  MakeEffectNameUnique(Effect, Table);
303  Table.Effects.Add(Effect);
304  }
305 
306  if (TCS.DurationMs > 0 || TCS.Blink > 0)
307  {
308  int Duration = (TCS.DurationMs > 0 ? TCS.DurationMs : (TCS.Blink * 2 - 1) * TCS.BlinkIntervalMs / 2 + 1);
309  Effect = new DurationEffect() { Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} DurationEffect".Build(LedWizNr, TCC.Number, SettingNumber), TargetEffectName = Effect.Name, DurationMs = Duration, RetriggerBehaviour = RetriggerBehaviourEnum.Restart };
310  MakeEffectNameUnique(Effect, Table);
311  Table.Effects.Add(Effect);
312  }
313  if (TCS.MaxDurationMs > 0)
314  {
315 
316  Effect = new MaxDurationEffect() { Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} MaxDurationEffect".Build(new object[] { LedWizNr, TCC.Number, SettingNumber }), TargetEffectName = Effect.Name, MaxDurationMs = TCS.MaxDurationMs };
317  MakeEffectNameUnique(Effect, Table);
318  Table.Effects.Add(Effect);
319  }
320 
321 
322  if (TCS.MinDurationMs > 0 || (Toy is IRGBAToy && EffectRGBMinDurationMs > 0) || (!(Toy is IRGBAToy) && EffectMinDurationMs > 0))
323  {
324  string N = (TCS.MinDurationMs > 0 ? "MinDuratonEffect" : "DefaultMinDurationEffect");
325  int Min = (TCS.MinDurationMs > 0 ? TCS.MinDurationMs : (Toy is IRGBAToy ? EffectRGBMinDurationMs : EffectMinDurationMs));
326  Effect = new MinDurationEffect() { Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} {3}".Build(new object[] { LedWizNr, TCC.Number, SettingNumber, N }), TargetEffectName = Effect.Name, MinDurationMs = Min };
327  MakeEffectNameUnique(Effect, Table);
328  Table.Effects.Add(Effect);
329  }
330 
331  if (TCS.ExtDurationMs > 0)
332  {
333  Effect = new ExtendDurationEffect() { Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} ExtDurationEffect".Build(LedWizNr, TCC.Number, SettingNumber), TargetEffectName = Effect.Name, DurationMs = TCS.ExtDurationMs };
334  MakeEffectNameUnique(Effect, Table);
335  Table.Effects.Add(Effect);
336 
337  }
338 
339  if (TCS.WaitDurationMs > 0)
340  {
341  Effect = new DelayEffect() { Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} DelayEffect".Build(LedWizNr, TCC.Number, SettingNumber), TargetEffectName = Effect.Name, DelayMs = TCS.WaitDurationMs };
342  MakeEffectNameUnique(Effect, Table);
343  Table.Effects.Add(Effect);
344  }
345 
346  if (TCS.Invert)
347  {
348  Effect = new ValueInvertEffect() { Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} ValueInvertEffect".Build(LedWizNr, TCC.Number, SettingNumber), TargetEffectName = Effect.Name };
349  MakeEffectNameUnique(Effect, Table);
350  Table.Effects.Add(Effect);
351  }
352  if (!TCS.NoBool)
353  {
354 
355  Effect = new ValueMapFullRangeEffect() { Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} FullRangeEffect".Build(LedWizNr, TCC.Number, SettingNumber), TargetEffectName = Effect.Name };
356  MakeEffectNameUnique(Effect, Table);
357  Table.Effects.Add(Effect);
358 
359 
360  }
361  switch (TCS.OutputControl)
362  {
363  case OutputControlEnum.Condition:
364 
365  Effect = new TableElementConditionEffect() { Name = "Ledwiz {0:00} Column {1:00} Setting {2:00} TableElementConditionEffect".Build(LedWizNr, TCC.Number, SettingNumber), TargetEffectName = Effect.Name, Condition = TCS.Condition };
366  MakeEffectNameUnique(Effect, Table);
367  Table.Effects.Add(Effect);
368 
369  foreach (string Variable in ((TableElementConditionEffect)Effect).GetVariables())
370  {
371  TableElementTypeEnum TET = (TableElementTypeEnum)Variable[0];
372  int TEN = Variable.Substring(1).ToInteger();
373  if (!Table.TableElements.Contains(TET, TEN))
374  {
375  Table.TableElements.UpdateState(TET, TEN, 0);
376  }
377  Table.TableElements[TET, TEN].AssignedEffects.Add(new AssignedEffect(Effect.Name));
378  }
379 
380  break;
381 
382 
383  case OutputControlEnum.FixedOn:
384  Table.AssignedStaticEffects.Add(new AssignedEffect(Effect.Name));
385  break;
386  case OutputControlEnum.Controlled:
387 
388  string[] ATE = TCS.TableElement.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
389  foreach (string TE in ATE)
390  {
392  int TEN = TE.Substring(1).ToInteger();
393  if (!Table.TableElements.Contains(TET, TEN))
394  {
395  Table.TableElements.UpdateState(TET, TEN, 0);
396  }
397  Table.TableElements[TET, TEN].AssignedEffects.Add(new AssignedEffect(Effect.Name));
398  }
399 
400 
401  break;
402  case OutputControlEnum.FixedOff:
403  default:
404  break;
405  }
406  }
407  }
408 
409 
410  }
411  }
412  }
413  }
414  }
415 
416 
417 
418  private void MakeEffectNameUnique(IEffect Effect, Table.Table Table)
419  {
420  if (Table.Effects.Contains(Effect.Name))
421  {
422  int Cnt = 1;
423  while (Table.Effects.Contains("{0} {1}".Build(Effect.Name, Cnt)))
424  {
425  Cnt++;
426  }
427  Effect.Name = "{0} {1}".Build(Effect.Name, Cnt);
428  }
429  }
430 
431 
432 
433 
434 
435  private Dictionary<int, Dictionary<int, IToy>> SetupCabinet(Dictionary<int, TableConfig> TableConfigDict, Cabinet Cabinet)
436  {
437  Dictionary<int, Dictionary<int, IToy>> ToyAssignments = new Dictionary<int, Dictionary<int, IToy>>();
438 
439  Dictionary<int, LedWizEquivalent> LedWizEquivalentDict = new Dictionary<int, LedWizEquivalent>();
440  foreach (IToy T in Cabinet.Toys.Where(Toy => Toy is LedWizEquivalent).ToList())
441  {
442  if (!LedWizEquivalentDict.Keys.Any(K => K == ((LedWizEquivalent)T).LedWizNumber))
443  {
444  LedWizEquivalentDict.Add(((LedWizEquivalent)T).LedWizNumber, (LedWizEquivalent)T);
445  }
446  else
447  {
448  Log.Warning("Found more than one ledwiz with number {0}.".Build(((LedWizEquivalent)T).LedWizNumber));
449 
450  }
451  }
452 
453  foreach (KeyValuePair<int, TableConfig> KV in TableConfigDict)
454  {
455  int LedWizNr = KV.Key;
456  ToyAssignments.Add(LedWizNr, new Dictionary<int, IToy>());
457 
458  TableConfig TC = KV.Value;
459  if (LedWizEquivalentDict.ContainsKey(LedWizNr))
460  {
461  LedWizEquivalent LWE = LedWizEquivalentDict[LedWizNr];
462 
463  foreach (TableConfigColumn TCC in TC.Columns)
464  {
465  IToy TargetToy = null;
466 
467  if (TCC.IsArea)
468  {
469  if (LWE.Outputs.Any(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber))
470  {
471  string OutputName = LWE.Outputs.First(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber).OutputName;
472  if (Cabinet.Toys.Any(O => (O is IMatrixToy<RGBAColor> || O is IMatrixToy<AnalogAlpha>) && O.Name == OutputName))
473  {
474 
475 
476  TargetToy = (IToy)Cabinet.Toys.FirstOrDefault(O => (O is IMatrixToy<RGBAColor> || O is IMatrixToy<AnalogAlpha>) && O.Name == OutputName);
477 
478 
479  }
480 
481 
482 
483  }
484  }
485  else
486  {
487 
488  switch (TCC.RequiredOutputCount)
489  {
490  case 3:
491  //RGB Led
492 
493  if (LWE.Outputs.Any(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber))
494  {
495  string OutputName = LWE.Outputs.First(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber).OutputName;
496  if (Cabinet.Toys.Any(O => O is IRGBAToy && O.Name == OutputName))
497  {
498  TargetToy = (IToy)Cabinet.Toys.FirstOrDefault(O => O is IRGBAToy && O.Name == OutputName);
499  }
500  }
501  if (TargetToy == null)
502  {
503  if (LWE.Outputs.Any(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber) && LWE.Outputs.Any(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber + 1) && LWE.Outputs.Any(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber + 2))
504  {
505  //Try to get the toy
506  try
507  {
508  //Toy does already exist
509  TargetToy = (IToy)Cabinet.Toys.First(Toy => Toy is IRGBOutputToy && ((IRGBOutputToy)Toy).OutputNameRed == LWE.Outputs.First(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber).OutputName && ((IRGBOutputToy)Toy).OutputNameGreen == LWE.Outputs.First(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber + 1).OutputName && ((IRGBOutputToy)Toy).OutputNameBlue == LWE.Outputs.First(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber + 2).OutputName);
510 
511  }
512  catch
513  {
514  //Toy does not exist. Create toyname and toy
515  string ToyName = "LedWiz {0:00} Column {1:00}".Build(LedWizNr, TCC.Number);
516  if (Cabinet.Toys.Contains(ToyName))
517  {
518  int Cnt = 1;
519  while (Cabinet.Toys.Contains("{0} {1}".Build(ToyName, Cnt)))
520  {
521  Cnt++;
522  }
523  ToyName = "{0} {1}".Build(ToyName, Cnt);
524  }
525  TargetToy = (IToy)new RGBAToy() { Name = ToyName, OutputNameRed = LWE.Outputs.First(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber).OutputName, OutputNameGreen = LWE.Outputs.First(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber + 1).OutputName, OutputNameBlue = LWE.Outputs.First(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber + 2).OutputName };
526  Cabinet.Toys.Add(TargetToy);
527  }
528 
529 
530  }
531  }
532 
533  break;
534  case 1:
535  //Single output
536 
537  //Analog output
538 
539  if (LWE.Outputs.Any(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber))
540  {
541  string OutputName = LWE.Outputs.First(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber).OutputName;
542  if (Cabinet.Toys.Any(O => O is IAnalogAlphaToy && O.Name == OutputName))
543  {
544  TargetToy = (IToy)Cabinet.Toys.FirstOrDefault(O => O is IAnalogAlphaToy && O.Name == OutputName);
545  }
546  }
547  if (TargetToy == null)
548  {
549 
550 
551  if (LWE.Outputs.Any(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber))
552  {
553  try
554  {
555  TargetToy = Cabinet.Toys.First(Toy => Toy is ISingleOutputToy && ((ISingleOutputToy)Toy).OutputName == LWE.Outputs.First(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber).OutputName);
556  }
557  catch
558  {
559  //Toy does not exist. Create toyname and toy
560  string ToyName = "LedWiz {0:00} Column {1:00}".Build(LedWizNr, TCC.Number);
561 
562  if (Cabinet.Toys.Contains(ToyName))
563  {
564  int Cnt = 1;
565  while (Cabinet.Toys.Contains("{0} {1}".Build(ToyName, Cnt)))
566  {
567  Cnt++;
568  }
569  ToyName = "{0} {1}".Build(ToyName, Cnt);
570  }
571  TargetToy = (IToy)new AnalogAlphaToy() { Name = ToyName, OutputName = LWE.Outputs.First(Output => Output.LedWizEquivalentOutputNumber == TCC.FirstOutputNumber).OutputName };
572  Cabinet.Toys.Add(TargetToy);
573  }
574 
575  }
576 
577  }
578 
579  break;
580 
581  default:
582  //Unknow value
583  Log.Warning("A illegal number ({0}) of required outputs has been found in a table config colum {0} for ledcontrol nr. {2}. Cant configure toy.".Build(TCC.RequiredOutputCount, TCC.Number, LedWizNr));
584  break;
585  }
586  }
587 
588  if (TargetToy != null)
589  {
590  ToyAssignments[LedWizNr].Add(TCC.Number, TargetToy);
591  }
592  }
593  }
594  }
595  return ToyAssignments;
596  }
597 
598 
599  }
600 }