-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.py
135 lines (97 loc) · 3.12 KB
/
events.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import pygame
from pygame.locals import *
__calls_down_event = {}
__calls_up_event = {}
__mouse_down_event = {}
__mouse_up_event = {}
# ==== Mouse input ==== #
def handle_mouse_input(game_event: pygame.event):
if not (game_event.type == MOUSEBUTTONDOWN
or game_event.type == MOUSEBUTTONDOWN):
return
button = button_to_str(game_event.button)
if game_event.type == MOUSEBUTTONDOWN:
__notify_mouse_button_down(button)
elif game_event.type == MOUSEBUTTONUP:
__notify_mouse_button_up(button)
def button_to_str(button: int) -> str:
if button == 1:
return "left"
elif button == 2:
return "middle"
elif button == 3:
return "right"
def add_mouse_button_down_event(key: str, function):
if key in __calls_down_event:
__calls_down_event[key].append(function)
else:
__calls_down_event[key] = [function]
def add_mouse_button_up_event(key: str, function):
if key in __calls_up_event:
__calls_up_event[key].append(function)
else:
__calls_up_event[key] = [function]
def remove_mouse_button_down_event(button: str, function):
__mouse_down_event[button].remove(function)
def remove_mouse_button_up_event(button: str, function):
__mouse_down_event[button].remove(function)
def __notify_mouse_button_down(button: str):
if button not in __mouse_down_event:
return
for f in __mouse_down_event[button]:
f()
def __notify_mouse_button_up(button: str):
if button not in __mouse_up_event:
return
for f in __mouse_up_event[button]:
f()
# ==== Keyboard input ==== #
def handle_keyboard_input(game_event: pygame.event):
if not (game_event.type == KEYDOWN or game_event.type == KEYUP):
return
key = key_to_str(game_event.key)
if game_event.type == KEYDOWN:
__notify_key_down(key)
elif game_event.type == KEYUP:
__notify_key_up(key)
def key_to_str(key: int) -> str:
if key >= 33 and key<=126:
return chr(key)
elif key == 32:
return "space"
elif key == 1073742050:
return "alt"
elif key == 1073742048:
return "ctrl"
elif key == 1073742049:
return "shift"
elif key == 1073741881:
return "caps"
elif key == 9:
return "tab"
#TODO
return "other"
def add_key_down_event(key: str, function):
if key in __calls_down_event:
__calls_down_event[key].append(function)
else:
__calls_down_event[key] = [function]
def add_key_up_event(key: str, function):
if key in __calls_up_event:
__calls_up_event[key].append(function)
else:
__calls_up_event[key] = [function]
def remove_key_down_event(key: str, function):
__calls_down_event[key].remove(function)
def remove_key_up_event(key: str, function):
__calls_up_event[key].remove(function)
def __notify_key_down(key: str):
if key not in __calls_down_event:
return
for f in __calls_down_event[key]:
f()
def __notify_key_up(key: str):
if key not in __calls_up_event:
return
for f in __calls_up_event[key]:
f()