component.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import pygame
  2. from game.sound import sound_player
  3. class PygameButton:
  4. """
  5. 文字渲染按钮
  6. """
  7. def __init__(self, screen, text, relx, rely, font_size, bg_color, fg_color=(0,0,0), action=None, anchor='center', is_bold=True, fix_shape=None, bg_alpha=255):
  8. self.screen = screen
  9. self.text = text
  10. self.lines = text.split('\n')
  11. self.font = pygame.font.SysFont("Microsoft JhengHei", font_size, bold=is_bold)
  12. self.bg_color_normal = pygame.Color(bg_color)
  13. # # 计算 hover 颜色:加深 20%
  14. # h = self.bg_color_normal
  15. # self.bg_color_hover = pygame.Color(max(0, int(h.r*0.8)), max(0, int(h.g*0.8)), max(0, int(h.b*0.8)))
  16. self.bg_color_normal.a = bg_alpha
  17. h = self.bg_color_normal
  18. hover_rgb = (
  19. max(0, int(h.r * 0.8)),
  20. max(0, int(h.g * 0.8)),
  21. max(0, int(h.b * 0.8)),
  22. )
  23. self.bg_color_hover = pygame.Color(*hover_rgb)
  24. self.bg_color_hover.a = bg_alpha
  25. self.fg_color = fg_color
  26. self.action = action
  27. self.relx = relx
  28. self.rely = rely
  29. self.fix_shape = fix_shape
  30. self.anchor = anchor # 'center', 'topleft'
  31. self.hovered = False
  32. self._render()
  33. def _render(self):
  34. # 渲染多行文本
  35. self.text_surfs = [self.font.render(line, True, self.fg_color) for line in self.lines]
  36. widths = [surf.get_width() for surf in self.text_surfs]
  37. heights = [surf.get_height() for surf in self.text_surfs]
  38. if self.fix_shape is None:
  39. total_width = max(widths)
  40. total_height = sum(heights) + (len(heights)-1) * 5
  41. else:
  42. total_width = self.fix_shape[0]
  43. total_height = self.fix_shape[1]
  44. sw, sh = self.screen.get_size()
  45. x = int(sw * self.relx)
  46. y = int(sh * self.rely)
  47. # 根据锚点决定位置
  48. if self.anchor == 'center':
  49. topleft = (x - total_width//2 - 10, y - total_height//2 - 5)
  50. elif self.anchor == 'topleft':
  51. topleft = (x, y)
  52. else:
  53. topleft = (x - total_width//2 - 10, y - total_height//2 - 5)
  54. self.bg_rect = pygame.Rect(topleft[0], topleft[1], total_width + 20, total_height + 10)
  55. # 计算每行文本位置
  56. self.text_positions = []
  57. offset_y = self.bg_rect.y + 5
  58. for surf in self.text_surfs:
  59. line_x = self.bg_rect.x + (self.bg_rect.width - surf.get_width())//2 if self.anchor=='center' else self.bg_rect.x + 10
  60. self.text_positions.append((line_x, offset_y))
  61. offset_y += surf.get_height() + 5
  62. def draw(self):
  63. # 绘制背景
  64. # color = self.bg_color_hover if self.hovered else self.bg_color_normal
  65. # pygame.draw.rect(self.screen, color, self.bg_rect)
  66. color = self.bg_color_hover if self.hovered else self.bg_color_normal
  67. bg_surf = pygame.Surface(
  68. (self.bg_rect.width, self.bg_rect.height),
  69. pygame.SRCALPHA # 允许 per-pixel alpha
  70. )
  71. bg_surf.fill(color) # color 中已包含 a 通道
  72. self.screen.blit(bg_surf, self.bg_rect.topleft)
  73. # 绘制文本
  74. for surf, pos in zip(self.text_surfs, self.text_positions):
  75. self.screen.blit(surf, pos)
  76. def handle_event(self, event):
  77. if event.type == pygame.MOUSEMOTION:
  78. self.hovered = self.bg_rect.collidepoint(event.pos)
  79. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  80. if self.hovered:
  81. if self.action:
  82. self.action()
  83. sound_player.play("uiLeave")
  84. class PygameButtonBackIMG:
  85. """
  86. 一个仅使用图片作为按钮的类,不含文字渲染,图片本身包含文字。
  87. - default_img_path: 默认状态下的背景图路径
  88. - hover_img_path: 悬停状态下的背景图路径
  89. """
  90. def __init__(self, screen, relx, rely,
  91. default_img_path, hover_img_path,
  92. action=None, anchor='center'):
  93. self.screen = screen
  94. # 加载按钮图片
  95. self.default_img = pygame.image.load(default_img_path).convert_alpha()
  96. self.hover_img = pygame.image.load(hover_img_path).convert_alpha()
  97. self.action = action
  98. self.relx = relx
  99. self.rely = rely
  100. self.anchor = anchor # 'center' 或 'topleft'
  101. self.hovered = False
  102. # 按钮尺寸使用图片原始尺寸
  103. self.width, self.height = self.default_img.get_size()
  104. # 计算按钮位置
  105. sw, sh = self.screen.get_size()
  106. x = int(sw * self.relx)
  107. y = int(sh * self.rely)
  108. if self.anchor == 'center':
  109. topleft = (x - self.width//2, y - self.height//2)
  110. else:
  111. topleft = (x, y)
  112. self.bg_rect = pygame.Rect(topleft[0], topleft[1], self.width, self.height)
  113. def draw(self):
  114. # 根据悬停状态绘制对应图片
  115. img = self.hover_img if self.hovered else self.default_img
  116. self.screen.blit(img, self.bg_rect.topleft)
  117. def handle_event(self, event):
  118. if event.type == pygame.MOUSEMOTION:
  119. self.hovered = self.bg_rect.collidepoint(event.pos)
  120. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  121. if self.hovered and self.action:
  122. sound_player.play("uiLeave")
  123. self.action()