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 tracker import GestureTracker, Gesture
from event_server import EventServer
from drivers import create_driver
from areas import *
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.
Currently, only a TUIO driver implementation exists. In the future, this
method should select one driver from a set of implementations.
Create an object of an event driver implementation, based on hardware
support. Currently, only a TUIO driver implementation exists. In the
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
class TuioDriver(EventDriver):
tuio_address = 'localhost', 3333
def __init__(self, event_server):
super(TuioDriver, self).__init__(event_server)
def __init__(self, root_area=None):
super(TuioDriver, self).__init__(root_area)
# OSC server that listens to incoming TUIO events
self.server = OSCServer(self.tuio_address)
......@@ -53,8 +53,7 @@ class TuioDriver(EventDriver):
for sid in released:
point = self.points[sid]
del self.points[sid]
self.event_server.delegate_event(PointUpEvent(point))
#self.event_server.on_point_up(sid)
self.delegate_event(PointUpEvent(point))
elif msg_type == 'set':
sid, x, y = data[1:4]
......@@ -71,23 +70,21 @@ class TuioDriver(EventDriver):
% (sid, x, y, px, py))
point = self.points[sid]
point.set_position(px, py)
self.event_server.delegate_event(PointMoveEvent(point))
#self.event_server.on_point_move(sid, px, py)
self.delegate_event(PointMoveEvent(point))
else:
self.debug('Down %d at (%.4f, %.4f), in pixels: (%d, %d).'
% (sid, x, y, px, py))
self.down.add(sid)
self.points[sid] = point = TouchPoint(px, py)
self.event_server.delegate_event(PointDownEvent(point))
#self.event_server.on_point_down(sid, px, py)
self.delegate_event(PointDownEvent(point))
def run(self):
self.server.handle_request()
def start(self):
def start_loop(self):
self.info('Starting OSC server')
self.server.serve_forever()
def stop(self):
self.info('Stopping OSC server')
self.server.close()
def run(self):
self.server.handle_request()
......@@ -3,16 +3,39 @@ from logger import Logger
class EventDriver(Logger):
"""
Abstract factory class for drivers. A driver translates driver-specific
messages to a common set of events. The minimal set is {point_down,
point_move, point_up}. A driver implementation should define the methods
'start' and 'stop', which starts/stops some event loop that triggers the
'delegate_event' method of a area.
The event driver receives driver-specific messages, which are delegated to
the root area in the form of common events such as 'piont_down' pr
'point_up'.
"""
def __init__(self, event_server):
self.event_server = event_server
def __init__(self, root_area=None):
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):
"""
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.
"""
......
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 src import FullscreenArea, create_driver
parse_args(create_parser())
# Create server and fullscreen area
# Create driver and fullscreen area
screen = FullscreenArea()
server = EventServer(screen)
driver = create_driver(screen)
# Bind handlers
# Add tracker and handlers
screen.on_point_down(lambda g: 0)
screen.on_point_move(lambda g: 0)
screen.on_point_up(lambda g: 0)
# Start listening to TUIO events
try:
server.start()
driver.start()
except KeyboardInterrupt:
server.stop()
driver.stop()
......@@ -162,8 +162,8 @@ if __name__ == '__main__':
create_context_window(640, 460, on_show)
# Run multi-touch gesture server in separate thread
server = mt.EventServer(root)
mt_thread = Thread(target=server.start)
driver = mt.create_driver(root)
mt_thread = Thread(target=driver.start)
mt_thread.daemon = True
mt_thread.start()
......
......@@ -4,9 +4,8 @@ import pygame
from threading import Thread
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 src import FullscreenArea, create_driver
from src.screen import screen_size
# Parse arguments
......@@ -147,7 +146,7 @@ def pinch(gesture):
area = FullscreenArea()
server = EventServer(area)
driver = create_driver(area)
area.on_rotate(rotate)
area.on_pinch(pinch)
......@@ -161,7 +160,7 @@ area.on_point_up(lambda g: points.remove(g.get_event().point))
try:
# Start touch gesture server in separate thread
thread = Thread(target=server.start)
thread = Thread(target=driver.start)
thread.daemon = True
thread.start()
......@@ -175,4 +174,4 @@ try:
except KeyboardInterrupt:
pass
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 src import FullscreenArea, create_driver
parse_args(create_parser())
# Create server and fullscreen area
# Create driver and fullscreen area
screen = FullscreenArea()
server = EventServer(screen)
driver = create_driver(screen)
# Bind handlers
screen.on_tap(lambda g: 0)
......@@ -15,6 +14,6 @@ screen.on_double_tap(lambda g: 0)
# Start listening to TUIO events
try:
server.start()
driver.start()
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 src import FullscreenArea, create_driver
parse_args(create_parser())
# Create server and fullscreen area
# Create driver and fullscreen area
screen = FullscreenArea()
server = EventServer(screen)
driver = create_driver(screen)
# Bind handlers
screen.on_rotate(lambda g: 0)
......@@ -15,6 +14,6 @@ screen.on_drag(lambda g: 0)
# Start listening to TUIO events
try:
server.start()
driver.start()
except KeyboardInterrupt:
server.stop()
driver.stop()
......@@ -2,20 +2,19 @@ import vtk
from threading import Thread
from math import degrees
from src.event_server import EventServer
from src.widgets import FullscreenWidget
from src import FullscreenArea, create_driver
class vtkMultitouchInteractor():
def __init__(self):
self.iren = vtk.vtkRenderWindowInteractor()
self.widget = FullscreenWidget()
self.server = EventServer(screen)
screen = FullscreenArea()
self.driver = create_driver(screen)
widget.on_rotate(self.on_rotate)
widget.on_pinch(self.on_pinch)
widget.on_tap(self.on_tap)
screen.on_rotate(self.on_rotate)
screen.on_pinch(self.on_pinch)
screen.on_tap(self.on_tap)
def SetRenderWindow(self, window):
self.iren.SetRenderWindow(window)
......@@ -29,9 +28,9 @@ class vtkMultitouchInteractor():
#t = Thread(target=self.iren.Start)
#t.daemon = True
#t.start()
#self.server.start()
#self.driver.start()
thread = Thread(target=self.server.start)
thread = Thread(target=self.driver.start)
thread.daemon = True
thread.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