server.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from input_server import InputServerHandler
  2. from tuio_server import TuioServer2D
  3. from window import FullscreenWindow
  4. from point import TouchPoint
  5. from screen import pixel_coords
  6. class GestureServer(InputServerHandler):
  7. """
  8. Multi-touch gesture server. This uses a TUIO server to receive basic touch
  9. events, which are translated to gestures using gesture trackers. Trackers
  10. are assigned to a Window object, and gesture handlers are bound to a
  11. tracker.
  12. """
  13. def __init__(self):
  14. # List of all connected windows
  15. self.windows = []
  16. # Map of point sid to TouchPoint object
  17. self.points = {}
  18. # Create TUIO server, to be started later
  19. self.tuio_server = TuioServer2D(self)
  20. def add_window(self, window):
  21. self.windows.append(window)
  22. def remove_window(self, window):
  23. self.windows.remove(window)
  24. # TODO: Remove window from touch points
  25. def on_point_down(self, sid, x, y):
  26. if sid in self.points:
  27. raise ValueError('Point with sid %d already exists.' % sid)
  28. # Translate to pixel coordinates
  29. px, py = pixel_coords(x, y)
  30. # Create a new touch point
  31. self.points[sid] = point = TouchPoint(px, py, sid)
  32. # Save the windows containing the point in a dictionary, and update
  33. # their trackers
  34. for window in self.windows:
  35. if window.contains(point):
  36. point.add_window(window)
  37. window.update_trackers('down', point)
  38. def on_point_move(self, sid, x, y):
  39. if sid not in self.points:
  40. raise KeyError('No point with sid %d exists.' % sid)
  41. # Translate to pixel coordinates
  42. px, py = pixel_coords(x, y)
  43. # Update point position and corresponding window trackers
  44. point = self.points[sid]
  45. point.set_position(px, py)
  46. point.update_window_trackers('move')
  47. def on_point_up(self, sid):
  48. if sid not in self.points:
  49. raise KeyError('No point with sid %d exists.' % sid)
  50. # Clear the list of windows containing the point, and update their
  51. # trackers
  52. self.points[sid].update_window_trackers('up')
  53. del self.points[sid]
  54. def start(self):
  55. # Assert that at least one window exists, by adding a fullscreen window
  56. # if none have been added yet
  57. if not self.windows:
  58. self.add_window(FullscreenWindow())
  59. self.tuio_server.start()
  60. def stop(self):
  61. self.tuio_server.stop()
  62. def run(self):
  63. self.tuio_server.run()