/// <summary> /// Marks the specified note as released /// </summary> /// <param name="note">The note to release</param> public void MarkKeyReleased(NoteEnum note) { if (!(note == NoteEnum.None || note == lastMouseNote)) { MidiPlayer.StopNote(note); if (pressedNotes.ContainsKey(note)) { Staff.StopAdjustingNote(pressedNotes[note]); pressedNotes.Remove(note); } Invalidate(GetNoteArea(note), false); } SilenceStopwatch.Restart(); }
/// <summary> /// Called when a mouse button is released on the piano keyboard /// </summary> protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); if (e.Button == MouseButtons.Left) { leftMouseDown = false; if (lastMouseNote != NoteEnum.None) { MidiPlayer.StopNote(lastMouseNote); Staff.StopAdjustingNote(currentMouseNote); currentMouseNote = null; Invalidate(GetNoteArea(lastMouseNote), false); pressedNotes.Remove(lastMouseNote); lastMouseNote = NoteEnum.None; } } SilenceStopwatch.Restart(); }
/// <summary> /// Marks the specified note as currently pressed /// </summary> /// <param name="note">The note to press on the piano keyboard</param> /// <param name="addToStaff">Whether to add the specified note to the staff</param> public void MarkKeyPressed(NoteEnum note, bool addToStaff) { if (!(note == NoteEnum.None || pressedNotes.ContainsKey(note))) { Stopwatch stopwatch = Stopwatch.StartNew(); MidiPlayer.PlayNote(note); if (addToStaff) { int silenceLength = (int)SilenceStopwatch.ElapsedMilliseconds; NoteLength restLength = Staff.ToNoteLength(silenceLength); if (restLength >= NoteLength.DemiSemiQuaver) { Staff.AddNote(NoteEnum.None, restLength); } else { MusicNote lastNote = Staff.LastNote; if (lastNote != null) { lastNote.LengthInMilliseconds += silenceLength; } } } SilenceStopwatch.Restart(); if (addToStaff) { MusicNote noteControl = Staff.AddNote(note, NoteLength.HemiDemiSemiQuaver); pressedNotes.Add(note, noteControl); Staff.StartAdjustingNote(noteControl, stopwatch); } else { pressedNotes.Add(note, null); } Invalidate(GetNoteArea(note), false); } }
/// <summary> /// Called when the mouse is moved on the piano keyboard /// </summary> protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (leftMouseDown) { Stopwatch stopwatch = Stopwatch.StartNew(); lastCursorLocation = e.Location; NoteEnum currentNote = GetNoteAtPoint(e.Location); NoteEnum oldNote = lastMouseNote; if (!pressedNotes.ContainsKey(currentNote)) { lastMouseNote = currentNote; if (oldNote != NoteEnum.None) { MidiPlayer.StopNote(oldNote); } if (currentNote != NoteEnum.None) { MidiPlayer.PlayNote(currentNote); } if (oldNote != NoteEnum.None) { Staff.StopAdjustingNote(currentMouseNote); currentMouseNote = null; pressedNotes.Remove(oldNote); Invalidate(GetNoteArea(oldNote)); } if (currentNote != NoteEnum.None) { currentMouseNote = Staff.AddNote(currentNote, NoteLength.HemiDemiSemiQuaver); pressedNotes.Add(currentNote, currentMouseNote); Staff.StartAdjustingNote(currentMouseNote, stopwatch); Invalidate(GetNoteArea(currentNote)); } } } }
/// <summary> /// Called when a mouse button is pressed onto the piano keyboard /// </summary> protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if (e.Button == MouseButtons.Left) { Stopwatch stopwatch = Stopwatch.StartNew(); lastCursorLocation = e.Location; leftMouseDown = true; lastMouseNote = GetNoteAtPoint(e.Location); if (lastMouseNote != NoteEnum.None) { MidiPlayer.PlayNote(lastMouseNote); int silenceLength = (int)SilenceStopwatch.ElapsedMilliseconds; NoteLength restLength = Staff.ToNoteLength(silenceLength); if (restLength >= NoteLength.DemiSemiQuaver) { Staff.AddNote(NoteEnum.None, restLength); } else { MusicNote lastNote = Staff.LastNote; if (lastNote != null) { lastNote.LengthInMilliseconds += silenceLength; } } SilenceStopwatch.Restart(); currentMouseNote = Staff.AddNote(lastMouseNote, NoteLength.HemiDemiSemiQuaver); if (!pressedNotes.ContainsKey(lastMouseNote)) { pressedNotes.Add(lastMouseNote, currentMouseNote); } Staff.StartAdjustingNote(currentMouseNote, stopwatch); Invalidate(GetNoteArea(lastMouseNote), false); } } }