TouchEventVisualizer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using BBIWARG.Input.InputHandling;
  2. using BBIWARG.Recognition.TouchRecognition;
  3. using BBIWARG.Utility;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.Linq;
  9. namespace BBIWARG.Output.DebugOutput
  10. {
  11. /// <summary>
  12. /// Provides an image showing touch events.
  13. /// </summary>
  14. internal class TouchEventVisualizer
  15. {
  16. /// <summary>
  17. /// all touch events starting at the last TouchDown, indexed by a unique id
  18. /// </summary>
  19. private Dictionary<int, List<Vector2D>> activeTouches;
  20. /// <summary>
  21. /// relative times of last frame updates which are stored for a limited time, indexed by a unique id
  22. /// </summary>
  23. private Dictionary<int, long> lastUpdates;
  24. /// <summary>
  25. /// the next free id, used as a unique id for the dictionaries
  26. /// </summary>
  27. private int nextFreeID;
  28. /// <summary>
  29. /// old touch events which are stored for a limited time, indexed by a unique id
  30. /// </summary>
  31. private Dictionary<int, List<Vector2D>> oldTouches;
  32. /// <summary>
  33. /// used to prevent running <see cref="handleNewFrameData"/> and <see cref="getOutputImage"/> simultaneously from different threads
  34. /// </summary>
  35. private Object sync;
  36. /// <summary>
  37. /// used to store the relative time at which the touch events occurred
  38. /// </summary>
  39. private Stopwatch timer;
  40. /// <summary>
  41. /// Creates a TouchEventVisualizer.
  42. /// </summary>
  43. public TouchEventVisualizer()
  44. {
  45. sync = new object();
  46. reset();
  47. }
  48. /// <summary>
  49. /// Returns an output image showing the touch events.
  50. /// </summary>
  51. /// <param name="imageSize">output image size</param>
  52. /// <param name="numRows">number of rows in the palm grid</param>
  53. /// <param name="numColumns">number columns in the palm grid</param>
  54. /// <returns>image showing touch events</returns>
  55. public OutputImage getOutputImage(ImageSize imageSize, int numRows, int numColumns, int sliderPos, int sliderMax, int sliderCurr)
  56. {
  57. lock (sync)
  58. {
  59. long currentTime = timer.ElapsedMilliseconds;
  60. removeOldPositions(currentTime - Parameters.TouchEventVisualizerFadeOutTime);
  61. OutputImage outputImage = new OutputImage(imageSize);
  62. int imageWidth = imageSize.Width;
  63. int imageHeight = imageSize.Height;
  64. // border
  65. outputImage.drawBorder(Parameters.TouchEventVisualizerGridColor);
  66. // draw grid
  67. int widthPerColumn = imageWidth / numColumns;
  68. int heightPerRow = imageHeight / numRows;
  69. // find active blocks
  70. List<List<bool>> activeBlocks = getActiveBlocks(numRows, numColumns, sliderPos, sliderMax, sliderCurr);
  71. if (numRows * numColumns > 1)
  72. {
  73. for (int i = 0; i < activeTouches.Values.Count; i++)
  74. {
  75. List<Vector2D> positions = activeTouches.Values.ElementAt(i);
  76. Vector2D lastPosition = positions.Last();
  77. int activeRow = (int)Math.Min(lastPosition.Y * numRows, numRows - 1);
  78. int activeCol = (int)Math.Min(lastPosition.X * activeBlocks.ElementAt(activeRow).Count,
  79. activeBlocks.ElementAt(activeRow).Count - 1);
  80. if (activeRow != sliderPos)
  81. activeBlocks.ElementAt(activeRow)[activeCol] = true;
  82. if (activeRow == sliderPos)
  83. {
  84. if (Parameters.PalmSliderLastTouched != -1)
  85. {
  86. int temp = Parameters.PalmSliderCurr + (activeCol - Parameters.PalmSliderLastTouched);
  87. if (temp < 0)
  88. Parameters.PalmSliderCurr = 0;
  89. if (temp > Parameters.PalmSliderMax)
  90. Parameters.PalmSliderCurr = Parameters.PalmSliderMax;
  91. else
  92. Parameters.PalmSliderCurr = temp;
  93. Parameters.PalmSliderLastTouched = activeCol;
  94. }
  95. else if (Parameters.PalmSliderLastTouched == -1)
  96. {
  97. Parameters.PalmSliderLastTouched = activeCol;
  98. }
  99. }
  100. else
  101. {
  102. Parameters.PalmSliderLastTouched = -1;
  103. }
  104. }
  105. if (activeTouches.Values.Count < 1)
  106. {
  107. Parameters.PalmSliderLastTouched = -1;
  108. }
  109. }
  110. if(sliderPos > -1 && sliderPos < numRows)
  111. {
  112. for (int i = 0; i < Parameters.PalmSliderCurr; i++)
  113. {
  114. activeBlocks.ElementAt(sliderPos)[i] = true;
  115. }
  116. }
  117. Parameters.ActiveTouches = activeBlocks;
  118. // draw blocks
  119. int index = 1;
  120. for (int row = 0; row < numRows; row++)
  121. {
  122. for (int col = 0; col < activeBlocks.ElementAt(row).Count; col++)
  123. {
  124. if (activeBlocks.ElementAt(row)[col])
  125. outputImage.fillRectangle(new Rectangle(col * imageWidth/activeBlocks.ElementAt(row).Count,
  126. row * heightPerRow, imageWidth / activeBlocks.ElementAt(row).Count, heightPerRow),
  127. Parameters.TouchEventVisualizerActiveBlockColor);
  128. if(row != sliderPos)
  129. {
  130. int x = (int)((col + 0.5f) * imageWidth / activeBlocks.ElementAt(row).Count) - 5;
  131. int y = (int)((row + 0.5f) * heightPerRow) + 5;
  132. outputImage.drawText(new Point(x, y), index.ToString(), Parameters.TouchEventVisualizerTextColor);
  133. index++;
  134. }
  135. }
  136. }
  137. // draw grid
  138. for(int i = 0; i < activeBlocks.Count; i++)
  139. {
  140. for(int j = 0; j < activeBlocks.ElementAt(i).Count; j++)
  141. {
  142. outputImage.drawLineSegment(new LineSegment2D(
  143. new Vector2D(j * imageWidth/activeBlocks.ElementAt(i).Count, i * heightPerRow),
  144. new Vector2D(j * imageWidth / activeBlocks.ElementAt(i).Count, (i + 1) * heightPerRow -1)),
  145. Parameters.TouchEventVisualizerGridColor);
  146. }
  147. }
  148. //for (int i = 0; i <= numColumns; i++)
  149. // outputImage.drawLineSegment(new LineSegment2D(new Vector2D(i * widthPerColumn, 0), new Vector2D(i * widthPerColumn, imageHeight - 1)), Parameters.TouchEventVisualizerGridColor);
  150. for (int i = 0; i <= numRows; i++)
  151. outputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, i * heightPerRow), new Vector2D(imageWidth - 1, i * heightPerRow)), Parameters.TouchEventVisualizerGridColor);
  152. // draw active touches
  153. foreach (List<Vector2D> positions in activeTouches.Values)
  154. outputImage.drawTouchGesture(positions, imageSize.MaxPixel);
  155. // draw old touches (fade out)
  156. foreach (int id in oldTouches.Keys)
  157. {
  158. List<Vector2D> positions = oldTouches[id];
  159. long lastUpdate = lastUpdates[id];
  160. float opacity = 1 - ((currentTime - lastUpdate) / (float)Parameters.TouchEventVisualizerFadeOutTime);
  161. outputImage.drawTouchGesture(positions, imageSize.MaxPixel, opacity);
  162. }
  163. return outputImage;
  164. }
  165. }
  166. private List<List<bool>> getActiveBlocks(int numRows, int numColumns, int sliderPos, int sliderMax, int sliderCurr)
  167. {
  168. List<List<bool>> res = new List<List<bool>>();
  169. for(int i = 0; i < numRows; i++)
  170. {
  171. List<bool> temp = new List<bool>();
  172. if (i == sliderPos)
  173. {
  174. for (int j = 0; j < sliderMax; j++) temp.Add(false);
  175. }
  176. else
  177. {
  178. for (int j = 0; j < numColumns; j++) temp.Add(false);
  179. }
  180. res.Add(temp);
  181. }
  182. return res;
  183. }
  184. /// <summary>
  185. /// Handles the event that a new frame is finished processing by updating the touch events.
  186. /// </summary>
  187. /// <param name="sender">event sender</param>
  188. /// <param name="e">event arguments</param>
  189. public void handleNewFrameData(object sender, NewProcessedFrameEventArgs e)
  190. {
  191. FrameData frameData = e.FrameData;
  192. lock (frameData) lock (sync)
  193. {
  194. if (frameData.ResetFlag)
  195. reset();
  196. foreach (TouchEvent te in frameData.TouchEvents)
  197. {
  198. switch (te.Type)
  199. {
  200. case TouchEventType.Down:
  201. activeTouches.Add(te.Touch.TrackID, new List<Vector2D>());
  202. activeTouches[te.Touch.TrackID].Add(te.Touch.RelativePosition);
  203. break;
  204. case TouchEventType.Move:
  205. activeTouches[te.Touch.TrackID].Add(te.Touch.RelativePosition);
  206. break;
  207. case TouchEventType.Up:
  208. activeTouches[te.Touch.TrackID].Add(te.Touch.RelativePosition);
  209. oldTouches.Add(nextFreeID, activeTouches[te.Touch.TrackID]);
  210. lastUpdates.Add(nextFreeID, timer.ElapsedMilliseconds);
  211. activeTouches.Remove(te.Touch.TrackID);
  212. nextFreeID++;
  213. break;
  214. }
  215. }
  216. }
  217. }
  218. /// <summary>
  219. /// Resets the touch events and the timer.
  220. /// </summary>
  221. public void reset()
  222. {
  223. timer = new Stopwatch();
  224. timer.Start();
  225. nextFreeID = 1;
  226. activeTouches = new Dictionary<int, List<Vector2D>>();
  227. oldTouches = new Dictionary<int, List<Vector2D>>();
  228. lastUpdates = new Dictionary<int, long>();
  229. }
  230. /// <summary>
  231. /// Removes old touch events and update times.
  232. /// </summary>
  233. /// <param name="breakTime">every touch event which occurred before breakTime is removed</param>
  234. private void removeOldPositions(long breakTime)
  235. {
  236. List<int> ids = new List<int>(lastUpdates.Keys);
  237. for (int i = ids.Count - 1; i >= 0; i--)
  238. {
  239. int id = ids[i];
  240. if (breakTime > lastUpdates[id])
  241. {
  242. oldTouches.Remove(id);
  243. lastUpdates.Remove(id);
  244. }
  245. }
  246. }
  247. }
  248. }