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