TouchEventVisualizer.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using bbiwarg.Recognition.TouchRecognition;
  9. using bbiwarg.Utility;
  10. using bbiwarg.Input.InputHandling;
  11. namespace bbiwarg.Output.DebugOutput
  12. {
  13. class TouchEventVisualizer
  14. {
  15. private Stopwatch timer;
  16. private Dictionary<int, List<Vector2D>> activeTouches;
  17. private Dictionary<int, List<Vector2D>> oldTouches;
  18. private Dictionary<int, long> lastUpdates;
  19. private int nextFreeID;
  20. private int lastUpdated;
  21. private bool active;
  22. public TouchEventVisualizer()
  23. {
  24. reset();
  25. }
  26. public void reset()
  27. {
  28. timer = new Stopwatch();
  29. timer.Start();
  30. nextFreeID = 1;
  31. lastUpdated = 0;
  32. activeTouches = new Dictionary<int,List<Vector2D>>();
  33. oldTouches = new Dictionary<int,List<Vector2D>>();
  34. lastUpdates = new Dictionary<int,long>();
  35. }
  36. public void handleNewFrameData(InputHandler sender, EventArgs e)
  37. {
  38. FrameData frameData = sender.FrameData;
  39. lock (frameData)
  40. {
  41. foreach (TouchEventArgs te in frameData.TouchEvents)
  42. {
  43. switch (te.Type)
  44. {
  45. case TouchEventType.Down:
  46. activeTouches.Add(te.TrackID, new List<Vector2D>());
  47. activeTouches[te.TrackID].Add(te.Touch.RelativePosition);
  48. break;
  49. case TouchEventType.Move:
  50. activeTouches[te.TrackID].Add(te.Touch.RelativePosition);
  51. break;
  52. case TouchEventType.Up:
  53. activeTouches[te.TrackID].Add(te.Touch.RelativePosition);
  54. oldTouches.Add(nextFreeID, activeTouches[te.TrackID]);
  55. lastUpdates.Add(nextFreeID, timer.ElapsedMilliseconds);
  56. activeTouches.Remove(te.TrackID);
  57. nextFreeID++;
  58. break;
  59. }
  60. }
  61. }
  62. }
  63. public OutputImage getOutputImage()
  64. {
  65. long currentTime = timer.ElapsedMilliseconds;
  66. removeOldPositions(currentTime - Parameters.TouchEventVisualizerFadeOutTime);
  67. int width = Parameters.ImageWidth;
  68. int height = Parameters.ImageHeight;
  69. OutputImage outputImage = new OutputImage(width, height);
  70. // border
  71. outputImage.drawRectangle(0, 0, width - 1, height - 1, Parameters.TouchEventVisualizerGridColor);
  72. // draw grid
  73. int numRows = Parameters.PalmGridNumRows;
  74. int numColumns = Parameters.PalmGridNumColumns;
  75. int widthPerColumn = width / numColumns;
  76. int heightPerRow = height / numRows;
  77. // find active blocks
  78. bool[,] activeBlocks = new bool[numRows, numColumns];
  79. if (numRows * numColumns > 1)
  80. {
  81. foreach (List<Vector2D> positions in activeTouches.Values)
  82. {
  83. Vector2D lastPosition = positions.Last();
  84. int activeRow = (int)Math.Floor(lastPosition.Y * Parameters.PalmGridNumRows);
  85. int activeCol = (int)Math.Floor(lastPosition.X * Parameters.PalmGridNumColumns);
  86. activeBlocks[activeRow, activeCol] = true;
  87. }
  88. }
  89. // draw blocks
  90. for (int row = 0; row < numRows; row++)
  91. {
  92. for (int col = 0; col < numColumns; col++)
  93. {
  94. if (activeBlocks[row, col])
  95. outputImage.fillRectangle(col * widthPerColumn, row * heightPerRow, widthPerColumn, heightPerRow, Parameters.TouchEventVisualizerActiveBlockColor);
  96. int x = (int)((col + 0.5f) * widthPerColumn) - 5;
  97. int y = (int)((row + 0.5f) * heightPerRow) + 5;
  98. outputImage.drawText(x, y, (1 + row * numColumns + col).ToString(), Parameters.TouchEventVisualizerTextColor);
  99. }
  100. }
  101. // draw grid
  102. for (int i = 0; i <= numColumns; i++)
  103. outputImage.drawLineSegment(new LineSegment2D(new Vector2D(i * widthPerColumn, 0), new Vector2D(i * widthPerColumn, height - 1)), Parameters.TouchEventVisualizerGridColor);
  104. for (int i = 0; i <= numRows; i++)
  105. outputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, i * heightPerRow), new Vector2D(width - 1, i * heightPerRow)), Parameters.TouchEventVisualizerGridColor);
  106. // draw active touches
  107. foreach (List<Vector2D> positions in activeTouches.Values)
  108. outputImage.drawTouchGesture(positions);
  109. // draw old touches (fade out)
  110. foreach (int id in oldTouches.Keys) {
  111. List<Vector2D> positions = oldTouches[id];
  112. long lastUpdate = lastUpdates[id];
  113. float opacity = 1 - ((currentTime - lastUpdate) / (float)Parameters.TouchEventVisualizerFadeOutTime);
  114. outputImage.drawTouchGesture(positions, opacity);
  115. }
  116. return outputImage;
  117. }
  118. private void removeOldPositions(long breakTime)
  119. {
  120. long currentTime = timer.ElapsedMilliseconds;
  121. List<int> ids = new List<int>(lastUpdates.Keys);
  122. for (int i = ids.Count - 1; i >= 0; i--)
  123. {
  124. int id = ids[i];
  125. if (breakTime > lastUpdates[id])
  126. {
  127. oldTouches.Remove(id);
  128. lastUpdates.Remove(id);
  129. }
  130. }
  131. }
  132. }
  133. }