-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e2d08e9
commit fd723de
Showing
4 changed files
with
137 additions
and
13 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,129 @@ | ||
from typing import List, Optional, Any, Dict | ||
from typing import List, Optional, Dict, Tuple | ||
from collections import OrderedDict | ||
|
||
import pygame | ||
|
||
from pygame.event import EventType | ||
from pygame.surface import Surface | ||
from pygame.rect import Rect | ||
|
||
from view.action import Action | ||
from view.action import Action, ActionType | ||
from view.view import View | ||
from view import colors | ||
|
||
|
||
class OptionsMenu(View): | ||
def __init__( | ||
self, available: Any, output_dataset: Dict[str, Any], next: Optional[int] = None | ||
) -> None: | ||
def __init__(self, tracks: Dict[str, Action]) -> None: | ||
super().__init__() | ||
pygame.font.init() | ||
self.font = pygame.font.SysFont("Verdana", 30) | ||
self.main_font = pygame.font.SysFont("comicsansms", 60) | ||
self.selected_item = 0 | ||
self._options = OrderedDict(tracks) | ||
self.font_color = colors.WHITE | ||
self.button_highlight = colors.GREEN | ||
self.button_color = colors.LIGHTGRAY | ||
self._background: Optional[Tuple[Surface, Tuple[int, int]]] = None | ||
self.background_image: Optional[Surface] = None | ||
self.divider = 0.4 | ||
self.selected_action: Optional[Action] = None | ||
self.img = pygame.image.load("resources/graphics/track1.jpg") | ||
self.logo_image = pygame.image.load("resources/graphics/logo.png") | ||
|
||
def draw(self, destination: Surface, events: List[EventType], delta_time: float) -> Optional[Action]: | ||
size = destination.get_size() | ||
self._update_geometry(size) | ||
|
||
if self._process_events(events): | ||
return self.selected_action | ||
|
||
if self._background: | ||
destination.blit(*self._background) | ||
if self._logo: | ||
destination.blit(*self._logo) | ||
|
||
mini_track = self._button_rect | ||
thumbnail_image = pygame.transform.scale(self.img, (mini_track.w, mini_track.h)) | ||
thumbnail = (thumbnail_image, mini_track) | ||
destination.blit(*thumbnail) | ||
|
||
main_label = self.main_font.render("Select track", True, self.font_color) | ||
destination.blit(main_label, (size[0] // 2 - 185, size[1] // 10)) | ||
|
||
track_label = self.font.render( | ||
list(self._options.keys())[self.selected_item], True, self.font_color | ||
) | ||
destination.blit( | ||
track_label, | ||
( | ||
mini_track.centerx - (track_label.get_height() // 2) - 28, | ||
mini_track.centery - (mini_track.size[1] // 2) - 68, | ||
), | ||
) | ||
|
||
info_label = self.font.render("PRESS ENTER TO START", True, self.font_color) | ||
destination.blit(info_label, (size[0] - 380, size[1] - 40)) | ||
|
||
left_arrow_points = ( | ||
(self.ofset_x - size[0] // 20, self.ofset_y + mini_track.h // 2), | ||
(self.ofset_x - 10, self.ofset_y + mini_track.h // 2 - size[0] // 40), | ||
(self.ofset_x - 10, self.ofset_y + mini_track.h // 2 + size[0] // 40), | ||
) | ||
right_arrow_points = ( | ||
(3 * self.ofset_x + size[0] // 20, self.ofset_y + mini_track.h // 2), | ||
(3 * self.ofset_x + 10, self.ofset_y + mini_track.h // 2 - size[0] // 40), | ||
(3 * self.ofset_x + 10, self.ofset_y + mini_track.h // 2 + size[0] // 40), | ||
) | ||
|
||
if self.selected_item != 0: | ||
pygame.draw.polygon(destination, self.font_color, left_arrow_points) | ||
if self.selected_item != len(self._options) - 1: | ||
pygame.draw.polygon(destination, self.font_color, right_arrow_points) | ||
return None | ||
|
||
def _update_geometry(self, size: Tuple[int, int]) -> None: | ||
self.ofset_y = int(self.divider * size[1]) | ||
self.ofset_x = size[0] // 4 | ||
|
||
self.button_dims = size[0] // 2, size[1] // 2 | ||
|
||
background_shape = Rect((0, 0), size) | ||
background_image = pygame.transform.scale(self.background_image, size) | ||
self._background = (background_image, background_shape) | ||
self._button_rect = Rect((self.ofset_x, self.ofset_y), self.button_dims) | ||
|
||
if not self.logo_image: | ||
return | ||
|
||
logo_image = pygame.transform.scale( | ||
self.logo_image, (size[0] // 6, size[1] // 12) | ||
) | ||
logo_shape = Rect((10, 10), (size[0] // 8, size[1] // 8)) | ||
self._logo = (logo_image, logo_shape) | ||
|
||
def draw( | ||
self, destination: Surface, events: List[EventType], delta_time: float | ||
) -> Optional[Action]: | ||
pass | ||
def _process_events(self, events: List[EventType]) -> bool: | ||
for event in events: | ||
if event.type == pygame.KEYDOWN: | ||
if event.key == pygame.K_LEFT and self.selected_item != 0: | ||
self.selected_item = self.selected_item - 1 | ||
elif ( | ||
event.key == pygame.K_RIGHT | ||
and self.selected_item != len(self._options) - 1 | ||
): | ||
self.selected_item = self.selected_item + 1 | ||
elif event.key == pygame.K_a and self.selected_item != 0: | ||
self.selected_item = self.selected_item - 1 | ||
elif ( | ||
event.key == pygame.K_d | ||
and self.selected_item != len(self._options) - 1 | ||
): | ||
self.selected_item = self.selected_item + 1 | ||
elif event.key == pygame.K_RETURN: | ||
self.selected_action = list(self._options.values())[ | ||
self.selected_item | ||
] | ||
return True | ||
elif event.key == pygame.K_ESCAPE: | ||
self.selected_action = Action(ActionType.CHANGE_VIEW, 0) | ||
return True | ||
return False |