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
MatrixBitmapAnimationEffectBase.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.BitmapHandling;
6 using DirectOutput.General;
7 using DirectOutput.Cab.Toys.Layer;
8 
9 namespace DirectOutput.FX.MatrixFX
10 {
11  public abstract class MatrixBitmapAnimationEffectBase<MatrixElementType> : MatrixEffectBase<MatrixElementType>
12  {
13  private MatrixAnimationStepDirectionEnum _AnimationStepDirection = MatrixAnimationStepDirectionEnum.Frame;
14 
21  public MatrixAnimationStepDirectionEnum AnimationStepDirection
22  {
23  get { return _AnimationStepDirection; }
24  set { _AnimationStepDirection = value; }
25  }
26 
27  private int _AnimationStepSize = 1;
28 
35  public int AnimationStepSize
36  {
37  get { return _AnimationStepSize; }
38  set { _AnimationStepSize = value; }
39  }
40 
41 
42  private int _AnimationFrameCount = 1;
43 
50  public int AnimationFrameCount
51  {
52  get { return _AnimationFrameCount; }
53  set { _AnimationFrameCount = value.Limit(1, int.MaxValue); }
54  }
55 
56  private AnimationBehaviourEnum _AnimationBehaviour= AnimationBehaviourEnum.Loop;
57 
64  public AnimationBehaviourEnum AnimationBehaviour
65  {
66  get { return _AnimationBehaviour; }
67  set { _AnimationBehaviour = value; }
68  }
69 
70 
71  private int _AnimationFrameDurationMs = 30;
72 
79  public int AnimationFrameDurationMs
80  {
81  get { return _AnimationFrameDurationMs; }
82  set { _AnimationFrameDurationMs = value.Limit(1,int.MaxValue); }
83  }
84 
85 
86 
87  private int _BitmapFrameNumber = 0;
88 
95  public int BitmapFrameNumber
96  {
97  get { return _BitmapFrameNumber; }
98  set { _BitmapFrameNumber = value; }
99  }
100 
101  private int _BitmapTop = 0;
102 
109  public int BitmapTop
110  {
111  get { return _BitmapTop; }
112  set { _BitmapTop = value.Limit(0,int.MaxValue); }
113  }
114 
115  private int _BitmapLeft = 0;
116 
123  public int BitmapLeft
124  {
125  get { return _BitmapLeft; }
126  set { _BitmapLeft = value.Limit(0, int.MaxValue); }
127  }
128 
129  private int _BitmapWidth = -1;
130 
137  public int BitmapWidth
138  {
139  get { return _BitmapWidth; }
140  set { _BitmapWidth = value.Limit(-1, int.MaxValue); }
141  }
142 
143  private int _BitmapHeight = -1;
144 
151  public int BitmapHeight
152  {
153  get { return _BitmapHeight; }
154  set { _BitmapHeight = value.Limit(-1, int.MaxValue); }
155  }
156 
157 
158  private FastBitmapDataExtractModeEnum _DataExtractMode = FastBitmapDataExtractModeEnum.BlendPixels;
159 
166  public FastBitmapDataExtractModeEnum DataExtractMode
167  {
168  get { return _DataExtractMode; }
169  set { _DataExtractMode = value; }
170  }
171 
172 
173 
174  private FilePattern _BitmapFilePattern;
175 
182  public FilePattern BitmapFilePattern
183  {
184  get { return _BitmapFilePattern; }
185  set { _BitmapFilePattern = value; }
186  }
187 
188  private PixelData[][,] Pixels;
189 
190 
191  private bool AnimationActive = false;
192  private int AnimationStep = 0;
193  private int AnimationFadeValue = 0;
194  private void Animate()
195  {
196 
197 
198  for (int y = 0; y < AreaHeight; y++)
199  {
200  int yd = y + AreaTop;
201  for (int x = 0; x < AreaWidth; x++)
202  {
203  int xd = x + AreaLeft;
204  MatrixLayer[xd, yd] = GetEffectValue(AnimationFadeValue, Pixels[AnimationStep][x, y]);
205  }
206  }
207 
208 
209 
210  AnimationStep++;
211  if (AnimationStep >= Pixels.GetUpperBound(0))
212  {
213  AnimationStep = 0;
214  if (AnimationBehaviour == AnimationBehaviourEnum.Once)
215  {
216  StopAnimation();
217  }
218  }
219 
220  }
221 
222  private void Clear()
223  {
224  MatrixElementType Off = GetEffectValue(0, new PixelData());
225 
226  for (int y = AreaTop; y <= AreaBottom; y++)
227  {
228 
229  for (int x = AreaLeft; x <= AreaRight; x++)
230  {
231  MatrixLayer[x, y] = Off;
232  }
233  }
234 
235  }
236 
244  protected abstract MatrixElementType GetEffectValue(int TriggerValue, PixelData Pixel);
245 
246 
247  private void ControlAnimation(int FadeValue)
248  {
249  if (FadeValue > 0)
250  {
251  //Start animation
252  this.AnimationFadeValue = FadeValue;
253 
254  if (!AnimationActive)
255  {
256  AnimationActive = true;
257 
258  if (AnimationBehaviour != AnimationBehaviourEnum.Continue)
259  {
260  AnimationStep = 0;
261  }
262  Table.Pinball.Alarms.RegisterIntervalAlarm(AnimationFrameDurationMs, Animate);
263 
264  Animate();
265  }
266  }
267  else
268  {
269  //Stop animation
270  StopAnimation();
271  }
272 
273 
274 
275  }
276 
277  private void StopAnimation()
278  {
279  if (AnimationActive)
280  {
281  Table.Pinball.Alarms.UnregisterIntervalAlarm(Animate);
282 
283  AnimationActive = false;
284 
285  Clear();
286  }
287  }
288 
289 
290 
291 
296  public override void Trigger(Table.TableElementData TableElementData)
297  {
298  if (InitOK)
299  {
300  int FadeValue = TableElementData.Value;
301  if (FadeMode == FadeModeEnum.OnOff) FadeValue = (FadeValue < 1 ? 0 : 255);
302  ControlAnimation(FadeValue);
303 
304 
305  }
306 
307  }
308 
309  private bool InitOK = false;
310 
311 
317  public override void Init(Table.Table Table)
318  {
319  InitOK = false;
320  Pixels = null;
321  base.Init(Table);
322 
323  //TODO: Insert replace values for file pattern
324  if (BitmapFilePattern.IsValid)
325  {
326 
327  string Filename = BitmapFilePattern.GetFirstMatchingFile().FullName;
328  if (!Filename.IsNullOrWhiteSpace())
329  {
330  FastImage BM;
331  try
332  {
333  BM = Table.Bitmaps[Filename];
334  }
335  catch (Exception E)
336  {
337  Log.Exception("MatrixBitmapAnimationEffectBase {0} cant initialize. Could not load file {1}.".Build(Name, Filename), E);
338  return;
339  }
340 
341  if (BM.Frames.ContainsKey(BitmapFrameNumber))
342  {
343  int StepCount = AnimationFrameCount;
344  switch (AnimationStepDirection)
345  {
347  if ((BitmapFrameNumber + (StepCount * AnimationStepSize)) > BM.Frames.Count)
348  {
349  StepCount = (BM.Frames.Count - BitmapFrameNumber) / AnimationStepSize;
350  }
351 
352  Pixels = new PixelData[StepCount][,];
353 
354  for (int s = 0; s < StepCount; s++)
355  {
356  Pixels[s] = BM.Frames[BitmapFrameNumber + s].GetClip(AreaWidth, AreaHeight, BitmapLeft, BitmapTop, BitmapWidth, BitmapHeight, DataExtractMode).Pixels;
357  }
358 
359 
360 
361  break;
363  //TODO: Check if there should be a restriction of steps for this direction
364 
365 
366  Pixels = new PixelData[StepCount][,];
367 
368  for (int s = 0; s < StepCount; s++)
369  {
370  Pixels[s] = BM.Frames[BitmapFrameNumber].GetClip(AreaWidth, AreaHeight, BitmapLeft+s*AnimationStepSize, BitmapTop, BitmapWidth, BitmapHeight, DataExtractMode).Pixels;
371  }
372 
373  break;
375  //TODO: Check if there should be a restriction of steps for this direction
376  Pixels = new PixelData[StepCount][,];
377 
378  for (int s = 0; s < StepCount; s++)
379  {
380  Pixels[s] = BM.Frames[BitmapFrameNumber].GetClip(AreaWidth, AreaHeight, BitmapLeft, BitmapTop + s * AnimationStepSize, BitmapWidth, BitmapHeight, DataExtractMode).Pixels;
381  }
382 
383  break;
384  default:
385  StepCount = 1;
386  Pixels = new PixelData[StepCount][,];
387  for (int s = 0; s < StepCount; s++)
388  {
389  Pixels[s] = BM.Frames[BitmapFrameNumber].GetClip(AreaWidth, AreaHeight, BitmapLeft, BitmapTop, BitmapWidth, BitmapHeight, DataExtractMode).Pixels;
390  }
391  break;
392  }
393 
394 
395 
396  }
397  else
398  {
399  Log.Warning("MatrixBitmapAnimationEffectBase {0} cant initialize. Frame {1} does not exist in source image {2}.".Build(Name, BitmapFrameNumber, Filename));
400 
401  }
402  }
403  else
404  {
405  Log.Warning("MatrixBitmapAnimationEffectBase {0} cant initialize. No file matches the BitmapFilePattern {1} is invalid".Build(Name, BitmapFilePattern.ToString()));
406  }
407  }
408  else
409  {
410  Log.Warning("MatrixBitmapAnimationEffectBase {0} cant initialize. The BitmapFilePattern {1} is invalid".Build(Name, BitmapFilePattern.ToString()));
411  }
412 
413 
414  InitOK = (Pixels != null && MatrixLayer != null);
415 
416  }
417 
421  public override void Finish()
422  {
423  StopAnimation();
424  Pixels = null;
425  base.Finish();
426  }
427 
428 
429  }
430 }