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
AnalogAlphaToy.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 DirectOutput.Cab.Out;
6 using DirectOutput.General;
7 using System.Xml.Serialization;
8 using DirectOutput.General.Analog;
9 
10 namespace DirectOutput.Cab.Toys.Layer
11 {
16  {
17 
24  [XmlIgnore]
25  public LayerDictionary<AnalogAlpha> Layers { get; private set; }
26 
31  protected int GetResultingValue()
32  {
33  if (Layers.Count > 0)
34  {
35  float Value = 0;
36 
37  foreach (KeyValuePair<int, AnalogAlpha> KV in Layers)
38  {
39  int Alpha = KV.Value.Alpha;
40  if (Alpha != 0)
41  {
42  Value = AlphaMappingTable.AlphaMapping[255 - Alpha, (int)Value] + AlphaMappingTable.AlphaMapping[Alpha, KV.Value.Value];
43  }
44  }
45 
46  return (int)Value;
47  }
48  else
49  {
50  return 0;
51  }
52  }
53 
54 
55  #region Outputs
56 
57  [XmlIgnore]
58  protected IOutput Output;
59 
60 
67  public string OutputName { get; set; }
68 
69 
70  #endregion
71 
72  #region Fading curve
73  private string _FadingCurveName = "Linear";
74  [XmlIgnore]
75  protected Curve FadingCurve = null;
76 
84  public string FadingCurveName
85  {
86  get { return _FadingCurveName; }
87  set { _FadingCurveName = value; }
88  }
89 
90  private void InitFadingCurve(Cabinet Cabinet)
91  {
92  if (Cabinet.Curves.Contains(FadingCurveName))
93  {
94  FadingCurve = Cabinet.Curves[FadingCurveName];
95  }
96  else if (!FadingCurveName.IsNullOrWhiteSpace())
97  {
98  if (Enum.GetNames(typeof(Curve.CurveTypeEnum)).Contains(FadingCurveName))
99  {
101  Enum.TryParse(FadingCurveName, out T);
102  FadingCurve = new Curve(T);
103  }
104  else
105  {
106  FadingCurve = new Curve(Curve.CurveTypeEnum.Linear) { Name = FadingCurveName };
107  Cabinet.Curves.Add(FadingCurveName);
108  }
109  }
110  else
111  {
112  FadingCurve = new Curve(Curve.CurveTypeEnum.Linear);
113  }
114 
115  }
116  #endregion
117 
118 
119 
120  [XmlIgnore]
121  protected Cabinet Cabinet;
122 
123 
128  public override void Init(Cabinet Cabinet)
129  {
130  this.Cabinet = Cabinet;
131  InitOutputs(Cabinet);
132  InitFadingCurve(Cabinet);
133  }
134 
135  private void InitOutputs(Cabinet Cabinet)
136  {
137  if (Cabinet.Outputs.Contains(OutputName))
138  {
139  Output = Cabinet.Outputs[OutputName];
140  }
141  else
142  {
143  Output = null;
144  }
145  }
146 
147 
148 
149 
153  public override void UpdateOutputs()
154  {
155  if (Output != null)
156  {
157 
158  Output.Value = FadingCurve.MapValue(GetResultingValue());
159  }
160  }
161 
162 
166  public override void Finish()
167  {
168  Reset();
169  Output = null;
170  Cabinet = null;
171  base.Finish();
172  }
173 
179  public override void Reset()
180  {
181  Layers.Clear();
182  if (Output != null)
183  {
184  Output.Value = 0;
185  }
186  }
187 
191  public AnalogAlphaToy()
192  {
193  Layers = new LayerDictionary<AnalogAlpha>();
194  }
195  }
196 }