TuioCommunicator.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using BBIWARG.Input.InputHandling;
  2. using BBIWARG.Recognition.PalmRecognition;
  3. using BBIWARG.Recognition.TouchRecognition;
  4. using BBIWARG.Utility;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using TUIO;
  12. namespace BBIWARG.TUIO
  13. {
  14. /// <summary>
  15. /// TuioCommunicator sends generated touch events and palm grid coordinates to tuio clients using a <see cref="TuioServer"/>.
  16. /// </summary>
  17. internal class TuioCommunicator
  18. {
  19. /// <summary>
  20. /// the tuio server
  21. /// </summary>
  22. private TuioServer server;
  23. /// <summary>
  24. /// dictionary of lists of tuio cursors indexed by touch id
  25. /// </summary>
  26. private Dictionary<int, TuioCursor> tcursors;
  27. /// <summary>
  28. /// dictionary of lists of tuio objects indexed by palm id
  29. /// </summary>
  30. private Dictionary<int, List<TuioObject>> tobjects;
  31. /// <summary>
  32. /// Constructs a TuioCommunicator.
  33. /// </summary>
  34. /// <param name="host">server ip</param>
  35. /// <param name="port">server port</param>
  36. public TuioCommunicator(string host, int port)
  37. {
  38. server = new TuioServer(host, port);
  39. tcursors = new Dictionary<int, TuioCursor>();
  40. tobjects = new Dictionary<int, List<TuioObject>>();
  41. }
  42. /// <summary>
  43. /// Tries to parse an ip address string.
  44. /// </summary>
  45. /// <param name="ipIn">the ip address string to parse</param>
  46. /// <param name="ipOut">out parameter for the ip address in *.*.*.* format if ipIn is valid and null otherwise</param>
  47. /// <returns>true iff ipIn is a valid ip address</returns>
  48. public static bool tryParseIPAddress(String ipIn, out String ipOut)
  49. {
  50. IPAddress ipAddress;
  51. bool result = IPAddress.TryParse(ipIn, out ipAddress) && ipAddress.AddressFamily == AddressFamily.InterNetwork;
  52. if (result)
  53. ipOut = ipAddress.ToString();
  54. else
  55. ipOut = null;
  56. return result;
  57. }
  58. /// <summary>
  59. /// Tries to parse a port string.
  60. /// </summary>
  61. /// <param name="portIn">the port string to parse</param>
  62. /// <param name="portOut">out parameter for the port if portIn is valid and undefined otherwise</param>
  63. /// <returns>true iff portIn is a valid port</returns>
  64. public static bool tryParsePort(String portIn, out Int16 portOut)
  65. {
  66. return Int16.TryParse(portIn, out portOut);
  67. }
  68. /// <summary>
  69. /// Closes the server.
  70. /// </summary>
  71. public void close()
  72. {
  73. server.close();
  74. }
  75. /// <summary>
  76. /// Handles the event that a new frame is finished processing by updating the cursors and objects and sending them.
  77. /// </summary>
  78. /// <param name="sender">event sender</param>
  79. /// <param name="e">event arguments</param>
  80. public void handleNewFrameData(object sender, NewProcessedFrameEventArgs e)
  81. {
  82. server.initFrame();
  83. FrameData frameData = e.FrameData;
  84. lock (frameData)
  85. {
  86. if (frameData.ResetFlag)
  87. reset();
  88. foreach (TouchEvent te in frameData.TouchEvents)
  89. {
  90. switch (te.Type)
  91. {
  92. case TouchEventType.Down:
  93. TuioCursor tcur = server.addTuioCursor(te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
  94. tcursors.Add(te.Touch.TrackID, tcur);
  95. break;
  96. case TouchEventType.Move:
  97. server.updateTuioCursor(tcursors[te.Touch.TrackID], te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
  98. break;
  99. case TouchEventType.Up:
  100. server.removeTuioCursor(tcursors[te.Touch.TrackID]);
  101. tcursors.Remove(te.Touch.TrackID);
  102. break;
  103. }
  104. }
  105. }
  106. server.commitFrame();
  107. }
  108. /// <summary>
  109. /// Resets the server by removing all cursors and objects.
  110. /// </summary>
  111. public void reset()
  112. {
  113. foreach (int id in tcursors.Keys)
  114. server.removeTuioCursor(tcursors[id]);
  115. tcursors.Clear();
  116. foreach (int id in tobjects.Keys)
  117. foreach (TuioObject tobj in tobjects[id])
  118. server.removeTuioObject(tobj);
  119. tobjects.Clear();
  120. }
  121. }
  122. }