VIS2 SS2013 CVD DVR
 All Classes Namespaces Functions Enumerations Properties
VisXnaConrol.cs
1 #region Using Statements
2 using System;
3 using System.Drawing;
4 using System.Windows.Forms;
5 using Microsoft.Xna.Framework.Graphics;
6 #endregion
7 
8 namespace visLU
9 {
10  //use System.Drawing.Color and Microsoft.Xna.Framework.Rectangle definitions
11  using Color = System.Drawing.Color;
12  using Rectangle = Microsoft.Xna.Framework.Rectangle;
13 
18  abstract public class VisXnaConrol: Control
19  {
20  #region Variables
21  VisGraphicsDevice visGraphicsDevice;
23  public bool swapBuffers = true;
24 
25 
26  #endregion
27 
28  #region Properties
29  public GraphicsDevice GraphicsDevice
30  {
31  get { return visGraphicsDevice.GraphicsDevice; }
32  }
33 
34  public VisServiceContainer Services
35  {
36  get { return services; }
37  }
38  #endregion
39 
40  #region Initialize
41 
42 
43  protected override void OnCreateControl()
44  {
45  // Don't initialize the graphics device if we are running in the designer.
46  if (!DesignMode)
47  {
48  visGraphicsDevice = VisGraphicsDevice.Add(Handle, ClientSize.Width, ClientSize.Height);
49 
50  // Register the service, so components like ContentManager can find it.
51 
52  services.AddService<IGraphicsDeviceService>(visGraphicsDevice);
53  // Give derived classes a chance to initialize themselves.
54 
55  Initialize();
56  LoadContent();
57 
58  //Update();
59  }
60 
61  base.OnCreateControl();
62  }
63 
64 
68  protected override void Dispose(bool disposing)
69  {
70  if (visGraphicsDevice != null)
71  {
72  visGraphicsDevice.Release(disposing);
73  visGraphicsDevice = null;
74  }
75 
76  base.Dispose(disposing);
77  }
78 
79 
80  #endregion
81 
82  #region Windows Form Update
83  protected override void OnInvalidated(InvalidateEventArgs e)
84  {
85  //Update();
86  base.OnInvalidated(e);
87  Console.WriteLine("Call OnInvalidated");
88  //Console.WriteLine(System.Environment.StackTrace);
89  //base.Update();
90 
91 
92  }
93 
94  /*public override void Refresh()
95  {
96  //Update();
97  base.Refresh();
98  }*/
99  #endregion
100 
101  #region resize
102  //implement onResize Event
103  #endregion
104 
105  #region Windows Form Paint
106  protected override void OnPaint(PaintEventArgs e)
110  {
111  Console.WriteLine("Call OnPaint");
112  Update();
113  //call update for the child controls
114  //if (Controls.Count > 0)
115  //{
116  // Console.WriteLine("Child: " + Controls.Count);
117  /*foreach (Control c in Controls)
118  {
119  if(c.GetType().Equals("Data"))
120  ((Data)c).Update();
121 
122  }*/
123  //}
124 
125  string beginDrawError = BeginDraw(); //xna BeginDraw
126 
127  if (string.IsNullOrEmpty(beginDrawError))
128  {
129  // Draw the control using the GraphicsDevice.
130  Draw(); //xna Draw
131  EndDraw(); //xna EndDraw
132  }
133  else
134  {
135  // If BeginDraw failed, show an error message using System.Drawing.
136  PaintUsingSystemDrawing(e.Graphics, beginDrawError);
137  }
138 
139  //todo: call draw for all child controls
140  }
141 
142 
148  string BeginDraw()
149  {
150  // If we have no graphics device, we must be running in the designer.
151  if (visGraphicsDevice == null)
152  {
153  return Text + "\n\n" + GetType();
154  }
155 
156  // Make sure the graphics device is big enough, and is not lost.
157  string deviceResetError = HandleDeviceReset();
158 
159  if (!string.IsNullOrEmpty(deviceResetError))
160  {
161  return deviceResetError;
162  }
163 
164  // Many GraphicsDeviceControl instances can be sharing the same
165  // GraphicsDevice. The device backbuffer will be resized to fit the
166  // largest of these controls. But what if we are currently drawing
167  // a smaller control? To avoid unwanted stretching, we set the
168  // viewport to only use the top left portion of the full backbuffer.
169  Viewport viewport = new Viewport();
170 
171  viewport.X = 0;
172  viewport.Y = 0;
173 
174  viewport.Width = ClientSize.Width;
175  viewport.Height = ClientSize.Height;
176 
177  viewport.MinDepth = 0;
178  viewport.MaxDepth = 1;
179 
180  GraphicsDevice.Viewport = viewport;
181 
182  return null;
183  }
184 
185 
192  void EndDraw()
193  {
194  if (swapBuffers)
195  {
196 
197 
198  try
199  {
200  Rectangle sourceRectangle = new Rectangle(0, 0, ClientSize.Width,
201  ClientSize.Height);
202 
203  GraphicsDevice.Present(sourceRectangle, null, this.Handle);
204  //GraphicsDevice.Present();
205  }
206  catch
207  {
208  // Present might throw if the device became lost while we were
209  // drawing. The lost device will be handled by the next BeginDraw,
210  // so we just swallow the exception.
211  }
212  }
213  else
214  {
215  swapBuffers = true;
216  }
217  }
218 
219 
226  string HandleDeviceReset()
227  {
228  bool deviceNeedsReset = false;
229 
230  switch (GraphicsDevice.GraphicsDeviceStatus)
231  {
232  case GraphicsDeviceStatus.Lost:
233  // If the graphics device is lost, we cannot use it at all.
234  return "Graphics device lost";
235 
236  case GraphicsDeviceStatus.NotReset:
237  // If device is in the not-reset state, we should try to reset it.
238  deviceNeedsReset = true;
239  break;
240 
241  default:
242  // If the device state is ok, check whether it is big enough.
243  PresentationParameters pp = GraphicsDevice.PresentationParameters;
244 
245  deviceNeedsReset = (ClientSize.Width > pp.BackBufferWidth) ||
246  (ClientSize.Height > pp.BackBufferHeight);
247  break;
248  }
249 
250  // Do we need to reset the device?
251  if (deviceNeedsReset)
252  {
253  try
254  {
255  visGraphicsDevice.ResetDevice(ClientSize.Width,
256  ClientSize.Height);
257  }
258  catch (Exception e)
259  {
260  return "Graphics device reset failed\n\n" + e;
261  }
262  }
263 
264  return null;
265  }
266 
267 
273  protected virtual void PaintUsingSystemDrawing(Graphics graphics, string text)
274  {
275  graphics.Clear(Color.CornflowerBlue);
276 
277  using (Brush brush = new SolidBrush(Color.Black))
278  {
279  using (StringFormat format = new StringFormat())
280  {
281  format.Alignment = StringAlignment.Center;
282  format.LineAlignment = StringAlignment.Center;
283 
284  graphics.DrawString(text, Font, brush, ClientRectangle, format);
285  }
286  }
287  }
288 
289 
296  protected override void OnPaintBackground(PaintEventArgs pevent)
297  {
298  }
299 
300 
301  #endregion
302 
303  #region Control Properties Events
304  protected override void OnSizeChanged(EventArgs e)
305  {
306  base.OnSizeChanged(e);
307  //Update();
308  }
309  #endregion
310 
311  //methods to implement in the engine and data classes
312  #region Abstract Methods
313 
314  protected abstract void Initialize();
315  protected abstract void LoadContent();
316  protected abstract new void Update();
317  protected abstract void Draw();
318 
319  #endregion
320  }
321 }