-
Notifications
You must be signed in to change notification settings - Fork 2
/
EventHandler.py
62 lines (52 loc) · 2.07 KB
/
EventHandler.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
import pygame
class EventHandler(object):
__soundboard = None
__file_map = dict()
__multiply_modifier = False
__profile_modifier = False
__pressed_stop = False
def __init__(self, soundboard, file_map: dict):
self.__soundboard = soundboard
self.__file_map = file_map
def handle(self, event_list: list):
"""
Handles the event contained in the passed event list
"""
for event in event_list:
if event.type == pygame.KEYDOWN:
self.__handle_key_down_event(event)
elif event.type == pygame.KEYUP:
self.__handle_key_up_event(event)
elif event.type == pygame.QUIT:
self.__soundboard.close()
def __handle_key_down_event(self, event):
"""
Handles the events of type keydown
"""
if event.key == pygame.K_ESCAPE:
self.__soundboard.close()
elif event.key == pygame.K_KP_DIVIDE:
self.__profile_modifier = True
elif event.key == pygame.K_KP_MULTIPLY:
self.__multiply_modifier = True
elif event.key == pygame.K_KP_PERIOD:
self.__pressed_stop = True
if event.key in self.__file_map and not self.__profile_modifier:
if self.__multiply_modifier:
self.__soundboard.play(self.__file_map[event.key], loop=True)
else:
self.__soundboard.play(self.__file_map[event.key], loop=False)
if self.__multiply_modifier and self.__pressed_stop:
self.__soundboard.stop_all_sounds()
def __handle_key_up_event(self, event):
"""
Handles the events of type keyup
"""
if self.__profile_modifier and event.key in self.__file_map:
self.__soundboard.use_profile(self.__file_map[event.key])
if event.key == pygame.K_KP_PERIOD:
self.__pressed_stop = False
elif event.key == pygame.K_KP_MULTIPLY:
self.__multiply_modifier = False
elif event.key == pygame.K_KP_DIVIDE:
self.__profile_modifier = False