touch_text_manager.py 4.9 KB

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