TuioCommunicator.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(InputHandler sender, EventArgs e)
  24. {
  25. server.initFrame();
  26. FrameData frameData = sender.FrameData;
  27. lock (frameData) {
  28. foreach (TouchEventArgs te in frameData.TouchEvents) {
  29. switch (te.Type) {
  30. case TouchEventType.Down:
  31. TuioCursor cursor = server.addTuioCursor(te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
  32. cursors.Add(te.TrackID, cursor);
  33. break;
  34. case TouchEventType.Move:
  35. server.updateTuioCursor(cursors[te.TrackID], te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
  36. break;
  37. case TouchEventType.Up:
  38. server.removeTuioCursor(cursors[te.TrackID]);
  39. cursors.Remove(te.TrackID);
  40. break;
  41. }
  42. }
  43. }
  44. server.commitFrame();
  45. }
  46. public void close()
  47. {
  48. server.close();
  49. }
  50. public void reset()
  51. {
  52. foreach (int id in cursors.Keys)
  53. {
  54. server.removeTuioCursor(cursors[id]);
  55. }
  56. cursors.Clear();
  57. }
  58. public static bool tryParseIPAddress(String ipString, out IPAddress ipAddress) {
  59. return (IPAddress.TryParse(ipString, out ipAddress) && ipAddress.AddressFamily == AddressFamily.InterNetwork);
  60. }
  61. public static bool tryParsePort(String portString, out Int16 port) {
  62. return Int16.TryParse(portString, out port);
  63. }
  64. }
  65. }