Key handler does not depend on curses's screen anymore
This commit is contained in:
parent
748561e87d
commit
0c17e74d6a
@ -1,4 +1,5 @@
|
||||
from enum import Enum, auto
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from dungeonbattle.settings import Settings
|
||||
|
||||
@ -19,23 +20,25 @@ class KeyValues(Enum):
|
||||
SPACE = auto()
|
||||
|
||||
@staticmethod
|
||||
def translate_key(key: str, settings: Settings) -> "KeyValues":
|
||||
def translate_key(key: str, settings: Settings) \
|
||||
-> Tuple[Optional["KeyValues"], str]:
|
||||
"""
|
||||
Translate the raw string key into an enum value that we can use.
|
||||
"""
|
||||
if key in (settings.KEY_DOWN_SECONDARY,
|
||||
settings.KEY_DOWN_PRIMARY):
|
||||
return KeyValues.DOWN
|
||||
return KeyValues.DOWN, key
|
||||
elif key in (settings.KEY_LEFT_PRIMARY,
|
||||
settings.KEY_LEFT_SECONDARY):
|
||||
return KeyValues.LEFT
|
||||
return KeyValues.LEFT, key
|
||||
elif key in (settings.KEY_RIGHT_PRIMARY,
|
||||
settings.KEY_RIGHT_SECONDARY):
|
||||
return KeyValues.RIGHT
|
||||
return KeyValues.RIGHT, key
|
||||
elif key in (settings.KEY_UP_PRIMARY,
|
||||
settings.KEY_UP_SECONDARY):
|
||||
return KeyValues.UP
|
||||
return KeyValues.UP, key
|
||||
elif key == settings.KEY_ENTER:
|
||||
return KeyValues.ENTER
|
||||
return KeyValues.ENTER, key
|
||||
elif key == ' ':
|
||||
return KeyValues.SPACE
|
||||
return KeyValues.SPACE, key
|
||||
return None, key
|
||||
|
@ -53,9 +53,10 @@ class Game:
|
||||
screen.refresh()
|
||||
self.display_refresh()
|
||||
key = screen.getkey()
|
||||
self.handle_key_pressed(KeyValues.translate_key(key, self.settings))
|
||||
self.handle_key_pressed(
|
||||
*KeyValues.translate_key(key, self.settings))
|
||||
|
||||
def handle_key_pressed(self, key: KeyValues) -> None:
|
||||
def handle_key_pressed(self, key: KeyValues, raw_key: str = '') -> None:
|
||||
"""
|
||||
Indicates what should be done when the given key is pressed,
|
||||
according to the current game state.
|
||||
@ -65,7 +66,7 @@ class Game:
|
||||
elif self.state == GameMode.MAINMENU:
|
||||
self.main_menu.handle_key_pressed(key, self)
|
||||
elif self.state == GameMode.SETTINGS:
|
||||
self.settings_menu.handle_key_pressed(key, self.settings)
|
||||
self.settings_menu.handle_key_pressed(key, raw_key, self)
|
||||
self.display_refresh()
|
||||
|
||||
def handle_key_pressed_play(self, key: KeyValues) -> None:
|
||||
|
@ -53,30 +53,34 @@ class MainMenu(Menu):
|
||||
|
||||
|
||||
class SettingsMenu(Menu):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
waiting_for_key: bool = False
|
||||
|
||||
def update_values(self, settings: Settings) -> None:
|
||||
s = settings.dumps_to_string()
|
||||
self.values = s[6:-2].replace("\"", "").split(",\n ")
|
||||
|
||||
def handle_key_pressed(self, key: KeyValues, settings: Settings) -> None:
|
||||
def handle_key_pressed(self, key: KeyValues, raw_key: str, game: 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.go_down()
|
||||
if key == KeyValues.UP:
|
||||
self.go_up()
|
||||
if key == KeyValues.ENTER:
|
||||
if not self.waiting_for_key:
|
||||
if key == KeyValues.SPACE:
|
||||
game.state = GameMode.MAINMENU
|
||||
if key == KeyValues.DOWN:
|
||||
self.go_down()
|
||||
if key == KeyValues.UP:
|
||||
self.go_up()
|
||||
if key == KeyValues.ENTER:
|
||||
option = self.validate().split(": ")[0]
|
||||
if option != "TEXTURE_PACK":
|
||||
self.waiting_for_key = True
|
||||
else:
|
||||
option = self.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)
|
||||
setattr(game.settings, option, raw_key)
|
||||
game.settings.write_settings()
|
||||
self.update_values(game.settings)
|
||||
self.waiting_for_key = False
|
||||
|
||||
|
||||
class ArbitraryMenu(Menu):
|
||||
|
@ -7,6 +7,7 @@ from dungeonbattle.display.display_manager import DisplayManager
|
||||
from dungeonbattle.entities.player import Player
|
||||
from dungeonbattle.game import Game, KeyValues, GameMode
|
||||
from dungeonbattle.menus import MainMenuValues
|
||||
from dungeonbattle.settings import Settings
|
||||
|
||||
|
||||
class TestGame(unittest.TestCase):
|
||||
@ -36,25 +37,39 @@ class TestGame(unittest.TestCase):
|
||||
"""
|
||||
Test key bindings.
|
||||
"""
|
||||
self.assertEqual(self.game.translate_key(
|
||||
self.game.settings.KEY_UP_PRIMARY), KeyValues.UP)
|
||||
self.assertEqual(self.game.translate_key(
|
||||
self.game.settings.KEY_UP_SECONDARY), KeyValues.UP)
|
||||
self.assertEqual(self.game.translate_key(
|
||||
self.game.settings.KEY_DOWN_PRIMARY), KeyValues.DOWN)
|
||||
self.assertEqual(self.game.translate_key(
|
||||
self.game.settings.KEY_DOWN_SECONDARY), KeyValues.DOWN)
|
||||
self.assertEqual(self.game.translate_key(
|
||||
self.game.settings.KEY_LEFT_PRIMARY), KeyValues.LEFT)
|
||||
self.assertEqual(self.game.translate_key(
|
||||
self.game.settings.KEY_LEFT_SECONDARY), KeyValues.LEFT)
|
||||
self.assertEqual(self.game.translate_key(
|
||||
self.game.settings.KEY_RIGHT_PRIMARY), KeyValues.RIGHT)
|
||||
self.assertEqual(self.game.translate_key(
|
||||
self.game.settings.KEY_RIGHT_SECONDARY), KeyValues.RIGHT)
|
||||
self.assertEqual(self.game.translate_key(
|
||||
self.game.settings.KEY_ENTER), KeyValues.ENTER)
|
||||
self.assertEqual(self.game.translate_key(' '), KeyValues.SPACE)
|
||||
self.game.settings = Settings()
|
||||
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_UP_PRIMARY, self.game.settings)[0],
|
||||
KeyValues.UP)
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_UP_SECONDARY, self.game.settings)[0],
|
||||
KeyValues.UP)
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_DOWN_PRIMARY, self.game.settings)[0],
|
||||
KeyValues.DOWN)
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_DOWN_SECONDARY, self.game.settings)[0],
|
||||
KeyValues.DOWN)
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_LEFT_PRIMARY, self.game.settings)[0],
|
||||
KeyValues.LEFT)
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_LEFT_SECONDARY, self.game.settings)[0],
|
||||
KeyValues.LEFT)
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_RIGHT_PRIMARY, self.game.settings)[0],
|
||||
KeyValues.RIGHT)
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_RIGHT_SECONDARY, self.game.settings)[0],
|
||||
KeyValues.RIGHT)
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_ENTER, self.game.settings)[0],
|
||||
KeyValues.ENTER)
|
||||
self.assertEqual(KeyValues.translate_key(' ', self.game.settings)[0],
|
||||
KeyValues.SPACE)
|
||||
self.assertEqual(KeyValues.translate_key('plop', self.game.settings)[0],
|
||||
None)
|
||||
|
||||
def test_key_press(self) -> None:
|
||||
"""
|
||||
|
@ -19,7 +19,7 @@ class TestInterfaces(unittest.TestCase):
|
||||
Try to load a map from a file.
|
||||
"""
|
||||
m = Map.load("resources/example_map.txt")
|
||||
self.assertEqual(m.width, 44)
|
||||
self.assertEqual(m.width, 52)
|
||||
self.assertEqual(m.height, 17)
|
||||
|
||||
def test_tiles(self) -> None:
|
||||
|
Loading…
Reference in New Issue
Block a user