Skip to content

Commit

Permalink
Select track UI implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
mareksmorag committed Jun 10, 2020
1 parent e2d08e9 commit fd723de
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 13 deletions.
Binary file added resources/graphics/track1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 17 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from view.menu import Menu
from view.track_view import TrackView
from view.window import Window
from view.options_menu import OptionsMenu

WINDOW_NAME = "CarsML"
WINDOW_SIZE = (1280, 800)
Expand Down Expand Up @@ -68,8 +69,7 @@ def main() -> None:
window = Window(WINDOW_NAME, WINDOW_SIZE, resizable=True, min_size=WINDOW_MIN_SIZE)

menu_options = {
"Track": Action(ActionType.CHANGE_VIEW, 1),
"Testing segment": Action(ActionType.CHANGE_VIEW, 2),
"Train": Action(ActionType.CHANGE_VIEW, 1),
"Exit": Action(ActionType.SYS_EXIT),
}
menu = Menu(menu_options)
Expand All @@ -78,10 +78,23 @@ def main() -> None:

tv1 = TrackView(Track.from_points(tracks[1]["points"]))
tv2 = TrackView(Track.from_points(tracks[3]["points"]))
tv3 = TrackView(Track.from_points(tracks[4]["points"]))

track_options = {
"Track1": Action(ActionType.CHANGE_VIEW, 2),
"Track2": Action(ActionType.CHANGE_VIEW, 3),
"Track3": Action(ActionType.CHANGE_VIEW, 4),
}
options_menu = OptionsMenu(tracks=track_options)
options_menu.background_image = pygame.image.load(
"resources/graphics/menu-background.png"
)

window.add_view(menu, 0, True)
window.add_view(tv1, 1)
window.add_view(tv2, 2)
window.add_view(tv1, 2)
window.add_view(tv2, 3)
window.add_view(tv3, 4)
window.add_view(options_menu, 1)

window.run()

Expand Down
1 change: 1 addition & 0 deletions src/view/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
LIGHTBLUE = (120, 140, 170)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BIZARRE_MASKING_PURPLE = (240, 0, 240)
LIME = (50, 205, 50)
128 changes: 119 additions & 9 deletions src/view/options_menu.py
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

0 comments on commit fd723de

Please sign in to comment.