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,37 +273,48 @@ class MultiTouchListener(Logger): ...@@ -273,37 +273,48 @@ class MultiTouchListener(Logger):
return cx - ocx, cy - ocy return cx - ocx, cy - ocy
#def detect_pan(self):
# """
# Look for multi-finger drag events. Multi-drag is defined as all the
# fingers moving close-ish together in the same direction.
# """
# l = len(self.points)
# if not l:
# return False
# m = MAX_MULTI_DRAG_DISTANCE
# clustered = l == 1 or all([p.distance(*self.centroid) <= m \
# for p in self.points])
# directions = [(cmp(p.dx(), 0), cmp(p.dy(), 0))
# for p in self.points]
# if not clustered or not any(map(all, zip(*directions))):
# return False
# if l == 1:
# p = self.points[0]
# cx, cy, dx, dy = p.x, p.y, p.dx(), p.dy()
# else:
# cx, cy = self.centroid
# old_cx, old_cy = self.old_centroid
# dx, dy = cx - old_cx, cy - old_cy
# self.trigger(Pan(cx, cy, dx, dy, l))
# return True
def detect_pan(self): def detect_pan(self):
"""
Look for multi-finger drag events. Multi-drag is defined as all the
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
m = MAX_MULTI_DRAG_DISTANCE
clustered = l == 1 or all([p.distance(*self.centroid) <= m \
for p in self.points])
directions = [(cmp(p.dx(), 0), cmp(p.dy(), 0))
for p in self.points]
if not clustered or not any(map(all, zip(*directions))):
return False
if l == 1: if distance(self.centroid, self.old_centroid) > DIST_THRESHOLD:
p = self.points[0]
cx, cy, dx, dy = p.x, p.y, p.dx(), p.dy()
else:
cx, cy = self.centroid cx, cy = self.centroid
old_cx, old_cy = self.old_centroid dx, dy = self.centroid_movement()
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
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):
if p.sid == sid: if p.sid == sid:
...@@ -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