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
ToyGroupBase.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Xml.Serialization;
6 using DirectOutput.Cab.Toys.Layer;
7 
8 namespace DirectOutput.Cab.Toys.Virtual
9 {
15  public abstract class ToyGroupBase<MatrixElementType> : ToyBaseUpdatable, IToy, IMatrixToy<MatrixElementType>
16  where MatrixElementType:new()
17  {
18  private List<List<string>> _ToyNames = new List<List<string>>();
19 
26  [XmlArrayItem("Row")]
27  [XmlArrayItem("Column",NestingLevel=1)]
28  public List<List<string>> ToyNames
29  {
30  get { return _ToyNames; }
31  set { _ToyNames = value; }
32  }
33 
34  [XmlIgnore]
35  private ILayerToy<MatrixElementType>[,] Toys = new ILayerToy<MatrixElementType>[0, 0];
36 
37  private int _LayerOffset;
38 
46  public int LayerOffset
47  {
48  get { return _LayerOffset; }
49  set { _LayerOffset = value; }
50  }
51 
52 
56  public override void UpdateOutputs()
57  {
58  foreach (KeyValuePair<int, MatrixElementType[,]> Layer in Layers)
59  {
60  int LayerNr = Layer.Key+LayerOffset;
61 
62  int Height = Toys.GetUpperBound(1) + 1;
63  int Width=Toys.GetUpperBound(0) + 1;
64 
65  for (int y = 0; y < Height; y++)
66  {
67  for (int x = 0; x < Width; x++)
68  {
69  if (Toys[x, y] != null)
70  {
71  Toys[x, y].Layers[LayerNr] = Layer.Value[x, y];
72  }
73  }
74  }
75  }
76  }
81  public override void Init(Cabinet Cabinet)
82  {
83  int ColCnt = 0;
84  int RowCnt = ToyNames.Count;
85  if (RowCnt > 0)
86  {
87  ColCnt = ToyNames.Max(X => X.Count).Limit(1, int.MaxValue);
88  }
89  else
90  {
91  ToyNames.Add(new List<string>(new []{""}));
92  RowCnt = 1;
93  ColCnt = 1;
94  }
95 
96  Toys = new ILayerToy<MatrixElementType>[ColCnt, RowCnt];
97 
98  for (int y = 0; y < RowCnt ; y++)
99  {
100  for (int x = 0; x < ColCnt ; x++)
101  {
102  if (ToyNames[y].Count > x)
103  {
104  if (Cabinet.Toys.Contains(ToyNames[y][x]) && Cabinet.Toys[ToyNames[y][x]] is ILayerToy<MatrixElementType>)
105  {
106  Toys[x, y] = (ILayerToy<MatrixElementType>)Cabinet.Toys[ToyNames[y][x]];
107  }
108  else
109  {
110  Toys[x, y] = null;
111  }
112  }
113  }
114  }
115 
116  Layers = new MatrixDictionaryBase<MatrixElementType>() { Width = Width, Height = Height };
117 
118  }
119 
123  public override void Reset()
124  {
125  Layers = new MatrixDictionaryBase<MatrixElementType>() { Width = Width, Height = Height };
126  }
127 
131  public override void Finish()
132  {
133  base.Finish();
134  Layers = null;
135  Toys = null;
136  }
137 
138 
139 
146  [XmlIgnore]
147  public MatrixDictionaryBase<MatrixElementType> Layers { get; private set; }
148 
149 
150  #region IMatrix Member
151 
160  public MatrixElementType[,] GetLayer(int LayerNr)
161  {
162  return Layers[LayerNr];
163  }
164 
171  public int Height
172  {
173  get
174  {
175  return Toys.GetUpperBound(1) + 1;
176  }
177 
178  }
179 
186  public int Width
187  {
188  get
189  {
190  return Toys.GetUpperBound(0) + 1;
191  }
192  }
193 
194  #endregion
195  }
196 }