from logger import Logger class GestureTracker(Logger): """ Abstract class for gesture tracker definitions. Contains methods for changing the state of touch points. """ # Supported gesture types __gesture_types__ = [] # Configurable properties (see configure() method) __configurable__ = [] def __init__(self, window=None): # Hashmap of gesture types self.handlers = {} if window: window.add_tracker(self) def bind(self, gesture_type, handler, *args, **kwargs): """ Bind a handler to a gesture type. Multiple handlers can be bound to a single gesture type. Optionally, (keyword) arguments that will be passed to the handler along with a Gesture object can be specified. """ if gesture_type not in self.__gesture_types__: raise ValueError('Unsupported gesture type "%s".' % gesture_type) h = handler, args, kwargs if gesture_type not in self.handlers: self.handlers[gesture_type] = [h] else: self.handlers[gesture_type].append(h) def trigger(self, gesture): if gesture.__type__ not in self.handlers: self.debug('Triggered "%s", but no handlers are bound.' % gesture.__type__) return self.info('Triggered %s.' % gesture) for handler, args, kwargs in self.handlers[gesture.__type__]: handler(gesture, *args, **kwargs) def is_type_bound(self, gesture_type): return gesture_type in self.handlers def on_point_down(self, point): pass def on_point_move(self, point): pass def on_point_up(self, point): pass def configure(self, **kwargs): for name, value in kwargs.iteritems(): if name not in self.__configurable__: raise ValueError('%s.%s is not a configurable property.' % (self.__class__.__name__, name)) setattr(self, name, value) class Gesture(object): """ Abstract class that represents a triggered gesture. """ pass