From: Einar Pehrson Date: Mon, 19 May 2003 14:37:49 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=commitdiff_plain;h=1f2ba8a0848ee0c5f0ea94f7bc40ea502c0dff74 *** empty log message *** --- 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 dfe6b7e..90aead1 100644 Binary files a/midi/test2.mid and b/midi/test2.mid differ