Skip to content

Commit bc670f5

Browse files
committed
mouse support
1 parent afc9ba5 commit bc670f5

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

app.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ def reset_lights(self):
161161
def get_octave(self, x, y):
162162
return self.octaves[y - self.board_h + self.flipped][x] + self.octave
163163

164+
def xy_to_midi(self, x, y):
165+
row = self.board_h - y
166+
r = x % self.board_w + 25.5 + 2.5 * row
167+
r *= 2
168+
r = int(r)
169+
r += (self.octave + self.octave_base) * 12
170+
return r
171+
164172
def get_note_index(self, x, y):
165173
y += self.flipped
166174
x += self.transpose
@@ -396,7 +404,7 @@ def __init__(self):
396404

397405
self.options = Options()
398406

399-
default_lights = '3,7,5,7,5,8,7,8,2,8,7,8'
407+
default_lights = '4,7,3,7,3,3,7,3,7,3,7,3'
400408

401409
# LIGHT = glm.ivec3(127)
402410
self.options.lights = get_option(opts,'lights',default_lights)
@@ -480,6 +488,9 @@ def __init__(self):
480488

481489
self.velocity_curve_ = self.options.velocity_curve
482490

491+
self.mouse_mark = glm.ivec2(0)
492+
self.mouse_midi = -1
493+
483494
self.init_board()
484495

485496
# load midi file from command line (playiung it is not yet impl)
@@ -683,6 +694,31 @@ def quit(self):
683694
self.reset_lights()
684695
self.done = True
685696

697+
def clear_marks(self, use_lights=False):
698+
for row in rows:
699+
x = 0
700+
for x in range(len(row)):
701+
idx = self.get_note_index(x, y)
702+
self.board[y+self.flipped][x] = state
703+
if use_lights:
704+
if state:
705+
self.set_light(x, y, 1)
706+
else:
707+
self.reset_light(x, y)
708+
y += 1
709+
self.dirty = True
710+
711+
def mark_xy(self, x, y, state, use_lights=False):
712+
idx = self.get_note_index(x, y)
713+
octave = self.get_octave(x, y)
714+
self.board[y+self.flipped][x] = state
715+
if use_lights:
716+
if state:
717+
self.set_light(x, y, 1)
718+
else:
719+
self.reset_light(x, y)
720+
self.dirty = True
721+
686722
def mark(self, midinote, state, use_lights=False, only_row=None):
687723
if only_row is not None:
688724
only_row = self.board_h - only_row - 1 - self.flipped #flip
@@ -746,6 +782,7 @@ def logic(self, dt):
746782
n -= 12
747783
self.mark(n + self.vis_octave * 12, 1, True)
748784
data = [0x90, n, 127]
785+
# TODO: add split for mouse?
749786
if self.midi_out:
750787
self.midi_write(self.midi_out, data, 0)
751788
except KeyError:
@@ -757,9 +794,30 @@ def logic(self, dt):
757794
self.mark(n + self.vis_octave * 12, 0, True)
758795
data = [0x80, n, 0]
759796
if self.midi_out:
797+
# TODO: add split for mouse?
760798
self.midi_write(self.midi_out, data, 0)
761799
except KeyError:
762800
pass
801+
elif ev.type == pygame.MOUSEBUTTONDOWN:
802+
x, y = ev.pos
803+
y -= self.menu_sz
804+
if y >= 0:
805+
x /= int(self.button_sz)
806+
y /= int(self.button_sz)
807+
x, y = int(x), int(y)
808+
self.mark_xy(x, y, True)
809+
self.mouse_mark = glm.ivec2(x, y)
810+
self.mouse_midi = self.xy_to_midi(self.mouse_mark.x, self.mouse_mark.y)
811+
data = [0x90, self.mouse_midi, 127]
812+
if self.midi_out:
813+
self.midi_write(self.midi_out, data, 0)
814+
elif ev.type == pygame.MOUSEBUTTONUP:
815+
if self.mouse_midi != -1:
816+
self.mark_xy(self.mouse_mark.x, self.mouse_mark.y, False)
817+
data = [0x80, self.mouse_midi, 127]
818+
if self.midi_out:
819+
self.midi_write(self.midi_out, data, 0)
820+
self.mouse_midi = -1
763821
elif ev.type == pygame_gui.UI_BUTTON_PRESSED:
764822
if ev.ui_element == self.btn_octave_down:
765823
self.octave -= 1
@@ -797,7 +855,7 @@ def logic(self, dt):
797855
self.dirty = self.dirty_lights = True
798856
elif ev.ui_element == self.btn_split:
799857
self.split_state = not self.split_state
800-
self.btn_split.set_text("SPLIT: " + ("ON" if SPLIT else "OFF"))
858+
self.btn_split.set_text("SPLIT: " + ("ON" if self.split_state else "OFF"))
801859
self.dirty = True
802860
# elif ev.type == pygame_gui.UI_HORIZONTAL_SLIDER_MOVED:
803861
# if ev.ui_element == self.slider_velocity:

0 commit comments

Comments
 (0)