TouchEventVisualizer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 touchDown(object sender, PalmTouchEventArgs ptea)
  29. {
  30. List<Vector2D> posList = new List<Vector2D>();
  31. posList.Add(ptea.Position);
  32. currentPositions.Add(ptea.TrackID, posList);
  33. }
  34. public void touchMove(object sender, PalmTouchEventArgs ptea)
  35. {
  36. currentPositions[ptea.TrackID].Add(ptea.Position);
  37. }
  38. public void touchUp(object sender, PalmTouchEventArgs ptea)
  39. {
  40. currentPositions[ptea.TrackID].Add(ptea.Position);
  41. List<Vector2D> posList = currentPositions[ptea.TrackID];
  42. oldPositions.Add(timer.ElapsedMilliseconds, posList);
  43. currentPositions.Remove(ptea.TrackID);
  44. }
  45. public void updateImage()
  46. {
  47. //remove old positions
  48. long currentTime = timer.ElapsedMilliseconds;
  49. List<long> removeKeys = new List<long>();
  50. foreach (long lastUpdateTime in oldPositions.Keys)
  51. {
  52. if (currentTime - lastUpdateTime > Constants.TouchEventVisualizerFadeOutTime)
  53. removeKeys.Add(lastUpdateTime);
  54. }
  55. foreach (long lastUpdateTime in removeKeys)
  56. oldPositions.Remove(lastUpdateTime);
  57. OutputImage = new OutputImage(width, height);
  58. //border
  59. OutputImage.drawRectangle(0, 0, width - 1, height - 1, Constants.TouchEventVisualizerGridColor);
  60. //draw grid
  61. int numRows = Constants.PalmGridNumRows;
  62. int numColumns = Constants.PalmGridNumColumns;
  63. int widthPerColumn = width / numColumns;
  64. int heightPerRow = height / numRows;
  65. for (int i = 0; i <= numColumns; i++)
  66. {
  67. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(i * widthPerColumn, 0), new Vector2D(i * widthPerColumn, height - 1)), Constants.TouchEventVisualizerGridColor);
  68. }
  69. for (int i = 0; i <= numRows; i++)
  70. {
  71. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, i * heightPerRow), new Vector2D(width - 1, i * heightPerRow)), Constants.TouchEventVisualizerGridColor);
  72. }
  73. foreach (int id in currentPositions.Keys)
  74. drawTouchGesture(currentPositions[id], 1);
  75. foreach (long lastUpdate in oldPositions.Keys)
  76. drawTouchGesture(oldPositions[lastUpdate], 1 - ((currentTime - lastUpdate) / (float)Constants.TouchEventVisualizerFadeOutTime));
  77. }
  78. public void drawTouchGesture(List<Vector2D> positions, float opacity)
  79. {
  80. Vector2D maxPixel = new Vector2D(width - 1, height - 1);
  81. int numPositions = positions.Count;
  82. Color lineColor = Constants.TouchEventVisualizerLineColor;
  83. Color pointColor = Constants.TouchEventVisualizerPointColor;
  84. Color lineColorFaded = Color.FromArgb((int)(opacity * lineColor.R), (int)(opacity * lineColor.G), (int)(opacity * lineColor.B));
  85. Color pointColorFaded = Color.FromArgb((int)(opacity * pointColor.R), (int)(opacity * pointColor.G), (int)(opacity * pointColor.B));
  86. for (int i = 1; i < numPositions; i++)
  87. {
  88. OutputImage.drawLineSegment(new LineSegment2D(positions[i - 1].scale(maxPixel), positions[i].scale(maxPixel)), lineColorFaded);
  89. }
  90. Vector2D lastPos = positions[numPositions - 1].scale(maxPixel);
  91. OutputImage.fillCircle(lastPos.IntX, lastPos.IntY, 3, pointColorFaded);
  92. }
  93. public void reset()
  94. {
  95. OutputImage = new OutputImage(width, height);
  96. currentPositions.Clear();
  97. }
  98. }
  99. }