-
Notifications
You must be signed in to change notification settings - Fork 2
/
KeyChecker.py
executable file
·219 lines (165 loc) · 7.72 KB
/
KeyChecker.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from Tkinter import *
from time import *
from InputChecker import *
class KeyChecker(Frame, InputChecker):
def __init__( self, slapKeys, hipKeys, lightPins):
Frame.__init__( self )
self.slapKeys = slapKeys
self.hipKeys = hipKeys
self.lightPins = lightPins
keys = []
for playerKeys in slapKeys:
keys.append(playerKeys[0])
keys.append(playerKeys[1])
keys.append(hipKeys[0])
keys.append(hipKeys[1])
InputChecker.__init__(self, keys, [])
self.keysPressed = dict()
for key in keys:
self.keysPressed[key] = True
def pressCb(key): self.graphicsCallback(key, True)
def releaseCb(key): self.graphicsCallback(key, False)
self.addPressCallback(pressCb)
self.addReleaseCallback(releaseCb)
self.pack( expand = YES, fill = BOTH )
self.master.title( "BeatDown Debug" )
self.master.geometry( "640x480" )
self.message1 = StringVar()
self.line1 = Label( self, textvariable = self.message1 )
self.message1.set( "Type any key or shift" )
self.line1.pack()
self.message2 = StringVar()
self.line2 = Label( self, textvariable = self.message2 )
self.message2.set( "" )
self.line2.pack()
self.master.bind( "<KeyPress>", self.keyPressed_cb )
self.master.bind( "<KeyRelease>", self.keyReleased_cb )
self.canvas = Canvas(self, width=640, height=480)
self.canvas.pack()
self.lightGfx = [[None, None], [None, None]]
self.hipButtonGfx = [None, None]
boxw = 50
boxh = 50
gapx = 25
gapy = 200
sx = 100
sy = 100
#slap buttons
dx = 0
dy = 0
self.lightGfx[0][0] = self.canvas.create_rectangle( \
sx + dx, \
sy + dy, \
sy + boxw + dx, \
sy + boxh + dy, \
fill="")
dx = boxw + gapx
dy = 0
self.lightGfx[0][1] = self.canvas.create_rectangle( \
sx + dx, \
sy + dy, \
sy + boxw + dx, \
sy + boxh + dy, \
fill="")
#hip hold button
dx = 2*boxw + 2*gapx
dy = 0
self.hipButtonGfx[0] = self.canvas.create_oval( \
sx + dx, \
sy + dy, \
sy + boxw + dx, \
sy + boxh + dy, \
fill="")
dx = 0
dy = boxh + gapy
self.lightGfx[1][0] = self.canvas.create_rectangle( \
sx + dx, \
sy + dy, \
sy + boxw + dx, \
sy + boxh + dy, \
fill="")
dx = boxw + gapx
dy = boxh + gapy
self.lightGfx[1][1] = self.canvas.create_rectangle( \
sx + dx, \
sy + dy, \
sy + boxw + dx, \
sy + boxh + dy, \
fill="")
#hip hold button
dx = 2*boxw + 2*gapx
dy = boxh + gapy
self.hipButtonGfx[1] = self.canvas.create_oval( \
sx + dx, \
sy + dy, \
sy + boxw + dx, \
sy + boxh + dy, \
fill="")
#self.lightGfx[0][1] = self.canvas.create_rectangle(sx + boxw + gap, sy, sx + 2*boxw + gap, sy + boxh, fill="")
#self.lightGfx[1][0] = self.canvas.create_rectangle(sx, sy, sx + boxw, sy + boxh, fill="")
#self.lightGfx[1][1] = self.canvas.create_rectangle(sx + boxw + gap, sy, sx + 2*boxw + gap, sy + boxh, fill="")
#self.lightGfx[0][1] = self.canvas.create_rectangle(sx, sy, 150, 150, fill="")
#self.waitingForSlap = False
#self.currentNote = self.pattern.getCurrentNote()
self.pack()
# for graphics purposes
def graphicsCallback(self, key, pressed):
slapPlayerSide = self.getSlapKeyPlayerSidePair(key)
hipPlayer = self.getHipKeyPlayer(key)
if slapPlayerSide != None:
self.slapPress(slapPlayerSide[0], slapPlayerSide[1], pressed)
if hipPlayer != None:
self.hipButtonPress(hipPlayer, pressed)
def getLightPinPlayerSidePair(self, pin):
for p in range(len(self.lightPins)):
for s in range(len(self.lightPins[p])):
if self.lightPins[p][s] == pin:
return [p, s]
return None
def getSlapKeyPlayerSidePair(self, key):
for p in range(len(self.slapKeys)):
for s in range(len(self.slapKeys[p])):
if self.slapKeys[p][s] == key:
return [p, s]
return None
def getHipKeyPlayer(self, key):
for p in range(len(self.hipKeys)):
if self.hipKeys[p] == key:
return p
return None
def setLightOn(self, pin, on):
pair = self.getLightPinPlayerSidePair(pin)
player = pair[0]
side = pair[1]
if (on):
self.canvas.itemconfig(self.lightGfx[player][side], fill="red")
else:
self.canvas.itemconfig(self.lightGfx[player][side], fill="")
def slapPress(self, playerIdx, buttonIdx, pressed): #player = person getting slapped, button = button they slapped
#outline box, for pc development
lWidth = 1
if pressed:
lWidth = 5
self.canvas.itemconfig(self.lightGfx[playerIdx][buttonIdx], width = lWidth)
def hipButtonPress(self, player, pressed):
fillColor = ""
if pressed:
if (player == 0):
fillColor = "black"
else:
fillColor = "gray"
self.canvas.itemconfig(self.hipButtonGfx[player], fill=fillColor)
def keyPressed_cb( self, event ):
self.keysPressed[event.keysym] = False
self.message1.set( "Key pressed: " + event.keysym )
if event.keysym == 'Escape':
print "tkinter quit"
self.quit()
self.quitNow = True
def keyReleased_cb( self, event ):
self.keysPressed[event.keysym] = True
self.message1.set( "Key released: " + event.keysym )
def buttonPressed(self, idx):
return self.keysPressed[idx]
def start(self):
self.mainloop()