1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using bbiwarg.Images;
- namespace bbiwarg.Detectors.Touch
- {
- class TouchTracker
- {
- private TouchImage touchImage;
- private List<TouchEvent>[] detectedTouchEvents;
- private List<TouchEvent> trackedTouchEvents;
- private int framesUntilTracked;
- public TouchTracker() {
- framesUntilTracked = 2;
- detectedTouchEvents = new List<TouchEvent>[framesUntilTracked];
- for (int i = 0; i < framesUntilTracked; i++) {
- detectedTouchEvents[i] = new List<TouchEvent>();
- }
- }
- public List<TouchEvent> getTouchEvents() {
- return trackedTouchEvents;
- }
- public void setDetectedTouchEventsThisFrame(List<TouchEvent> touchEventsThisFrame, TouchImage touchImage) {
- this.touchImage = touchImage;
- for (int i = (framesUntilTracked-1); i > 0; i--) {
- detectedTouchEvents[i] = detectedTouchEvents[i - 1];
- }
- detectedTouchEvents[0] = touchEventsThisFrame;
- findTrackedTouches();
- }
- private void findTrackedTouches() {
- trackedTouchEvents = new List<TouchEvent>();
- foreach (TouchEvent te in detectedTouchEvents[0]) {
- bool tracked = true;
- for (int i = 1; i < framesUntilTracked; i++) {
- if (!hasSimilarTouchEventInFrame(te, i)) tracked = false;
- }
- if (tracked)
- {
- touchImage.setTouchAt(te.Position.IntX, te.Position.IntY, TouchImageState.touchTracked);
- trackedTouchEvents.Add(te);
- //Console.WriteLine("touch tracked at x:" + te.getX() + " y:" + te.getY() + " [floodValue:" + te.getFloodValue() + "]");
- }
- }
- }
- private bool hasSimilarTouchEventInFrame(TouchEvent touchEvent, int frame) {
- foreach (TouchEvent te in detectedTouchEvents[frame]) {
- if (touchEvent.isSimilarTo(te))
- return true;
- }
- return false;
- }
- }
- }
|