testapp.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #!/usr/bin/env python
  2. from __future__ import division
  3. import gtk
  4. from threading import Thread
  5. from math import pi, tan
  6. import src as mt
  7. from utils import BoundingBoxArea, Flick, FlickThread, GtkEventWindow
  8. RED = 1, 0, 0
  9. GREEN = 0, 1, 0
  10. BLUE = 0, 0, 1
  11. WHITE = 1, 1, 1
  12. BLACK = 0, 0, 0
  13. class Rectangle(mt.RectangularArea):
  14. def __init__(self, x, y, width, height, color=(1, 0, 0)):
  15. super(Rectangle, self).__init__(x, y, width, height)
  16. self.color = color
  17. self.on_drag(self.handle_drag)
  18. def handle_drag(self, g):
  19. tx, ty = g.get_translation()
  20. self.translate(tx, ty)
  21. refresh()
  22. def draw(self, cr):
  23. cr.rectangle(self.x, self.y, self.width, self.height)
  24. cr.set_source_rgb(*self.color)
  25. cr.fill()
  26. class Polygon(BoundingBoxArea):
  27. def __init__(self, x, y, points, margin=0, color=BLUE, border_color=RED):
  28. super(Polygon, self).__init__(x, y, points)
  29. self.fill_color = color
  30. self.border_color = border_color
  31. self.margin = margin
  32. self.on_drag(self.handle_drag)
  33. self.on_pinch(self.handle_pinch)
  34. self.on_rotate(self.handle_rotate)
  35. self.on_flick(self.handle_flick)
  36. def flick_drag(self, amt):
  37. tx, ty = self.flick_direction
  38. self.translate(tx * amt, ty * amt)
  39. refresh()
  40. def handle_flick(self, g):
  41. trans = g.get_translation()
  42. print trans.distance_to((0, 0))
  43. if trans.distance_to((0, 0)) > 10:
  44. self.flick_direction = trans
  45. flicks.add(Flick(self.flick_drag, 0.7, 0.4))
  46. def contains(self, x, y):
  47. if draw_bounding_boxes:
  48. return mt.RectangularArea.contains(self, x, y)
  49. return BoundingBoxArea.contains(self, x, y)
  50. def handle_drag(self, g):
  51. tx, ty = g.get_translation()
  52. self.translate(tx, ty)
  53. refresh()
  54. def handle_pinch(self, g):
  55. cx, cy = g.get_position()
  56. self.scale_points(g.get_scale(), cx, cy)
  57. self.update_bounds()
  58. refresh()
  59. def handle_rotate(self, g):
  60. cx, cy = g.get_position()
  61. self.rotate_points(g.get_angle(), cx, cy)
  62. self.update_bounds()
  63. refresh()
  64. #def contains(self, x, y):
  65. # m = self.margin
  66. # return self.x - m <= x < self.x + self.width + m \
  67. # and self.y - m <= y < self.y + self.height + m
  68. def draw(self, cr):
  69. # Draw bounding box
  70. if draw_bounding_boxes:
  71. m = self.margin
  72. cr.rectangle(self.x - m, self.y - m,
  73. self.width + 2 * m, self.height + 2 * m)
  74. cr.set_source_rgb(*self.border_color)
  75. cr.set_line_width(3)
  76. cr.stroke()
  77. # Fill polygon
  78. rx, ry = self.get_root_offset()
  79. cr.translate(rx, ry)
  80. cr.new_path()
  81. for x, y in zip(*self.points):
  82. cr.line_to(x, y)
  83. cr.set_source_rgb(*self.fill_color)
  84. cr.fill()
  85. fullscreen = False
  86. draw_bounding_boxes = False
  87. draw_touch_objects = True
  88. W, H = mt.screen.screen_size
  89. def create_context_window(w, h, callback):
  90. def create_context(area, event):
  91. """Add Cairo context to GTK window and draw state."""
  92. global cr
  93. cr = area.window.cairo_create()
  94. draw()
  95. def update_window():
  96. """Synchronize overlay with GTK window."""
  97. overlay.set_size(*window.get_size())
  98. refresh()
  99. def handle_key(win, event):
  100. """Handle key event. 'f' toggles fullscreen, 'b' toggles bounding
  101. boxes, 'i' toggles input points, 'q' exits the program."""
  102. if event.keyval >= 256:
  103. return
  104. key = chr(event.keyval)
  105. if key == 'f':
  106. global fullscreen
  107. (win.unfullscreen if fullscreen else win.fullscreen)()
  108. fullscreen = not fullscreen
  109. elif key == 'b':
  110. global draw_bounding_boxes
  111. draw_bounding_boxes = not draw_bounding_boxes
  112. refresh()
  113. elif key == 'i':
  114. global draw_touch_objects
  115. draw_touch_objects = not draw_touch_objects
  116. refresh()
  117. elif key == 'q':
  118. quit()
  119. # Root area (will be synchronized with GTK window)
  120. global overlay
  121. overlay = mt.RectangularArea(0, 0, w, h)
  122. # GTK window
  123. global window, root
  124. window = GtkEventWindow()
  125. window.set_title('Cairo test')
  126. window.connect('destroy', quit)
  127. window.connect('key-press-event', handle_key)
  128. window.connect('show', callback)
  129. window.on_update(update_window)
  130. root = window.get_area()
  131. if fullscreen:
  132. window.fullscreen()
  133. # Drawing area, needed by cairo context for drawing
  134. area = gtk.DrawingArea()
  135. area.set_size_request(w, h)
  136. area.connect('expose-event', create_context)
  137. window.add(area)
  138. area.show()
  139. window.show()
  140. def draw():
  141. if not cr:
  142. return
  143. # Background
  144. cr.rectangle(0, 0, *root.get_size())
  145. cr.set_source_rgb(*BLACK)
  146. cr.fill()
  147. # Drawable objects (use save and restore to allow transformations)
  148. for obj in draw_objects:
  149. cr.save()
  150. obj.draw(cr)
  151. cr.restore()
  152. if draw_touch_objects:
  153. ox, oy = root.get_position()
  154. cr.set_source_rgb(*WHITE)
  155. for hand in touch_hands:
  156. cx, cy = hand.get_centroid()
  157. # Filled centroid circle
  158. if len(hand) > 1:
  159. cr.arc(cx - ox, cy - oy, 20, 0, 2 * pi)
  160. cr.fill()
  161. for x, y in hand:
  162. x -= ox
  163. y -= oy
  164. # Circle outline
  165. cr.set_line_width(3)
  166. cr.arc(x, y, 20, 0, 2 * pi)
  167. cr.stroke()
  168. # Line to centroid
  169. if len(hand) > 1:
  170. cr.move_to(x, y)
  171. cr.line_to(cx - ox, cy - oy)
  172. cr.set_line_width(2)
  173. cr.stroke()
  174. # Cross
  175. cr.set_line_width(1)
  176. cr.move_to(x - 8, y)
  177. cr.line_to(x + 8, y)
  178. cr.move_to(x, y - 8)
  179. cr.line_to(x, y + 8)
  180. cr.stroke()
  181. def refresh(*args):
  182. window.queue_draw()
  183. def quit(*args):
  184. gtk.main_quit()
  185. # Global variables
  186. window = cr = root = overlay = flicks = None
  187. draw_objects = []
  188. touch_hands = []
  189. def triangle_height(width):
  190. return abs(.5 * width * tan(2 / 3 * pi))
  191. def on_show(window):
  192. def root_dtap(g): print 'double tapped on root'
  193. root.on_double_tap(root_dtap)
  194. # Create blue rectangle
  195. x, y, w, h = 0, 0, 250, 150
  196. rect = Polygon(x, y, [(0, 0), (0, h), (w, h), (w, 0)], margin=20)
  197. draw_objects.append(rect)
  198. root.add_area(rect)
  199. def rect_tap(g): print 'tapped on rectangle'
  200. rect.on_tap(rect_tap, propagate_up_event=False)
  201. # Create green triangle
  202. x, y, w = 400, 400, 200
  203. h = triangle_height(w)
  204. triangle = Polygon(x, y, [(0, h), (w, h), (w / 2, 0)],
  205. margin=20, color=GREEN)
  206. draw_objects.append(triangle)
  207. root.add_area(triangle)
  208. # Overlay catches finger events to be able to draw touch points
  209. def handle_down(gesture):
  210. if gesture.is_first():
  211. touch_hands.append(gesture.get_hand())
  212. if draw_touch_objects:
  213. refresh()
  214. def handle_up(gesture):
  215. if gesture.is_last():
  216. touch_hands.remove(gesture.get_hand())
  217. if draw_touch_objects:
  218. refresh()
  219. overlay.on_finger_down(handle_down)
  220. overlay.on_finger_move(lambda g: draw_touch_objects and refresh())
  221. overlay.on_finger_up(handle_up)
  222. root.add_area(overlay)
  223. if __name__ == '__main__':
  224. from parse_arguments import create_parser, parse_args
  225. # Parse arguments
  226. parser = create_parser()
  227. parser.add_argument('-f', '--fullscreen', action='store_true',
  228. default=False, help='run in fullscreen initially')
  229. args = parse_args(parser)
  230. fullscreen = args.fullscreen
  231. # Create a window with a Cairo context in it and a multi-touch area
  232. # syncronized with it
  233. create_context_window(800, 600, on_show)
  234. # Run multi-touch gesture server in separate thread
  235. driver = mt.create_driver(root)
  236. mt_thread = Thread(target=driver.start)
  237. mt_thread.daemon = True
  238. mt_thread.start()
  239. # Flick movement is also handled in a separate thread
  240. flicks = FlickThread()
  241. flicks.daemon = True
  242. flicks.start()
  243. # Initialize threads in GTK so that the thread started above will work
  244. gtk.gdk.threads_init()
  245. # Start main loop in current thread
  246. gtk.main()