TouchTracker.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 List<TouchEvent> trackedTouchEvents;
  14. private int framesUntilTracked;
  15. public TouchTracker() {
  16. framesUntilTracked = 2;
  17. detectedTouchEvents = new List<TouchEvent>[framesUntilTracked];
  18. for (int i = 0; i < framesUntilTracked; i++) {
  19. detectedTouchEvents[i] = new List<TouchEvent>();
  20. }
  21. }
  22. public List<TouchEvent> getTouchEvents() {
  23. return trackedTouchEvents;
  24. }
  25. public void setDetectedTouchEventsThisFrame(List<TouchEvent> touchEventsThisFrame, TouchImage touchImage) {
  26. this.touchImage = touchImage;
  27. for (int i = (framesUntilTracked-1); i > 0; i--) {
  28. detectedTouchEvents[i] = detectedTouchEvents[i - 1];
  29. }
  30. detectedTouchEvents[0] = touchEventsThisFrame;
  31. findTrackedTouches();
  32. }
  33. private void findTrackedTouches() {
  34. trackedTouchEvents = new List<TouchEvent>();
  35. foreach (TouchEvent te in detectedTouchEvents[0]) {
  36. bool tracked = true;
  37. for (int i = 1; i < framesUntilTracked; i++) {
  38. if (!hasSimilarTouchEventInFrame(te, i)) tracked = false;
  39. }
  40. if (tracked)
  41. {
  42. touchImage.setTouchAt(te.Position.IntX, te.Position.IntY, TouchImageState.touchTracked);
  43. trackedTouchEvents.Add(te);
  44. //Console.WriteLine("touch tracked at x:" + te.getX() + " y:" + te.getY() + " [floodValue:" + te.getFloodValue() + "]");
  45. }
  46. }
  47. }
  48. private bool hasSimilarTouchEventInFrame(TouchEvent touchEvent, int frame) {
  49. foreach (TouchEvent te in detectedTouchEvents[frame]) {
  50. if (touchEvent.isSimilarTo(te))
  51. return true;
  52. }
  53. return false;
  54. }
  55. }
  56. }