TuioCommunicator.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using bbiwarg.Recognition.TouchRecognition;
  9. using bbiwarg.Utility;
  10. using bbiwarg.Input.InputHandling;
  11. using TUIO;
  12. namespace bbiwarg.TUIO
  13. {
  14. class TuioCommunicator
  15. {
  16. private TuioServer server;
  17. private Dictionary<int, TuioCursor> cursors;
  18. public TuioCommunicator(string host, int port)
  19. {
  20. server = new TuioServer(host, port);
  21. cursors = new Dictionary<int, TuioCursor>();
  22. }
  23. public void handleNewFrameData(object sender, NewProcessedFrameEventArgs e)
  24. {
  25. server.initFrame();
  26. FrameData frameData = e.FrameData;
  27. lock (frameData)
  28. {
  29. if (frameData.ResetFlag)
  30. reset();
  31. foreach (TouchEvent te in frameData.TouchEvents)
  32. {
  33. switch (te.Type)
  34. {
  35. case TouchEventType.Down:
  36. TuioCursor cursor = server.addTuioCursor(te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
  37. cursors.Add(te.Touch.TrackID, cursor);
  38. break;
  39. case TouchEventType.Move:
  40. server.updateTuioCursor(cursors[te.Touch.TrackID], te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
  41. break;
  42. case TouchEventType.Up:
  43. server.removeTuioCursor(cursors[te.Touch.TrackID]);
  44. cursors.Remove(te.Touch.TrackID);
  45. break;
  46. }
  47. }
  48. }
  49. server.commitFrame();
  50. }
  51. public void close()
  52. {
  53. server.close();
  54. }
  55. public void reset()
  56. {
  57. foreach (int id in cursors.Keys)
  58. {
  59. server.removeTuioCursor(cursors[id]);
  60. }
  61. cursors.Clear();
  62. }
  63. public static bool tryParseIPAddress(String ipIn, out String ipOut)
  64. {
  65. IPAddress ipAddress;
  66. bool result = (IPAddress.TryParse(ipIn, out ipAddress) && ipAddress.AddressFamily == AddressFamily.InterNetwork);
  67. if (result)
  68. ipOut = ipAddress.ToString();
  69. else
  70. ipOut = null;
  71. return result;
  72. }
  73. public static bool tryParsePort(String portIn, out Int16 portOut)
  74. {
  75. return Int16.TryParse(portIn, out portOut);
  76. }
  77. }
  78. }