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
MatrixFlickerEffectBase.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.General.Color;
6 using DirectOutput.Cab.Toys.Layer;
7 
8 namespace DirectOutput.FX.MatrixFX
9 {
13  public abstract class MatrixFlickerEffectBase<MatrixElementType> : MatrixEffectBase<MatrixElementType>
14  {
15  private const int RefreshIntervalMs = 30;
16 
17 
18 
19 
20 
21 
22 
23  private int _Density = 10;
24 
32  public int Density
33  {
34  get { return _Density; }
35  set { _Density = value.Limit(0, 100); }
36  }
37 
38 
39  private int _MinFlickerDurationMs = 60;
40 
47  public int MinFlickerDurationMs
48  {
49  get { return _MinFlickerDurationMs; }
50  set { _MinFlickerDurationMs = value.Limit(1, int.MaxValue); }
51  }
52 
53  private int _MaxFlickerDurationMs = 150;
54 
61 
62  public int MaxFlickerDurationMs
63  {
64  get { return _MaxFlickerDurationMs; }
65  set { _MaxFlickerDurationMs = value.Limit(1, int.MaxValue); }
66  }
67 
74  private bool Active { get; set; }
75 
76 
77  private SortedDictionary<int, List<System.Drawing.Point>> ElementDictionary = new SortedDictionary<int, List<System.Drawing.Point>>();
78  private int CurrentStep = 0;
79  private int CurrentValue = 0;
80  private int CurrentFlickerElements = 0;
81 
82  private Random R = new Random();
83 
84  private void DoFlicker()
85  {
86  MatrixElementType D;
87  MatrixElementType I = GetEffectValue(0);
88 
89  int V = CurrentValue.Limit(0, 255);
90  if (V > 0)
91  {
92  if (!Active)
93  {
94  Table.Pinball.Alarms.RegisterIntervalAlarm(RefreshIntervalMs, DoFlicker);
95  Active = true;
96  }
97 
98 
99  //Effect is active (V>0)
100  if (V > 0 && FadeMode == FadeModeEnum.OnOff) { V = 255; }
101 
102  D = GetEffectValue(V);
103 
104  int NumberOfLeds = AreaWidth * AreaHeight;
105  int FlickerLeds = ((int)((double)NumberOfLeds / 100 * Density)).Limit(1, NumberOfLeds);
106 
107  int Min = MinFlickerDurationMs;
108  int Max = MaxFlickerDurationMs;
109  if (Max < Min)
110  {
111  int Tmp = Min; Min = Max; Max = Tmp;
112  }
113  while (CurrentFlickerElements < FlickerLeds)
114  {
115  int S = CurrentStep + (int)((float)(R.Next(Min ,Max)) / RefreshIntervalMs);
116  if (!ElementDictionary.ContainsKey(S))
117  {
118  ElementDictionary.Add(S, new List<System.Drawing.Point>());
119  }
120  ElementDictionary[S].Add(new System.Drawing.Point(R.Next(AreaLeft, AreaRight+1), R.Next(AreaTop,AreaBottom+1)));
121  CurrentFlickerElements++;
122  }
123 
124 
125 
126  List<int> DropKeys = new List<int>();
127 
128  foreach (KeyValuePair<int, List<System.Drawing.Point>> KV in ElementDictionary)
129  {
130  if (KV.Key < CurrentStep)
131  {
132  foreach (System.Drawing.Point P in KV.Value)
133  {
134  MatrixLayer[P.X, P.Y] = I;
135  CurrentFlickerElements--;
136  }
137  DropKeys.Add(KV.Key);
138  }
139  else
140  {
141  foreach (System.Drawing.Point P in KV.Value)
142  {
143  MatrixLayer[P.X, P.Y] = D;
144  }
145  }
146  }
147 
148 
149  foreach (int S in DropKeys)
150  {
151  ElementDictionary.Remove(S);
152  }
153 
154  CurrentStep++;
155 
156 
157 
158  }
159  else
160  {
161  //Deactivate effect (V=0)
162 
163  foreach (KeyValuePair<int, List<System.Drawing.Point>> KV in ElementDictionary)
164  {
165  foreach (System.Drawing.Point P in KV.Value)
166  {
167  MatrixLayer[P.X, P.Y] = I;
168  }
169  }
170  ElementDictionary.Clear();
171  CurrentStep = 0;
172  CurrentFlickerElements = 0;
173  Table.Pinball.Alarms.UnregisterIntervalAlarm(DoFlicker);
174  Active = false;
175 
176  }
177 
178  }
179 
180 
181 
182 
183 
188  public override void Trigger(Table.TableElementData TableElementData)
189  {
190  if (MatrixLayer != null)
191  {
192  CurrentValue = TableElementData.Value;
193  if (CurrentValue > 0 && !Active)
194  {
195  DoFlicker();
196  }
197  }
198  }
199 
200 
207  protected abstract MatrixElementType GetEffectValue(int TriggerValue);
208 
209  }
210 }