widgets.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from widget import Widget
  2. from screen import screen_size
  3. class RectangularWidget(Widget):
  4. """
  5. Rectangular widget, has a position and a size.
  6. """
  7. def __init__(self, x, y, width, height):
  8. super(RectangularWidget, self).__init__(x, y)
  9. self.set_size(width, height)
  10. def __str__(self):
  11. return '<%s at (%s, %s) size=(%s, %s)>' \
  12. % (self.__class__.__name__, self.x, self.y, self.width,
  13. self.height)
  14. def set_size(self, width, height):
  15. self.width = width
  16. self.height = height
  17. def get_size(self):
  18. return self.width, self.height
  19. def contains_event(self, event):
  20. x, y = event.get_position()
  21. return self.x <= x <= self.x + self.width \
  22. and self.y <= y <= self.y + self.height
  23. class CircularWidget(Widget):
  24. """
  25. Circular widget, has a position and a radius.
  26. """
  27. def __init__(self, x, y, radius):
  28. super(CircularWidget, self).__init__(x, y)
  29. self.set_radius(radius)
  30. def __str__(self):
  31. return '<%s at (%s, %s) size=(%s, %s)>' \
  32. % (self.__class__.__name__, self.x, self.y, self.width,
  33. self.height)
  34. def set_radius(self, radius):
  35. self.radius = radius
  36. def get_radius(self):
  37. return self.radius
  38. def contains_event(self, event):
  39. return self.distance_to(event.get_touch_object()) <= self.radius
  40. class FullscreenWidget(RectangularWidget):
  41. """
  42. Widget representation for the entire screen. This class provides an easy
  43. way to create a single rectangular widget that catches all gestures.
  44. """
  45. def __init__(self):
  46. super(FullscreenWidget, self).__init__(0, 0, *screen_size)
  47. def __str__(self):
  48. return '<FullscreenWidget size=(%d, %d)>' % self.get_size()
  49. def contains_event(self, event):
  50. return True