123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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, int> idTranslations;
- private Dictionary<int, List<Vector2D>> positions;
- private Dictionary<int, long> lastUpdates;
- private int nextFreeID;
- private int lastUpdated;
- private bool active;
- public TouchEventVisualizer(int width, int height)
- {
- this.width = width;
- this.height = height;
- reset();
- }
- public void reset()
- {
- timer = new Stopwatch();
- timer.Start();
- nextFreeID = 1;
- lastUpdated = 0;
- active = false;
- idTranslations = new Dictionary<int, int>();
- positions = new Dictionary<int, List<Vector2D>>();
- lastUpdates = new Dictionary<int, long>();
- }
- public void touchDown(object sender, TouchEventArgs tea)
- {
- int id = nextFreeID;
- nextFreeID++;
- idTranslations.Add(tea.TrackID, id);
- positions.Add(id, new List<Vector2D>());
- positions[id].Add(tea.RelativePosition);
- lastUpdates.Add(id, timer.ElapsedMilliseconds);
- lastUpdated = id;
- active = true;
- }
- public void touchMove(object sender, TouchEventArgs tea)
- {
- int id = idTranslations[tea.TrackID];
- positions[id].Add(tea.RelativePosition);
- lastUpdates[id] = timer.ElapsedMilliseconds;
- lastUpdated = id;
- }
- public void touchUp(object sender, TouchEventArgs tea)
- {
- int id = idTranslations[tea.TrackID];
- positions[id].Add(tea.RelativePosition);
- lastUpdates[id] = timer.ElapsedMilliseconds;
- lastUpdated = id;
- idTranslations.Remove(tea.TrackID);
- }
- public void updateImage()
- {
- //remove old positions
- long currentTime = timer.ElapsedMilliseconds;
- List<int> ids = new List<int>(lastUpdates.Keys);
- for (int i = ids.Count - 1; i >= 0; i--) {
- int id = ids[i];
- if (currentTime - lastUpdates[id] > Parameters.TouchEventVisualizerFadeOutTime) {
- positions.Remove(id);
- lastUpdates.Remove(id);
- if (id == lastUpdated)
- active = false;
- }
- }
- OutputImage = new OutputImage(width, height);
- //border
- OutputImage.drawRectangle(0, 0, width - 1, height - 1, Parameters.TouchEventVisualizerGridColor);
- //draw grid
- int numRows = Parameters.PalmGridNumRows;
- int numColumns = Parameters.PalmGridNumColumns;
- int widthPerColumn = width / numColumns;
- int heightPerRow = height / numRows;
- int activeRow = -1;
- int activeCol = -1;
- if (active && numRows*numColumns > 1) {
- Vector2D position = positions[lastUpdated][positions[lastUpdated].Count-1];
- activeRow = (int)Math.Floor(position.Y * Parameters.PalmGridNumRows);
- activeCol = (int)Math.Floor(position.X * Parameters.PalmGridNumColumns);
- }
- for (int i = 0; i <= numColumns; i++)
- {
- OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(i * widthPerColumn, 0), new Vector2D(i * widthPerColumn, height - 1)), Parameters.TouchEventVisualizerGridColor);
- }
- for (int i = 0; i <= numRows; i++)
- {
- OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, i * heightPerRow), new Vector2D(width - 1, i * heightPerRow)), Parameters.TouchEventVisualizerGridColor);
- }
-
- for (int row = 0; row < numRows; row++) {
- for (int col = 0; col < numColumns; col++) {
- if(row == activeRow && col == activeCol)
- OutputImage.fillRectangle(col*widthPerColumn, row*heightPerRow, widthPerColumn, heightPerRow, Parameters.TouchEventVisualizerActiveBlockColor);
-
- int x = (int)((col+0.5f)*widthPerColumn)-5;
- int y = (int)((row+0.5f)*heightPerRow)+5;
- OutputImage.drawText(x,y,(1+row*numColumns+col).ToString(),Parameters.TouchEventVisualizerTextColor);
- }
- }
- foreach (int id in positions.Keys)
- drawTouchGesture(positions[id], 1 - ((currentTime - lastUpdates[id]) / (float)Parameters.TouchEventVisualizerFadeOutTime));
- }
- public void drawTouchGesture(List<Vector2D> positions, float opacity)
- {
- Vector2D maxPixel = new Vector2D(width - 1, height - 1);
- int numPositions = positions.Count;
- Color lineColor = Parameters.TouchEventVisualizerLineColor;
- Color pointColor = Parameters.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);
- }
- }
- }
|