Commit 2f572483 authored by Taddeus Kroes's avatar Taddeus Kroes

Added simple pan detection.

parent eba31ca6
...@@ -107,6 +107,6 @@ class Pan(Gesture): ...@@ -107,6 +107,6 @@ class Pan(Gesture):
self.n = n self.n = n
def __str__(self): def __str__(self):
return '<%s (%s, %s) to (%s, %s) n=%d>' % \ return '<%s (%s, %s) direction=(%s, %s) n=%d>' % \
(self.__class__.__name__, self.x, self.y, self.dx, self.dy, (self.__class__.__name__, self.x, self.y, self.dx, self.dy,
self.amount, self.n) self.n)
...@@ -273,36 +273,47 @@ class MultiTouchListener(Logger): ...@@ -273,36 +273,47 @@ class MultiTouchListener(Logger):
return cx - ocx, cy - ocy return cx - ocx, cy - ocy
def detect_pan(self): #def detect_pan(self):
""" # """
Look for multi-finger drag events. Multi-drag is defined as all the # Look for multi-finger drag events. Multi-drag is defined as all the
fingers moving close-ish together in the same direction. # fingers moving close-ish together in the same direction.
""" # """
l = len(self.points) # l = len(self.points)
if not l: # if not l:
return False # return False
m = MAX_MULTI_DRAG_DISTANCE # m = MAX_MULTI_DRAG_DISTANCE
clustered = l == 1 or all([p.distance(*self.centroid) <= m \ # clustered = l == 1 or all([p.distance(*self.centroid) <= m \
for p in self.points]) # for p in self.points])
directions = [(cmp(p.dx(), 0), cmp(p.dy(), 0)) # directions = [(cmp(p.dx(), 0), cmp(p.dy(), 0))
for p in self.points] # for p in self.points]
if not clustered or not any(map(all, zip(*directions))): # if not clustered or not any(map(all, zip(*directions))):
return False # return False
if l == 1: # if l == 1:
p = self.points[0] # p = self.points[0]
cx, cy, dx, dy = p.x, p.y, p.dx(), p.dy() # cx, cy, dx, dy = p.x, p.y, p.dx(), p.dy()
else: # else:
cx, cy = self.centroid # cx, cy = self.centroid
old_cx, old_cy = self.old_centroid # old_cx, old_cy = self.old_centroid
dx, dy = cx - old_cx, cy - old_cy # dx, dy = cx - old_cx, cy - old_cy
# self.trigger(Pan(cx, cy, dx, dy, l))
self.trigger(Pan(cx, cy, dx, dy, l)) # return True
return True def detect_pan(self):
l = len(self.points)
if not l:
return
if distance(self.centroid, self.old_centroid) > DIST_THRESHOLD:
cx, cy = self.centroid
dx, dy = self.centroid_movement()
self.trigger(Pan(cx, cy, dx, dy, l))
def find_point(self, sid, index=False): def find_point(self, sid, index=False):
for i, p in enumerate(self.points): for i, p in enumerate(self.points):
...@@ -340,8 +351,10 @@ class MultiTouchListener(Logger): ...@@ -340,8 +351,10 @@ class MultiTouchListener(Logger):
# If a pan event is detected, ignore any rotate or pinch movement # If a pan event is detected, ignore any rotate or pinch movement
# (they are considered noise) # (they are considered noise)
if not self.detect_pan(): self.detect_pan()
self.detect_rotation_and_pinch() self.detect_rotation_and_pinch()
#if not self.detect_pan():
# self.detect_rotation_and_pinch()
self.points_changed = False self.points_changed = False
...@@ -427,8 +440,11 @@ if __name__ == '__main__': ...@@ -427,8 +440,11 @@ if __name__ == '__main__':
listener.bind('tap', tap, 0) listener.bind('tap', tap, 0)
listener.bind('single_tap', tap, 1) listener.bind('single_tap', tap, 1)
listener.bind('double_tap', tap, 2) listener.bind('double_tap', tap, 2)
# Add empty handlers sp that the events are actually triggered
listener.bind('rotate', lambda e: 0) listener.bind('rotate', lambda e: 0)
listener.bind('pinch', lambda e: 0) listener.bind('pinch', lambda e: 0)
listener.bind('pan', lambda e: 0)
try: try:
listener.start() listener.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