TouchEventVisualizer.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. namespace bbiwarg.Graphics
  11. {
  12. class TouchEventVisualizer
  13. {
  14. public OutputImage OutputImage { get; private set; }
  15. private int width, height;
  16. private Stopwatch timer;
  17. private Dictionary<int, List<Vector2D>> currentPositions;
  18. private Dictionary<long, List<Vector2D>> oldPositions;
  19. public TouchEventVisualizer(int width, int height)
  20. {
  21. this.width = width;
  22. this.height = height;
  23. this.timer = new Stopwatch();
  24. this.currentPositions = new Dictionary<int, List<Vector2D>>();
  25. this.oldPositions = new Dictionary<long, List<Vector2D>>();
  26. this.timer.Start();
  27. }
  28. public void reset()
  29. {
  30. currentPositions.Clear();
  31. }
  32. public void touchDown(object sender, TouchEventArgs tea)
  33. {
  34. List<Vector2D> posList = new List<Vector2D>();
  35. posList.Add(tea.RelativePosition);
  36. currentPositions.Add(tea.TrackID, posList);
  37. }
  38. public void touchMove(object sender, TouchEventArgs tea)
  39. {
  40. currentPositions[tea.TrackID].Add(tea.RelativePosition);
  41. }
  42. public void touchUp(object sender, TouchEventArgs tea)
  43. {
  44. currentPositions[tea.TrackID].Add(tea.RelativePosition);
  45. List<Vector2D> posList = currentPositions[tea.TrackID];
  46. oldPositions.Add(timer.ElapsedMilliseconds, posList);
  47. currentPositions.Remove(tea.TrackID);
  48. }
  49. public void updateImage()
  50. {
  51. //remove old positions
  52. long currentTime = timer.ElapsedMilliseconds;
  53. List<long> removeKeys = new List<long>();
  54. foreach (long lastUpdateTime in oldPositions.Keys)
  55. {
  56. if (currentTime - lastUpdateTime > Constants.TouchEventVisualizerFadeOutTime)
  57. removeKeys.Add(lastUpdateTime);
  58. }
  59. foreach (long lastUpdateTime in removeKeys)
  60. oldPositions.Remove(lastUpdateTime);
  61. OutputImage = new OutputImage(width, height);
  62. //border
  63. OutputImage.drawRectangle(0, 0, width - 1, height - 1, Constants.TouchEventVisualizerGridColor);
  64. //draw grid
  65. int numRows = Constants.PalmGridNumRows;
  66. int numColumns = Constants.PalmGridNumColumns;
  67. int widthPerColumn = width / numColumns;
  68. int heightPerRow = height / numRows;
  69. for (int i = 0; i <= numColumns; i++)
  70. {
  71. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(i * widthPerColumn, 0), new Vector2D(i * widthPerColumn, height - 1)), Constants.TouchEventVisualizerGridColor);
  72. }
  73. for (int i = 0; i <= numRows; i++)
  74. {
  75. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, i * heightPerRow), new Vector2D(width - 1, i * heightPerRow)), Constants.TouchEventVisualizerGridColor);
  76. }
  77. for (int row = 0; row < numRows; row++) {
  78. for (int col = 0; col < numColumns; col++) {
  79. int x = (int)((col+0.5f)*widthPerColumn)-5;
  80. int y = (int)((row+0.5f)*heightPerRow)+5;
  81. OutputImage.drawText(x,y,(1+row*numColumns+col).ToString(),Constants.TouchEventVisualizerTextColor);
  82. }
  83. }
  84. foreach (int id in currentPositions.Keys)
  85. drawTouchGesture(currentPositions[id], 1);
  86. foreach (long lastUpdate in oldPositions.Keys)
  87. drawTouchGesture(oldPositions[lastUpdate], 1 - ((currentTime - lastUpdate) / (float)Constants.TouchEventVisualizerFadeOutTime));
  88. }
  89. public void drawTouchGesture(List<Vector2D> positions, float opacity)
  90. {
  91. Vector2D maxPixel = new Vector2D(width - 1, height - 1);
  92. int numPositions = positions.Count;
  93. Color lineColor = Constants.TouchEventVisualizerLineColor;
  94. Color pointColor = Constants.TouchEventVisualizerPointColor;
  95. Color lineColorFaded = Color.FromArgb((int)(opacity * lineColor.R), (int)(opacity * lineColor.G), (int)(opacity * lineColor.B));
  96. Color pointColorFaded = Color.FromArgb((int)(opacity * pointColor.R), (int)(opacity * pointColor.G), (int)(opacity * pointColor.B));
  97. for (int i = 1; i < numPositions; i++)
  98. {
  99. OutputImage.drawLineSegment(new LineSegment2D(positions[i - 1].scale(maxPixel), positions[i].scale(maxPixel)), lineColorFaded);
  100. }
  101. Vector2D lastPos = positions[numPositions - 1].scale(maxPixel);
  102. OutputImage.fillCircle(lastPos.IntX, lastPos.IntY, 3, pointColorFaded);
  103. }
  104. }
  105. }