touch_text_manager.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import pygame
  2. import logging
  3. logger = logging.getLogger(__name__)
  4. class TouchTextManager():
  5. def __init__(self, size, base_size):
  6. self.size = size
  7. self.base_size = base_size
  8. self.touch_objects = {}
  9. self.text_objects = {}
  10. def add_object(self, key, text, pos, pos2, color):
  11. self.text_objects[key] = TextItem(text, pos, pos2, color, self.base_size)
  12. def get_object(self, key):
  13. return self.text_objects[key]
  14. def add_touch_object(self, key, text, pos, color):
  15. self.touch_objects['key'] = TouchAndTextItem(text, pos, color, self.base_size)
  16. def get_touch_object(self, key):
  17. return self.touch_objects['key']
  18. def add_progressbar(self, key, pos, pos2, max):
  19. self.touch_objects['key'] = Progressbar(pos, pos2, max)
  20. def render(self, surface):
  21. for key in self.text_objects:
  22. self.text_objects[key].update()
  23. self.text_objects[key].render(surface)
  24. for key in self.touch_objects:
  25. self.touch_objects[key].render(surface)
  26. class BaseItem():
  27. def __init__(self, pos, pos2):
  28. self.pos = pos
  29. self.pos2 = pos2
  30. self.size = [0, 0]
  31. self.size[0] = self.pos2[0] - self.pos[0]
  32. self.size[1] = self.pos2[1] - self.pos[1]
  33. self.rect = pygame.Rect(0, 0, self.size[0], self.size[1])
  34. class TextItem(BaseItem):
  35. def __init__(self, text, pos, pos2, color, text_size):
  36. if pos2 is not None:
  37. BaseItem.__init__(self, pos, pos2)
  38. self.text_size = text_size
  39. self.font = pygame.font.SysFont("arial", text_size)
  40. self.text = text
  41. self.color = color
  42. self.box = self.font.render(text, True, self.color)
  43. if pos2 is not None:
  44. if self.pos[0] + self.box.get_rect().width > pos2[0]:
  45. self.fit_horizontal = False
  46. self.text = self.text + " "
  47. self.original_text = self.text
  48. self.step = 0
  49. else:
  50. self.fit_horizontal = True
  51. if self.pos[1] + self.box.get_rect().height > pos2[1]:
  52. self.fit_vertical = False
  53. else:
  54. self.fit_vertical = True
  55. else:
  56. BaseItem.__init__(self, pos, (pos[0] + self.box.get_rect().width, pos[1] + self.box.get_rect().height))
  57. self.fit_horizontal = True
  58. self.fit_vertical = True
  59. def update(self):
  60. if not self.fit_horizontal:
  61. if self.text == self.original_text:
  62. if self.step > 90:
  63. self.step = 0
  64. new_text = self.text[1:]
  65. new_text = new_text + self.text[:1]
  66. self.text = new_text
  67. else:
  68. self.step = self.step + 1
  69. elif self.step > 5:
  70. self.step = 0
  71. new_text = self.text[1:]
  72. new_text = new_text + self.text[:1]
  73. self.text = new_text
  74. else:
  75. self.step = self.step + 1
  76. def render(self, surface):
  77. if self.fit_horizontal:
  78. self.box
  79. else:
  80. self.box = self.font.render(self.text, True, self.color)
  81. surface.blit(self.box, self.pos, area=self.rect)
  82. def set_text(self, text):
  83. self.__init__(text, self.pos, self.pos2, self.color, self.text_size)
  84. class TouchObject(BaseItem):
  85. def __init__(self, pos, pos2):
  86. BaseItem.__init__(self, pos, pos2)
  87. self.active = False
  88. self.background_box = pygame.Surface(self.size)
  89. self.background_box.fill((0, 128, 255))
  90. def render(self, surface):
  91. surface.blit(self.background_box, self.pos)
  92. class TouchAndTextItem(TouchObject, TextItem):
  93. def __init__(self, text, pos, color, text_size):
  94. TextItem.__init__(self, text, pos, None, color, text_size)
  95. TouchObject.__init__(self, pos, self.pos2)
  96. def update(self):
  97. TextItem.update()
  98. def render(self, surface):
  99. TouchObject.render(self, surface)
  100. TextItem.render(self, surface)
  101. class Progressbar(BaseItem):
  102. def __init__(self, pos, pos2, max):
  103. BaseItem.__init__(self, pos, pos2)
  104. logger.error(pos2)
  105. self.value = 0
  106. self.max = max
  107. self.back_color = (0, 0, 0)
  108. self.main_color = (255, 255, 255)
  109. self.surface = pygame.Surface(self.size)
  110. self.surface.fill(self.back_color)
  111. def update(self):
  112. pass
  113. def render(self, surface):
  114. surface.blit(self.surface, self.pos)
  115. def set_value(self, value):
  116. self.value = value
  117. self.surface.fill(self.back_color)
  118. pos_pixel = value * self.size[0] / self.max
  119. rect = pygame.Rect(0, 0, pos_pixel, self.size[1])
  120. self.surface.fill(self.main_color, rect)