draw.py 4.1 KB

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