forked from BoffinFactory/pianostairs
/
gui.py
251 lines (197 loc) · 8.28 KB
/
gui.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import config as G, player, threading, collections, math, sys, parser, pygame, challenge
from ScrolledText import ScrolledText
from Tkinter import *
from PIL import Image, ImageTk
def ternary(expr, expr0, expr1):
if expr: return expr0
else: return expr1
class ToggleButton:
def __init__(self, parent, label, callback):
self.var = IntVar()
self.var.set(0)
self.button = Checkbutton(master=parent, text=label, indicatoron=0, var=self.var, command=callback)
self.button.pack(side=LEFT)
def set(self, n):
self.var.set(n)
def get(self):
return self.var.get()
class GUI:
MAX_MESSAGES = 1024 # Fine if it's large, but it should be finite to avoid running out of memory
PIANO_WIDTH = 0
PIANO_HEIGHT = 0
PIANO_BORDER = 10
keys = {}
def write(self, s):
self.messages.append(s)
msg = ''
for i in self.messages:
msg = '%s\n%s' % (msg,i)
#set text and scroll to end
self.output.delete(1.1, END)
self.output.insert(END, msg)
self.output.see(END)
def key_up(self, note):
color = self.keys[note].config('disabledforeground')[4]
self.keys[note].config(relief=RAISED)
self.keys[note].config(background=color)
def key_down(self, note):
color = self.keys[note].config('activebackground')[4]
self.keys[note].config(relief=SUNKEN)
self.keys[note].config(background=color)
def press_key(self, note):
player.sound_play(note)
def draw_keyboard(self):
""" draw standard 88 key piano, with each flight of stairs highlighted in a different color """
def avg_color(color0, color1):
""" takes colors in RGB hex form. Average each component individually """
def avg_byte(n):
a = (color0 >> n) & 0xFF
b = (color1 >> n) & 0xFF
return ((a + b) / 2) << n
return avg_byte(16) + avg_byte(8) + avg_byte(0)
def draw_white_key(key):
x0 = key * white_key_width + self.PIANO_BORDER
x1 = x0 + white_key_width
y0 = self.PIANO_BORDER
y1 = y0 + white_key_height
tone = chr(ord("A") + key % 7)
octave = (key + 5) / 7
note = tone + str(octave)
color = 0xFFFFFF
outline = "#888888"
for i in range(0, len(flights)):
if (note in flights[i]):
color = avg_color(colors[i % len(colors)], 0xFFFFFF)
outline = "#444444"
#self.canvas.create_rectangle(x0, y0, x1, y1, outline=outline, fill="#%06x" % (color))
#self.canvas.create_text(x0 + (x1-x0)/2, y0 + 0.8 * (y1-y0), text=note, fill="blue")
active_color = avg_color(color, 0xCCCCCC)
self.keys[note] = Button(self.canvas,
highlightbackground=outline, bg="#%06x" % (color),
highlightcolor=outline, activebackground="#%06x" % (active_color),
disabledforeground="#%06x" % (color), # kludge to store it
borderwidth=1, highlightthickness=0,
command=lambda : self.press_key(note))
self.keys[note].place(x=x0, y=y0, width=x1-x0, height=y1-y0)
def draw_black_key(key):
""" key is the white key that this is the flat of """
mod = key % 7
if (2 == mod or 5 == mod): return
x0 = key * white_key_width - black_key_width / 2 + self.PIANO_BORDER
x1 = x0 + black_key_width
y0 = self.PIANO_BORDER
y1 = y0 + black_key_height
tone = chr(ord("A") + key % 7)
octave = (key + 5) / 7
note = tone + "b" + str(octave)
color=0x000000
for i in range(0, len(flights)):
if (note in flights[i]):
color = avg_color(colors[i % len(colors)], 0x000000)
#self.canvas.create_rectangle(x0, y0, x1, y1, outline="grey", fill="#%06x" % (color))
#self.canvas.create_text(x0 + (x1-x0)/2, y0 + 5 + (y1-y0), text=note, fill="blue")
active_color = avg_color(color, 0xFFFFFF)
self.keys[note] = Button(self.canvas,
highlightbackground="grey", bg="#%06x" % (color),
highlightcolor="grey", activebackground="#%06x" % (active_color),
disabledforeground="#%06x" % (color), # kludge to store it
borderwidth=1, highlightthickness=0,
command=lambda : self.press_key(note))
self.keys[note].place(x=x0, y=y0, width=x1-x0, height=y1-y0)
max = int(math.ceil(len(G.note_from_stair) / G.STEPS_PER_FLIGHT))
flights = [ G.note_from_stair[i * G.STEPS_PER_FLIGHT : (i + 1) * G.STEPS_PER_FLIGHT] for i in range(0, max) ]
colors = ( 0x0000FF, 0x00FF00, 0xFF0000, 0x00FFFF, 0xFF00FF, 0xFFFF00 )
white_key_width = self.PIANO_WIDTH / 52
white_key_height = self.PIANO_HEIGHT
black_key_width = white_key_width * 0.55
black_key_height = self.PIANO_HEIGHT * 0.55
# The actual border size will vary a bit, since white_key_width is restricted to integers.
# This is preferable to what it looks like otherwise
self.canvas.destroy()
self.canvas = Canvas(self.frame,
height = self.PIANO_HEIGHT + 2 * self.PIANO_BORDER,
width = self.PIANO_WIDTH)
self.canvas.pack(side=BOTTOM)
for key in range (0, 52): draw_white_key(key)
for key in range (1, 52): draw_black_key(key)
def toggle_demo(self):
if self.button_demo.get():
parser.playsong('copeland.score')
else:
self.draw_keyboard() # reset any active buttons
def resize(self, event):
if G.REQUIRE_FULLSCREEN: self.win.attributes('-fullscreen', 1)
self.PIANO_WIDTH = self.win.winfo_width() - 2 * self.PIANO_BORDER
self.PIANO_HEIGHT = self.PIANO_WIDTH / 10.625
self.draw_keyboard()
def __init__(self):
def update_settings():
player.set_instrument(self.selected_instrument.get())
player.set_key(self.selected_key.get(), self.use_accidentals.get())
self.resize(None)
self.messages = collections.deque(maxlen=self.MAX_MESSAGES)
self.win = Tk()
self.win.attributes('-fullscreen', 1)
self.win.minsize(640, 540)
self.frame = Frame(self.win)
self.frame.pack(fill=BOTH, expand=1)
self.win.title('pianostairs')
buttons = Frame(self.frame)
buttons.pack(side=TOP, fill=X)
self.button_mute = ToggleButton(buttons, ' Mute ',
lambda: player.sound_off() if self.button_mute.get() else player.sound_on())
self.button_demo = ToggleButton(buttons, ' Demo ', self.toggle_demo)
#TODO: Need some sort of audio feedback. Beeps/buzzers/dueling banjos/whatever. may need to add banjo as an instrument
#This may be worth bring up a separate modal window on top of the normal one. Or replacing the normal text box with
#a frame
#Be sure to disable the demo if it's running
#Single or two-player mode. single player just has the AI pick random notes within n stairs of the last
self.button_challenge = ToggleButton(buttons, ' Challenge Mode ',
# lambda: self.challenge.start() if self.button_challenge.get() else self.challenge.stop())
# lambda: [ self.challenge.stop, self.challenge.start ][self.button_challenge.get()]() )
lambda: ternary(self.button_challenge.get(), self.challenge.start, self.challenge.stop)())
#Volume control
vol_frame = Frame(buttons)
volume = DoubleVar()
def set_volume(*args):
G.volume = volume.get() / 11.0
volume.trace("w", set_volume)
volume.set(10.0)
Label(vol_frame, text=' Volume: ').pack(side=LEFT)
Scale(vol_frame, showvalue=0, from_=0, to=11, resolution=.1,
length=220, orient=HORIZONTAL, variable=volume).pack(side=LEFT)
Label(vol_frame, textvariable=volume, width=4).pack(side=LEFT)
vol_frame.pack(side=RIGHT)
self.output = ScrolledText(self.frame)
self.output.pack(fill=BOTH, expand = 1)
self.challenge_frame = Frame(self.frame)
self.challenge = challenge.Challenge(self.challenge_frame)
# Menu
menu = Menu(self.frame)
menu_instrument = Menu(menu, tearoff=0)
menu_key = Menu(menu, tearoff=0)
# instrument
self.selected_instrument = StringVar()
self.selected_instrument.set(G.instrument)
for name in player.list_instruments():
menu_instrument.add_radiobutton(label=name.replace('_',' '), command=update_settings, var=self.selected_instrument)
# accidentals
self.use_accidentals = IntVar()
self.use_accidentals.set(0)
menu_key.add_checkbutton(label='Include Accidentals', command=update_settings, var=self.use_accidentals)
menu_key.add_separator()
# key
self.selected_key = StringVar()
self.selected_key.set('C')
for key in [ 'C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B']:
menu_key.add_radiobutton(label=key, command=update_settings, var=self.selected_key)
menu.add_cascade(label='Instrument', menu=menu_instrument)
menu.add_cascade(label='Key', menu=menu_key)
self.frame.bind('<Configure>', self.resize)
self.canvas = Canvas(self.frame)
self.win.config(menu=menu)
update_settings()
def run(f):
G.gui = GUI()
G.gui.win.after(0, f)
G.gui.win.mainloop()