diff options
author | Jonas Bernard <public.jbernard@web.de> | 2021-04-24 04:19:39 +0200 |
---|---|---|
committer | Jonas Bernard <public.jbernard@web.de> | 2021-04-24 04:19:39 +0200 |
commit | a0a40e9259b9e8be4e677c8c29dfdfb89eb81ce8 (patch) | |
tree | 47768e46da3e6682acf6b72dddabaef78d0bfeb8 /txtgameengine/input/keyboard.py | |
parent | 635185baac3a2a9c5e50971d994ca7bcdec1d495 (diff) | |
download | txtgameengine-a0a40e9259b9e8be4e677c8c29dfdfb89eb81ce8.tar.gz txtgameengine-a0a40e9259b9e8be4e677c8c29dfdfb89eb81ce8.tar.bz2 txtgameengine-a0a40e9259b9e8be4e677c8c29dfdfb89eb81ce8.zip |
Progress at night
Diffstat (limited to 'txtgameengine/input/keyboard.py')
-rw-r--r-- | txtgameengine/input/keyboard.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/txtgameengine/input/keyboard.py b/txtgameengine/input/keyboard.py new file mode 100644 index 0000000..1938016 --- /dev/null +++ b/txtgameengine/input/keyboard.py @@ -0,0 +1,52 @@ +from enum import IntFlag +import glfw +import typing +from dataclasses import dataclass + +if typing.TYPE_CHECKING: + from ..scenes import Scene + + SceneLike = typing.TypeAlias('SceneLike', typing.Union[Scene, typing.Type[Scene]]) + + +class ModKey(IntFlag): + SHIFT = glfw.MOD_SHIFT + CONTROL = glfw.MOD_CONTROL + ALT = glfw.MOD_ALT + SUPER = glfw.MOD_SUPER + + +@dataclass +class KeyboardEvent: + window: typing.Any + keycode: int + scancode: int + action: int + mod_keys: int + + def has_modkey(self, modkey: ModKey): + return bool(self.mod_keys & modkey) + + +class KeyboardCallbackBuilder: + def __init__(self, app): + self.app = app + self.scene = None + self.required_mod_keys = ModKey(0) + + def build(self, callback: typing.Callable[[KeyboardEvent], None]): + KeyboardCallback(self.required_mod_keys, callback) + + def only_in_scene(self, scene: 'SceneLike'): + self.scene = scene + return self + + def with_mod_key(self, mod_key: ModKey): + self.required_mod_keys |= mod_key + return self + + +class KeyboardCallback: + def __init__(self, required_mod_keys, callback: typing.Callable[[KeyboardEvent], None]): + self.required_mod_keys = required_mod_keys + self.callback = callback |