Browse Source

First attempt to fix pinch (failed).

Taddeus Kroes 14 years ago
parent
commit
cd83ef1790
3 changed files with 18 additions and 5 deletions
  1. 7 4
      src/draw.py
  2. 1 1
      src/events.py
  3. 10 0
      src/touch.py

+ 7 - 4
src/draw.py

@@ -44,20 +44,23 @@ def coord(x, y):
 # Rotated rectangle
 angle = 0
 scale = 1
+w, h = W, H
 
 def update():
-    global rotations
+    global rotations, w, h, scale
     cx, cy = coord(*listener.centroid)
 
     # Clear previous frame
     screen.fill(BG_COLOR)
 
     # Apply rotation to rectangle canvas
-    canvas = pygame.Surface((W, H))
+    canvas = pygame.Surface((w, h))
     canvas.fill(BG_COLOR)
     pygame.draw.rect(canvas, RECT_COLOR, RECT_POS + RECT_SIZE)
+    w, h = int(round(scale * w)), int(round(scale * h))
+    scale = 1
 
-    scaled_canvas = pygame.transform.scale(canvas, coord(scale, scale))
+    scaled_canvas = pygame.transform.scale(canvas, (w, h))
     rotated_canvas = pygame.transform.rotate(scaled_canvas, degrees(angle))
     rect = rotated_canvas.get_rect()
     rect.center = W / 2, H / 2
@@ -85,7 +88,7 @@ def rotate(event):
 
 def pinch(event):
     global scale
-    scale += event.amount
+    scale = event.amount
 
 
 # Start touch event listener in separate thread

+ 1 - 1
src/events.py

@@ -88,7 +88,7 @@ class PinchEvent(GestureEvent):
     _name = 'pinch'
 
     def __init__(self, cx, cy, amount, n):
-        super(RotateEvent, self).__init__(cx, cy)
+        super(PinchEvent, self).__init__(cx, cy)
         self.amount = amount
         self.n = n
 

+ 10 - 0
src/touch.py

@@ -270,9 +270,19 @@ class MultiTouchListener(Logger):
             p.update(x, y)
             self.update_centroid(moving=p)
             self.trigger(MoveEvent(p))
+            self.detect_pinch(p)
 
             # TODO: Detect pan
 
+    def detect_pinch(self, moved):
+        cx, cy = self.centroid
+        dist = moved.distance_to(cx, cy)
+        old_dist = distance((moved.px, moved.py), self.centroid)
+
+        if abs(dist - old_dist) > DIST_THRESHOLD:
+            self.trigger(PinchEvent(cx, cy, dist / old_dist,
+                                    len(self.points)))
+
     def stop(self):
         self.log('Stopping event loop')