]> ruin.nu Git - moosique.git/commitdiff
*** empty log message ***
authorEinar Pehrson <einarp@itstud.chalmers.se>
Mon, 19 May 2003 14:37:49 +0000 (14:37 +0000)
committerEinar Pehrson <einarp@itstud.chalmers.se>
Mon, 19 May 2003 14:37:49 +0000 (14:37 +0000)
MooNote.java
MooNoteElement.java
MooTrackView.java
Moosique.java
midi/test2.mid

index 023de207da3aa053ffafff81f2febabe076ea996..fb73341e327f969ff51a50c23fbc5387d9f36b2b 100644 (file)
@@ -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());
+       }
+
 }
index 9ed1ab9b2004f4a4023cd1467b7343c5ea9e8fd8..60808898339361e3b70a53b21b6ac03c325713d7 100644 (file)
@@ -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);
+                               
+                       }
                }
 
                /**
index bd7d7bb0b9c5a88aaf1d9909f046b27f3e0e8cc5..8133cda05f372feff297b0718db8b6e0853abef2 100644 (file)
@@ -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
index 09e8395707b175f41a9ab18e94b4a0858c8ef333..8de399fc3f2f11c884a4cffd4a5facd4144a756c 100644 (file)
@@ -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());
                }
index dfe6b7e5eb22bd2489edbbf1d3109522642ce0fd..90aead13b67240d4dfe52bbf30030424566bdcf7 100644 (file)
Binary files a/midi/test2.mid and b/midi/test2.mid differ