screen_objects.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import pygame
  2. import logging
  3. logger = logging.getLogger(__name__)
  4. class ScreenObjectsManager():
  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. def get_touch_objects_in_pos(self, pos):
  27. objects = []
  28. for key in self.touch_objects:
  29. if self.touch_objects[key].is_pos_inside(pos):
  30. objects.append(key)
  31. return objects
  32. class BaseItem():
  33. def __init__(self,pos,pos2):
  34. self.pos = pos
  35. self.pos2 = pos2
  36. self.size=[0,0]
  37. self.size[0] = self.pos2[0] - self.pos[0]
  38. self.size[1] = self.pos2[1] - self.pos[1]
  39. self.rect = pygame.Rect(0,0,self.size[0],self.size[1])
  40. self.rect_in_pos = pygame.Rect(self.pos[0],self.pos[1],self.size[0],self.size[1])
  41. class TextItem(BaseItem):
  42. def __init__(self, text, pos,pos2, color,text_size):
  43. if pos2 is not None:
  44. BaseItem.__init__(self,pos,pos2)
  45. self.text_size = text_size
  46. self.font = pygame.font.SysFont("arial", text_size)
  47. self.text = text
  48. self.color = color
  49. self.box = self.font.render(text, True, self.color)
  50. if pos2 is not None:
  51. if self.pos[0] + self.box.get_rect().width > pos2[0]:
  52. self.fit_horizontal = False
  53. self.text = self.text + " "
  54. self.original_text = self.text
  55. self.step = 0
  56. else:
  57. self.fit_horizontal = True
  58. if self.pos[1] + self.box.get_rect().height > pos2[1]:
  59. self.fit_vertical = False
  60. else:
  61. self.fit_vertical = True
  62. else:
  63. BaseItem.__init__(self,pos,(pos[0]+self.box.get_rect().width,pos[1]+self.box.get_rect().height))
  64. self.fit_horizontal = True
  65. self.fit_vertical = True
  66. def update(self):
  67. if not self.fit_horizontal:
  68. if self.text == self.original_text:
  69. if self.step > 90:
  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. elif self.step > 5:
  77. self.step = 0
  78. new_text = self.text[1:]
  79. new_text = new_text + self.text[:1]
  80. self.text = new_text
  81. else:
  82. self.step = self.step + 1
  83. def render(self,surface):
  84. if self.fit_horizontal:
  85. self.box
  86. else:
  87. self.box = self.font.render(self.text, True, self.color)
  88. surface.blit(self.box,self.pos,area=self.rect)
  89. def set_text(self, text):
  90. self.__init__(text,self.pos,self.pos2,self.color,self.text_size)
  91. class TouchObject(BaseItem):
  92. def __init__(self,pos,pos2):
  93. BaseItem.__init__(self,pos,pos2)
  94. self.active = False
  95. self.background_box = pygame.Surface(self.size)
  96. self.background_box.fill((0,128,255))
  97. def render(self,surface):
  98. surface.blit(self.background_box, self.pos)
  99. def is_pos_inside(self, pos):
  100. return self.rect_in_pos.collidepoint(pos)
  101. class TouchAndTextItem(TouchObject, TextItem):
  102. def __init__(self, text, pos, color,text_size):
  103. TextItem.__init__(self,text, pos,None, color,text_size)
  104. TouchObject.__init__(self,pos,self.pos2)
  105. def update(self):
  106. TextItem.update()
  107. def render(self,surface):
  108. TouchObject.render(self,surface)
  109. TextItem.render(self,surface)
  110. class Progressbar(TouchObject):
  111. def __init__(self, pos, pos2, max):
  112. BaseItem.__init__(self, pos, pos2)
  113. logger.error(pos2)
  114. self.value = 0
  115. self.max = max
  116. self.back_color = (0,0,0,128)
  117. self.main_color = (255,255,255)
  118. self.surface = pygame.Surface(self.size, pygame.SRCALPHA)
  119. self.surface.fill(self.back_color)
  120. def update(self):
  121. pass
  122. def render(self, surface):
  123. surface.blit(self.surface, self.pos)
  124. def set_value(self, value):
  125. self.value = value
  126. self.surface.fill(self.back_color)
  127. pos_pixel = value * self.size[0] / self.max
  128. rect = pygame.Rect(0,0,pos_pixel,self.size[1])
  129. self.surface.fill(self.main_color, rect)
  130. def get_pos_value(self, pos):
  131. x = pos[0] - self.pos[0]
  132. return x * self.max / self.size[0]