126 lines
5.1 KiB
Python
126 lines
5.1 KiB
Python
# ui/hud.py
|
|
"""Handles HUD elements and text overlays."""
|
|
|
|
import pygame
|
|
from config.constants import *
|
|
|
|
|
|
class HUD:
|
|
def __init__(self):
|
|
self.font = pygame.font.Font("freesansbold.ttf", FONT_SIZE)
|
|
self.legend_font = pygame.font.Font("freesansbold.ttf", LEGEND_FONT_SIZE)
|
|
|
|
def render_mouse_position(self, screen, camera):
|
|
"""Render mouse position in top left."""
|
|
mouse_x, mouse_y = camera.get_real_coordinates(*pygame.mouse.get_pos())
|
|
mouse_text = self.font.render(f"Mouse: ({mouse_x:.2f}, {mouse_y:.2f})", True, WHITE)
|
|
text_rect = mouse_text.get_rect()
|
|
text_rect.topleft = (HUD_MARGIN, HUD_MARGIN)
|
|
screen.blit(mouse_text, text_rect)
|
|
|
|
def render_fps(self, screen, clock):
|
|
"""Render FPS in top right."""
|
|
fps_text = self.font.render(f"FPS: {int(clock.get_fps())}", True, WHITE)
|
|
fps_rect = fps_text.get_rect()
|
|
fps_rect.topright = (SCREEN_WIDTH - HUD_MARGIN, HUD_MARGIN)
|
|
screen.blit(fps_text, fps_rect)
|
|
|
|
def render_tps(self, screen, actual_tps):
|
|
"""Render TPS in bottom right."""
|
|
tps_text = self.font.render(f"TPS: {actual_tps}", True, WHITE)
|
|
tps_rect = tps_text.get_rect()
|
|
tps_rect.bottomright = (SCREEN_WIDTH - HUD_MARGIN, SCREEN_HEIGHT - HUD_MARGIN)
|
|
screen.blit(tps_text, tps_rect)
|
|
|
|
def render_tick_count(self, screen, total_ticks):
|
|
"""Render total tick count in bottom left."""
|
|
tick_text = self.font.render(f"Ticks: {total_ticks}", True, WHITE)
|
|
tick_rect = tick_text.get_rect()
|
|
tick_rect.bottomleft = (HUD_MARGIN, SCREEN_HEIGHT - HUD_MARGIN)
|
|
screen.blit(tick_text, tick_rect)
|
|
|
|
def render_pause_indicator(self, screen, is_paused):
|
|
"""Render pause indicator when paused."""
|
|
if is_paused:
|
|
pause_text = self.font.render("Press 'Space' to unpause", True, WHITE)
|
|
pause_rect = pause_text.get_rect()
|
|
pause_rect.center = (SCREEN_WIDTH // 2, 20)
|
|
screen.blit(pause_text, pause_rect)
|
|
|
|
def render_selected_objects_info(self, screen, selected_objects):
|
|
"""Render information about selected objects."""
|
|
if len(selected_objects) < 1:
|
|
return
|
|
|
|
max_width = SCREEN_WIDTH - 20
|
|
i = 0
|
|
|
|
for obj in selected_objects:
|
|
text = f"Object: {str(obj)}"
|
|
words = text.split()
|
|
line = ""
|
|
line_offset = 0
|
|
|
|
for word in words:
|
|
test_line = f"{line} {word}".strip()
|
|
test_width, _ = self.font.size(test_line)
|
|
|
|
if test_width > max_width and line:
|
|
obj_text = self.font.render(line, True, WHITE)
|
|
obj_rect = obj_text.get_rect()
|
|
obj_rect.topleft = (HUD_MARGIN, 30 + i * LINE_HEIGHT + line_offset)
|
|
screen.blit(obj_text, obj_rect)
|
|
line = word
|
|
line_offset += LINE_HEIGHT
|
|
else:
|
|
line = test_line
|
|
|
|
if line:
|
|
obj_text = self.font.render(line, True, WHITE)
|
|
obj_rect = obj_text.get_rect()
|
|
obj_rect.topleft = (HUD_MARGIN, 30 + i * LINE_HEIGHT + line_offset)
|
|
screen.blit(obj_text, obj_rect)
|
|
|
|
i += 1
|
|
|
|
def render_legend(self, screen, showing_legend):
|
|
"""Render the controls legend."""
|
|
if not showing_legend:
|
|
legend_text = self.legend_font.render("Press 'L' to show controls", True, WHITE)
|
|
legend_rect = legend_text.get_rect()
|
|
legend_rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 20)
|
|
screen.blit(legend_text, legend_rect)
|
|
return
|
|
|
|
# Split into two columns
|
|
mid = (len(KEYMAP_LEGEND) + 1) // 2
|
|
left_col = KEYMAP_LEGEND[:mid]
|
|
right_col = KEYMAP_LEGEND[mid:]
|
|
|
|
legend_font_height = self.legend_font.get_height()
|
|
column_gap = 40 # Space between columns
|
|
|
|
# Calculate max width for each column
|
|
left_width = max(self.legend_font.size(f"{k}: {v}")[0] for k, v in left_col)
|
|
right_width = max(self.legend_font.size(f"{k}: {v}")[0] for k, v in right_col)
|
|
legend_width = left_width + right_width + column_gap
|
|
legend_height = max(len(left_col), len(right_col)) * legend_font_height + 10
|
|
|
|
legend_x = (SCREEN_WIDTH - legend_width) // 2
|
|
legend_y = SCREEN_HEIGHT - legend_height - 10
|
|
|
|
# Draw left column
|
|
for i, (key, desc) in enumerate(left_col):
|
|
text = self.legend_font.render(f"{key}: {desc}", True, WHITE)
|
|
text_rect = text.get_rect()
|
|
text_rect.left = legend_x
|
|
text_rect.top = legend_y + 5 + i * legend_font_height
|
|
screen.blit(text, text_rect)
|
|
|
|
# Draw right column
|
|
for i, (key, desc) in enumerate(right_col):
|
|
text = self.legend_font.render(f"{key}: {desc}", True, WHITE)
|
|
text_rect = text.get_rect()
|
|
text_rect.left = legend_x + left_width + column_gap
|
|
text_rect.top = legend_y + 5 + i * legend_font_height
|
|
screen.blit(text, text_rect) |