Changed settings menu to let the display decide how the settings are printed

This commit is contained in:
Nicolas Margulies 2020-11-13 15:40:44 +01:00
parent eca6b9af1f
commit aa81d455f6
3 changed files with 11 additions and 20 deletions

View File

@ -1,7 +1,7 @@
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 +18,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

@ -59,6 +59,10 @@ class MenuDisplay(Display):
def values(self) -> List[str]: def values(self) -> List[str]:
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):

View File

@ -57,21 +57,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 = [] self.values = list(settings.__dict__.items())
for i, key in enumerate(settings.settings_keys): self.values.append(("RETURN",["","Retour"]))
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:
@ -90,9 +77,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(
@ -103,7 +90,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):