DebugWindow.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using BBIWARG.Input.InputHandling;
  2. using BBIWARG.Input.InputProviding;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. namespace BBIWARG.Output.DebugOutput
  7. {
  8. /// <summary>
  9. /// A Windows Form which shows the debug images.
  10. /// </summary>
  11. public partial class DebugWindow : Form
  12. {
  13. /// <summary>
  14. /// id of the current frame
  15. /// </summary>
  16. private int currentFrameID;
  17. private API.Grid grid;
  18. /// <summary>
  19. /// the debug image creator
  20. /// </summary>
  21. private DebugImageCreator debugImageCreator;
  22. /// <summary>
  23. /// true iff the window is showing the latest data
  24. /// </summary>
  25. private bool guiUpToDate;
  26. /// <summary>
  27. /// the input handler
  28. /// </summary>
  29. private InputHandler inputHandler;
  30. /// <summary>
  31. /// the input provider
  32. /// </summary>
  33. private IInputProvider inputProvider;
  34. /// <summary>
  35. /// timer to periodically update the window
  36. /// </summary>
  37. private System.Windows.Forms.Timer timer;
  38. /// <summary>
  39. /// Creates the DebugWindow.
  40. /// </summary>
  41. /// <param name="inputProvider">input provider</param>
  42. /// <param name="inputHandler">input handle</param>
  43. /// <param name="name">the title of the window</param>
  44. /// <param name="updateInterval">the update interval for the window in milliseconds</param>
  45. public DebugWindow(IInputProvider inputProvider, InputHandler inputHandler, String name, int updateInterval)
  46. {
  47. InitializeComponent();
  48. this.inputProvider = inputProvider;
  49. this.inputHandler = inputHandler;
  50. guiUpToDate = false;
  51. Name = name;
  52. Text = name;
  53. TouchEventVisualizer touchEventVisualizer = new TouchEventVisualizer();
  54. inputHandler.NewProcessedFrameEvent += touchEventVisualizer.handleNewFrameData;
  55. debugImageCreator = new DebugImageCreator(touchEventVisualizer);
  56. if (inputProvider is VideoInputProvider)
  57. playPauseButton.Enabled = true;
  58. palmGridNumRowsTrackBar.Value = Parameters.PalmGridDefaultNumRows;
  59. palmGridNumColumnsTrackBar.Value = Parameters.PalmGridDefaultNumColumns;
  60. grid = API.Grid.getInctance();
  61. grid.Cols = Parameters.PalmGridDefaultNumColumns;
  62. grid.Rows = Parameters.PalmGridDefaultNumRows;
  63. timer = new System.Windows.Forms.Timer();
  64. timer.Interval = updateInterval;
  65. timer.Tick += update;
  66. timer.Start();
  67. }
  68. /// <summary>
  69. /// Stops the input provider when closing the window.
  70. /// </summary>
  71. /// <param name="e">event arguments</param>
  72. protected override void OnClosing(CancelEventArgs e)
  73. {
  74. base.OnClosing(e);
  75. inputProvider.stop();
  76. }
  77. /// <summary>
  78. /// Jumps to the next movie frame.
  79. /// </summary>
  80. private void handleGoToNextFrame()
  81. {
  82. VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider;
  83. videoInputProvider.goToNextFrame();
  84. }
  85. /// <summary>
  86. /// Jumps to the previous movie frame.
  87. /// </summary>
  88. private void handleGoToPreviousFrame()
  89. {
  90. VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider;
  91. videoInputProvider.goToPreviousFrame();
  92. }
  93. /// <summary>
  94. /// Toggles the paused state of the movie.
  95. /// </summary>
  96. private void handlePlayPause()
  97. {
  98. VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider;
  99. if (videoInputProvider.IsPaused)
  100. {
  101. videoInputProvider.play();
  102. playPauseButton.Text = "Pause";
  103. nextFrameButton.Enabled = false;
  104. previousFrameButton.Enabled = false;
  105. }
  106. else
  107. {
  108. videoInputProvider.pause();
  109. playPauseButton.Text = "Play";
  110. nextFrameButton.Enabled = true;
  111. previousFrameButton.Enabled = true;
  112. }
  113. }
  114. /// <summary>
  115. /// Handles the click on the next frame button.
  116. /// </summary>
  117. /// <param name="sender">event sender</param>
  118. /// <param name="e">event arguments</param>
  119. private void nextFrameButton_Click(object sender, EventArgs e)
  120. {
  121. handleGoToNextFrame();
  122. }
  123. /// <summary>
  124. /// Updates the window when the number of rows or columns in the palm grid are changed.
  125. /// </summary>
  126. /// <param name="sender">event sender</param>
  127. /// <param name="e">event arguments</param>
  128. private void palmGridTrackBar_Scroll(object sender, EventArgs e)
  129. {
  130. currentFrameID = -1;
  131. update(sender, e);
  132. }
  133. /// <summary>
  134. /// Handles the click on the play / pause button.
  135. /// </summary>
  136. /// <param name="sender">event sender</param>
  137. /// <param name="e">event arguments</param>
  138. private void playPauseButton_Click(object sender, EventArgs e)
  139. {
  140. handlePlayPause();
  141. }
  142. /// <summary>
  143. /// Handles the click on the previous frame button.
  144. /// </summary>
  145. /// <param name="sender">event sender</param>
  146. /// <param name="e">event arguments</param>
  147. private void previousFrameButton_Click(object sender, EventArgs e)
  148. {
  149. handleGoToPreviousFrame();
  150. }
  151. /// <summary>
  152. /// Updates the window.
  153. /// </summary>
  154. /// <param name="sender">the event sender</param>
  155. /// <param name="e">the event arguments</param>
  156. private void update(object sender, EventArgs e)
  157. {
  158. Utility.Timer.start("DebugWindow.update");
  159. if (!inputProvider.IsActive)
  160. Close();
  161. FrameData frameData = inputHandler.FrameData;
  162. if (frameData != null)
  163. {
  164. lock (frameData)
  165. {
  166. if (currentFrameID != frameData.FrameID)
  167. {
  168. currentFrameID = frameData.FrameID;
  169. Utility.Timer.start("DebugWindow.update::updateImages");
  170. updateImages(frameData);
  171. Utility.Timer.stop("DebugWindow.update::updateImages");
  172. }
  173. }
  174. }
  175. if (!guiUpToDate)
  176. {
  177. Utility.Timer.start("DebugWindow.update::updateGUI");
  178. updateGUI();
  179. Utility.Timer.stop("DebugWindow.update::updateGUI");
  180. }
  181. Utility.Timer.stop("DebugWindow.update");
  182. }
  183. /// <summary>
  184. /// Updates the GUI elements.
  185. /// </summary>
  186. private void updateGUI()
  187. {
  188. // update image boxes
  189. depthImageBox.Image = debugImageCreator.DepthImage;
  190. fingerImageBox.Image = debugImageCreator.FingerImage;
  191. handImageBox.Image = debugImageCreator.HandImage;
  192. palmImageBox.Image = debugImageCreator.PalmImage;
  193. touchImageBox.Image = debugImageCreator.TouchImage;
  194. // update frame label
  195. frameLabel.Text = "Frame: " + currentFrameID;
  196. guiUpToDate = true;
  197. }
  198. /// <summary>
  199. /// Updates the debug images.
  200. /// </summary>
  201. /// <param name="frameData">data for the new frame</param>
  202. private void updateImages(FrameData frameData)
  203. {
  204. guiUpToDate = false;
  205. int numRows = palmGridNumRowsTrackBar.Value;
  206. int numColumns = palmGridNumColumnsTrackBar.Value;
  207. debugImageCreator.updateImages(frameData, numRows, numColumns);
  208. }
  209. }
  210. }