draw.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env python
  2. from __future__ import division
  3. import pygame
  4. from threading import Thread
  5. from math import degrees
  6. from src.event_server import EventServer
  7. from src.areas import FullscreenArea
  8. from tests.parse_arguments import create_parser, parse_args
  9. from src.screen import screen_size
  10. # Parse arguments
  11. parser = create_parser()
  12. parser.add_argument('-f', '--fullscreen', action='store_true', default=False,
  13. help='run in fullscreen')
  14. args = parse_args(parser)
  15. pygame.init()
  16. # Config
  17. FINGER_RADIUS = 20
  18. CENTROID_RADIUS = 15
  19. W, H = screen_size if args.fullscreen else (640, 480)
  20. BG_COLOR = 0, 0, 0
  21. LINE_COLOR = 128, 128, 128
  22. CIRCLE_COLOR = 255, 255, 255
  23. RECT_COLOR = 0, 200, 0
  24. RECT_POS = W / 2, H / 2
  25. RECT_SIZE = W / 6., H / 6.
  26. TAP_RADIUS = 65
  27. TAP_INCREMENT = .3
  28. BEAM_COLOR = 255, 50, 50
  29. BEAM_LENGTH = 50
  30. BEAM_WIDTH = 3
  31. BEAM_INCREMENT = .8
  32. MAX_SCALE = 10
  33. # Create canvas GUI
  34. flags = pygame.FULLSCREEN if args.fullscreen else 0
  35. screen = pygame.display.set_mode((W, H), flags)
  36. def coord(x, y):
  37. w, h = screen_size
  38. return int(round(W / w * x)), int(round(H / h * y))
  39. # Global state
  40. angle = 0
  41. scale = 1
  42. taps = []
  43. dtaps = []
  44. points = []
  45. def update():
  46. global taps, dtaps
  47. # Clear previous frame
  48. screen.fill(BG_COLOR)
  49. # Apply scale and rotation to a fixed-size rectangle canvas
  50. canvas = pygame.Surface(RECT_SIZE)
  51. canvas.fill(RECT_COLOR)
  52. transformed = pygame.transform.rotozoom(canvas, degrees(angle), scale)
  53. rect = transformed.get_rect()
  54. rect.center = W / 2, H / 2
  55. screen.blit(transformed, rect)
  56. # Draw touch points
  57. if transform and transform.centroid:
  58. c = coord(*transform.centroid)
  59. for p in points:
  60. xy = coord(p.x, p.y)
  61. # Draw line to centroid
  62. if transform and transform.centroid:
  63. pygame.draw.line(screen, LINE_COLOR, xy, c, 1)
  64. # Draw outlined circle around touch point
  65. pygame.draw.circle(screen, CIRCLE_COLOR, xy, FINGER_RADIUS, 1)
  66. # Fill filled circle with background color within the outline
  67. pygame.draw.circle(screen, BG_COLOR, xy, FINGER_RADIUS - 1, 0)
  68. # Draw filled circle around centroid
  69. if transform and transform.centroid:
  70. pygame.draw.circle(screen, CIRCLE_COLOR, c, CENTROID_RADIUS)
  71. # Draw an expanding circle around each tap event
  72. taps = filter(lambda (xy, r): r <= TAP_RADIUS, taps)
  73. for tap in taps:
  74. xy, radius = tap
  75. pygame.draw.circle(screen, CIRCLE_COLOR, xy, int(radius), 2)
  76. # Increment radius
  77. tap[1] += TAP_INCREMENT
  78. # Shoot a beam from each double tap event to the left
  79. dtaps = filter(lambda (x, y, s): 0 <= x <= W, dtaps)
  80. for dtap in dtaps:
  81. x, y, single = dtap
  82. if single:
  83. start_x = x
  84. end_x = x + BEAM_LENGTH
  85. dtap[0] += BEAM_INCREMENT
  86. else:
  87. start_x = x - BEAM_LENGTH
  88. end_x = x
  89. dtap[0] -= BEAM_INCREMENT
  90. pygame.draw.line(screen, BEAM_COLOR, (start_x, y), (end_x, y),
  91. BEAM_WIDTH)
  92. # Update canvas
  93. pygame.display.flip()
  94. transform = None
  95. def save_tracker(gesture):
  96. global transform
  97. if not transform:
  98. transform = gesture.get_tracker()
  99. def rotate(gesture):
  100. global angle
  101. angle += gesture.get_angle()
  102. save_tracker(gesture)
  103. def pinch(gesture):
  104. global scale
  105. scale = min(scale * gesture.get_scale(), MAX_SCALE)
  106. save_tracker(gesture)
  107. area = FullscreenArea()
  108. server = EventServer(area)
  109. area.on_rotate(rotate)
  110. area.on_pinch(pinch)
  111. area.on_tap(lambda g: taps.append([coord(*g), FINGER_RADIUS]))
  112. area.on_single_tap(lambda g: dtaps.append(list(coord(*g)) + [1]))
  113. area.on_double_tap(lambda g: dtaps.append(list(coord(*g)) + [0]))
  114. area.on_point_down(lambda g: points.append(g.get_event().point))
  115. area.on_point_up(lambda g: points.remove(g.get_event().point))
  116. try:
  117. # Start touch gesture server in separate thread
  118. thread = Thread(target=server.start)
  119. thread.daemon = True
  120. thread.start()
  121. # Start GUI event loop
  122. def is_quit_event(e):
  123. return e.type == pygame.QUIT \
  124. or (e.type == pygame.KEYDOWN and e.key == ord('q'))
  125. while not any(filter(is_quit_event, pygame.event.get())):
  126. update()
  127. except KeyboardInterrupt:
  128. pass
  129. finally:
  130. server.stop()