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
|
2020-11-11 22:48:46 +00:00
|
|
|
from .enums import GameMode, KeyValues, DisplayActions
|
2020-11-11 13:56:00 +00:00
|
|
|
from .settings import Settings
|
2020-11-06 15:40:43 +00:00
|
|
|
|
|
|
|
|
2020-11-06 15:20:07 +00:00
|
|
|
class Menu:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
A Menu object is the logical representation of a menu in the game
|
|
|
|
"""
|
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:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Moves the pointer of the menu on the previous value
|
|
|
|
"""
|
2020-11-06 17:11:59 +00:00
|
|
|
self.position = max(0, self.position - 1)
|
|
|
|
|
|
|
|
def go_down(self) -> None:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Moves the pointer of the menu on the next value
|
|
|
|
"""
|
2020-11-06 17:11:59 +00:00
|
|
|
self.position = min(len(self.values) - 1, self.position + 1)
|
|
|
|
|
2020-11-06 17:25:02 +00:00
|
|
|
def validate(self) -> Any:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Selects the value that is pointed by the menu pointer
|
|
|
|
"""
|
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-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Values of the main menu
|
|
|
|
"""
|
2020-11-08 21:59:11 +00:00
|
|
|
START = 'Jouer'
|
2020-11-16 00:01:18 +00:00
|
|
|
SAVE = 'Sauvegarder'
|
|
|
|
LOAD = 'Charger'
|
2020-11-08 21:59:11 +00:00
|
|
|
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-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
A special instance of a menu : the main 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 19:36:43 +00:00
|
|
|
|
|
|
|
class SettingsMenu(Menu):
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
A special instance of a menu : the settings menu
|
|
|
|
"""
|
2020-11-11 21:36:42 +00:00
|
|
|
waiting_for_key: bool = False
|
2020-11-11 19:36:43 +00:00
|
|
|
|
|
|
|
def update_values(self, settings: Settings) -> None:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
The settings can change, so they are updated
|
|
|
|
"""
|
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
|
|
|
"""
|
2020-11-18 11:19:27 +00:00
|
|
|
In the setting menu, we van select a setting and change it
|
2020-11-11 21:22:33 +00:00
|
|
|
"""
|
2020-11-11 21:36:42 +00:00
|
|
|
if not self.waiting_for_key:
|
2020-11-12 01:03:08 +00:00
|
|
|
# Navigate normally through the menu.
|
2020-11-11 22:10:49 +00:00
|
|
|
if key == KeyValues.SPACE or \
|
|
|
|
key == KeyValues.ENTER and \
|
|
|
|
self.position == len(self.values) - 1:
|
2020-11-12 01:03:08 +00:00
|
|
|
# Go back
|
2020-11-11 22:48:46 +00:00
|
|
|
game.display_actions(DisplayActions.UPDATE)
|
2020-11-11 21:36:42 +00:00
|
|
|
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-12 01:03:08 +00:00
|
|
|
# Change a setting
|
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:
|
2020-11-11 21:36:42 +00:00
|
|
|
self.waiting_for_key = True
|
2020-11-11 22:00:45 +00:00
|
|
|
self.update_values(game.settings)
|
2020-11-11 21:36:42 +00:00
|
|
|
else:
|
2020-11-11 22:00:45 +00:00
|
|
|
option = list(game.settings.settings_keys)[self.position]
|
2020-11-12 01:03:08 +00:00
|
|
|
# Don't use an already mapped key
|
|
|
|
if any(getattr(game.settings, opt) == raw_key
|
|
|
|
for opt in game.settings.settings_keys if opt != option):
|
|
|
|
return
|
2020-11-11 21:36:42 +00:00
|
|
|
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)
|