using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows; using bbiwarg.Input.InputProviding; using bbiwarg.Input.InputHandling; using bbiwarg.Utility; using Emgu.CV.UI; namespace bbiwarg.Output.DebugOutput { /// /// A Windows Form which shows the debug images. /// public partial class DebugWindow : Form { /// /// the input handler /// private InputHandler inputHandler; /// /// the input provider /// private InputProvider inputProvider; /// /// the debug image creator /// private DebugImageCreator debugImageCreator; /// /// timer to periodically update the window /// private System.Windows.Forms.Timer timer; /// /// id of the current frame /// private int currentFrameID; /// /// true iff the window is showing the latest data /// private bool guiUpToDate; /// /// Creates the DebugWindow. /// /// input provider /// input handle /// the title of the window /// the update interval for the window in ms public DebugWindow(InputProvider inputProvider, InputHandler inputHandler, String name, int updateInterval) { InitializeComponent(); this.inputProvider = inputProvider; this.inputHandler = inputHandler; guiUpToDate = false; Name = name; Text = name; TouchEventVisualizer touchEventVisualizer = new TouchEventVisualizer(); inputHandler.NewProcessedFrameEvent += touchEventVisualizer.handleNewFrameData; debugImageCreator = new DebugImageCreator(touchEventVisualizer); if (inputProvider is VideoInputProvider) playPauseButton.Enabled = true; palmGridNumRowsTrackBar.Value = Parameters.PalmGridDefaultNumRows; palmGridNumColumnsTrackBar.Value = Parameters.PalmGridDefaultNumColumns; timer = new System.Windows.Forms.Timer(); timer.Interval = updateInterval; timer.Tick += update; timer.Start(); } /// /// Stops the input provider when closing the window. /// /// event arguments protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); inputProvider.stop(); } /// /// Updates the window. /// /// the event sender /// the event arguments private void update(object sender, EventArgs e) { Utility.Timer.start("DebugWindow.update"); if (!inputProvider.IsActive) Close(); FrameData frameData = inputHandler.FrameData; if (frameData != null) { lock (frameData) { if (currentFrameID != frameData.FrameID) { currentFrameID = frameData.FrameID; Utility.Timer.start("DebugWindow.update::updateImages"); updateImages(frameData); Utility.Timer.stop("DebugWindow.update::updateImages"); } } } if (!guiUpToDate) { Utility.Timer.start("DebugWindow.update::updateGUI"); updateGUI(); Utility.Timer.stop("DebugWindow.update::updateGUI"); } Utility.Timer.stop("DebugWindow.update"); } /// /// Updates the debug images. /// /// data for the new frame private void updateImages(FrameData frameData) { guiUpToDate = false; int numRows = palmGridNumRowsTrackBar.Value; int numColumns = palmGridNumColumnsTrackBar.Value; debugImageCreator.updateImages(frameData, numRows, numColumns); } /// /// Updates the gui elements. /// private void updateGUI() { // update image boxes depthImageBox.Image = debugImageCreator.DepthImage; fingerImageBox.Image = debugImageCreator.FingerImage; handImageBox.Image = debugImageCreator.HandImage; palmImageBox.Image = debugImageCreator.PalmImage; touchImageBox.Image = debugImageCreator.TouchImage; // update frame label frameLabel.Text = "Frame: " + currentFrameID; guiUpToDate = true; } /// /// Handles the click on the play / pause button. /// /// event sender /// event arguments private void playPauseButton_Click(object sender, EventArgs e) { handlePlayPause(); } /// /// Handles the click on the next frame button. /// /// event sender /// event arguments private void nextFrameButton_Click(object sender, EventArgs e) { handleGoToNextFrame(); } /// /// Handles the click on the previous frame button. /// /// event sender /// event arguments private void previousFrameButton_Click(object sender, EventArgs e) { handleGoToPreviousFrame(); } /// /// Toggles the paused state of the movie. /// private void handlePlayPause() { VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider; if (videoInputProvider.IsPaused) { videoInputProvider.play(); playPauseButton.Text = "Pause"; nextFrameButton.Enabled = false; previousFrameButton.Enabled = false; } else { videoInputProvider.pause(); playPauseButton.Text = "Play"; nextFrameButton.Enabled = true; previousFrameButton.Enabled = true; } } /// /// Jumps to the next movie frame. /// private void handleGoToNextFrame() { VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider; videoInputProvider.goToNextFrame(); } /// /// Jumps to the previous movie frame. /// private void handleGoToPreviousFrame() { VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider; videoInputProvider.goToPreviousFrame(); } /// /// Updates the window when the number of rows or columns in the palm grid are changed. /// /// event sender /// event arguments private void palmGridTrackBar_Scroll(object sender, EventArgs e) { currentFrameID = -1; update(sender, e); } } }