DebugWindow.cs 7.9 KB

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