Commit 3036a05c authored by UVA Multi-touch's avatar UVA Multi-touch

Merged 'event driver' and 'event server' into 'event driver'.

parent 502fe8e6
from logger import Logger from logger import Logger
from tracker import GestureTracker, Gesture from tracker import GestureTracker, Gesture
from event_server import EventServer from drivers import create_driver
from areas import * from areas import *
from tuio import TuioDriver from tuio import TuioDriver
def select_driver(event_server): def create_driver(root_area=None):
""" """
Create an object of a driver implementation, based on hardware support. Create an object of an event driver implementation, based on hardware
Currently, only a TUIO driver implementation exists. In the future, this support. Currently, only a TUIO driver implementation exists. In the
method should select one driver from a set of implementations. future, this method should select one driver from a set of implementations.
""" """
return TuioDriver(event_server) return TuioDriver(root_area)
...@@ -11,8 +11,8 @@ from ..screen import pixel_coords ...@@ -11,8 +11,8 @@ from ..screen import pixel_coords
class TuioDriver(EventDriver): class TuioDriver(EventDriver):
tuio_address = 'localhost', 3333 tuio_address = 'localhost', 3333
def __init__(self, event_server): def __init__(self, root_area=None):
super(TuioDriver, self).__init__(event_server) super(TuioDriver, self).__init__(root_area)
# OSC server that listens to incoming TUIO events # OSC server that listens to incoming TUIO events
self.server = OSCServer(self.tuio_address) self.server = OSCServer(self.tuio_address)
...@@ -53,8 +53,7 @@ class TuioDriver(EventDriver): ...@@ -53,8 +53,7 @@ class TuioDriver(EventDriver):
for sid in released: for sid in released:
point = self.points[sid] point = self.points[sid]
del self.points[sid] del self.points[sid]
self.event_server.delegate_event(PointUpEvent(point)) self.delegate_event(PointUpEvent(point))
#self.event_server.on_point_up(sid)
elif msg_type == 'set': elif msg_type == 'set':
sid, x, y = data[1:4] sid, x, y = data[1:4]
...@@ -71,23 +70,21 @@ class TuioDriver(EventDriver): ...@@ -71,23 +70,21 @@ class TuioDriver(EventDriver):
% (sid, x, y, px, py)) % (sid, x, y, px, py))
point = self.points[sid] point = self.points[sid]
point.set_position(px, py) point.set_position(px, py)
self.event_server.delegate_event(PointMoveEvent(point)) self.delegate_event(PointMoveEvent(point))
#self.event_server.on_point_move(sid, px, py)
else: else:
self.debug('Down %d at (%.4f, %.4f), in pixels: (%d, %d).' self.debug('Down %d at (%.4f, %.4f), in pixels: (%d, %d).'
% (sid, x, y, px, py)) % (sid, x, y, px, py))
self.down.add(sid) self.down.add(sid)
self.points[sid] = point = TouchPoint(px, py) self.points[sid] = point = TouchPoint(px, py)
self.event_server.delegate_event(PointDownEvent(point)) self.delegate_event(PointDownEvent(point))
#self.event_server.on_point_down(sid, px, py)
def run(self): def start_loop(self):
self.server.handle_request()
def start(self):
self.info('Starting OSC server') self.info('Starting OSC server')
self.server.serve_forever() self.server.serve_forever()
def stop(self): def stop(self):
self.info('Stopping OSC server') self.info('Stopping OSC server')
self.server.close() self.server.close()
def run(self):
self.server.handle_request()
...@@ -3,16 +3,39 @@ from logger import Logger ...@@ -3,16 +3,39 @@ from logger import Logger
class EventDriver(Logger): class EventDriver(Logger):
""" """
Abstract factory class for drivers. A driver translates driver-specific The event driver receives driver-specific messages, which are delegated to
messages to a common set of events. The minimal set is {point_down, the root area in the form of common events such as 'piont_down' pr
point_move, point_up}. A driver implementation should define the methods 'point_up'.
'start' and 'stop', which starts/stops some event loop that triggers the
'delegate_event' method of a area.
""" """
def __init__(self, event_server): def __init__(self, root_area=None):
self.event_server = event_server self.root_area = root_area
def get_root_area(self):
return self.root_area
def set_root_area(self, area):
self.root_area = area
def delegate_event(self, event):
"""
Delegate an event that has been triggered by the event driver to the
area tree.
"""
if self.root_area.contains_event(event):
event.set_root_area(self.root_area)
self.root_area.delegate_event(event)
def start(self): def start(self):
"""
Start the event loop. A root area is needed to be able to delegate
events, so check if it exists first.
"""
if not self.root_area:
raise ValueError('Cannot start event server without root area.')
self.start_loop()
def start_loop(self):
""" """
Start the event loop. Start the event loop.
""" """
......
from logger import Logger
from drivers import select_driver
class EventServer(Logger):
"""
The event server uses an event driver to receive events, which are
delegated to a area tree (and eventually to gesture trackers).
"""
def __init__(self, root_area=None):
# Root area to which events are delegated
self.root_area = root_area
# Driver implementation that will be serving events
self.event_driver = select_driver(self)
def get_root_area(self):
return self.root_area
def set_root_area(self, area):
self.root_area = area
def delegate_event(self, event):
"""
Delegate an event that has been triggered by the event driver to the
area tree.
"""
if self.root_area.contains_event(event):
event.set_root_area(self.root_area)
self.root_area.delegate_event(event)
def start(self):
"""
Start the event loop. A root area is needed to be able to delegate
events, so check if it exists first.
"""
if not self.root_area:
raise ValueError('Cannot start event server without root area.')
self.event_driver.start()
def stop(self):
"""
Stop the event loop.
"""
self.event_driver.stop()
def run(self):
"""
Execute a single loop iteration of the event driver.
"""
self.event_driver.run()
from src.event_server import EventServer
from src.areas import FullscreenArea
from tests.parse_arguments import create_parser, parse_args from tests.parse_arguments import create_parser, parse_args
from src import FullscreenArea, create_driver
parse_args(create_parser()) parse_args(create_parser())
# Create server and fullscreen area # Create driver and fullscreen area
screen = FullscreenArea() screen = FullscreenArea()
server = EventServer(screen) driver = create_driver(screen)
# Bind handlers # Bind handlers
# Add tracker and handlers
screen.on_point_down(lambda g: 0) screen.on_point_down(lambda g: 0)
screen.on_point_move(lambda g: 0) screen.on_point_move(lambda g: 0)
screen.on_point_up(lambda g: 0) screen.on_point_up(lambda g: 0)
# Start listening to TUIO events # Start listening to TUIO events
try: try:
server.start() driver.start()
except KeyboardInterrupt: except KeyboardInterrupt:
server.stop() driver.stop()
...@@ -162,8 +162,8 @@ if __name__ == '__main__': ...@@ -162,8 +162,8 @@ if __name__ == '__main__':
create_context_window(640, 460, on_show) create_context_window(640, 460, on_show)
# Run multi-touch gesture server in separate thread # Run multi-touch gesture server in separate thread
server = mt.EventServer(root) driver = mt.create_driver(root)
mt_thread = Thread(target=server.start) mt_thread = Thread(target=driver.start)
mt_thread.daemon = True mt_thread.daemon = True
mt_thread.start() mt_thread.start()
......
...@@ -4,9 +4,8 @@ import pygame ...@@ -4,9 +4,8 @@ import pygame
from threading import Thread from threading import Thread
from math import degrees from math import degrees
from src.event_server import EventServer
from src.areas import FullscreenArea
from tests.parse_arguments import create_parser, parse_args from tests.parse_arguments import create_parser, parse_args
from src import FullscreenArea, create_driver
from src.screen import screen_size from src.screen import screen_size
# Parse arguments # Parse arguments
...@@ -147,7 +146,7 @@ def pinch(gesture): ...@@ -147,7 +146,7 @@ def pinch(gesture):
area = FullscreenArea() area = FullscreenArea()
server = EventServer(area) driver = create_driver(area)
area.on_rotate(rotate) area.on_rotate(rotate)
area.on_pinch(pinch) area.on_pinch(pinch)
...@@ -161,7 +160,7 @@ area.on_point_up(lambda g: points.remove(g.get_event().point)) ...@@ -161,7 +160,7 @@ area.on_point_up(lambda g: points.remove(g.get_event().point))
try: try:
# Start touch gesture server in separate thread # Start touch gesture server in separate thread
thread = Thread(target=server.start) thread = Thread(target=driver.start)
thread.daemon = True thread.daemon = True
thread.start() thread.start()
...@@ -175,4 +174,4 @@ try: ...@@ -175,4 +174,4 @@ try:
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally:
server.stop() driver.stop()
from src.event_server import EventServer
from src.areas import FullscreenArea
from tests.parse_arguments import create_parser, parse_args from tests.parse_arguments import create_parser, parse_args
from src import FullscreenArea, create_driver
parse_args(create_parser()) parse_args(create_parser())
# Create server and fullscreen area # Create driver and fullscreen area
screen = FullscreenArea() screen = FullscreenArea()
server = EventServer(screen) driver = create_driver(screen)
# Bind handlers # Bind handlers
screen.on_tap(lambda g: 0) screen.on_tap(lambda g: 0)
...@@ -15,6 +14,6 @@ screen.on_double_tap(lambda g: 0) ...@@ -15,6 +14,6 @@ screen.on_double_tap(lambda g: 0)
# Start listening to TUIO events # Start listening to TUIO events
try: try:
server.start() driver.start()
except KeyboardInterrupt: except KeyboardInterrupt:
server.stop() driver.stop()
from src.event_server import EventServer
from src.areas import FullscreenArea
from tests.parse_arguments import create_parser, parse_args from tests.parse_arguments import create_parser, parse_args
from src import FullscreenArea, create_driver
parse_args(create_parser()) parse_args(create_parser())
# Create server and fullscreen area # Create driver and fullscreen area
screen = FullscreenArea() screen = FullscreenArea()
server = EventServer(screen) driver = create_driver(screen)
# Bind handlers # Bind handlers
screen.on_rotate(lambda g: 0) screen.on_rotate(lambda g: 0)
...@@ -15,6 +14,6 @@ screen.on_drag(lambda g: 0) ...@@ -15,6 +14,6 @@ screen.on_drag(lambda g: 0)
# Start listening to TUIO events # Start listening to TUIO events
try: try:
server.start() driver.start()
except KeyboardInterrupt: except KeyboardInterrupt:
server.stop() driver.stop()
...@@ -2,20 +2,19 @@ import vtk ...@@ -2,20 +2,19 @@ import vtk
from threading import Thread from threading import Thread
from math import degrees from math import degrees
from src.event_server import EventServer from src import FullscreenArea, create_driver
from src.widgets import FullscreenWidget
class vtkMultitouchInteractor(): class vtkMultitouchInteractor():
def __init__(self): def __init__(self):
self.iren = vtk.vtkRenderWindowInteractor() self.iren = vtk.vtkRenderWindowInteractor()
self.widget = FullscreenWidget() screen = FullscreenArea()
self.server = EventServer(screen) self.driver = create_driver(screen)
widget.on_rotate(self.on_rotate) screen.on_rotate(self.on_rotate)
widget.on_pinch(self.on_pinch) screen.on_pinch(self.on_pinch)
widget.on_tap(self.on_tap) screen.on_tap(self.on_tap)
def SetRenderWindow(self, window): def SetRenderWindow(self, window):
self.iren.SetRenderWindow(window) self.iren.SetRenderWindow(window)
...@@ -29,9 +28,9 @@ class vtkMultitouchInteractor(): ...@@ -29,9 +28,9 @@ class vtkMultitouchInteractor():
#t = Thread(target=self.iren.Start) #t = Thread(target=self.iren.Start)
#t.daemon = True #t.daemon = True
#t.start() #t.start()
#self.server.start() #self.driver.start()
thread = Thread(target=self.server.start) thread = Thread(target=self.driver.start)
thread.daemon = True thread.daemon = True
thread.start() thread.start()
#self.iren.Start() #self.iren.Start()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment