54 lines
2.1 KiB
Python
54 lines
2.1 KiB
Python
|
import pygame
|
||
|
|
||
|
from level.Level import Level
|
||
|
from ui_elements.ColoredRectangleUiElement import ColoredRectangleUiElement
|
||
|
from ui_elements.ClickEvent import ClickEvent
|
||
|
from ui_elements.TextLabel import TextLabel
|
||
|
|
||
|
|
||
|
class LevelMenu:
|
||
|
|
||
|
def __init__(self, levels: [Level], x_pos: float, y_pos: float, width: float, height: float, column_count: int):
|
||
|
super().__init__()
|
||
|
self.levels = levels
|
||
|
self.level_select_listener = lambda selected_level: None
|
||
|
self.level_text_labels = []
|
||
|
self.width = width
|
||
|
self.height = height
|
||
|
self.column_count = column_count
|
||
|
|
||
|
self.position = (x_pos, y_pos)
|
||
|
|
||
|
self.create_level_text_labels()
|
||
|
self.background = ColoredRectangleUiElement(self.width, self.height, (100, 100, 100, 180))
|
||
|
self.background.position_scale.position = self.position
|
||
|
|
||
|
def create_level_text_labels(self):
|
||
|
current_text_label_position = [self.position[0], self.position[1]]
|
||
|
|
||
|
for index, level in enumerate(self.levels):
|
||
|
text_label = TextLabel(level.name,
|
||
|
current_text_label_position[0],
|
||
|
current_text_label_position[1],
|
||
|
60)
|
||
|
|
||
|
text_label.position_scale.scale = (0.5, 0.5)
|
||
|
text_label.add_click_listener(
|
||
|
lambda click_event, selected_level=level:
|
||
|
self.handle_level_text_label_click(click_event, selected_level)
|
||
|
)
|
||
|
self.level_text_labels.append(text_label)
|
||
|
|
||
|
if (index + 1) % self.column_count == 0:
|
||
|
current_text_label_position[0] = self.position[0]
|
||
|
current_text_label_position[1] += self.height / (len(self.levels) / self.column_count)
|
||
|
else:
|
||
|
current_text_label_position[0] += self.width / self.column_count
|
||
|
|
||
|
def handle_level_text_label_click(self, click_event: ClickEvent, level: Level):
|
||
|
if click_event.event.type == pygame.MOUSEBUTTONUP:
|
||
|
self.level_select_listener(level)
|
||
|
|
||
|
def extract_level(self):
|
||
|
return self
|