@@ -163,7 +163,7 @@ def get_octave(self, x, y):
163
163
164
164
def xy_to_midi (self , x , y ):
165
165
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
167
167
r *= 2
168
168
r = int (r )
169
169
r += (self .octave + self .octave_base ) * 12
@@ -187,6 +187,54 @@ def get_color(self, x, y):
187
187
note = self .get_note_index (x , y )
188
188
return COLOR_CODES [self .options .lights [note ]]
189
189
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
+
190
238
# Given an x,y position, find the octave
191
239
# (used to initialize octaves 2D array)
192
240
def init_octave (self , x , y ):
@@ -613,7 +661,7 @@ def __init__(self):
613
661
for i in range (len (outnames )):
614
662
name = outnames [i ]
615
663
name_lower = name .lower ()
616
- print (name_lower )
664
+ # print(name_lower)
617
665
if "linnstrument" in name_lower :
618
666
print ("LinnStrument (Out): " + name )
619
667
self .linn_out = rtmidi2 .MidiOut ()
@@ -801,23 +849,14 @@ def logic(self, dt):
801
849
elif ev .type == pygame .MOUSEBUTTONDOWN :
802
850
x , y = ev .pos
803
851
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 )
814
858
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 ()
821
860
elif ev .type == pygame_gui .UI_BUTTON_PRESSED :
822
861
if ev .ui_element == self .btn_octave_down :
823
862
self .octave -= 1
0 commit comments