2021-05-11 20:30:01 +02:00
|
|
|
import pygame
|
|
|
|
from ui.button import Button
|
2021-05-12 14:33:26 +02:00
|
|
|
from ui.input_box import InputBox
|
2021-05-11 20:30:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-05-12 14:33:26 +02:00
|
|
|
def run_all(self, window, mouse_position, events):
|
|
|
|
for component in filter(lambda x: x not in self.selectable_ui_components, self.ui_components):
|
|
|
|
component.draw(window)
|
|
|
|
|
|
|
|
for component in filter(lambda x: isinstance(x, Button), self.selectable_ui_components):
|
|
|
|
component.draw(window, mouse_position)
|
|
|
|
|
|
|
|
for component in filter(lambda x: isinstance(x, InputBox), self.selectable_ui_components):
|
|
|
|
component.run(window, mouse_position, events)
|
|
|
|
|
2021-05-11 20:30:01 +02:00
|
|
|
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)
|