From 1f2ba8a0848ee0c5f0ea94f7bc40ea502c0dff74 Mon Sep 17 00:00:00 2001 From: Einar Pehrson Date: Mon, 19 May 2003 14:37:49 +0000 Subject: [PATCH] *** empty log message *** --- MooNote.java | 11 ++++++++++- MooNoteElement.java | 17 +++++++++++++++-- MooTrackView.java | 30 +++++++++++++++++------------- Moosique.java | 4 ++-- midi/test2.mid | Bin 1558 -> 1402 bytes 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/MooNote.java b/MooNote.java index 023de20..fb73341 100644 --- a/MooNote.java +++ b/MooNote.java @@ -7,7 +7,7 @@ import javax.sound.midi.*; * @author Einar Pehrson */ -public class MooNote extends MidiEvent implements Cloneable { +public class MooNote extends MidiEvent implements Cloneable, Comparable { private MidiEvent noteOffEvent; private ShortMessage noteOnMsg, noteOffMsg; @@ -161,4 +161,13 @@ public class MooNote extends MidiEvent implements Cloneable { new MidiEvent((ShortMessage)noteOffEvent.getMessage().clone(), noteOffEvent.getTick()) ); } + + /** + * Compares this note to another note. + * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object + */ + public int compareTo(Object o) { + return (int)(((MidiEvent)o).getTick() - getTick()); + } + } diff --git a/MooNoteElement.java b/MooNoteElement.java index 9ed1ab9..6080889 100644 --- a/MooNoteElement.java +++ b/MooNoteElement.java @@ -9,7 +9,7 @@ import java.awt.event.*; * @version 1 */ -public class MooNoteElement extends JPanel { +public class MooNoteElement extends JPanel implements Comparable{ private MooTrackView mtv; private MooNote note; @@ -65,6 +65,14 @@ public class MooNoteElement extends JPanel { return note; } + /** + * Compares the note of this element to that of another note. + * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object + */ + public int compareTo(Object o) { + return note.compareTo(((MooNoteElement)o).getNote()); + } + /** * Selects the current NoteElement. */ @@ -227,7 +235,12 @@ public class MooNoteElement extends JPanel { } public void mouseReleased(MouseEvent e) { - if (!maybeShowPopup(e) && !mouseIn) mtv.maybeMoveSelectedNotes(getY(), getY() + e.getY()); + if (!maybeShowPopup(e) && !mouseIn) { + int y = e.getY(); + if (y < 0) mtv.maybeMoveSelectedNotes((int)Math.floor((double)y / MooTrackView.NOTE_HEIGHT) * MooTrackView.NOTE_HEIGHT); + if (y > getHeight()) mtv.maybeMoveSelectedNotes((int)Math.ceil(((double)y - getHeight()) / MooTrackView.NOTE_HEIGHT) * MooTrackView.NOTE_HEIGHT); + + } } /** diff --git a/MooTrackView.java b/MooTrackView.java index bd7d7bb..8133cda 100644 --- a/MooTrackView.java +++ b/MooTrackView.java @@ -22,7 +22,8 @@ public class MooTrackView extends JPanel { private JMenuItem popupAdd, popupPaste; private JMenuItem selPopupCopy, selPopupCut, selPopupRemove, selPopupTranspUpOct, selPopupTranspDownOct; - private ArrayList coords, selection, copyBuffer; + private ArrayList coords, copyBuffer; + private TreeSet selection; private Insets insets; private int ticksPerSixteenth, popupY = 0; private boolean leftMouseButtonPressed = false; @@ -43,7 +44,7 @@ public class MooTrackView extends JPanel { // Creates instance variables insets = getInsets(); coords = new ArrayList(track.size() / 2); - selection = new ArrayList(); + selection = new TreeSet(); copyBuffer = new ArrayList(); // Creates temporary variables @@ -233,7 +234,7 @@ public class MooTrackView extends JPanel { * @param the note to deselect */ public void deselectNote(MooNoteElement elem) { - selection.remove(selection.indexOf(elem)); + selection.remove(elem); } /** @@ -268,7 +269,7 @@ public class MooTrackView extends JPanel { while(it.hasNext()) { copyBuffer.add(((MooNoteElement)it.next()).getNote().clone()); } - Collections.sort(copyBuffer, new Moosique.NoteComparator()); + Collections.sort(copyBuffer); } /** @@ -319,23 +320,26 @@ public class MooTrackView extends JPanel { } /** - * Moves the current selection, if the mouse was pressed on a note element - * and then released in another row. - * @param srcY the y-coordinate of the point in which the mouse button was pressed - * @param destY the y-coordinate of the point in which the mouse button was released + * Moves the current selection the given number of ticks. + * @param ticks the number of ticks to move the selection. */ - public void maybeMoveSelectedNotes(int srcY, int destY) { - int srcRow = (srcY - insets.top) / NOTE_HEIGHT; - int destRow = (destY - insets.top) / NOTE_HEIGHT; - long timeDiff = (long)(ticksPerSixteenth * (destRow - srcRow)); + public void moveSelectedNotes(int ticks) { Iterator it = selection.iterator(); while(it.hasNext()) { MooNoteElement elem = (MooNoteElement)it.next(); - elem.getNote().setTick(elem.getNote().getTick() + timeDiff); + elem.getNote().setTick(elem.getNote().getTick() + ticks); layoutElement(elem, true); } } + /** + * Moves the current selection, depending on the given delta y. + * @param y the number of pixels the selection is moved. + */ + public void maybeMoveSelectedNotes(int y) { + moveSelectedNotes(ticksPerSixteenth * (y / NOTE_HEIGHT)); + } + /** * Shows a popup-menu with options for the current selection of note elements. * @param c the component over which to display the menu diff --git a/Moosique.java b/Moosique.java index 09e8395..8de399f 100644 --- a/Moosique.java +++ b/Moosique.java @@ -527,7 +527,7 @@ public class Moosique { if (noteOns.size() == 0) emptyTracks.add(tracks[i]); // Sorts the note lists by tick position. - Comparator c = new NoteComparator(); + Comparator c = new MidiEventComparator(); Collections.sort(noteOns, c); Collections.sort(noteOffs, c); @@ -625,7 +625,7 @@ public class Moosique { /** * A Comparator for sorting lists of MidiEvents. */ - public static class NoteComparator implements Comparator { + public static class MidiEventComparator implements Comparator { public int compare(Object o1, Object o2) { return (int)(((MidiEvent)o1).getTick() - ((MidiEvent)o2).getTick()); } diff --git a/midi/test2.mid b/midi/test2.mid index dfe6b7e5eb22bd2489edbbf1d3109522642ce0fd..90aead13b67240d4dfe52bbf30030424566bdcf7 100644 GIT binary patch literal 1402 zcmcgrJ#Q015Pf!5=x~9IlmQ^w%KxZVMVnlf#6BwnIFm5O5(&wgOl?=Oqf{8K?r}n1UM<@DP6i z?zsC`j}fSag5)N%O3bb?$7kMln4W=5$SIzc;Fuw+=9tYHF`PfaP}AEYrYz3ztl+&| zKsADb&k8I>ztpu;&o@3 zSXw_P>#OHHF)Y%jUvw(ykCgU0O2@h$@^t&~9*4AWpAtAs)%Eth;*nbk7bF2kPEkl| z8re65q+ZeHH6b_Ez0*^U5Ru;y(kD-Aqas`AMqVx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