DebugWindow.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. public partial class DebugWindow : Form
  18. {
  19. private InputHandler inputHandler;
  20. private InputProvider inputProvider;
  21. private DebugImageCreator debugImageCreator;
  22. private System.Windows.Forms.Timer timer;
  23. private int currentFrameID;
  24. private bool guiUpToDate;
  25. public DebugWindow(InputProvider inputProvider, InputHandler inputHandler, String name, int updateInterval)
  26. {
  27. InitializeComponent();
  28. this.inputProvider = inputProvider;
  29. this.inputHandler = inputHandler;
  30. guiUpToDate = false;
  31. Name = name;
  32. Text = name;
  33. TouchEventVisualizer touchEventVisualizer = new TouchEventVisualizer();
  34. inputHandler.NewProcessedFrameEvent += touchEventVisualizer.handleNewFrameData;
  35. debugImageCreator = new DebugImageCreator(touchEventVisualizer);
  36. if (inputProvider is VideoInputProvider)
  37. playPauseButton.Enabled = true;
  38. palmGridNumRowsTrackBar.Value = Parameters.PalmGridDefaultNumRows;
  39. palmGridNumColumnsTrackBar.Value = Parameters.PalmGridDefaultNumColumns;
  40. timer = new System.Windows.Forms.Timer();
  41. timer.Interval = updateInterval;
  42. timer.Tick += update;
  43. timer.Start();
  44. }
  45. protected override void OnClosing(CancelEventArgs e)
  46. {
  47. base.OnClosing(e);
  48. inputProvider.stop();
  49. }
  50. private void update(object sender, EventArgs e)
  51. {
  52. Utility.Timer.start("DebugWindow.update");
  53. if (!inputProvider.IsActive)
  54. Close();
  55. FrameData frameData = inputHandler.FrameData;
  56. if (frameData != null) {
  57. lock (frameData) {
  58. if (currentFrameID != frameData.FrameID)
  59. {
  60. currentFrameID = frameData.FrameID;
  61. Utility.Timer.start("DebugWindow.update::updateImages");
  62. updateImages(frameData);
  63. Utility.Timer.stop("DebugWindow.update::updateImages");
  64. }
  65. }
  66. }
  67. if (!guiUpToDate)
  68. {
  69. Utility.Timer.start("DebugWindow.update::updateGUI");
  70. updateGUI();
  71. Utility.Timer.stop("DebugWindow.update::updateGUI");
  72. }
  73. Utility.Timer.stop("DebugWindow.update");
  74. }
  75. private void updateImages(FrameData frameData) {
  76. guiUpToDate = false;
  77. int numRows = palmGridNumRowsTrackBar.Value;
  78. int numColumns = palmGridNumColumnsTrackBar.Value;
  79. debugImageCreator.updateImages(frameData, numRows, numColumns);
  80. }
  81. private void updateGUI() {
  82. // update image boxes
  83. depthImageBox.Image = debugImageCreator.DepthImage;
  84. fingerImageBox.Image = debugImageCreator.FingerImage;
  85. handImageBox.Image = debugImageCreator.HandImage;
  86. palmImageBox.Image = debugImageCreator.PalmImage;
  87. touchImageBox.Image = debugImageCreator.TouchImage;
  88. // update frame label
  89. frameLabel.Text = "Frame: " + currentFrameID;
  90. guiUpToDate = true;
  91. }
  92. private void playPauseButton_Click(object sender, EventArgs e)
  93. {
  94. handlePlayPause();
  95. }
  96. private void nextFrameButton_Click(object sender, EventArgs e)
  97. {
  98. handleGoToNextFrame();
  99. }
  100. private void previousFrameButton_Click(object sender, EventArgs e)
  101. {
  102. handleGoToPreviousFrame();
  103. }
  104. private void handlePlayPause() {
  105. VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider;
  106. if (videoInputProvider.IsPaused)
  107. {
  108. videoInputProvider.play();
  109. playPauseButton.Text = "Pause";
  110. nextFrameButton.Enabled = false;
  111. previousFrameButton.Enabled = false;
  112. }
  113. else {
  114. videoInputProvider.pause();
  115. playPauseButton.Text = "Play";
  116. nextFrameButton.Enabled = true;
  117. previousFrameButton.Enabled = true;
  118. }
  119. }
  120. private void handleGoToNextFrame() {
  121. VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider;
  122. videoInputProvider.goToNextFrame();
  123. }
  124. private void handleGoToPreviousFrame()
  125. {
  126. VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider;
  127. videoInputProvider.goToPreviousFrame();
  128. }
  129. private void palmGridTrackBar_Scroll(object sender, EventArgs e)
  130. {
  131. currentFrameID = -1;
  132. update(sender, e);
  133. }
  134. }
  135. }