|
@@ -0,0 +1,108 @@
|
|
|
|
|
+#!/usr/bin/env python
|
|
|
|
|
+import pygame, sys, time
|
|
|
|
|
+from touch import add, MultiTouchListener
|
|
|
|
|
+from math import degrees, cos, sin
|
|
|
|
|
+
|
|
|
|
|
+from events import RotateEvent
|
|
|
|
|
+
|
|
|
|
|
+pygame.init()
|
|
|
|
|
+
|
|
|
|
|
+# Config
|
|
|
|
|
+FINGER_RADIUS = 20
|
|
|
|
|
+CENTROID_RADIUS = 15
|
|
|
|
|
+W, H = 640, 480
|
|
|
|
|
+
|
|
|
|
|
+BG_COLOR = 0, 0, 0
|
|
|
|
|
+LINE_COLOR = 128, 128, 128
|
|
|
|
|
+CIRCLE_COLOR = 255, 255, 255
|
|
|
|
|
+RECT_COLOR = 0, 200, 0
|
|
|
|
|
+RECT_SIZE = 150, 100
|
|
|
|
|
+RECT_POS = W / 2, H / 2
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Create canvas GUI in the current thread
|
|
|
|
|
+screen = pygame.display.set_mode((W, H))
|
|
|
|
|
+rotations = [RotateEvent(W / 2, H / 2, 70, 2)]
|
|
|
|
|
+#rotations = []
|
|
|
|
|
+#x, y, w, h = RECT_SIZE
|
|
|
|
|
+#rect = [(x, y), (x, y + h), (x + w, y + h), (x + w, y)]
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def rotate_point(point, axis, angle):
|
|
|
|
|
+ px, py = point
|
|
|
|
|
+ ax, ay = axis
|
|
|
|
|
+ x, y = px - ax, py - ay
|
|
|
|
|
+ radius = (x * x + y * y) ** .5
|
|
|
|
|
+
|
|
|
|
|
+ return ax + radius * cos(angle), ay + radius * sin(angle)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def coord(x, y):
|
|
|
|
|
+ return int(round(W * x)), int(round(H * y))
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Rotated rectangle
|
|
|
|
|
+angle = 0
|
|
|
|
|
+scale = 1
|
|
|
|
|
+
|
|
|
|
|
+def update():
|
|
|
|
|
+ global rotations
|
|
|
|
|
+ cx, cy = coord(*listener.centroid)
|
|
|
|
|
+
|
|
|
|
|
+ # Clear previous frame
|
|
|
|
|
+ screen.fill(BG_COLOR)
|
|
|
|
|
+
|
|
|
|
|
+ # Apply rotation to rectangle canvas
|
|
|
|
|
+ canvas = pygame.Surface((W, H))
|
|
|
|
|
+ canvas.fill(BG_COLOR)
|
|
|
|
|
+ pygame.draw.rect(canvas, RECT_COLOR, RECT_POS + RECT_SIZE)
|
|
|
|
|
+
|
|
|
|
|
+ scaled_canvas = pygame.transform.scale(canvas, coord(scale, scale))
|
|
|
|
|
+ rotated_canvas = pygame.transform.rotate(scaled_canvas, degrees(angle))
|
|
|
|
|
+ rect = rotated_canvas.get_rect()
|
|
|
|
|
+ rect.center = W / 2, H / 2
|
|
|
|
|
+ screen.blit(rotated_canvas, rect)
|
|
|
|
|
+
|
|
|
|
|
+ # Draw touch points
|
|
|
|
|
+ for p in listener.points:
|
|
|
|
|
+ x, y = coord(p.x, p.y)
|
|
|
|
|
+
|
|
|
|
|
+ # Draw line to centroid
|
|
|
|
|
+ pygame.draw.line(screen, LINE_COLOR, (x, y), (cx, cy), 1)
|
|
|
|
|
+
|
|
|
|
|
+ # Draw outlined circle around touch point
|
|
|
|
|
+ pygame.draw.circle(screen, CIRCLE_COLOR, (x, y), FINGER_RADIUS, 1)
|
|
|
|
|
+
|
|
|
|
|
+ # Draw filled circle around centroid
|
|
|
|
|
+ pygame.draw.circle(screen, CIRCLE_COLOR, (cx, cy), CENTROID_RADIUS)
|
|
|
|
|
+
|
|
|
|
|
+ # Update canvas
|
|
|
|
|
+ pygame.display.flip()
|
|
|
|
|
+
|
|
|
|
|
+def rotate(event):
|
|
|
|
|
+ global angle
|
|
|
|
|
+ angle += event.angle
|
|
|
|
|
+
|
|
|
|
|
+def pinch(event):
|
|
|
|
|
+ global scale
|
|
|
|
|
+ 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.start(threaded=True)
|
|
|
|
|
+
|
|
|
|
|
+# Start GUI event loop
|
|
|
|
|
+try:
|
|
|
|
|
+ while True:
|
|
|
|
|
+ for event in pygame.event.get():
|
|
|
|
|
+ if event.type == pygame.QUIT:
|
|
|
|
|
+ sys.exit()
|
|
|
|
|
+
|
|
|
|
|
+ update()
|
|
|
|
|
+except KeyboardInterrupt:
|
|
|
|
|
+ pass
|
|
|
|
|
+finally:
|
|
|
|
|
+ print 'Stopping program...'
|