WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
FadeEffect.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 
6 namespace DirectOutput.FX.TimmedFX
7 {
13  {
14  private const int FadingRefreshIntervalMs = 30;
15 
16 
17  private int _FadeUpDuration = 300;
18 
25  public int FadeUpDuration
26  {
27  get { return _FadeUpDuration; }
28  set { _FadeUpDuration = value; }
29  }
30 
31  private int _FadeDownDuration = 300;
32 
39  public int FadeDownDuration
40  {
41  get { return _FadeDownDuration; }
42  set { _FadeDownDuration = value; }
43  }
44 
45 
46  private FadeEffectDurationModeEnum _FadeDurationMode = FadeEffectDurationModeEnum.CurrentToTarget;
54  public FadeEffectDurationModeEnum FadeDurationMode
55  {
56  get { return _FadeDurationMode; }
57  set { _FadeDurationMode = value; }
58  }
59 
60 
61 
62  float TargetValue = -1;
63  float CurrentValue = 0;
64  float StepValue = 0;
65  int LastTargetTriggerValue = -1;
66  Table.TableElementData TableElementData;
67 
72  public override void Trigger(Table.TableElementData TableElementData)
73  {
74  if (TargetEffect!=null && TableElementData.Value != TargetValue)
75  {
76  TargetValue = TableElementData.Value.Limit(0,255);
77 
78  this.TableElementData = TableElementData;
79 
80  double Duration = (CurrentValue < TargetValue ? FadeUpDuration : FadeDownDuration);
81  if (FadeDurationMode == FadeEffectDurationModeEnum.FullValueRange)
82  {
83  Duration = Duration / 255 * (TargetValue - CurrentValue).Abs();
84  }
85  int Steps = (int)(Duration>0?(Duration / FadingRefreshIntervalMs):0);
86 
87  if (Steps > 0)
88  {
89  StepValue = (float)(TargetValue - CurrentValue) / Steps;
90  LastTargetTriggerValue = -1;
91  FadingStep();
92 
93  }
94  else
95  {
96  Table.Pinball.Alarms.UnregisterAlarm( FadingStep);
97 
98  CurrentValue=TargetValue;
99  LastTargetTriggerValue = -1;
100  TriggerTargetEffect(TableElementData);
101  }
102 
103  }
104  }
105 
106  private void FadingStep()
107  {
108  CurrentValue += StepValue;
109 
110  if ((CurrentValue < TargetValue && StepValue > 0) || (CurrentValue > TargetValue && StepValue < 0))
111  {
112  //Continue fading
113  Table.Pinball.Alarms.RegisterIntervalAlarm(FadingRefreshIntervalMs, FadingStep);
114  }
115  else
116  {
117  CurrentValue = TargetValue;
118  }
119 
120  if (LastTargetTriggerValue != (int)CurrentValue)
121  {
122  LastTargetTriggerValue = (int)CurrentValue;
123  TableElementData.Value = LastTargetTriggerValue;
124  TriggerTargetEffect(TableElementData);
125  }
126  }
127 
128  }
129 }
This effect fades towards the value passed to the effect in the TableElementData of the trigger metho...
Definition: FadeEffect.cs:12
FadeEffectDurationModeEnum
Options which define how the durations of the FadeEffect are used.
override void Trigger(Table.TableElementData TableElementData)
Triggers the effect with the given TableElementData.
Definition: FadeEffect.cs:72
Base class for effects targeting another effect.