TouchTracker.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using bbiwarg.Images;
  7. using bbiwarg.Graphics;
  8. namespace bbiwarg.Detectors.Touch
  9. {
  10. class TouchTracker
  11. {
  12. private OutputImage outputImage;
  13. private List<TouchEvent>[] detectedTouchEvents;
  14. private int framesUntilTracked;
  15. public List<TouchEvent> TrackedTouchEvents;
  16. public TouchTracker()
  17. {
  18. framesUntilTracked = 2;
  19. detectedTouchEvents = new List<TouchEvent>[framesUntilTracked];
  20. for (int i = 0; i < framesUntilTracked; i++)
  21. {
  22. detectedTouchEvents[i] = new List<TouchEvent>();
  23. }
  24. }
  25. public void setDetectedTouchEventsThisFrame(List<TouchEvent> touchEventsThisFrame, OutputImage outputImage)
  26. {
  27. this.outputImage = outputImage;
  28. for (int i = (framesUntilTracked - 1); i > 0; i--)
  29. {
  30. detectedTouchEvents[i] = detectedTouchEvents[i - 1];
  31. }
  32. detectedTouchEvents[0] = touchEventsThisFrame;
  33. findTrackedTouches();
  34. }
  35. private void findTrackedTouches()
  36. {
  37. TrackedTouchEvents = new List<TouchEvent>();
  38. foreach (TouchEvent te in detectedTouchEvents[0])
  39. {
  40. bool tracked = true;
  41. for (int i = 1; i < framesUntilTracked; i++)
  42. {
  43. if (!hasSimilarTouchEventInFrame(te, i)) tracked = false;
  44. }
  45. if (tracked)
  46. {
  47. outputImage.fillCircle(te.Position.IntX, te.Position.IntY, 5, 127, 0, 0);
  48. TrackedTouchEvents.Add(te);
  49. }
  50. }
  51. }
  52. private bool hasSimilarTouchEventInFrame(TouchEvent touchEvent, int frame)
  53. {
  54. foreach (TouchEvent te in detectedTouchEvents[frame])
  55. {
  56. if (touchEvent.isSimilarTo(te))
  57. return true;
  58. }
  59. return false;
  60. }
  61. }
  62. }