| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- from event_server import EventServerHandler
- from tuio_server import TuioServer2D
- from window import FullscreenWindow
- from point import TouchPoint
- class GestureServer(EventServerHandler):
- """
- Multi-touch gesture server. This uses a TUIO server to receive basic touch
- events, which are translated to gestures using gesture trackers. Trackers
- are assigned to a Window object, and gesture handlers are bound to a
- tracker.
- """
- def __init__(self):
- # List of all connected windows
- self.windows = []
- # Map of point sid to TouchPoint object
- self.points = {}
- # Create TUIO server, to be started later
- self.tuio_server = TuioServer2D(self)
- def add_window(self, window):
- self.windows.append(window)
- def remove_window(self, window):
- self.windows.remove(window)
- # TODO: Remove window from touch points
- def on_point_down(self, sid, x, y):
- if sid in self.points:
- raise ValueError('Point with sid %d already exists.' % sid)
- # Create a new touch point
- self.points[sid] = point = TouchPoint(x, y, sid)
- # Save the windows containing the point in a dictionary, and update
- # their trackers
- for window in self.windows:
- if window.contains(point):
- point.add_window(window)
- window.update_trackers('down', point)
- def on_point_move(self, sid, x, y):
- if sid not in self.points:
- raise KeyError('No point with sid %d exists.' % sid)
- # Update point position and corresponding window trackers
- point = self.points[sid]
- point.set_position(x, y)
- point.update_window_trackers('move')
- def on_point_up(self, sid):
- if sid not in self.points:
- raise KeyError('No point with sid %d exists.' % sid)
- # Clear the list of windows containing the point, and update their
- # trackers
- self.points[sid].update_window_trackers('up')
- del self.points[sid]
- def start(self):
- # Assert that at least one window exists, by adding a fullscreen window
- # if none have been added yet
- if not self.windows:
- self.add_window(FullscreenWindow())
- self.tuio_server.start()
- def stop(self):
- self.tuio_server.stop()
- def run(self):
- self.tuio_server.run()
|