From 0e98e1f866392b7629ae398de271fae5f45c5cb0 Mon Sep 17 00:00:00 2001 From: Matej Straka Date: Thu, 3 Oct 2024 09:57:21 +0200 Subject: [PATCH] refactor: Add Enums for keybindings --- generals/gui/event_handler.py | 52 +++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/generals/gui/event_handler.py b/generals/gui/event_handler.py index 8946c8a..4c6d1f1 100644 --- a/generals/gui/event_handler.py +++ b/generals/gui/event_handler.py @@ -1,18 +1,23 @@ import pygame +from enum import Enum from pygame.event import Event from abc import abstractmethod from .properties import Properties from generals.core import config as c -# keybindings # -RIGHT = pygame.K_RIGHT -LEFT = pygame.K_LEFT -SPACE = pygame.K_SPACE -Q = pygame.K_q -R = pygame.K_r -H = pygame.K_h -L = pygame.K_l + +class Keybindings(Enum): + ### General ### + Q = pygame.K_q # Quit the game + + ### Replay ### + RIGHT = pygame.K_RIGHT # Increase speed + LEFT = pygame.K_LEFT # Decrease speed + SPACE = pygame.K_SPACE # Pause + R = pygame.K_r # Restart + L = pygame.K_l # Move forward one frame + H = pygame.K_h # Move back one frame class Command: @@ -103,20 +108,21 @@ def __init__(self, properties: Properties): self.command = ReplayCommand() def handle_key_event(self, event: Event) -> ReplayCommand: - if event.key == Q: - self.command.quit = True - elif event.key == RIGHT: - self.command.speed_change = 2.0 - elif event.key == LEFT: - self.command.speed_change = 0.5 - elif event.key == SPACE: - self.command.pause_toggle = True - elif event.key == R: - self.command.restart = True - elif event.key == H: - self.command.frame_change = -1 - elif event.key == L: - self.command.frame_change = 1 + match event.key: + case Keybindings.Q.value: + self.command.quit = True + case Keybindings.RIGHT.value: + self.command.speed_change = 2.0 + case Keybindings.LEFT.value: + self.command.speed_change = 0.5 + case Keybindings.SPACE.value: + self.command.pause_toggle = True + case Keybindings.R.value: + self.command.restart = True + case Keybindings.H.value: + self.command.frame_change = -1 + case Keybindings.L.value: + self.command.frame_change = 1 return self.command def handle_mouse_event(self) -> None: @@ -144,7 +150,7 @@ def __init__(self, properties: Properties): self.command = TrainCommand() def handle_key_event(self, event: Event) -> TrainCommand: - if event.key == Q: + if event.key == Keybindings.Q.value: self.command.quit = True return self.command