window.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from geometry import Surface, RectangularSurface, CircularSurface
  2. from screen import screen_size
  3. class Window(Surface):
  4. """
  5. Abstract class that represents a 2D object on the screen to which gesture
  6. trackers can be added. Implementations of this class should define a method
  7. that can detect wether a touch point is located within the window.
  8. Because any type of window on the screen has (at least) an (x, y) position,
  9. it extends the Positionable object.
  10. Note: Dimensions used in implementations of this class should be in pixels.
  11. """
  12. def __init__(self, **kwargs):
  13. # All trackers that are currently bound to this window
  14. self.trackers = []
  15. # List of points that originated in this window
  16. self.points = []
  17. if 'server' in kwargs:
  18. kwargs['server'].add_window(self)
  19. def add_tracker(self, tracker):
  20. self.trackers.append(tracker)
  21. def remove_tracker(self, tracker):
  22. self.trackers.remove(tracker)
  23. def on_point_down(self, point):
  24. self.points.append(point)
  25. self.update_trackers('down', point)
  26. def on_point_move(self, point):
  27. self.update_trackers('move', point)
  28. def on_point_up(self, point):
  29. self.points.remove(point)
  30. self.update_trackers('up', point)
  31. def update_trackers(self, event_type, point):
  32. """
  33. Update all gesture trackers that are bound to this window with an
  34. added/moved/removed touch point.
  35. """
  36. handler_name = 'on_point_' + event_type
  37. for tracker in self.trackers:
  38. if hasattr(tracker, handler_name):
  39. getattr(tracker, handler_name)(point)
  40. class RectangularWindow(Window, RectangularSurface):
  41. """
  42. Rectangular window.
  43. """
  44. def __init__(self, x, y, width, height, **kwargs):
  45. Window.__init__(self, **kwargs)
  46. RectangularSurface.__init__(self, x, y, width, height)
  47. class CircularWindow(Window, CircularSurface):
  48. """
  49. Circular window.
  50. """
  51. def __init__(self, x, y, width, height, **kwargs):
  52. Window.__init__(self, **kwargs)
  53. CircularSurface.__init__(self, x, y, radius)
  54. class FullscreenWindow(RectangularWindow):
  55. """
  56. Window representation for the entire screen. This class provides an easy
  57. way to create a single rectangular window that catches all gestures.
  58. """
  59. def __init__(self, **kwargs):
  60. super(FullscreenWindow, self).__init__(0, 0, *screen_size, **kwargs)
  61. def __str__(self):
  62. return '<FullscreenWindow size=(%d, %d)>' % (self.width, self.height)
  63. def contains(self, point):
  64. return True