DebugWindow.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using OpenTK;
  8. using OpenTK.Input;
  9. using OpenTK.Graphics.OpenGL;
  10. using OpenTK.Graphics;
  11. using bbiwarg.Input.InputHandling;
  12. using bbiwarg.Input.InputProviding;
  13. using bbiwarg.Utility;
  14. namespace bbiwarg.Output.DebugOutput
  15. {
  16. public enum PalmGridControlFocus
  17. {
  18. Rows,
  19. Columns
  20. }
  21. class DebugWindow : GameWindow
  22. {
  23. private InputHandler inputHandler;
  24. private InputProvider inputProvider;
  25. private PalmGridControlFocus palmGridControlFocus;
  26. private uint textureID;
  27. private int currentFrameID;
  28. private DebugImages debugImages;
  29. public DebugWindow(InputProvider inputProvider, InputHandler inputHandler)
  30. : base((int)(Parameters.DebugWindowScaleFactor * Math.Max(1, Math.Min(Parameters.DebugWindowNumImagesPerRow, Parameters.DebugWindowNumImages)) * Parameters.ImageWidth),
  31. (int)(Parameters.DebugWindowScaleFactor * (1 + (Parameters.DebugWindowNumImages - 1) / Parameters.DebugWindowNumImagesPerRow) * Parameters.ImageHeight))
  32. {
  33. this.inputProvider = inputProvider;
  34. this.inputHandler = inputHandler;
  35. TouchEventVisualizer touchEventVisualizer = new TouchEventVisualizer();
  36. inputHandler.NewProcessedFrameEvent += touchEventVisualizer.handleNewFrameData;
  37. debugImages = new DebugImages(touchEventVisualizer);
  38. }
  39. protected override void OnLoad(EventArgs e)
  40. {
  41. base.OnLoad(e);
  42. Title = Parameters.DebugWindowTitle;
  43. GL.ClearColor(Color.Black);
  44. // Texture
  45. GL.Enable(EnableCap.Texture2D);
  46. GL.GenTextures(1, out textureID);
  47. GL.BindTexture(TextureTarget.Texture2D, textureID);
  48. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  49. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  50. palmGridControlFocus = PalmGridControlFocus.Rows;
  51. Keyboard.KeyDown += HandleKeyDownPalmGridControls;
  52. if (inputProvider is VideoInputProvider)
  53. Keyboard.KeyDown += HandleKeyDownVideoControls;
  54. }
  55. protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
  56. {
  57. base.OnClosing(e);
  58. inputProvider.stop();
  59. }
  60. protected override void OnResize(EventArgs e)
  61. {
  62. base.OnResize(e);
  63. int screenWidth = ClientRectangle.Width;
  64. int screenHeight = ClientRectangle.Height;
  65. int numRows = 1 + (Parameters.DebugWindowNumImages - 1) / Parameters.DebugWindowNumImagesPerRow;
  66. int numCols = Math.Min(Parameters.DebugWindowNumImages, Parameters.DebugWindowNumImagesPerRow);
  67. int heightForWidth = (int)((float)screenWidth / ((float)numCols * Parameters.ImageAspectRatio) * (float)numRows);
  68. GL.Viewport(0, (screenHeight - heightForWidth) / 2, screenWidth, heightForWidth);
  69. // top left at (0,0) every image from (i, j) to (i + 1, j + 1)
  70. Matrix4 projection = Matrix4.CreateOrthographicOffCenter(0, numCols, numRows, 0, 0.001f, 10f);
  71. GL.MatrixMode(MatrixMode.Projection);
  72. GL.LoadMatrix(ref projection);
  73. }
  74. protected override void OnUpdateFrame(FrameEventArgs e)
  75. {
  76. Timer.start("DebugWindow.OnUpdateFrame");
  77. base.OnUpdateFrame(e);
  78. if (!inputProvider.IsActive)
  79. Exit();
  80. FrameData frameData = inputHandler.FrameData;
  81. if (frameData != null)
  82. {
  83. lock (frameData)
  84. {
  85. if (currentFrameID != frameData.FrameID)
  86. {
  87. currentFrameID = frameData.FrameID;
  88. Timer.start("DebugWindow.OnUpdateFrame::updateImages");
  89. debugImages.updateImages(frameData);
  90. Timer.stop("DebugWindow.OnUpdateFrame::updateImages");
  91. }
  92. }
  93. }
  94. Timer.stop("DebugWindow.OnUpdateFrame");
  95. }
  96. protected override void OnRenderFrame(FrameEventArgs e)
  97. {
  98. Timer.start("DebugWindow.OnRenderFrame");
  99. base.OnRenderFrame(e);
  100. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  101. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  102. GL.MatrixMode(MatrixMode.Modelview);
  103. GL.LoadMatrix(ref modelview);
  104. Title = Parameters.DebugWindowTitle + " (Frame: " + currentFrameID + ")";
  105. GL.Enable(EnableCap.Texture2D);
  106. Timer.start("DebugWindow.OnRenderFrame::drawImages");
  107. int imageIndex = 0;
  108. foreach (OutputImage image in debugImages.Images)
  109. {
  110. int column = imageIndex % Parameters.DebugWindowNumImagesPerRow;
  111. int row = imageIndex / Parameters.DebugWindowNumImagesPerRow;
  112. GL.BindTexture(TextureTarget.Texture2D, textureID);
  113. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Parameters.ImageWidth, Parameters.ImageHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
  114. GL.Begin(PrimitiveType.Quads);
  115. GL.Color3(1.0, 1.0, 1.0);
  116. GL.TexCoord2(0, 0); GL.Vertex3(0 + column, 0 + row, -1);
  117. GL.TexCoord2(1, 0); GL.Vertex3(1 + column, 0 + row, -1);
  118. GL.TexCoord2(1, 1); GL.Vertex3(1 + column, 1 + row, -1);
  119. GL.TexCoord2(0, 1); GL.Vertex3(0 + column, 1 + row, -1);
  120. GL.End();
  121. imageIndex++;
  122. }
  123. Timer.stop("DebugWindow.OnRenderFrame::drawImages");
  124. Timer.start("DebugWindow.OnRenderFrame::SwapBuffers");
  125. SwapBuffers();
  126. Timer.stop("DebugWindow.OnRenderFrame::SwapBuffers");
  127. Timer.stop("DebugWindow.OnRenderFrame");
  128. }
  129. private void HandleKeyDownVideoControls(object sender, KeyboardKeyEventArgs e)
  130. {
  131. VideoInputProvider vip = (VideoInputProvider)inputProvider;
  132. switch (e.Key)
  133. {
  134. case Key.Space:
  135. if (vip.IsPaused)
  136. vip.play();
  137. else
  138. vip.pause();
  139. break;
  140. case Key.Right:
  141. if (vip.IsPaused)
  142. {
  143. vip.goToNextFrame();
  144. }
  145. break;
  146. case Key.Left:
  147. if (vip.IsPaused)
  148. {
  149. vip.goToPreviousFrame();
  150. }
  151. break;
  152. }
  153. }
  154. private void HandleKeyDownPalmGridControls(object sender, KeyboardKeyEventArgs e)
  155. {
  156. switch (e.Key)
  157. {
  158. case Key.R:
  159. palmGridControlFocus = PalmGridControlFocus.Rows;
  160. break;
  161. case Key.C:
  162. palmGridControlFocus = PalmGridControlFocus.Columns;
  163. break;
  164. case Key.Plus:
  165. case Key.KeypadPlus:
  166. case Key.BracketRight: //fix
  167. if (palmGridControlFocus == PalmGridControlFocus.Rows)
  168. Parameters.setPalmGridParameters(Parameters.PalmGridNumRows + 1, Parameters.PalmGridNumColumns);
  169. else
  170. Parameters.setPalmGridParameters(Parameters.PalmGridNumRows, Parameters.PalmGridNumColumns + 1);
  171. break;
  172. case Key.Minus:
  173. case Key.KeypadMinus:
  174. case Key.Slash: //fix
  175. if (palmGridControlFocus == PalmGridControlFocus.Rows)
  176. Parameters.setPalmGridParameters(Math.Max(1, Parameters.PalmGridNumRows - 1), Parameters.PalmGridNumColumns);
  177. else
  178. Parameters.setPalmGridParameters(Parameters.PalmGridNumRows, Math.Max(1, Parameters.PalmGridNumColumns - 1));
  179. break;
  180. }
  181. }
  182. }
  183. }