import sys import pygame import os from .input import Input from .command import Command from .game import Game import logging import json WHITE = (255, 255, 255) BLACK = (0, 0, 0) IS_DEVELOPMENT_MODE = os.getenv("DEVELOPMENT", "").lower() != "" OPTIONS = Game.all() + ["Quit"] logger = logging.getLogger(__name__) WIDTH = HEIGHT = FONT = screen = selected_index = None def init_display(): global WIDTH, HEIGHT, FONT, screen, selected_index WIDTH, HEIGHT = ( pygame.display.Info().current_w // 2, pygame.display.Info().current_h // 2, ) FONT = pygame.font.Font(None, 36) screen = pygame.display.set_mode( (WIDTH, HEIGHT) if IS_DEVELOPMENT_MODE else (0, 0), 0 if IS_DEVELOPMENT_MODE else pygame.FULLSCREEN, ) selected_index = 0 def draw_menu(): screen.fill(WHITE) for i, option in enumerate(OPTIONS): text = FONT.render(option, True, BLACK if i != selected_index else (200, 0, 0)) screen.blit(text, (WIDTH // 2 - text.get_width() // 2, 150 + i * 50)) pygame.display.flip() def process_events(serial, instance, is_joint_mode): global selected_index input_handler = Input(debug=IS_DEVELOPMENT_MODE) for event in pygame.event.get(): if event.type == pygame.QUIT: return {"command": Command.QUIT.value, "action": None, "value": None} action = input_handler.get_input(event) if action == "UP": selected_index = (selected_index - 1) % len(OPTIONS) elif action == "DOWN": selected_index = (selected_index + 1) % len(OPTIONS) elif action == "SELECT": if OPTIONS[selected_index] == "Quit": if instance == "primary": return { "command": Command.QUIT.value, "action": None, "value": None, } else: pygame.quit() sys.exit() else: return { "command": Command.SELECT_GAME.value, "action": None, "value": OPTIONS[selected_index], } elif action == "QUIT": return {"command": Command.QUIT.value, "action": None, "value": None} if ( event.type == pygame.KEYDOWN and event.key == pygame.K_b and instance == "primary" and IS_DEVELOPMENT_MODE ): is_joint_mode[0] = not is_joint_mode[0] logger.info(f"Toggled joint mode to {is_joint_mode[0]}") command = ( Command.ENTER_JOINT_MODE.value if is_joint_mode[0] else Command.EXIT_JOINT_MODE.value ) serial.write(json.dumps({"command": command}).encode("utf-8")) return None