From: Einar Pehrson Date: Sat, 17 May 2003 00:10:51 +0000 (+0000) Subject: Added some popup-menu items. X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=commitdiff_plain;h=8acc1b9983cf0c679fbe556f480a6d7f4d45e518 Added some popup-menu items. Fixed the popup loader. Fixed the save function. --- diff --git a/MooKeyboard.java b/MooKeyboard.java index 2a2bd9e..35b2496 100644 --- a/MooKeyboard.java +++ b/MooKeyboard.java @@ -23,7 +23,6 @@ public class MooKeyboard extends KeyAdapter { // If note is not already on and the key is mapped to a note, sends the NoteOn event. if (!isOn[noteNumber] && noteNumber > 0) Moosique.getActiveChannel().noteOn(noteNumber, 127); isOn[noteNumber] = true; - System.out.println("NoteON"); } catch (ArrayIndexOutOfBoundsException x) { return; } @@ -39,7 +38,6 @@ public class MooKeyboard extends KeyAdapter { // Sends the NoteOff event. Moosique.getActiveChannel().noteOff(noteNumber); isOn[noteNumber] = false; - System.out.println("NoteOFF"); } catch (ArrayIndexOutOfBoundsException x) { return; } diff --git a/MooMenu.java b/MooMenu.java index d25ec0c..053030b 100644 --- a/MooMenu.java +++ b/MooMenu.java @@ -161,23 +161,9 @@ public class MooMenu extends JMenuBar implements ActionListener { Moosique.load(chooser.getSelectedFile().getAbsolutePath()); } } else if (command == "Save") { - Moosique.save(); + if (!Moosique.save()) showSaveAsDialog(); } else if (command == "Save as...") { - // Shows a file chooser. If shown previously, starts in the current directory. - if (directory != null) { - chooser = new JFileChooser(directory); - } else { - chooser = new JFileChooser(); - } - chooser.addChoosableFileFilter(new MidiFileFilter()); - int returnVal = chooser.showSaveDialog(this); - - // Stores the current directory and loads the selected file. - File file = chooser.getSelectedFile(); - if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) { - directory = file.getParentFile(); - Moosique.saveAs(file.getAbsolutePath()); - } + showSaveAsDialog(); } else if (command == "Exit") { Moosique.quit(); } else if (command == "Copy") { @@ -240,6 +226,24 @@ public class MooMenu extends JMenuBar implements ActionListener { } } + private void showSaveAsDialog() { + // Shows a file chooser. If shown previously, starts in the current directory. + if (directory != null) { + chooser = new JFileChooser(directory); + } else { + chooser = new JFileChooser(); + } + chooser.addChoosableFileFilter(new MidiFileFilter()); + int returnVal = chooser.showSaveDialog(this); + + // Stores the current directory and loads the selected file. + File file = chooser.getSelectedFile(); + if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) { + directory = file.getParentFile(); + Moosique.saveAs(file.getAbsolutePath()); + } + } + class MidiFileFilter extends javax.swing.filechooser.FileFilter { public boolean accept(File f) { if(f != null) { diff --git a/MooNoteElement.java b/MooNoteElement.java index 8d8778d..3a068cd 100644 --- a/MooNoteElement.java +++ b/MooNoteElement.java @@ -19,7 +19,7 @@ public class MooNoteElement extends JPanel { private String notePitch; private String noteVelocity; private JPopupMenu popup; - private JMenuItem popupRemove, popupProp; + private JMenuItem popupRemove, popupProp, popupTransposeOctUp, popupTransposeOctDown; /** * Creates a new note element. @@ -44,10 +44,15 @@ public class MooNoteElement extends JPanel { popupProp = new JMenuItem("Preferences..."); popupProp.addActionListener(pList); popup.add(popupProp); - popupRemove = new JMenuItem("Remove"); popupRemove.addActionListener(pList); popup.add(popupRemove); + popupTransposeOctUp = new JMenuItem("Transpose one octave up"); + popupTransposeOctUp.addActionListener(pList); + popup.add(popupTransposeOctUp); + popupTransposeOctDown = new JMenuItem("Transpose one octave down"); + popupTransposeOctDown.addActionListener(pList); + popup.add(popupTransposeOctDown); } @@ -131,15 +136,14 @@ public class MooNoteElement extends JPanel { class MAdapter extends MouseAdapter { public void mouseClicked(MouseEvent e) { - + // Play the note } /** * Checks if the mouse is pressed. - * Pops up the menu if right mousebutton is used. * Increases the pitch or velocity if the right mousebutton is pressed while holding ctrl down. * Decreases the pitch or velocity if the left mousebutton is pressed while holding ctrl down. - * @param e The events recieved. + * @param e the event recieved. */ public void mousePressed(MouseEvent e) { if (e.isControlDown()) { @@ -150,6 +154,7 @@ public class MooNoteElement extends JPanel { note.setPitch(note.getPitch() - 1); } calculateString(); + repaint(); } else if (veloRect.contains(e.getPoint())) { if (SwingUtilities.isRightMouseButton(e)) { note.setVelocity(note.getVelocity() + 1); @@ -157,11 +162,20 @@ public class MooNoteElement extends JPanel { note.setVelocity(note.getVelocity() - 1); } calculateString(); + repaint(); } - e.getComponent().repaint(); - } else if (e.isPopupTrigger()) { - popup.show(e.getComponent(), e.getX(), e.getY()); - } + } else maybeShowPopup(e); + } + + public void mouseReleased(MouseEvent e) { + maybeShowPopup(e); + } + + /** + * Shows the menu if an OS-specific popup-trigger was activated. + */ + private void maybeShowPopup(MouseEvent e) { + if (e.isPopupTrigger() && !e.isControlDown()) popup.show(e.getComponent(), e.getX(), e.getY()); } } @@ -171,19 +185,29 @@ public class MooNoteElement extends JPanel { class PopupListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); - if (source == popupProp) { + if (source == popupProp) { new MooDialog(note); - } else if (source == popupRemove) { + } else if (source == popupRemove) { remove(); + } else if (source == popupTransposeOctUp) { + note.setPitch(note.getPitch() + 12); + update(); + } else if (source == popupTransposeOctDown) { + note.setPitch(note.getPitch() - 12); + update(); } } + + private void update() { + calculateString(); + repaint(); + } } /** - * Asks the MooTrackView that it's painted on to remove this element and the note. + * Asks the MooTrackView that the note element is painted on to remove this element and the note. */ protected void remove(){ mtv.removeNote(this); } - } diff --git a/MooTrackView.java b/MooTrackView.java index 4d58e1d..bac8cbc 100644 --- a/MooTrackView.java +++ b/MooTrackView.java @@ -172,7 +172,8 @@ public class MooTrackView extends JPanel { * Adds a standard note to this track. */ private void addStandardNote() { - long timestamp = (long)(ticksPerSixteenth * (popupY - insets.top) / NOTE_HEIGHT); + int row = (popupY - insets.top) / NOTE_HEIGHT; + long timestamp = (long)(ticksPerSixteenth * row); addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4)); } @@ -196,6 +197,7 @@ public class MooTrackView extends JPanel { * The adapter used to listen on mouse actions */ class MAdapter extends MouseAdapter { + /** * Adds a standard note if doubleclicked. */ @@ -206,10 +208,18 @@ public class MooTrackView extends JPanel { } } + public void mousePressed(MouseEvent e) { + maybeShowPopup(e); + } + + public void mouseReleased(MouseEvent e) { + maybeShowPopup(e); + } + /** - * Shows the menu if on standard poptriggers. + * Shows the menu if an OS-specific popup-trigger was activated. */ - public void mousePressed(MouseEvent e) { + private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { popupY = e.getY(); popup.show(e.getComponent(), e.getX(), e.getY()); diff --git a/Moosique.java b/Moosique.java index 9b4349c..e03cca0 100644 --- a/Moosique.java +++ b/Moosique.java @@ -22,7 +22,7 @@ public class Moosique { private static MidiEvent[] timeSignatures, tempoChanges; private static ArrayList emptyTracks; - private static String filename, fileArg; + private static String filename; private static long editPosition; private static boolean makeGUI = true, isEdited = false, drawEmptyTracks = false; private static Thread player; @@ -35,6 +35,7 @@ public class Moosique { System.out.println("\nMoosique version 1.0\n"); // Parses command-line arguments. + String fileArg = null; for (int i = 0; i < args.length; i++) { if (args[i].equals("-n")) {makeGUI = false;} else if (fileArg == null) {fileArg = args[i];} @@ -54,7 +55,6 @@ public class Moosique { setActiveChannel(0); } catch (MidiUnavailableException e) { System.out.println("Failed, quitting."); -// System.exit(1); } System.out.println("Done"); @@ -62,7 +62,7 @@ public class Moosique { if (fileArg != null) { System.out.print("Loading MIDI sequence from " + fileArg + "..."); if (!load(fileArg)) { - System.out.println("Failed"); + System.out.println("Failed, creating new sequence"); clearSequence(); } else { System.out.println("Done"); @@ -153,6 +153,7 @@ public class Moosique { try { seq = new Sequence(Sequence.PPQ, DEFAULT_RESOLUTION, DEFAULT_TRACKS); sequencer.setSequence(seq); + filename = null; emptyTracks = new ArrayList(); } catch (InvalidMidiDataException e) {} // Sends sequence to GUI. @@ -193,15 +194,7 @@ public class Moosique { // Creates the visualisation thread and starts it. player = new Thread () { public void run() { - long ticksPerSixteenth = seq.getResolution()/4; - System.out.println("Ticks/16: " + ticksPerSixteenth); - long position = sequencer.getTickPosition(); while(sequencer.isRunning()) { - long pos = sequencer.getTickPosition(); - long tickDiff = pos - position; - System.out.print(" ... " + tickDiff); - position = pos; - // Updates the GUI with the current tick position. gui.update(sequencer.getTickPosition()); @@ -435,19 +428,27 @@ public class Moosique { * Saves the current sequence to the given filename * @param file the filename to use */ - public static void saveAs(String file) { + public static boolean saveAs(String file) { try { - MidiSystem.write(seq, 1, new File(filename)); - } catch (IOException e) {} - filename = file; - gui.setStatus("Saved " + file); + MidiSystem.write(seq, 1, new File(file)); + filename = file; + gui.setStatus("Saved " + file); + return true; + } catch (IOException e) { + gui.setStatus("Failed in saving " + file); + return false; + } } /** * Saves the current sequence to the previously given filename. */ - public static void save() { - saveAs(filename); + public static boolean save() { + if (filename == null) return false; + else { + saveAs(filename); + return true; + } } /** diff --git a/To Do.txt b/To Do.txt index 93b5d13..2fb00c4 100644 --- a/To Do.txt +++ b/To Do.txt @@ -1,7 +1,5 @@ -Diverse - x Spara konfiguration? Arbetskatalog Fem senast öppnade filerna @@ -24,11 +22,8 @@ x G x Gör en ruta för taktarten i MooViews övre vänstra hörn. x Implementera klart menyn, med alla dialoger. x Lägg till en tom panel i MooGUI för att fylla ut skärmen. Använd setBounds() - +x Fixa markera, kopiera, klipp ut och klistra in. x Highlighta noter som spelas? (Enligt kravspec.) - - -MooNoteProp - * Textfält som gör att man bara kan skriva in siffror? +x Textfält som gör att man bara kan skriva in siffror? (MooNoteProp) \ No newline at end of file diff --git a/midi/test3.mid b/midi/test3.mid index 5893970..2e2aa18 100644 Binary files a/midi/test3.mid and b/midi/test3.mid differ