TouchTracker.cs 2.2 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. namespace bbiwarg.Detectors.Touch
  8. {
  9. class TouchTracker
  10. {
  11. private TouchImage touchImage;
  12. private List<TouchEvent>[] detectedTouchEvents;
  13. private int framesUntilTracked;
  14. public List<TouchEvent> TrackedTouchEvents;
  15. public TouchTracker()
  16. {
  17. framesUntilTracked = 2;
  18. detectedTouchEvents = new List<TouchEvent>[framesUntilTracked];
  19. for (int i = 0; i < framesUntilTracked; i++)
  20. {
  21. detectedTouchEvents[i] = new List<TouchEvent>();
  22. }
  23. }
  24. public void setDetectedTouchEventsThisFrame(List<TouchEvent> touchEventsThisFrame, TouchImage touchImage)
  25. {
  26. this.touchImage = touchImage;
  27. for (int i = (framesUntilTracked - 1); i > 0; i--)
  28. {
  29. detectedTouchEvents[i] = detectedTouchEvents[i - 1];
  30. }
  31. detectedTouchEvents[0] = touchEventsThisFrame;
  32. findTrackedTouches();
  33. }
  34. private void findTrackedTouches()
  35. {
  36. TrackedTouchEvents = new List<TouchEvent>();
  37. foreach (TouchEvent te in detectedTouchEvents[0])
  38. {
  39. bool tracked = true;
  40. for (int i = 1; i < framesUntilTracked; i++)
  41. {
  42. if (!hasSimilarTouchEventInFrame(te, i)) tracked = false;
  43. }
  44. if (tracked)
  45. {
  46. touchImage.setStateAt(te.Position, TouchImageState.touchTracked);
  47. TrackedTouchEvents.Add(te);
  48. //Console.WriteLine("touch tracked at x:" + te.getX() + " y:" + te.getY() + " [floodValue:" + te.getFloodValue() + "]");
  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. }