DebugWindow.cs 7.7 KB

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