DirectOutputR1
DirectOutput framework R1 for virtual pinball cabinets.
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
AnalogToyFadeOnOffEffect.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.Toys.Layer;
6 
7 namespace DirectOutput.FX.AnalogToyFX
8 {
14  {
15  private const int FadingRefreshIntervalMs = 30;
16 
17  private RetriggerBehaviourEnum _RetriggerBehaviour;
18 
27  public RetriggerBehaviourEnum RetriggerBehaviour
28  {
29  get { return _RetriggerBehaviour; }
30  set { _RetriggerBehaviour = value; }
31  }
32 
33  private FadeModeEnum _FadeMode = FadeModeEnum.DefinedToDefined;
34 
42  public FadeModeEnum FadeMode
43  {
44  get { return _FadeMode; }
45  set { _FadeMode = value; }
46  }
47 
48  //TODO: Maybe split fademode into fademodeactive and fademode inactive
49 
50  private int _FadeInactiveDurationMs = 500;
51 
58  public int FadeInactiveDurationMs
59  {
60  get { return _FadeInactiveDurationMs; }
61  set { _FadeInactiveDurationMs = value; }
62  }
63 
64 
65  private int _FadeActiveDurationMs = 500;
66 
73  public int FadeActiveDurationMs
74  {
75  get { return _FadeActiveDurationMs; }
76  set { _FadeActiveDurationMs = value; }
77  }
78 
79 
80 
81 
82  private AnalogAlphaValue _ActiveValue = new AnalogAlphaValue(255, 255);
83 
90  public AnalogAlphaValue ActiveValue
91  {
92  get { return _ActiveValue; }
93  set { _ActiveValue = value; }
94  }
95 
96  private AnalogAlphaValue _InactiveValue = new AnalogAlphaValue(0, 0);
97 
104  public AnalogAlphaValue InactiveValue
105  {
106  get { return _InactiveValue; }
107  set { _InactiveValue = value; }
108  }
109 
110 
111 
112 
113  float[] Current = new float[2];
114  float[] Step = new float[2];
115  float[] Target = new float[2];
116  bool IsFading = false;
117 
118  private void StartFading(bool Active)
119  {
120  Table.Pinball.Alarms.UnregisterAlarm(FadingStep);
121 
122  AnalogAlphaValue TargetValue = (Active ? ActiveValue : InactiveValue);
123 
124  int Duration = (Active ? FadeActiveDurationMs : FadeInactiveDurationMs);
125  int Steps = Duration / FadingRefreshIntervalMs;
126 
127  if (Steps > 0)
128  {
129 
130  IsFading = true;
131 
132  AnalogAlphaValue CurrentAnalogAlphaValue;
133  switch (FadeMode)
134  {
135  case FadeModeEnum.CurrentToDefined:
136  CurrentAnalogAlphaValue = Toy.Layers[Layer].GetAnalogAlphaValue();
137  break;
138  case FadeModeEnum.DefinedToDefined:
139  default:
140  CurrentAnalogAlphaValue = (!Active ? ActiveValue : InactiveValue);
141  break;
142  }
143 
144  Current[0] = CurrentAnalogAlphaValue.Value;
145  Current[1] = CurrentAnalogAlphaValue.Alpha;
146 
147  Target[0] = TargetValue.Value;
148  Target[1] = TargetValue.Alpha;
149 
150 
151  for (int i = 0; i < 2; i++)
152  {
153  Step[i] = (Target[i] - Current[i]) / Steps;
154  }
155  FadingStep();
156  }
157  else
158  {
159  Toy.Layers[Layer].Set(TargetValue);
160  }
161 
162  }
163 
164  private void FadingStep()
165  {
166  bool ContinueFading = false;
167  for (int i = 0; i < 2; i++)
168  {
169  if (Step[i] > 0)
170  {
171  Current[i] += Step[i];
172  if (Current[i] < Target[i] && Current[i] < 255)
173  {
174  ContinueFading = true;
175  }
176  else
177  {
178  Current[i] = Target[i];
179  Step[i] = 0;
180  }
181  }
182  else if (Step[i] < 0)
183  {
184  Current[i] += Step[i];
185  if (Current[i] > Target[i] && Current[i] > 0)
186  {
187  ContinueFading = true;
188  }
189  else
190  {
191  Current[i] = Target[i];
192  Step[i] = 0;
193  }
194  }
195  }
196 
197  Toy.Layers[Layer].Set((int)Current[0], (int)Current[1]);
198 
199  if (ContinueFading)
200  {
201  Table.Pinball.Alarms.RegisterAlarm(FadingRefreshIntervalMs, FadingStep);
202  IsFading = true;
203  }
204  else
205  {
206  IsFading = false;
207  }
208  }
209 
210  bool LastTriggerState = false;
217  public override void Trigger(Table.TableElementData TableElementData)
218  {
219  if (Toy != null)
220  {
221 
222  bool TriggerState = TableElementData.Value != 0;
223 
224  if (TriggerState != LastTriggerState || IsFading == false || RetriggerBehaviour == RetriggerBehaviourEnum.RestartEffect)
225  {
226  StartFading(TriggerState);
227  }
228 
229  LastTriggerState = TriggerState;
230  }
231  }
232 
233 
238  public override void Init(Table.Table Table)
239  {
240  base.Init(Table);
241  }
242 
246  public override void Finish()
247  {
248  try
249  {
250  Table.Pinball.Alarms.UnregisterAlarm(FadingStep);
251  }
252  catch { }
253  if (Toy != null)
254  {
255  Toy.Layers.Remove(Layer);
256  }
257  base.Finish();
258  }
259  }
260 }