47 lines
2.1 KiB
Python
47 lines
2.1 KiB
Python
import pygame
|
|
from ui.button import Button
|
|
|
|
|
|
class UiComponentsList:
|
|
selected = 0
|
|
|
|
def __init__(self, ui_components=None, select_first_item=True):
|
|
if ui_components is not None:
|
|
self.ui_components = ui_components
|
|
self.selectable_ui_components = list()
|
|
|
|
for component in ui_components:
|
|
self.selectable_ui_components.append(component) if issubclass(component.__class__, Button) else 0
|
|
|
|
else:
|
|
self.ui_components = list()
|
|
|
|
self.selectable_ui_components[0].set_is_selected(select_first_item)
|
|
|
|
def draw_all(self, window, mouse_position):
|
|
for component in filter(lambda x: x not in self.selectable_ui_components, self.ui_components):
|
|
component.draw(window)
|
|
|
|
for component in self.selectable_ui_components:
|
|
component.draw(window, mouse_position)
|
|
|
|
def switch_selected_objects_with_arrow_keys(self, events, mouse_position):
|
|
for event in events:
|
|
if event.type == pygame.KEYDOWN:
|
|
if event.key == pygame.K_ESCAPE:
|
|
self.selectable_ui_components[self.selected].set_is_selected(False)
|
|
|
|
if event.key == pygame.MOUSEBUTTONDOWN:
|
|
selected_component = self.selectable_ui_components[self.selected]
|
|
selected_component.set_is_selected(selected_component.is_over(mouse_position))
|
|
|
|
if event.key == pygame.K_UP or event.key == pygame.K_LEFT:
|
|
self.selectable_ui_components[self.selected].set_is_selected(False)
|
|
self.selected = self.selected - 1 if self.selected != 0 else len(self.selectable_ui_components) - 1
|
|
self.selectable_ui_components[self.selected].set_is_selected(True)
|
|
|
|
if event.key == pygame.K_DOWN or event.key == pygame.K_RIGHT:
|
|
self.selectable_ui_components[self.selected].set_is_selected(False)
|
|
self.selected = (self.selected + 1) % len(self.selectable_ui_components)
|
|
self.selectable_ui_components[self.selected].set_is_selected(True)
|