display.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!./bin/python3
  2. import os
  3. import yaml
  4. import pygame # type: ignore
  5. from typing import Dict
  6. import random
  7. import tempfile
  8. # Color constants
  9. BLACK = (0, 0, 0)
  10. GREEN = (0, 255, 0)
  11. WHITE = (255, 255, 255)
  12. class Display:
  13. '''
  14. Display object which displays a list of strings. It can optionally highlight
  15. a string in the list and display a picture.
  16. '''
  17. def __init__(self, config, pydsp):
  18. # Set config values
  19. self._dx = config.get("dimx", 128) # Drawing dimensions
  20. self._dy = config.get("dimy", 160)
  21. self._pydsp = pydsp # Pygame display object
  22. self._fontsize = config.get("fontsize", 12) # Fontsize to use
  23. self.drawlines = int(self._dy / 12) - 5 # Max number of drawn lines
  24. def update(self, text=None, highlight=-1, title=None, img=None):
  25. '''
  26. Update the display.
  27. text - List of max self.drawlines items to display as list.
  28. highlight - Text item to highlight.
  29. title - List of max 4 items for the title.
  30. img - Image to display in upper left corner.
  31. '''
  32. self._pydsp.fill((0,0,0))
  33. font = pygame.font.Font('freesansbold.ttf', self._fontsize)
  34. if not img:
  35. img = os.path.join("img", "c64tetris.png")
  36. i = pygame.transform.scale(pygame.image.load(img), (50, 50))
  37. self._pydsp.blit(i, (0,0), (0, 0, self._dx, 50))
  38. if not title:
  39. title = ["", "Hacker", " Radio"]
  40. for i, item in enumerate(title):
  41. rendertext = font.render(item, True, WHITE, BLACK)
  42. self._pydsp.blit(rendertext, (55, 2 + i*12))
  43. if i == 3:
  44. break
  45. if not text:
  46. text = []
  47. for i, item in enumerate(text):
  48. if i == highlight:
  49. rendertext = font.render(item, True, BLACK, GREEN)
  50. else:
  51. rendertext = font.render(item, True, GREEN, BLACK)
  52. self._pydsp.blit(rendertext, (0, i * self._fontsize+50))
  53. if i == self.drawlines:
  54. break