VIS2 SS2013 CVD DVR
 All Classes Namespaces Functions Enumerations Properties
InputManager.cs
1 #region using statements
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Windows.Forms;
7 using System.Xml;
8 using Microsoft.Xna.Framework;
9 #endregion
10 
11 namespace visLU.Gui
12 {
16  public class InputManager
17  {
18 
19  bool xnaDrawSurface_MouseDownEvent = false;
20  int mouseDownPosX, mouseDownPosY, mouseDownPosZ = 0;
21  Vector3 mouseMovePos;
22 
23  #region properties
24  public bool XnaDrawSurface_MouseDownEvent
25  {
26  set { xnaDrawSurface_MouseDownEvent = value; }
27  get { return xnaDrawSurface_MouseDownEvent; }
28 
29  }
30 
31  public int MouseDownPosX
32  {
33  set { mouseDownPosX = value; }
34  }
35  public int MouseDownPosY
36  {
37  set { mouseDownPosY = value; }
38  }
39  public Vector3 MouseMovePos
40  {
41  set { MouseMovePos = value; }
42  }
43  #endregion
44 
49  public void setViewMode(ViewMode newViewMode)
50  {
51  GameProperties.Instance.viewMode = newViewMode;
52  }
53 
58  public int getViewMode()
59  {
60  return (int)GameProperties.Instance.viewMode;
61  }
62 
63 
69  public void setViewMode()
70  {
71 
72  ViewMode currentViewMode = GameProperties.Instance.viewMode;
73  switch (currentViewMode)
74  {
75  case ViewMode.VolView:
76  GameProperties.Instance.viewMode = ViewMode.TopView;
77  break;
78  case ViewMode.TopView:
79  GameProperties.Instance.viewMode = ViewMode.SideView;
80  break;
81  case ViewMode.SideView:
82  GameProperties.Instance.viewMode = ViewMode.FrontView;
83  break;
84  case ViewMode.FrontView:
85  GameProperties.Instance.viewMode = ViewMode.VolView;
86  break;
87  default:
88  GameProperties.Instance.viewMode = ViewMode.VolView;
89  break;
90  }
91 
92  }
93 
98  public void setZoom(int zoomStep)
99  {
100  //zoom for all views???
101  //zoom +/-
102  if (GameProperties.Instance.viewMode == ViewMode.VolView )
103  {
104  // : logic in the camera class
105  //int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;
106  //scrollStep --> e.Delta, 120->max zoom value ex. 10
107  //if endvalue > 0 -> zoom + , < 0 zoom -, = 0 nothing
108  GameProperties.Instance.position.Z = zoomStep;
109  // mouseDownPosZ = scrollStep;
110  }
111  }
112 
119  public void setSliceValue(decimal xValue, decimal yValue, decimal zValue)
120  {
121  GameProperties.Instance.xSliceValue = (float)xValue;
122  GameProperties.Instance.ySliceValue = (float)yValue;
123  GameProperties.Instance.zSliceValue = (float)zValue;
124 
125  if (!GameProperties.Instance.viewMode.Equals(ViewMode.VolView))
126  {
127  updateXnaEngineState(false, false, false, true);
128  }
129  }
130 
136  public void setSliceValue(int slice, decimal value)
137  {
138  switch (slice)
139  {
140  case 1:
141  GameProperties.Instance.xSliceValue = (float) value;
142  break;
143  case 2:
144  GameProperties.Instance.ySliceValue = (float) value;
145  break;
146  case 3:
147  GameProperties.Instance.zSliceValue = (float) value;
148  break;
149  default:
150  break;
151  }
152 
153  if (!GameProperties.Instance.viewMode.Equals(ViewMode.VolView))
154  {
155  updateXnaEngineState(false, false, false, true);
156  }
157 
158  }
159 
165  public bool enableSliceGroup(int slice)
166  {
167  if (GameProperties.Instance.viewMode.Equals(ViewMode.VolView)) return false;
168 
169  switch (slice)
170  {
171  case 1: //x slice -> side view
172  if (GameProperties.Instance.viewMode.Equals(ViewMode.SideView)) return true;
173  break;
174  case 2: //y slice -> top view
175  if (GameProperties.Instance.viewMode.Equals(ViewMode.TopView)) return true;
176  break;
177  case 3: //z slice -> front view
178  if (GameProperties.Instance.viewMode.Equals(ViewMode.FrontView)) return true;
179  break;
180  default:
181  return false;
182  }
183 
184  return false;
185  }
186 
192  public void enableTransferFunction(bool enable, bool enableAlpha)
193  {
194  GameProperties.Instance.enableTransferFunction = enable;
195  GameProperties.Instance.enableTransferAlphaValues = enableAlpha;
196  updateXnaEngineState(false, false, true, true);
197 
198  }
199 
200  #region extended feature set
201  public void enableClippingPlane(bool enable)
206  {
207  GameProperties.Instance.enableClippingPlane = enable;
208  updateXnaEngineState(true, false, true, true);
209  }
210 
215  public void enableMaxIntensityProjection(bool enable)
216  {
217  GameProperties.Instance.maximumIntensityProjection = enable;
218  updateXnaEngineState(true, false, true, true);
219  }
220 
225  public void enablePerspProjection(bool enable)
226  {
227  GameProperties.Instance.perspectiveProjection = enable;
228  updateXnaEngineState(true, false, true, true);
229  }
230 
235  public void enableFrontToBackBlending(bool enable)
236  {
237  GameProperties.Instance.enableFrontToBackBlend = enable;
238  updateXnaEngineState(true, false, true, true);
239  }
240 
245  public void enableShading(bool enable)
246  {
247  GameProperties.Instance.enableShading = enable;
248  updateXnaEngineState(true, false, false, false);
249  }
250 
251  #region old version fullscreen option
252  /*public void enableFullscreenMode(bool enable)
257  {
258  GameProperties.Instance.fullscreen = enable;
259  updateXnaEngineState(true, false, true, true);
260  }*/
261  #endregion
262 
263  #endregion
264 
271  public void updateMousePos(int posX, int posY)
272  {
273  if (GameProperties.Instance.viewMode == ViewMode.VolView)
274  {
275  GameProperties.Instance.position.X = mouseDownPosX - posX;
276  GameProperties.Instance.position.Y = mouseDownPosY - posY;
277  mouseDownPosX = posX;
278  mouseDownPosY = posY;
279  }
280  }
281 
282  public void resetMousePos()
283  {
284  GameProperties.Instance.position.X = 0;
285  GameProperties.Instance.position.Y = 0;
286  GameProperties.Instance.position.Z = 0;
287  mouseDownPosX = 0;
288  mouseDownPosY = 0;
289  mouseDownPosZ = 0;
290  }
291 
297  public void updateMouseScroll(int scrollStep)
298  {
299  int mousePos = getMousePos(mouseDownPosX, mouseDownPosY);
300 
301  //zoom +/-
302  if (GameProperties.Instance.viewMode == ViewMode.VolView )
303  {
304  // : logic in the camera class
305  //int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;
306  //scrollStep --> e.Delta, 120->max zoom value ex. 10
307  //if endvalue > 0 -> zoom + , < 0 zoom -, = 0 nothing
308  GameProperties.Instance.position.Z = scrollStep;
309  // mouseDownPosZ = scrollStep;
310  }
311  //move slice
312  else
313  {
314  if (GameProperties.Instance.viewMode == ViewMode.TopView)
315  {
316  //GameProperties.Instance.ySliceValue = -scrollStep/10;
317  }
318  else if (GameProperties.Instance.viewMode == ViewMode.SideView)
319  {
320  //GameProperties.Instance.xSliceValue = -scrollStep/10;
321  }
322  else if (GameProperties.Instance.viewMode == ViewMode.FrontView)
323  {
324  //GameProperties.Instance.zSliceValue = -scrollStep/10;
325  }
326 
327  }
328 
329  }
330 
343  private int getMousePos(int posX, int posY)
344  {
345  /*if ((posX >= GameProperties.Instance.XNASingleViewport.X && posY >= GameProperties.Instance.XNASingleViewport.Y)
346  && (posX <= GameProperties.Instance.XNASingleViewport.X + GameProperties.Instance.XNASingleViewport.Width && posY <= GameProperties.Instance.XNASingleViewport.Y + GameProperties.Instance.XNASingleViewport.Height))
347  {
348 
349  for (int i = 0; i < 4; i++)
350  {
351  if ((posX >= GameProperties.Instance.XNAViewports[i].X && posY >= GameProperties.Instance.XNAViewports[i].Y)
352  && (posX <= GameProperties.Instance.XNAViewports[i].X + GameProperties.Instance.XNAViewports[i].Width && posY <= GameProperties.Instance.XNAViewports[i].Y + GameProperties.Instance.XNAViewports[i].Height))
353  return i;
354  }
355  return 1;
356 
357  }*/
358 
359  //mouse outside the viewports
360  return -1;
361  }
362 
363  public void loadTransferFunction(String fileName)
364  {
365  GameProperties.Instance.colorControlPoints.Clear();
366  GameProperties.Instance.alphaControlPoints.Clear();
367 
368  XmlTextReader xmlReader = new XmlTextReader(fileName);
369  xmlReader.MoveToElement();
370 
371  float r = 0.0f;
372  float g = 0.0f;
373  float b = 0.0f;
374  float a = 0.0f;
375  int isoValue = 0;
376 
377  while (xmlReader.Read())
378  {
379  if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name.Equals("ControlPoint"))
380  {
381  while (!(xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name.Equals("ControlPoint")))
382  {
383  xmlReader.Read();
384 
385  if (xmlReader.NodeType.Equals(XmlNodeType.Element))
386  {
387  String elementName = xmlReader.Name;
388  xmlReader.Read();
389  if (xmlReader.NodeType.Equals(XmlNodeType.Text))
390  {
391  switch (elementName)
392  {
393  case "ColorR":
394  r = float.Parse(xmlReader.Value);
395  break;
396 
397  case "ColorG":
398  g = float.Parse(xmlReader.Value);
399  break;
400 
401  case "ColorB":
402  b = float.Parse(xmlReader.Value);
403  break;
404 
405  case "ColorA":
406  a = float.Parse(xmlReader.Value);
407  break;
408 
409  case "IsoValue":
410  isoValue = int.Parse(xmlReader.Value);
411  break;
412  }//end switch
413  }//end if
414  }
415  }//end while
416 
417  //save new control point
418  GameProperties.Instance.colorControlPoints.Add(new TransferControlPoint(r, g, b, isoValue));
419  GameProperties.Instance.alphaControlPoints.Add(new TransferControlPoint(a, isoValue));
420  }
421  }
422  }
423 
427  public void saveTransferFunction(string fileName)
428  {
429  XmlTextWriter xmlWriter = new XmlTextWriter(fileName, null);
430 
431  xmlWriter.WriteStartDocument();
432  xmlWriter.WriteStartElement("TransferFunction");
433 
434  for (int i = 0; i < GameProperties.Instance.colorControlPoints.Count(); i++)
435  {
436  xmlWriter.WriteStartElement("ControlPoint");
437 
438  xmlWriter.WriteStartElement("ColorR");
439  xmlWriter.WriteString(GameProperties.Instance.colorControlPoints[i].color.X.ToString());
440  xmlWriter.WriteEndElement();
441 
442  xmlWriter.WriteStartElement("ColorG");
443  xmlWriter.WriteString(GameProperties.Instance.colorControlPoints[i].color.Y.ToString());
444  xmlWriter.WriteEndElement();
445 
446  xmlWriter.WriteStartElement("ColorB");
447  xmlWriter.WriteString(GameProperties.Instance.colorControlPoints[i].color.Z.ToString());
448  xmlWriter.WriteEndElement();
449 
450  xmlWriter.WriteStartElement("ColorA");
451  xmlWriter.WriteString(GameProperties.Instance.alphaControlPoints[i].color.W.ToString());
452  xmlWriter.WriteEndElement();
453 
454  xmlWriter.WriteStartElement("IsoValue");
455  xmlWriter.WriteString(GameProperties.Instance.colorControlPoints[i].isoValue.ToString());
456  xmlWriter.WriteEndElement();
457 
458  xmlWriter.WriteEndElement();
459  }
460 
461  xmlWriter.WriteEndElement();
462  xmlWriter.WriteEndDocument();
463  xmlWriter.Close();
464  }
465 
466  #region update game and engine state
467  public void updateXnaEngineState(bool updateCam, bool load, bool update, bool draw)
468  {
469  GameProperties.Instance.engineState.updateCamera = updateCam;
470  GameProperties.Instance.engineState.loadData = load;
471  GameProperties.Instance.engineState.updateData = update;
472  GameProperties.Instance.engineState.drawData = draw;
473  }
474  #endregion
475  }
476 }