Skip to content

Commit ca7671b

Browse files
committed
improved mouse control
1 parent bc670f5 commit ca7671b

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

app.py

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def get_octave(self, x, y):
163163

164164
def xy_to_midi(self, x, y):
165165
row = self.board_h - y
166-
r = x % self.board_w + 25.5 + 2.5 * row
166+
r = x % self.board_w + 25.5 + 2.5 * row # FIXME: make this simpler
167167
r *= 2
168168
r = int(r)
169169
r += (self.octave + self.octave_base) * 12
@@ -187,6 +187,54 @@ def get_color(self, x, y):
187187
note = self.get_note_index(x, y)
188188
return COLOR_CODES[self.options.lights[note]]
189189

190+
def mouse_held(self):
191+
return self.mouse_midi != -1
192+
193+
def mouse_press(self, x, y, state=True, hold=False):
194+
if y < 0:
195+
return
196+
197+
# if we're not intending to hold the note, we release the previous primary note
198+
if not hold:
199+
if self.mouse_held():
200+
self.mouse_release()
201+
202+
vel = y % int(self.button_sz)
203+
x /= int(self.button_sz)
204+
y /= int(self.button_sz)
205+
206+
vel = vel / int(self.button_sz)
207+
vel = 1 - vel
208+
vel *= 127
209+
vel = clamp(0, 127, int(vel))
210+
211+
x, y = int(x), int(y)
212+
213+
self.mark_xy(x, y, state)
214+
v = glm.ivec2(x, y)
215+
midinote = self.xy_to_midi(v.x, v.y)
216+
if not hold:
217+
self.mouse_mark = v
218+
self.mouse_midi = midinote
219+
data = [0x90 if state else 0x80, midinote, vel]
220+
if self.midi_out:
221+
self.midi_write(self.midi_out, data, 0)
222+
223+
def mouse_hold(self, x, y):
224+
return self.mouse_press(x, y, True, hold=True)
225+
226+
def mouse_release(self, x=None, y=None):
227+
# x and y provided? it's a specific coordinate
228+
if x is not None and y is not None:
229+
return self.mouse_press(x, y, False)
230+
# x and y not provided? it uses the primary mouse coordinate
231+
if self.mouse_midi != -1:
232+
self.mark_xy(self.mouse_mark.x, self.mouse_mark.y, False)
233+
data = [0x80, self.mouse_midi, 127]
234+
if self.midi_out:
235+
self.midi_write(self.midi_out, data, 0)
236+
self.mouse_midi = -1
237+
190238
# Given an x,y position, find the octave
191239
# (used to initialize octaves 2D array)
192240
def init_octave(self, x, y):
@@ -613,7 +661,7 @@ def __init__(self):
613661
for i in range(len(outnames)):
614662
name = outnames[i]
615663
name_lower = name.lower()
616-
print(name_lower)
664+
# print(name_lower)
617665
if "linnstrument" in name_lower:
618666
print("LinnStrument (Out): " + name)
619667
self.linn_out = rtmidi2.MidiOut()
@@ -801,23 +849,14 @@ def logic(self, dt):
801849
elif ev.type == pygame.MOUSEBUTTONDOWN:
802850
x, y = ev.pos
803851
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)
852+
if ev.button == 1:
853+
self.mouse_press(x, y)
854+
elif ev.button == 2:
855+
self.mouse_release(x, y)
856+
elif ev.button == 3:
857+
self.mouse_hold(x, y)
814858
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
859+
self.mouse_release()
821860
elif ev.type == pygame_gui.UI_BUTTON_PRESSED:
822861
if ev.ui_element == self.btn_octave_down:
823862
self.octave -= 1

0 commit comments

Comments
 (0)