Quellcode durchsuchen

Added double, single and simple tap to test draw program.

Taddeus Kroes vor 13 Jahren
Ursprung
Commit
22aa3086b0
1 geänderte Dateien mit 50 neuen und 9 gelöschten Zeilen
  1. 50 9
      src/draw.py

+ 50 - 9
src/draw.py

@@ -19,6 +19,13 @@ RECT_COLOR = 0, 200, 0
 RECT_POS = W / 2, H / 2
 RECT_SIZE = W / 6., H / 6.
 
+TAP_RADIUS = 60
+TAP_INCREMENT = .25
+
+BEAM_COLOR = 255, 50, 50
+BEAM_LENGTH = 50
+BEAM_WIDTH = 3
+BEAM_INCREMENT = .8
 
 # Create canvas GUI in the current thread
 screen = pygame.display.set_mode((W, H))
@@ -37,13 +44,15 @@ def coord(x, y):
     return int(round(W * x)), int(round(H * y))
 
 
-# Rotated rectangle
+# Global state
 angle = 0
 scale = 1
+taps = []
+dtaps = []
 
 
 def update():
-    cx, cy = coord(*listener.centroid)
+    global taps, dtaps
 
     # Clear previous frame
     screen.fill(BG_COLOR)
@@ -57,20 +66,49 @@ def update():
     screen.blit(transformed, rect)
 
     # Draw touch points
+    c = coord(*listener.centroid)
+
     for p in listener.points:
-        x, y = coord(p.x, p.y)
+        xy = coord(p.x, p.y)
 
         # Draw line to centroid
-        pygame.draw.line(screen, LINE_COLOR, (x, y), (cx, cy), 1)
+        pygame.draw.line(screen, LINE_COLOR, xy, c, 1)
 
         # Draw outlined circle around touch point
-        pygame.draw.circle(screen, CIRCLE_COLOR, (x, y), FINGER_RADIUS, 1)
+        pygame.draw.circle(screen, CIRCLE_COLOR, xy, FINGER_RADIUS, 1)
 
-        # Fill outlined circle with background color
-        pygame.draw.circle(screen, BG_COLOR, (x, y), FINGER_RADIUS - 1, 0)
+        # Fill filled circle with background color within the outline
+        pygame.draw.circle(screen, BG_COLOR, xy, FINGER_RADIUS - 1, 0)
 
     # Draw filled circle around centroid
-    pygame.draw.circle(screen, CIRCLE_COLOR, (cx, cy), CENTROID_RADIUS)
+    pygame.draw.circle(screen, CIRCLE_COLOR, c, CENTROID_RADIUS)
+
+    # Draw an expanding circle around each tap event
+    taps = filter(lambda (xy, r): r <= TAP_RADIUS, taps)
+
+    for tap in taps:
+        xy, radius = tap
+        pygame.draw.circle(screen, CIRCLE_COLOR, xy, int(radius), 2)
+
+        # Increment radius
+        tap[1] += TAP_INCREMENT
+
+    # Shoot a beam from each double tap event to the left
+    dtaps = filter(lambda (x, y, s): 0 <= x <= W, dtaps)
+
+    for dtap in dtaps:
+        x, y, single = dtap
+
+        if single:
+            start_x = x
+            end_x = x + BEAM_LENGTH
+            dtap[0] += BEAM_INCREMENT
+        else:
+            start_x = x - BEAM_LENGTH
+            end_x = x
+            dtap[0] -= BEAM_INCREMENT
+
+        pygame.draw.line(screen, BEAM_COLOR, (start_x, y), (end_x, y), BEAM_WIDTH)
 
     # Update canvas
     pygame.display.flip()
@@ -83,13 +121,16 @@ def rotate(event):
 
 def pinch(event):
     global scale
-    scale += event.amount
+    scale *= event.amount
 
 
 # Start touch event listener in separate thread
 listener = MultiTouchListener(verbose=1, tuio_verbose=0)
 listener.bind('rotate', rotate)
 listener.bind('pinch', pinch)
+#listener.bind('tap', lambda e: taps.append([coord(*e.xy), FINGER_RADIUS]))
+#listener.bind('single_tap', lambda e: dtaps.append(list(coord(*e.xy)) + [1]))
+#listener.bind('double_tap', lambda e: dtaps.append(list(coord(*e.xy)) + [0]))
 listener.start(threaded=True)
 
 # Start GUI event loop