DirectOutputR1
DirectOutput framework R1 for virtual pinball cabinets.
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
RGBAFadeOnOffEffect.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 using DirectOutput.Cab.Color;
7 
8 namespace DirectOutput.FX.RGBAFX
9 {
15  {
16  private const int FadingRefreshIntervalMs = 30;
17 
18  private RetriggerBehaviourEnum _RetriggerBehaviour;
19 
28  public RetriggerBehaviourEnum RetriggerBehaviour
29  {
30  get { return _RetriggerBehaviour; }
31  set { _RetriggerBehaviour = value; }
32  }
33 
34  private FadeModeEnum _FadeMode = FadeModeEnum.DefinedToDefined;
35 
43  public FadeModeEnum FadeMode
44  {
45  get { return _FadeMode; }
46  set { _FadeMode = value; }
47  }
48 
49  //TODO: Maybe split fademode into fademodeactive and fademode inactive
50 
51  private int _FadeInactiveDurationMs = 500;
52 
59  public int FadeInactiveDurationMs
60  {
61  get { return _FadeInactiveDurationMs; }
62  set { _FadeInactiveDurationMs = value; }
63  }
64 
65 
66  private int _FadeActiveDurationMs = 500;
67 
74  public int FadeActiveDurationMs
75  {
76  get { return _FadeActiveDurationMs; }
77  set { _FadeActiveDurationMs = value; }
78  }
79 
80 
81 
82  float[] Current = new float[4];
83  float[] Step = new float[4];
84  float[] Target = new float[4];
85  bool IsFading = false;
86 
87  private void StartFading(bool Active)
88  {
89  Table.Pinball.Alarms.UnregisterAlarm(FadingStep);
90 
91  RGBAColor TargetColor = (Active ? ActiveColor : InactiveColor);
92 
93  int Duration = (Active ? FadeActiveDurationMs : FadeInactiveDurationMs);
94  int Steps = Duration / FadingRefreshIntervalMs;
95 
96  if (Steps > 0)
97  {
98  IsFading = true;
99 
100  RGBAColor CurrentColor;
101  switch (FadeMode)
102  {
103  case FadeModeEnum.CurrentToDefined:
104  CurrentColor = RGBAToy.Layers[Layer].GetRGBAColor();
105  break;
106  case FadeModeEnum.DefinedToDefined:
107  default:
108  CurrentColor = (!Active ? ActiveColor.GetRGBAColor() : InactiveColor.GetRGBAColor());
109  break;
110  }
111 
112  Current[0] = CurrentColor.Red;
113  Current[1] = CurrentColor.Green;
114  Current[2] = CurrentColor.Blue;
115  Current[3] = CurrentColor.Alpha;
116 
117  Target[0] = TargetColor.Red;
118  Target[1] = TargetColor.Green;
119  Target[2] = TargetColor.Blue;
120  Target[3] = TargetColor.Alpha;
121 
122  for (int i = 0; i < 4; i++)
123  {
124  Step[i] = (Target[i] - Current[i]) / Steps;
125  }
126 
127  FadingStep();
128  }
129  else
130  {
131  RGBAToy.Layers[Layer].Set(TargetColor);
132  }
133  }
134 
135  private void FadingStep()
136  {
137  bool ContinueFading = false;
138  for (int i = 0; i < 4; i++)
139  {
140  if (Step[i] > 0)
141  {
142  Current[i] += Step[i];
143  if (Current[i] < Target[i] && Current[i] < 255)
144  {
145  ContinueFading = true;
146  }
147  else
148  {
149  Current[i] = Target[i];
150  Step[i] = 0;
151  }
152  }
153  else if (Step[i] < 0)
154  {
155  Current[i] += Step[i];
156  if (Current[i] > Target[i] && Current[i] > 0)
157  {
158  ContinueFading = true;
159  }
160  else
161  {
162  Current[i] = Target[i];
163  Step[i] = 0;
164  }
165  }
166  }
167 
168  RGBAToy.Layers[Layer].Set((int)Current[0], (int)Current[1], (int)Current[2], (int)Current[3]);
169 
170  if (ContinueFading)
171  {
172  Table.Pinball.Alarms.RegisterAlarm(FadingRefreshIntervalMs, FadingStep);
173  IsFading = true;
174  }
175  else
176  {
177  IsFading = false;
178  }
179  }
180 
181 
182 
183 
184  private RGBAColor _ActiveColor = new RGBAColor(0, 0, 0, 0);
185 
192  public RGBAColor ActiveColor
193  {
194  get { return _ActiveColor; }
195  set { _ActiveColor = value; }
196  }
197 
198 
199  private RGBAColor _InactiveColor = new RGBAColor(0, 0, 0, 0);
200 
207  public RGBAColor InactiveColor
208  {
209  get { return _InactiveColor; }
210  set { _InactiveColor = value; }
211  }
212 
213 
214 
215  bool LastTriggerState = false;
221  public override void Trigger(Table.TableElementData TableElementData)
222  {
223  if (RGBAToy != null)
224  {
225  bool TriggerState = (TableElementData == null || TableElementData.Value != 0);
226 
227  if (TriggerState != LastTriggerState || IsFading == false || RetriggerBehaviour == RetriggerBehaviourEnum.RestartEffect)
228  {
229  StartFading(TriggerState);
230  }
231 
232  LastTriggerState = TriggerState;
233  }
234  }
235 
239  public override void Finish()
240  {
241  try
242  {
243  Table.Pinball.Alarms.UnregisterAlarm(FadingStep);
244  }
245  catch { }
246  if (RGBAToy != null)
247  {
248  RGBAToy.Layers.Remove(Layer);
249  }
250  base.Finish();
251  }
252 
253  }
254 }