X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=blobdiff_plain;f=MooTrackView.java;h=2f96085e25f6a362ea22b4d34e28045ac7177736;hp=a591d374853bd246647f16dd0c087ce38f58e335;hb=f7097bc07b6688d1629e6894c1c42dc06485dc12;hpb=6154ba318198471a2b94391df6aab6f2b6cd9b29 diff --git a/MooTrackView.java b/MooTrackView.java index a591d37..2f96085 100644 --- a/MooTrackView.java +++ b/MooTrackView.java @@ -15,17 +15,21 @@ public class MooTrackView extends JPanel { private Track track; private MooTrackTitle title; - private Rectangle box; + private MooKeyboard keyboard; private JPopupMenu popup, selPopup; private JMenu selPopupTranspUp, selPopupTranspDown; private JMenuItem popupAdd, popupPaste; - private JMenuItem selPopupCopy, selPopupCut, selPopupRemove, selPopupTranspUpOct, selPopupTranspDownOct; + private JMenuItem selPopupCopy, selPopupCut, selPopupRemove; + private JMenuItem[] selPopupTranspUpItems, selPopupTranspDownItems; - private ArrayList coords, selection, copyBuffer; + private ArrayList coords; + private TreeSet selection; private Insets insets; + private Rectangle box; private int ticksPerSixteenth, popupY = 0; private boolean leftMouseButtonPressed = false; + private static boolean snapToSixteenths = true; protected static int viewLength = 0; protected static int extraHeight = 0; public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200; @@ -37,19 +41,11 @@ public class MooTrackView extends JPanel { */ public MooTrackView (Track track, MooTrackTitle title) { super(true); + + // Defines instance variables this.track = track; this.title = title; - - // Creates instance variables insets = getInsets(); - coords = new ArrayList(track.size() / 2); - selection = new ArrayList(); - copyBuffer = new ArrayList(); - - // Creates temporary variables - MidiEvent note; - MooNoteElement elem; - extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150; // Configures panel setBackground(Color.white); @@ -57,6 +53,43 @@ public class MooTrackView extends JPanel { setLayout(null); setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT)); + placeNoteElements(); + + // Creates panel pop-up menu. + popup = new JPopupMenu(); + popupAdd = addMenuItem(popup, "Add note..."); + popupPaste = addMenuItem(popup, "Paste"); + + // Creates selection pop-up menu. + selPopup = new JPopupMenu(); + selPopupCopy = addMenuItem(selPopup, "Copy selection"); + selPopupCut = addMenuItem(selPopup, "Cut selection"); + selPopupRemove = addMenuItem(selPopup, "Remove selection"); + selPopupTranspUpItems = new JMenuItem[12]; + selPopupTranspDownItems = new JMenuItem[12]; + selPopupTranspUp = createTransposeMenu(selPopup, selPopupTranspUpItems, "selection up"); + selPopupTranspDown = createTransposeMenu(selPopup, selPopupTranspDownItems, "selection down"); + + // Adds listeners for popup menu and keyboard synthesizer. + addMouseListener(new MAdapter()); + keyboard = new MooKeyboard(title); + addKeyListener(keyboard); + } + + /** + * Creates note elements for all MooNotes in the track, and places them in the appropriate place. + */ + public void placeNoteElements() { + // Empties the container + removeAll(); + coords = new ArrayList(track.size() / 2); + selection = new TreeSet(); + + // Creates temporary variables + MidiEvent note; + MooNoteElement elem; + extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150; + // Places note elements for (int i = 0; i < track.size(); i++) { note = track.get(i); @@ -68,45 +101,8 @@ public class MooTrackView extends JPanel { layoutElement(elem, false); } setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight)); - } - - // Creates panel pop-up menu. - popup = new JPopupMenu(); - PopupListener pList = new PopupListener(); - popupAdd = new JMenuItem("Add note..."); - popupAdd.addActionListener(pList); - popup.add(popupAdd); - popupPaste = new JMenuItem("Paste"); - popupPaste.addActionListener(pList); - popup.add(popupPaste); - - // Creates selection pop-up menu. - selPopup = new JPopupMenu(); - selPopupCopy = new JMenuItem("Copy selection"); - selPopupCopy.addActionListener(pList); - selPopup.add(selPopupCopy); - selPopupCut = new JMenuItem("Cut selection"); - selPopupCut.addActionListener(pList); - selPopup.add(selPopupCut); - selPopupRemove = new JMenuItem("Remove selection"); - selPopupRemove.addActionListener(pList); - selPopup.add(selPopupRemove); - selPopupTranspUp = new JMenu("Transpose selection up"); - selPopup.add(selPopupTranspUp); - selPopupTranspUpOct = new JMenuItem("One octave"); - selPopupTranspUpOct.addActionListener(pList); - selPopupTranspUp.add(selPopupTranspUpOct); - selPopupTranspDown = new JMenu("Transpose selection down"); - selPopup.add(selPopupTranspDown); - selPopupTranspDownOct = new JMenuItem("One octave"); - selPopupTranspDownOct.addActionListener(pList); - selPopupTranspDown.add(selPopupTranspDownOct); - - // Adds listeners for popup menu and keyboard synthesizer. - addMouseListener(new MAdapter()); - addKeyListener(new MooKeyboard()); - } + } /** * Layouts the element to the right place. @@ -134,9 +130,10 @@ public class MooTrackView extends JPanel { // Calculates coordinates. x = insets.left; - y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT; - height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT; + y = insets.top + (int)((mn.getTick() * NOTE_HEIGHT) / ticksPerSixteenth); + height = (mn.getDuration() * NOTE_HEIGHT) / ticksPerSixteenth; if (height == 0) height = NOTE_HEIGHT; + if (snapToSixteenths && height < NOTE_HEIGHT) height = NOTE_HEIGHT; r = new Rectangle(x, y, NOTE_WIDTH, height); // Places the element in the appropriate place. @@ -233,7 +230,7 @@ public class MooTrackView extends JPanel { * @param the note to deselect */ public void deselectNote(MooNoteElement elem) { - selection.remove(selection.indexOf(elem)); + selection.remove(elem); } /** @@ -263,12 +260,13 @@ public class MooTrackView extends JPanel { * Copies the current selection. */ public void copySelectedNotes() { - copyBuffer = new ArrayList(selection.size()); + ArrayList copyBuffer = new ArrayList(selection.size()); Iterator it = selection.iterator(); while(it.hasNext()) { copyBuffer.add(((MooNoteElement)it.next()).getNote().clone()); } - Collections.sort(copyBuffer, new Moosique.NoteComparator()); + Collections.sort(copyBuffer); + Moosique.setCopyBuffer(copyBuffer); } /** @@ -285,12 +283,14 @@ public class MooTrackView extends JPanel { public void pasteCopiedNotes() { int row = (popupY - insets.top) / NOTE_HEIGHT; long timestamp = (long)(ticksPerSixteenth * row); + ArrayList copyBuffer = Moosique.getCopyBuffer(); if (copyBuffer.size() > 0) { long startTime = ((MooNote)copyBuffer.get(0)).getTick(); Iterator it = copyBuffer.iterator(); while(it.hasNext()) { MooNote mn = (MooNote)((MooNote)it.next()).clone(); mn.setTick(mn.getTick() - startTime + timestamp); + mn.setChannel(title.getChannel()); addNote(mn); } } @@ -318,6 +318,87 @@ public class MooTrackView extends JPanel { } } + /** + * Moves the current selection the given number of ticks. + * @param ticks the number of ticks to move the selection. + */ + public void moveSelectedNotes(int ticks) { + if (ticks < 0) { + // If the selection should be moved upwards, traverses the list in the natural order. + Iterator it = selection.iterator(); + while(it.hasNext()) { + MooNoteElement elem = (MooNoteElement)it.next(); + elem.getNote().setTick(elem.getNote().getTick() + ticks); + layoutElement(elem, true); + } + } else { + // If the selection should be moved downwards, traverses the list in the opposite order. + ArrayList selectedList = new ArrayList(selection); + ListIterator it = selectedList.listIterator(selectedList.size()); + while(it.hasPrevious()) { + MooNoteElement elem = (MooNoteElement)it.previous(); + 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)); + } + + /** + * Enables keyboard recording. + */ + public void enableKeyboardRecording() { + keyboard.recordEnable(); + } + + /** + * Disables keyboard recording. + */ + public void disableKeyboardRecording() { + keyboard.recordDisable(); + } + + /** + * Adds a menu item with the given command to the given popup menu. + */ + private JMenuItem addMenuItem(JPopupMenu menu, String command) { + JMenuItem item = new JMenuItem(command); + item.addActionListener(new PopupListener()); + menu.add(item); + return item; + } + + /** + * Adds a menu item with the given command to the given menu. + */ + private JMenuItem addMenuItem(JMenu menu, String command) { + JMenuItem item = new JMenuItem(command); + item.addActionListener(new PopupListener()); + menu.add(item); + return item; + } + + /** + * Creates a transpose sub menu with the given title in the given popup menu, + * inserting the items into the given array. + */ + private JMenu createTransposeMenu(JPopupMenu menu, JMenuItem[] items, String title) { + JMenu trans = new JMenu("Transpose " + title); + menu.add(trans); + items[0] = addMenuItem(trans, "One octave"); + for (int i = 1; i < 12; i++) { + items[i] = addMenuItem(trans, (i) + " halftones"); + } + return trans; + } + /** * Shows a popup-menu with options for the current selection of note elements. * @param c the component over which to display the menu @@ -380,13 +461,6 @@ public class MooTrackView extends JPanel { maybeShowPopup(e); } - /** - * Selects the notes within the area that was selected. - */ - public void mouseDragged(MouseEvent e) { - - } - /** * Shows the menu if an OS-specific popup-trigger was activated. */ @@ -407,7 +481,7 @@ public class MooTrackView extends JPanel { } /** - * Listens on actions on the popup menu and executes the appropriate action. + * Takes the appropriate action when a user selects an item on the popup menu. */ class PopupListener implements ActionListener { public void actionPerformed(ActionEvent e) { @@ -424,10 +498,15 @@ public class MooTrackView extends JPanel { cutSelectedNotes(); } else if (source == selPopupRemove) { removeSelectedNotes(); - } else if (source == selPopupTranspUpOct) { + } else if (source == selPopupTranspUpItems[0]) { transposeSelectedNotes(12); - } else if (source == selPopupTranspDownOct) { + } else if (source == selPopupTranspDownItems[0]) { transposeSelectedNotes(-12); + } else { + for (int i = 1; i < 12; i++) { + if (source == selPopupTranspUpItems[i]) transposeSelectedNotes(i); + else if (source == selPopupTranspDownItems[i]) transposeSelectedNotes(-i); + } } } }