Repaired tthe display problem for settings menu

This commit is contained in:
eichhornchen 2020-11-11 14:56:00 +01:00
parent d9b7db742a
commit e3d28409f5
3 changed files with 28 additions and 5 deletions

View File

@ -11,7 +11,6 @@ class MenuDisplay(Display):
def update_menu(self, menu: Menu) -> None:
self.menu = menu
self.values = [str(a) for a in menu.values]
self.trueheight = len(self.values)
self.truewidth = max([len(a) for a in self.values])
@ -54,6 +53,10 @@ class MenuDisplay(Display):
def preferred_height(self) -> int:
return self.trueheight + 2
@property
def values(self):
return [str(a) for a in self.menu.values]
class MainMenuDisplay(Display):
def __init__(self, menu: MainMenu, *args):

View File

@ -37,9 +37,11 @@ class Game:
"""
self.state = GameMode.MAINMENU
self.main_menu = menus.MainMenu()
self.settings_menu = menus.SettingsMenu()
self.settings = Settings()
self.settings.load_settings()
self.settings.write_settings()
self.settings_menu.update_values(self.settings)
def new_game(self) -> None:
"""
@ -70,7 +72,7 @@ class Game:
screen.refresh()
self.display_refresh()
key = screen.getkey()
self.handle_key_pressed(self.translate_key(key))
self.handle_key_pressed(self.translate_key(key), screen)
def translate_key(self, key: str) -> KeyValues:
"""
@ -93,7 +95,7 @@ class Game:
elif key == ' ':
return KeyValues.SPACE
def handle_key_pressed(self, key: KeyValues) -> None:
def handle_key_pressed(self, key: KeyValues, screen) -> None:
"""
Indicates what should be done when the given key is pressed,
according to the current game state.
@ -103,7 +105,7 @@ class Game:
elif self.state == GameMode.MAINMENU:
self.handle_key_pressed_main_menu(key)
elif self.state == GameMode.SETTINGS:
self.handle_key_pressed_settings(key)
self.handle_key_pressed_settings(key,screen)
self.display_refresh()
def handle_key_pressed_play(self, key: KeyValues) -> None:
@ -142,9 +144,20 @@ class Game:
elif option == menus.MainMenuValues.EXIT:
sys.exit(0)
def handle_key_pressed_settings(self, key: KeyValues) -> None:
def handle_key_pressed_settings(self, key: KeyValues, screen : Any) -> None:
"""
For now, in the settings mode, we can only go backwards.
"""
if key == KeyValues.SPACE:
self.state = GameMode.MAINMENU
if key == KeyValues.DOWN:
self.settings_menu.go_down()
if key == KeyValues.UP:
self.settings_menu.go_up()
if key == KeyValues.ENTER:
option = self.settings_menu.validate().split(": ")[0]
if option != "TEXTURE_PACK":
newkey = screen.getkey()
self.settings.__setattr__(option, newkey)
self.settings.write_settings()
self.settings_menu.update_values(self.settings)

View File

@ -1,5 +1,6 @@
from enum import Enum
from typing import Any
from .settings import Settings
class Menu:
@ -30,6 +31,12 @@ class MainMenuValues(Enum):
class MainMenu(Menu):
values = [e for e in MainMenuValues]
class SettingsMenu(Menu) :
def __init__(self):
super().__init__()
def update_values(self, settings : Settings):
s = settings.dumps_to_string()
self.values = s[6:-2].replace("\"","").split(",\n ")
class ArbitraryMenu(Menu):
def __init__(self, values: list):