summaryrefslogtreecommitdiff
path: root/txtgameengine/input/keyboard.py
blob: 1938016a569043edb2557cdc8f5fd43e8fedb85c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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