Merge branch 'settings-menu' into 'master'

Settings menu

See merge request ynerant/dungeon-battle!13
This commit is contained in:
ynerant 2020-11-19 01:24:24 +01:00
commit 78059f0399
3 changed files with 21 additions and 26 deletions

View File

@ -1,7 +1,8 @@
import curses import curses
from dungeonbattle.display.mapdisplay import MapDisplay from dungeonbattle.display.mapdisplay import MapDisplay
from dungeonbattle.display.statsdisplay import StatsDisplay from dungeonbattle.display.statsdisplay import StatsDisplay
from dungeonbattle.display.menudisplay import MenuDisplay, MainMenuDisplay from dungeonbattle.display.menudisplay import SettingsMenuDisplay, \
MainMenuDisplay
from dungeonbattle.display.texturepack import TexturePack from dungeonbattle.display.texturepack import TexturePack
from typing import Any from typing import Any
from dungeonbattle.game import Game, GameMode from dungeonbattle.game import Game, GameMode
@ -18,7 +19,7 @@ class DisplayManager:
self.statsdisplay = StatsDisplay(screen, pack) self.statsdisplay = StatsDisplay(screen, pack)
self.mainmenudisplay = MainMenuDisplay(self.game.main_menu, self.mainmenudisplay = MainMenuDisplay(self.game.main_menu,
screen, pack) screen, pack)
self.settingsmenudisplay = MenuDisplay(screen, pack) self.settingsmenudisplay = SettingsMenuDisplay(screen, pack)
self.displays = [self.statsdisplay, self.mapdisplay, self.displays = [self.statsdisplay, self.mapdisplay,
self.mainmenudisplay, self.settingsmenudisplay] self.mainmenudisplay, self.settingsmenudisplay]
self.update_game_components() self.update_game_components()

View File

@ -60,22 +60,32 @@ class MenuDisplay(Display):
return [str(a) for a in self.menu.values] return [str(a) for a in self.menu.values]
class SettingsMenuDisplay(MenuDisplay):
@property
def values(self) -> List[str]:
return [a[1][1] + (" : "
+ ("?" if self.menu.waiting_for_key else a[1][0])
if a[1][0] else "") for a in self.menu.values]
class MainMenuDisplay(Display): class MainMenuDisplay(Display):
def __init__(self, menu: MainMenu, *args): def __init__(self, menu: MainMenu, *args):
super().__init__(*args) super().__init__(*args)
self.menu = menu self.menu = menu
self.pad = self.newpad(self.rows, self.cols)
with open("resources/ascii_art.txt", "r") as file: with open("resources/ascii_art.txt", "r") as file:
self.title = file.read().split("\n") self.title = file.read().split("\n")
self.pad = self.newpad(max(self.rows, len(self.title) + 30),
max(len(self.title[0]) + 5, self.cols))
self.menudisplay = MenuDisplay(self.screen, self.pack) self.menudisplay = MenuDisplay(self.screen, self.pack)
self.menudisplay.update_menu(self.menu) self.menudisplay.update_menu(self.menu)
def display(self) -> None: def display(self) -> None:
for i in range(len(self.title)): for i in range(len(self.title)):
self.pad.addstr(4 + i, self.width // 2 self.pad.addstr(4 + i, max(self.width // 2
- len(self.title[0]) // 2 - 1, self.title[i]) - len(self.title[0]) // 2 - 1, 0), self.title[i])
self.pad.refresh(0, 0, self.y, self.x, self.height, self.width) self.pad.refresh(0, 0, self.y, self.x, self.height, self.width)
menuwidth = min(self.menudisplay.preferred_width, self.width) menuwidth = min(self.menudisplay.preferred_width, self.width)
menuy, menux = len(self.title) + 8, self.width // 2 - menuwidth // 2 - 1 menuy, menux = len(self.title) + 8, self.width // 2 - menuwidth // 2 - 1

View File

@ -62,24 +62,8 @@ class SettingsMenu(Menu):
waiting_for_key: bool = False waiting_for_key: bool = False
def update_values(self, settings: Settings) -> None: def update_values(self, settings: Settings) -> None:
""" self.values = list(settings.__dict__.items())
The settings can change, so they are updated self.values.append(("RETURN", ["", "Retour"]))
"""
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:
s += "?"
else:
s += getattr(settings, key).replace("\n", "\\n")
s += 8 * " " # Write over old text
self.values.append(s)
self.values.append("")
self.values.append("Changer le pack de textures n'aura effet")
self.values.append("qu'après avoir relancé le jeu.")
self.values.append("")
self.values.append("Retour (espace)")
def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str, def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str,
game: Any) -> None: game: Any) -> None:
@ -98,9 +82,9 @@ class SettingsMenu(Menu):
self.go_down() self.go_down()
if key == KeyValues.UP: if key == KeyValues.UP:
self.go_up() self.go_up()
if key == KeyValues.ENTER and self.position < len(self.values) - 3: if key == KeyValues.ENTER and self.position < len(self.values) - 1:
# Change a setting # Change a setting
option = list(game.settings.settings_keys)[self.position] option = self.values[self.position][0]
if option == "TEXTURE_PACK": if option == "TEXTURE_PACK":
game.settings.TEXTURE_PACK = \ game.settings.TEXTURE_PACK = \
TexturePack.get_next_pack_name( TexturePack.get_next_pack_name(
@ -111,7 +95,7 @@ class SettingsMenu(Menu):
self.waiting_for_key = True self.waiting_for_key = True
self.update_values(game.settings) self.update_values(game.settings)
else: else:
option = list(game.settings.settings_keys)[self.position] option = self.values[self.position][0]
# Don't use an already mapped key # Don't use an already mapped key
if any(getattr(game.settings, opt) == raw_key if any(getattr(game.settings, opt) == raw_key
for opt in game.settings.settings_keys if opt != option): for opt in game.settings.settings_keys if opt != option):