DebugWindow.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. lock (frameData) {
  95. if (currentFrameID != frameData.FrameID)
  96. {
  97. currentFrameID = frameData.FrameID;
  98. Utility.Timer.start("DebugWindow.update::updateImages");
  99. updateImages(frameData);
  100. Utility.Timer.stop("DebugWindow.update::updateImages");
  101. }
  102. }
  103. }
  104. if (!guiUpToDate)
  105. {
  106. Utility.Timer.start("DebugWindow.update::updateGUI");
  107. updateGUI();
  108. Utility.Timer.stop("DebugWindow.update::updateGUI");
  109. }
  110. Utility.Timer.stop("DebugWindow.update");
  111. }
  112. /// <summary>
  113. /// Updates the debug images.
  114. /// </summary>
  115. /// <param name="frameData">data for the new frame</param>
  116. private void updateImages(FrameData frameData) {
  117. guiUpToDate = false;
  118. int numRows = palmGridNumRowsTrackBar.Value;
  119. int numColumns = palmGridNumColumnsTrackBar.Value;
  120. debugImageCreator.updateImages(frameData, numRows, numColumns);
  121. }
  122. /// <summary>
  123. /// Updates the gui elements.
  124. /// </summary>
  125. private void updateGUI() {
  126. // update image boxes
  127. depthImageBox.Image = debugImageCreator.DepthImage;
  128. fingerImageBox.Image = debugImageCreator.FingerImage;
  129. handImageBox.Image = debugImageCreator.HandImage;
  130. palmImageBox.Image = debugImageCreator.PalmImage;
  131. touchImageBox.Image = debugImageCreator.TouchImage;
  132. // update frame label
  133. frameLabel.Text = "Frame: " + currentFrameID;
  134. guiUpToDate = true;
  135. }
  136. /// <summary>
  137. /// Handles the click on the play / pause button.
  138. /// </summary>
  139. /// <param name="sender">event sender</param>
  140. /// <param name="e">event arguments</param>
  141. private void playPauseButton_Click(object sender, EventArgs e)
  142. {
  143. handlePlayPause();
  144. }
  145. /// <summary>
  146. /// Handles the click on the next frame button.
  147. /// </summary>
  148. /// <param name="sender">event sender</param>
  149. /// <param name="e">event arguments</param>
  150. private void nextFrameButton_Click(object sender, EventArgs e)
  151. {
  152. handleGoToNextFrame();
  153. }
  154. /// <summary>
  155. /// Handles the click on the previous frame button.
  156. /// </summary>
  157. /// <param name="sender">event sender</param>
  158. /// <param name="e">event arguments</param>
  159. private void previousFrameButton_Click(object sender, EventArgs e)
  160. {
  161. handleGoToPreviousFrame();
  162. }
  163. /// <summary>
  164. /// Toggles the paused state of the movie.
  165. /// </summary>
  166. private void handlePlayPause() {
  167. VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider;
  168. if (videoInputProvider.IsPaused)
  169. {
  170. videoInputProvider.play();
  171. playPauseButton.Text = "Pause";
  172. nextFrameButton.Enabled = false;
  173. previousFrameButton.Enabled = false;
  174. }
  175. else {
  176. videoInputProvider.pause();
  177. playPauseButton.Text = "Play";
  178. nextFrameButton.Enabled = true;
  179. previousFrameButton.Enabled = true;
  180. }
  181. }
  182. /// <summary>
  183. /// Jumps to the next movie frame.
  184. /// </summary>
  185. private void handleGoToNextFrame() {
  186. VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider;
  187. videoInputProvider.goToNextFrame();
  188. }
  189. /// <summary>
  190. /// Jumps to the previous movie frame.
  191. /// </summary>
  192. private void handleGoToPreviousFrame()
  193. {
  194. VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider;
  195. videoInputProvider.goToPreviousFrame();
  196. }
  197. /// <summary>
  198. /// Updates the window when the number of rows or columns in the palm grid are changed.
  199. /// </summary>
  200. /// <param name="sender">event sender</param>
  201. /// <param name="e">event arguments</param>
  202. private void palmGridTrackBar_Scroll(object sender, EventArgs e)
  203. {
  204. currentFrameID = -1;
  205. update(sender, e);
  206. }
  207. }
  208. }