squirrel-battle/squirrelbattle/menus.py

181 lines
5.2 KiB
Python
Raw Permalink Normal View History

2021-01-10 09:46:17 +00:00
# Copyright (C) 2020-2021 by ÿnérant, eichhornchen, nicomarg, charlse
2020-11-27 15:33:17 +00:00
# SPDX-License-Identifier: GPL-3.0-or-later
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
2021-01-10 10:25:53 +00:00
from .entities.friendly import Chest, Merchant
2020-12-04 15:28:37 +00:00
from .entities.player import Player
2021-01-10 10:25:53 +00:00
from .enums import DisplayActions, GameMode, KeyValues
from .settings import Settings
2020-11-28 00:59:52 +00:00
from .translations import gettext as _, Translator
2020-11-06 15:40:43 +00:00
2020-11-06 15:20:07 +00:00
class Menu:
"""
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:
"""
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:
"""
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:
"""
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):
"""
Values of the main menu.
"""
2020-11-27 20:44:17 +00:00
START = "New game"
RESUME = "Resume"
SAVE = "Save"
LOAD = "Load"
SETTINGS = "Settings"
EXIT = "Exit"
2020-11-06 15:40:43 +00:00
2020-11-10 19:22:53 +00:00
def __str__(self):
2020-11-27 20:44:17 +00:00
return _(self.value)
2020-11-10 19:22:53 +00:00
2020-11-06 17:11:59 +00:00
2020-11-06 15:40:43 +00:00
class MainMenu(Menu):
"""
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):
"""
A special instance of a menu : the settings menu.
"""
waiting_for_key: bool = False
2020-11-11 19:36:43 +00:00
def update_values(self, settings: Settings) -> None:
self.values = list(settings.__dict__.items())
2020-11-27 19:42:19 +00:00
self.values.append(("RETURN", ["", _("Back")]))
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
"""
In the setting menu, we can select a setting and change it.
2020-11-11 21:22:33 +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
game.display_actions(DisplayActions.UPDATE)
game.state = GameMode.MAINMENU
if key == KeyValues.DOWN:
self.go_down()
if key == KeyValues.UP:
self.go_up()
if key == KeyValues.ENTER and self.position < len(self.values) - 1:
2020-11-12 01:03:08 +00:00
# Change a setting
option = self.values[self.position][0]
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)
2020-11-27 19:53:24 +00:00
elif option == "LOCALE":
game.settings.LOCALE = 'fr' if game.settings.LOCALE == 'en'\
2020-12-07 00:01:39 +00:00
else 'de' if game.settings.LOCALE == 'fr' else 'es' \
if game.settings.LOCALE == 'de' else 'en'
2020-11-28 00:59:52 +00:00
Translator.setlocale(game.settings.LOCALE)
2020-11-27 19:53:24 +00:00
game.settings.write_settings()
self.update_values(game.settings)
2020-11-11 22:09:15 +00:00
else:
self.waiting_for_key = True
2020-11-11 22:00:45 +00:00
self.update_values(game.settings)
else:
option = self.values[self.position][0]
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
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-12-04 13:41:59 +00:00
class InventoryMenu(Menu):
"""
A special instance of a menu : the menu for the inventory of the player.
"""
2020-12-04 15:28:37 +00:00
player: Player
def update_player(self, player: Player) -> None:
"""
Updates the player.
"""
2020-12-04 15:28:37 +00:00
self.player = player
@property
def values(self) -> list:
"""
Returns the values of the menu.
"""
2020-12-04 15:28:37 +00:00
return self.player.inventory
2020-12-07 20:22:06 +00:00
class StoreMenu(Menu):
"""
A special instance of a menu : the menu for the inventory of a merchant.
"""
2020-12-18 01:17:06 +00:00
merchant: Merchant = None
def update_merchant(self, merchant: Merchant) -> None:
"""
Updates the merchant.
"""
self.merchant = merchant
@property
def values(self) -> list:
"""
Returns the values of the menu.
"""
2020-12-18 01:17:06 +00:00
return self.merchant.inventory if self.merchant else []
class ChestMenu(Menu):
"""
A special instance of a menu : the menu for the inventory of a chest.
"""
chest: Chest = None
def update_chest(self, chest: Chest) -> None:
"""
Updates the player.
"""
self.chest = chest
@property
def values(self) -> list:
"""
Returns the values of the menu.
"""
return self.chest.inventory if self.chest else []