DebugWindow.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.DebugOutputScaleFactor * Math.Max(1, Math.Min(Parameters.DebugOutputNumImagesPerRow, Parameters.DebugOutputNumImages)) * Parameters.ImageWidth),
  31. (int)(Parameters.DebugOutputScaleFactor * (1 + (Parameters.DebugOutputNumImages - 1) / Parameters.DebugOutputNumImagesPerRow) * 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.DebugOutputTitle;
  43. GL.ClearColor(Color.Black);
  44. // transparency
  45. GL.Enable(EnableCap.Blend);
  46. GL.BlendEquation(BlendEquationMode.Max);
  47. // Depth Test
  48. GL.Enable(EnableCap.DepthTest);
  49. // Texture
  50. GL.Enable(EnableCap.Texture2D);
  51. GL.GenTextures(1, out textureID);
  52. GL.BindTexture(TextureTarget.Texture2D, textureID);
  53. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  54. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  55. palmGridControlFocus = PalmGridControlFocus.Rows;
  56. Keyboard.KeyDown += HandleKeyDownPalmGridControls;
  57. if (inputProvider is VideoInputProvider)
  58. Keyboard.KeyDown += HandleKeyDownVideoControls;
  59. }
  60. protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
  61. {
  62. base.OnClosing(e);
  63. inputProvider.stop();
  64. }
  65. protected override void OnResize(EventArgs e)
  66. {
  67. base.OnResize(e);
  68. int screenWidth = ClientRectangle.Width;
  69. int screenHeight = ClientRectangle.Height;
  70. int numRows = 1 + (Parameters.DebugOutputNumImages - 1) / Parameters.DebugOutputNumImagesPerRow;
  71. int numCols = Math.Min(Parameters.DebugOutputNumImages, Parameters.DebugOutputNumImagesPerRow);
  72. int heightForWidth = (int)((float)screenWidth / ((float)numCols * Parameters.ImageAspectRatio) * (float)numRows);
  73. GL.Viewport(0, (screenHeight - heightForWidth) / 2, screenWidth, heightForWidth);
  74. // top left at (0,0) every image from (i, j) to (i + 1, j + 1)
  75. Matrix4 projection = Matrix4.CreateOrthographicOffCenter(0, numCols, numRows, 0, 0.001f, 10f);
  76. GL.MatrixMode(MatrixMode.Projection);
  77. GL.LoadMatrix(ref projection);
  78. }
  79. protected override void OnUpdateFrame(FrameEventArgs e)
  80. {
  81. Timer.start("DebugWindow.OnUpdateFrame");
  82. base.OnUpdateFrame(e);
  83. FrameData frameData = inputHandler.FrameData;
  84. if (frameData != null)
  85. {
  86. lock (frameData)
  87. {
  88. if (currentFrameID != frameData.FrameID)
  89. {
  90. currentFrameID = frameData.FrameID;
  91. Timer.start("DebugWindow.OnUpdateFrame::updateImages");
  92. debugImages.updateImages(frameData);
  93. Timer.stop("DebugWindow.OnUpdateFrame::updateImages");
  94. }
  95. }
  96. }
  97. Timer.stop("DebugWindow.OnUpdateFrame");
  98. }
  99. protected override void OnRenderFrame(FrameEventArgs e)
  100. {
  101. Timer.start("DebugWindow.OnRenderFrame");
  102. base.OnRenderFrame(e);
  103. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  104. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  105. GL.MatrixMode(MatrixMode.Modelview);
  106. GL.LoadMatrix(ref modelview);
  107. Title = Parameters.DebugOutputTitle + " (Frame: " + currentFrameID + ")";
  108. GL.Enable(EnableCap.Texture2D);
  109. Timer.start("DebugWindow.OnRenderFrame::drawImages");
  110. int imageIndex = 0;
  111. foreach (OutputImage image in debugImages.Images)
  112. {
  113. int column = imageIndex % Parameters.DebugOutputNumImagesPerRow;
  114. int row = imageIndex / Parameters.DebugOutputNumImagesPerRow;
  115. GL.BindTexture(TextureTarget.Texture2D, textureID);
  116. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Parameters.ImageWidth, Parameters.ImageHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
  117. GL.Begin(PrimitiveType.Quads);
  118. GL.Color3(1.0, 1.0, 1.0);
  119. GL.TexCoord2(0, 0); GL.Vertex3(0 + column, 0 + row, -1);
  120. GL.TexCoord2(1, 0); GL.Vertex3(1 + column, 0 + row, -1);
  121. GL.TexCoord2(1, 1); GL.Vertex3(1 + column, 1 + row, -1);
  122. GL.TexCoord2(0, 1); GL.Vertex3(0 + column, 1 + row, -1);
  123. GL.End();
  124. imageIndex++;
  125. }
  126. Timer.stop("DebugWindow.OnRenderFrame::drawImages");
  127. Timer.start("DebugWindow.OnRenderFrame::SwapBuffers");
  128. SwapBuffers();
  129. Timer.stop("DebugWindow.OnRenderFrame::SwapBuffers");
  130. Timer.stop("DebugWindow.OnRenderFrame");
  131. }
  132. private void HandleKeyDownVideoControls(object sender, KeyboardKeyEventArgs e)
  133. {
  134. VideoInputProvider vip = (VideoInputProvider)inputProvider;
  135. switch (e.Key)
  136. {
  137. case Key.Space:
  138. if (vip.IsPaused)
  139. vip.play();
  140. else
  141. vip.pause();
  142. break;
  143. case Key.Right:
  144. if (vip.IsPaused)
  145. {
  146. vip.goToNextFrame();
  147. }
  148. break;
  149. case Key.Left:
  150. if (vip.IsPaused)
  151. {
  152. vip.goToPreviousFrame();
  153. }
  154. break;
  155. }
  156. }
  157. private void HandleKeyDownPalmGridControls(object sender, KeyboardKeyEventArgs e)
  158. {
  159. switch (e.Key)
  160. {
  161. case Key.R:
  162. palmGridControlFocus = PalmGridControlFocus.Rows;
  163. break;
  164. case Key.C:
  165. palmGridControlFocus = PalmGridControlFocus.Columns;
  166. break;
  167. case Key.Plus:
  168. case Key.KeypadPlus:
  169. case Key.BracketRight: //fix
  170. if (palmGridControlFocus == PalmGridControlFocus.Rows)
  171. Parameters.setPalmGridParameters(Parameters.PalmGridNumRows + 1, Parameters.PalmGridNumColumns);
  172. else
  173. Parameters.setPalmGridParameters(Parameters.PalmGridNumRows, Parameters.PalmGridNumColumns + 1);
  174. break;
  175. case Key.Minus:
  176. case Key.KeypadMinus:
  177. case Key.Slash: //fix
  178. if (palmGridControlFocus == PalmGridControlFocus.Rows)
  179. Parameters.setPalmGridParameters(Math.Max(1, Parameters.PalmGridNumRows - 1), Parameters.PalmGridNumColumns);
  180. else
  181. Parameters.setPalmGridParameters(Parameters.PalmGridNumRows, Math.Max(1, Parameters.PalmGridNumColumns - 1));
  182. break;
  183. }
  184. }
  185. }
  186. }