Commit 0b7df1c3 authored by Taddeus Kroes's avatar Taddeus Kroes

Started implementing Event divider (wrapper that assigns a global TUIO event...

Started implementing Event divider (wrapper that assigns a global TUIO event to an EventWindow object).
parent e93fa853
from touch import MultitouchServer, screen_size
W, H = screen_size
def convert_to_pixels(x, y):
return x * W, y * H
class EventWindow(object):
def __init__(self, divider=None):
self.handlers = {}
self.divider = divider
def set_position(self, x, y):
self.x = x
self.y = y
def set_size(self, width, height):
self.width = width
self.height = height
def contains_coords(self, x, y):
return self.x <= x < self.x + self.width \
and self.y <= y < self.y + self.height
def bind(self, name, handler, *args, **kwargs):
if name not in self.handlers:
self.handlers[name] = []
self.handlers[name].append((handler, args, kwargs))
self.divider.bind_event_name(name, self)
def trigger(self, event):
"""
Call all handlers bound to the name of the triggered event.
"""
# Convert event coordinates to offset coordinates
offset_event = event.create_offset_event(self.x, self.y)
name = event.__class__._name
if name in self.handlers:
for handler, args, kwargs in self.handlers[name]:
handler(offset_event, *args, **kwargs)
class EventDivider(object):
def __init__(self):
self.server = MultitouchServer()
self.windows = []
def create_window(self, x, y, width, height):
window = EventWindow()
window.set_position(x, y)
window.set_size(width, height)
self.add_window(window)
return window
def add_window(self, window):
window.divider = self
self.windows.append(window)
def start_server(self):
self.server.start()
def bind_event_name(self, name, window):
self.server.bind(name, self.trigger, window)
def trigger(self, event, window):
if window.contains_coords(*event.pixel_coords()):
window.trigger(event)
if __name__ == '__main__':
def tap(event):
print 'tap:', event
div = EventDivider()
win = div.create_window(100, 100, 640, 480)
win.bind('tap', tap)
div.start_server()
class Event(object):
import copy
from screen import screen_size
W, H = screen_size
class OffsetEvent(object):
def __init__(self, x, y):
self.x = x
self.y = y
@property
def xy(self):
return self.x, self.y
def create_offset_event(self, ox, oy):
x, y = self.pixel_coords()
offset_event = copy.copy(self)
offset_event.x = x - ox
offset_event.y = y - oy
offset_event.original_event = self
return offset_event
def pixel_coords(self):
return self.x * W, self.y * H
class Event(OffsetEvent):
def __init__(self, point):
OffsetEvent.__init__(self, point.x, point.y)
self.point = point
def __str__(self):
return '<%s (%s, %s) sid=%d>' % \
(self.__class__.__name__, self.point.x, self.point.y,
self.point.sid)
(self.__class__.__name__, self.x, self.y, self.point.sid)
def __getattr__(self, name):
"""
......@@ -17,6 +48,11 @@ class Event(object):
raise AttributeError
class Gesture(OffsetEvent):
def __str__(self):
return '<%s (%s, %s)>' % (self.__class__.__name__, self.x, self.y)
class DownEvent(Event):
_name = 'down'
......@@ -34,19 +70,6 @@ class MoveEvent(Event):
self.point.x, self.point.y, self.point.sid)
class Gesture(object):
def __init__(self, x, y):
self.x = x
self.y = y
@property
def xy(self):
return self.x, self.y
def __str__(self):
return '<%s (%s, %s)>' % (self.__class__.__name__, self.x, self.y)
class Tap(Gesture):
_name = 'tap'
......
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