squirrel-battle/dungeonbattle/menus.py

114 lines
3.6 KiB
Python
Raw Normal View History

2020-11-11 21:22:33 +00:00
import sys
2020-11-08 22:26:54 +00:00
from enum import Enum
2020-11-11 21:45:15 +00:00
from typing import Any, Optional
2020-11-11 21:22:33 +00:00
2020-11-11 22:09:15 +00:00
from .display.texturepack import TexturePack
from .enums import GameMode, KeyValues, DisplayActions
from .settings import Settings
2020-11-06 15:40:43 +00:00
2020-11-06 15:20:07 +00:00
class Menu:
2020-11-06 17:11:59 +00:00
values: list
2020-11-06 15:40:43 +00:00
def __init__(self):
2020-11-06 15:20:07 +00:00
self.position = 0
2020-11-06 17:11:59 +00:00
def go_up(self) -> None:
self.position = max(0, self.position - 1)
def go_down(self) -> None:
self.position = min(len(self.values) - 1, self.position + 1)
2020-11-06 17:25:02 +00:00
def validate(self) -> Any:
2020-11-06 15:40:43 +00:00
return self.values[self.position]
2020-11-06 17:11:59 +00:00
2020-11-06 15:40:43 +00:00
class MainMenuValues(Enum):
2020-11-08 21:59:11 +00:00
START = 'Jouer'
SETTINGS = 'Paramètres'
EXIT = 'Quitter'
2020-11-06 15:40:43 +00:00
2020-11-10 19:22:53 +00:00
def __str__(self):
return self.value
2020-11-06 17:11:59 +00:00
2020-11-06 15:40:43 +00:00
class MainMenu(Menu):
2020-11-06 17:11:59 +00:00
values = [e for e in MainMenuValues]
2020-11-06 15:40:43 +00:00
2020-11-11 21:22:33 +00:00
def handle_key_pressed(self, key: KeyValues, game: Any) -> None:
"""
In the main menu, we can navigate through options.
"""
if key == KeyValues.DOWN:
self.go_down()
if key == KeyValues.UP:
self.go_up()
if key == KeyValues.ENTER:
option = self.validate()
if option == MainMenuValues.START:
game.state = GameMode.PLAY
elif option == MainMenuValues.SETTINGS:
game.state = GameMode.SETTINGS
elif option == MainMenuValues.EXIT:
sys.exit(0)
2020-11-11 19:36:43 +00:00
class SettingsMenu(Menu):
waiting_for_key: bool = False
2020-11-11 19:36:43 +00:00
def update_values(self, settings: Settings) -> None:
2020-11-11 22:00:45 +00:00
self.values = []
for i, key in enumerate(settings.settings_keys):
s = settings.get_comment(key)
s += " : "
if self.waiting_for_key and i == self.position:
2020-11-11 22:09:15 +00:00
s += "?"
2020-11-11 22:00:45 +00:00
else:
s += getattr(settings, key).replace("\n", "\\n")
2020-11-11 22:09:15 +00:00
s += 8 * " " # Write over old text
2020-11-11 22:00:45 +00:00
self.values.append(s)
2020-11-11 22:09:15 +00:00
self.values.append("")
self.values.append("Changer le pack de textures n'aura effet")
self.values.append("qu'après avoir relancé le jeu.")
2020-11-11 22:10:49 +00:00
self.values.append("")
self.values.append("Retour (espace)")
2020-11-11 19:36:43 +00:00
2020-11-11 21:45:15 +00:00
def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str,
game: Any) -> None:
2020-11-11 21:22:33 +00:00
"""
For now, in the settings mode, we can only go backwards.
"""
if not self.waiting_for_key:
2020-11-11 22:10:49 +00:00
if key == KeyValues.SPACE or \
key == KeyValues.ENTER and \
self.position == len(self.values) - 1:
game.display_actions(DisplayActions.UPDATE)
game.state = GameMode.MAINMENU
if key == KeyValues.DOWN:
self.go_down()
if key == KeyValues.UP:
self.go_up()
2020-11-11 22:09:15 +00:00
if key == KeyValues.ENTER and self.position < len(self.values) - 3:
2020-11-11 22:00:45 +00:00
option = list(game.settings.settings_keys)[self.position]
2020-11-11 22:09:15 +00:00
if option == "TEXTURE_PACK":
game.settings.TEXTURE_PACK = \
TexturePack.get_next_pack_name(
game.settings.TEXTURE_PACK)
game.settings.write_settings()
self.update_values(game.settings)
else:
self.waiting_for_key = True
2020-11-11 22:00:45 +00:00
self.update_values(game.settings)
else:
2020-11-11 22:00:45 +00:00
option = list(game.settings.settings_keys)[self.position]
setattr(game.settings, option, raw_key)
game.settings.write_settings()
self.waiting_for_key = False
2020-11-11 22:00:45 +00:00
self.update_values(game.settings)
2020-11-11 21:22:33 +00:00
2020-11-06 15:40:43 +00:00
2020-11-06 17:32:51 +00:00
class ArbitraryMenu(Menu):
2020-11-06 17:11:59 +00:00
def __init__(self, values: list):
2020-11-06 17:32:51 +00:00
super().__init__()
2020-11-06 17:11:59 +00:00
self.values = values