TouchEventVisualizer.cs 5.0 KB

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