123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using bbiwarg.Recognition.TouchRecognition;
- using bbiwarg.Utility;
- namespace bbiwarg.Graphics
- {
- class TouchEventVisualizer
- {
- public OutputImage OutputImage { get; private set; }
- private int width, height;
- private Stopwatch timer;
- private Dictionary<int, List<Vector2D>> currentPositions;
- private Dictionary<long, List<Vector2D>> oldPositions;
- public TouchEventVisualizer(int width, int height)
- {
- this.width = width;
- this.height = height;
- this.timer = new Stopwatch();
- this.currentPositions = new Dictionary<int, List<Vector2D>>();
- this.oldPositions = new Dictionary<long, List<Vector2D>>();
- this.timer.Start();
- }
- public void touchDown(object sender, PalmTouchEventArgs ptea)
- {
- List<Vector2D> posList = new List<Vector2D>();
- posList.Add(ptea.Position);
- currentPositions.Add(ptea.TrackID, posList);
- }
- public void touchMove(object sender, PalmTouchEventArgs ptea)
- {
- currentPositions[ptea.TrackID].Add(ptea.Position);
- }
- public void touchUp(object sender, PalmTouchEventArgs ptea)
- {
- currentPositions[ptea.TrackID].Add(ptea.Position);
- List<Vector2D> posList = currentPositions[ptea.TrackID];
- oldPositions.Add(timer.ElapsedMilliseconds, posList);
- currentPositions.Remove(ptea.TrackID);
- }
- public void updateImage()
- {
-
- long currentTime = timer.ElapsedMilliseconds;
- List<long> removeKeys = new List<long>();
- foreach (long lastUpdateTime in oldPositions.Keys)
- {
- if (currentTime - lastUpdateTime > Constants.TouchEventVisualizerFadeOutTime)
- removeKeys.Add(lastUpdateTime);
- }
- foreach (long lastUpdateTime in removeKeys)
- oldPositions.Remove(lastUpdateTime);
- OutputImage = new OutputImage(width, height);
-
- OutputImage.drawRectangle(0, 0, width - 1, height - 1, Constants.TouchEventVisualizerGridColor);
-
- int numRows = Constants.PalmGridNumRows;
- int numColumns = Constants.PalmGridNumColumns;
- int widthPerColumn = width / numColumns;
- int heightPerRow = height / numRows;
- for (int i = 0; i <= numColumns; i++)
- {
- OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(i * widthPerColumn, 0), new Vector2D(i * widthPerColumn, height - 1)), Constants.TouchEventVisualizerGridColor);
- }
- for (int i = 0; i <= numRows; i++)
- {
- OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, i * heightPerRow), new Vector2D(width - 1, i * heightPerRow)), Constants.TouchEventVisualizerGridColor);
- }
- foreach (int id in currentPositions.Keys)
- drawTouchGesture(currentPositions[id], 1);
- foreach (long lastUpdate in oldPositions.Keys)
- drawTouchGesture(oldPositions[lastUpdate], 1 - ((currentTime - lastUpdate) / (float)Constants.TouchEventVisualizerFadeOutTime));
- }
- public void drawTouchGesture(List<Vector2D> positions, float opacity)
- {
- Vector2D maxPixel = new Vector2D(width - 1, height - 1);
- int numPositions = positions.Count;
- Color lineColor = Constants.TouchEventVisualizerLineColor;
- Color pointColor = Constants.TouchEventVisualizerPointColor;
- Color lineColorFaded = Color.FromArgb((int)(opacity * lineColor.R), (int)(opacity * lineColor.G), (int)(opacity * lineColor.B));
- Color pointColorFaded = Color.FromArgb((int)(opacity * pointColor.R), (int)(opacity * pointColor.G), (int)(opacity * pointColor.B));
- for (int i = 1; i < numPositions; i++)
- {
- OutputImage.drawLineSegment(new LineSegment2D(positions[i - 1].scale(maxPixel), positions[i].scale(maxPixel)), lineColorFaded);
- }
- Vector2D lastPos = positions[numPositions - 1].scale(maxPixel);
- OutputImage.fillCircle(lastPos.IntX, lastPos.IntY, 3, pointColorFaded);
- }
- public void reset()
- {
- OutputImage = new OutputImage(width, height);
- currentPositions.Clear();
- }
- }
- }
|