-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinputManager.py
53 lines (44 loc) · 1.59 KB
/
inputManager.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
#################
# Input Manager #
#################
import queue
#from enums import PlayerState
from utils import Singleton
#from eventManager import Events, EventManager
from collections import deque
from framework import Keys, Events
@Singleton
class __InputManagerClass:
def __init__(self):
self._keyFuncDict = {}
self.keysPressed = []
self._keysQueued = deque()
Events.subscribe(Events.KEYDOWN, self.addKey)
Events.subscribe(Events.KEYUP, self.removeKey)
def subscribe(self, keys, func):
for keyEnum in keys:
key = keyEnum.value
if self._keyFuncDict.get(key, False) is False:
self._keyFuncDict[key] = []
self._keyFuncDict[key].append(func)
#print("Listen For Called", key, str(func))
def unsubscribe(self, keys, func):
for keyEnum in keys:
key = keyEnum.value
self._keyFuncDict[key].remove(func)
#print("Stop Listening Called", key, str(func))
def handleInput(self):
for key in self.keysPressed:
self._keysQueued.append(key)
for key in self._keysQueued:
if key in self._keyFuncDict:
for func in self._keyFuncDict[key]:
func(Keys(key)) # Convert the value back to an enum with Keys(#)
self._keysQueued.clear()
def addKey(self, key):
if key not in self.keysPressed:
self.keysPressed.append(key)
def removeKey(self, key):
if key in self.keysPressed:
self.keysPressed.remove(key)
InputManager = __InputManagerClass()