-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.py
153 lines (91 loc) · 2.71 KB
/
Input.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from numpy import ndarray
from STL import Enum
from STL import Copy
from STL import Arrays
from STL import Typing
from OSPlatform import Platform
import LAL
EKeyCode = Platform.EKeyCode
class EState () :
None_ = 0
Released = 1
Pressed = 2
Held = 3
CKeys_NumTracked = 6
class KeySignals :
Up = False
Down = False
Left = False
Right = False
Enter = False
Tab = False
def GetViaIndex(self, _index) :
switcher = {
0 : self.Up,
1 : self.Down,
2 : self.Left,
3 : self.Right,
4 : self.Enter,
5 : self.Tab
}
return switcher.get(_index, -1)
@staticmethod
def GetIndexViaKeyCode(_key) :
switcher = {
EKeyCode.Arrow_Up : 0,
EKeyCode.Arrow_Down : 1,
EKeyCode.Arrow_Left : 2,
EKeyCode.Arrow_Right : 3,
EKeyCode.Enter : 4,
EKeyCode.Tab : 5,
}
return switcher.get(_key, -1)
@staticmethod
def GetKeyCodeViaIndex(_index) :
switcher = {
0 : EKeyCode.Arrow_Up,
1 : EKeyCode.Arrow_Down,
2 : EKeyCode.Arrow_Left,
3 : EKeyCode.Arrow_Right,
4 : EKeyCode.Enter,
5 : EKeyCode.Tab
}
return switcher.get(_index, -1)
__CurrentSignalState = KeySignals()
__PreviousSignalState = KeySignals()
__KeyStates = Arrays.empty(CKeys_NumTracked, dtype = EState )
__KeyEventSubs = Arrays.empty(CKeys_NumTracked, dtype = ndarray)
def Update() :
global __CurrentSignalState, __PreviousSignalState, __KeyStates, __KeyEventSubs
__PreviousSignalState = Copy.copy(__CurrentSignalState)
for index in range(CKeys_NumTracked) :
current = __CurrentSignalState.GetViaIndex(index)
current = Platform.GetKeySignal(__CurrentSignalState.GetKeyCodeViaIndex(index))
previous = __PreviousSignalState.GetViaIndex(index)
currentState = __KeyStates[index]
latestState = EState.None_
if current == previous :
if current == True : latestState = EState.Held
elif currentState != EState.Held : latestState = EState.None_
else :
if current == False : latestState = EState.Released
else : latestState = EState.Pressed
if latestState != currentState :
currentState = latestState
if __KeyEventSubs[index] == None : return
for sub in __KeyEventSubs[index] :
if sub != None : sub()
def Subscribe(_key = EKeyCode, _callback = Typing.Callable) :
global __KeyEventSubs
subs = __KeyEventSubs[KeySignals.GetIndexViaKeyCode(_key)]
for sub in subs :
if sub == None :
sub = _callback
return
subs = Arrays.append(subs, _callback)
def Unsubscribe(_key = EKeyCode, _callback = Typing.Callable) :
global __KeyEventSubs
subs = __KeyEventSubs[KeySignals.GetIndexViaKeyCode(_key)]
for sub in subs :
if sub == _callback :
sub = None