WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
MatrixBitmapEffectBase.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
6 
8 using System.IO;
9 
10 namespace DirectOutput.FX.MatrixFX
11 {
15  public abstract class MatrixBitmapEffectBase<MatrixElementType> : MatrixEffectBase<MatrixElementType>, IMatrixBitmapEffect
16  {
17 
18  private int _BitmapFrameNumber = 0;
19 
26  public int BitmapFrameNumber
27  {
28  get { return _BitmapFrameNumber; }
29  set { _BitmapFrameNumber = value; }
30  }
31 
32  private int _BitmapTop = 0;
33 
40  public int BitmapTop
41  {
42  get { return _BitmapTop; }
43  set { _BitmapTop = value; }
44  }
45 
46  private int _BitmapLeft = 0;
47 
54  public int BitmapLeft
55  {
56  get { return _BitmapLeft; }
57  set { _BitmapLeft = value; }
58  }
59 
60  private int _BitmapWidth = -1;
61 
68  public int BitmapWidth
69  {
70  get { return _BitmapWidth; }
71  set { _BitmapWidth = value; }
72  }
73 
74  private int _BitmapHeight = -1;
75 
82  public int BitmapHeight
83  {
84  get { return _BitmapHeight; }
85  set { _BitmapHeight = value; }
86  }
87 
88 
89  private FastBitmapDataExtractModeEnum _DataExtractMode = FastBitmapDataExtractModeEnum.BlendPixels;
90 
97  public FastBitmapDataExtractModeEnum DataExtractMode
98  {
99  get { return _DataExtractMode; }
100  set { _DataExtractMode = value; }
101  }
102 
103 
104  private FilePattern _BitmapFilePattern;
105 
112  public FilePattern BitmapFilePattern
113  {
114  get { return _BitmapFilePattern; }
115  set { _BitmapFilePattern = value; }
116  }
117 
118  protected PixelData[,] Pixels;
119 
120  private void OutputBitmap(int FadeValue)
121  {
122  if (FadeMode == FadeModeEnum.OnOff) FadeValue = (FadeValue < 1 ? 0 : 255);
123 
124 
125  for (int y = 0; y < AreaHeight; y++)
126  {
127  int yd = y + AreaTop;
128  for (int x = 0; x < AreaWidth; x++)
129  {
130  int xd = x + AreaLeft;
131  MatrixLayer[xd, yd] = GetEffectValue(FadeValue, Pixels[x, y]);
132  }
133  }
134 
135 
136  }
137 
145  public abstract MatrixElementType GetEffectValue(int TriggerValue, PixelData Pixel);
146 
151  public override void Trigger(Table.TableElementData TableElementData)
152  {
153  if (InitOK)
154  {
155  OutputBitmap(TableElementData.Value);
156  }
157 
158  }
159 
160  private bool InitOK = false;
161 
162 
168  public override void Init(Table.Table Table)
169  {
170  InitOK = false;
171  Pixels = null;
172  base.Init(Table);
173 
174  //TODO: Insert replace values for file pattern
175  if (BitmapFilePattern.IsValid)
176  {
177  FileInfo FI = BitmapFilePattern.GetFirstMatchingFile(Table.Pinball.GlobalConfig.GetReplaceValuesDictionary());
178  if (FI!=null && FI.Exists)
179  {
180  FastImage BM;
181  try
182  {
183  BM = Table.Bitmaps[FI.FullName];
184  }
185  catch (Exception E)
186  {
187  Log.Exception("MatrixBitmapEffectBase {0} cant initialize. Could not load file {1}.".Build(Name, FI.FullName), E);
188  return;
189  }
190 
191  if (BM.Frames.ContainsKey(BitmapFrameNumber))
192  {
193  Log.Debug("BitmapEffectBase. Grabbing image clip: W: {0}, H:{1}, BML: {2}, BMT: {3}, BMW: {4}, BMH: {5}".Build(new object[] { AreaWidth, AreaHeight, BitmapLeft, BitmapTop, BitmapWidth, BitmapHeight }));
194  Pixels = BM.Frames[BitmapFrameNumber].GetClip(AreaWidth, AreaHeight, BitmapLeft, BitmapTop, BitmapWidth, BitmapHeight, DataExtractMode).Pixels;
195 
196  }
197  else
198  {
199  Log.Warning("MatrixBitmapEffectBase {0} cant initialize. Frame {1} does not exist in source image {2}.".Build(Name, BitmapFrameNumber, FI.FullName));
200 
201  }
202  }
203  else
204  {
205  Log.Warning("MatrixBitmapEffectBase {0} cant initialize. No file matches the BitmapFilePattern {1} is invalid".Build(Name, BitmapFilePattern.ToString()));
206  }
207  }
208  else
209  {
210  Log.Warning("MatrixBitmapEffectBase {0} cant initialize. The BitmapFilePattern {1} is invalid".Build(Name, BitmapFilePattern.ToString()));
211  }
212 
213 
214  InitOK = (Pixels != null && MatrixLayer != null);
215 
216  }
217 
221  public override void Finish()
222  {
223  Pixels = null;
224  base.Finish();
225  }
226  }
227 }
override void Finish()
Finishes the effect and releases object references
Struct holding the data for a single pixel in a bitmap.
Definition: PixelData.cs:12
Base class for effects targeting a matrix of toys (e.g. addressable ledstrip)
static void Warning(string Message)
Writes a warning message to the log.
Definition: Log.cs:134
A simple logger used to record important events and exceptions.
Definition: Log.cs:14
override void Trigger(Table.TableElementData TableElementData)
Triggers the effect with the given data.
override void Init(Table.Table Table)
Initializes the effect. Resolves object references, extracts source image data.
static void Debug(string Message="")
Writes the specified debug message to the log file.
Definition: Log.cs:216
FadeModeEnum
Defines the fading behaviour.
Definition: FadeModeEnum.cs:11
FastBitmapDataExtractModeEnum
The enum defines how the pixels are extracted from the source image.
Dictionary< int, FastBitmap > Frames
Definition: FastImage.cs:21
The namespace DirectOutput.General contains classes for general use.
A file pattern class used to lookup files matching a specified pattern.
Definition: FilePattern.cs:12
Outputs a defined part of a bitmap on a area of a matrix
static void Exception(string Message, Exception E=null)
Writes a exception message to the log.
Definition: Log.cs:144