]> ruin.nu Git - moosique.git/blobdiff - MooNoteElement.java
no message
[moosique.git] / MooNoteElement.java
index 1729f4aeeed79d06196fc4bb2a504de3e208b1c0..beed5c416acb498a25a95ff46291e419d3733ddd 100644 (file)
@@ -9,32 +9,34 @@ import java.awt.event.*;
  * @version 1
  */
  
-public class MooNoteElement extends JPanel {
+public class MooNoteElement extends JPanel implements Comparable{
 
        private MooTrackView mtv;
        private MooNote note;
-       private int columns;
-       private boolean selected;
-       private Rectangle pitchRect, veloRect;
-       public static final Color bgColor = new Color(160, 218, 255);
-       private String notePitch;
-       private String noteVelocity;
        private JPopupMenu popup;
+       private JMenu popupTranspUp, popupTranspDown;
        private JMenuItem popupRemove, popupProp;
+       private JMenuItem[] popupTranspUpItems, popupTranspDownItems;
+       private Rectangle pitchRect, veloRect;
+       private String notePitch, noteVelocity;
+       private boolean selected = false, leftMouseButtonPressed = false, mouseIn = false;
+       public Color textColor;
+       public static final Color bgColor = new Color(160, 218, 255);
+       public static final Color invBgColor = new Color(96, 38, 0);
 
        /** 
         * Creates a new note element.
-        * @param mn    the note that will be graphically represented
-        * @param rows  the number of rows that the note will occupy
+        * @param parent        The MooTrackView that this element will be painted on.
+        * @param mn            the note that will be graphically represented
         */
        public MooNoteElement (MooTrackView parent, MooNote mn) {
                mtv = parent;
                note = mn;
                calculateString();
-               columns = mn.getDuration() / (Moosique.getSequence().getResolution() / 4);
+               addMouseListener(new MAdapter());
                setBorder(BorderFactory.createLineBorder(Color.black));
                setBackground(bgColor);
-               addMouseListener(new MAdapter());
+               textColor = Color.black;
 
                // Defines coordinates.
                pitchRect = new Rectangle(0, 0, 15, 10);
@@ -42,41 +44,71 @@ public class MooNoteElement extends JPanel {
 
                // Creates pop-up menu.
                popup = new JPopupMenu();
-               PopupListener pList = new PopupListener();
-               popupProp = new JMenuItem("Preferences...");
-               popupProp.addActionListener(pList);
-               popup.add(popupProp);
-
-               popupRemove = new JMenuItem("Remove");
-               popupRemove.addActionListener(pList);
-               popup.add(popupRemove);
+               popupProp = addMenuItem(popup, "Preferences...");
+               popupRemove = addMenuItem(popup, "Remove");
+               popupTranspUpItems = new JMenuItem[12];
+               popupTranspDownItems = new JMenuItem[12];
+               popupTranspUp = createTransposeMenu(popup, popupTranspUpItems, "note up");
+               popupTranspDown = createTransposeMenu(popup, popupTranspDownItems, "note down");
+       }
 
+       /** 
+        * Returns the note of this element.
+        * @return the note
+        */
+       public MooNote getNote() {
+               return note;
        }
 
        /** 
-        * Returns true if the current NoteElement is selected, otherwise false.
-        * @return if the element is selected
+        * 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 boolean isSelected() {
-               return selected;
+       public int compareTo(Object o) {
+               return note.compareTo(((MooNoteElement)o).getNote());
        }
 
        /** 
         * Selects the current NoteElement.
-        * @param state if the element should be selected
         */
-       public void setSelected(boolean state) {
-               selected = state;
+       public void select() {
+               selected = true;
+               mtv.selectNote(this);
+               setBackground(invBgColor);
+               textColor = Color.white;
+               repaint();
+       }
+
+       /** 
+        * Deselects the current NoteElement.
+        */
+       public void deselect() {
+               selected = false;
+               // mtv.deselectNote(this);
+               setBackground(bgColor);
+               textColor = Color.black;
+               repaint();
+       }
+
+       /** 
+        * Transposes the current note element the given number of halftones.
+        * @param halftones     the number of halftones to transpose - positive for up, negative for down
+        */
+       public void transpose(int halftones) {
+               note.transpose(halftones);
+               update();
        }
 
        /**
-        *
+        * Draws the string that shows the note's properties.
+        * @param g     The Graphics object used to draw the strings.
         */
        public void paintComponent(Graphics g)
        {
                super.paintComponent(g);
                if (!(g instanceof Graphics2D)) return;
                Graphics2D g2 = (Graphics2D)g;
+               g2.setColor(textColor);
                g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
        /*
                switch(columns) {
@@ -86,10 +118,13 @@ public class MooNoteElement extends JPanel {
                }
        */
                
-               g2.drawString(notePitch, 1, 9);
-               g2.drawString("" + noteVelocity, 21, 9);
+               g2.drawString(notePitch, 1, 8);
+               g2.drawString("" + noteVelocity, 21, 8);
        }
 
+       /**
+        * Calculate what the string that shows the note properties should look like.
+        */
        protected void calculateString(){
 
                noteVelocity = ""; 
@@ -111,53 +146,177 @@ public class MooNoteElement extends JPanel {
                        case 10: notePitch = "A#"; break;
                        case 11: notePitch = "B";  break;
                }
-               notePitch += pitch / 12;
+               notePitch += pitch / 12 - 1;
                noteVelocity = ""+note.getVelocity();
        }
 
-       public MooNote getNote(){
-               return note;
+
+       /**
+        * Asks the MooTrackView that the note element is painted on to remove this element and the note.
+        */
+       protected void remove(){
+               mtv.removeNote(this);
+       }
+
+       /**
+        * Updates the graphical content of the element and repaints it.
+        */
+       public void update() {
+               calculateString();
+               repaint();
+       }
+
+       /**
+        * Layout this changed elemnt.
+        */
+       protected void newLayout(){
+               mtv.layoutElement(this,true);
+       }
+
+       /**
+        * 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;
+       }
+
+       /**
+        * Listener that checks the mouse actions on this element.
+        */
        class MAdapter extends MouseAdapter {
+       
+               /**
+                * Plays the note on double-click.
+                */
+               public void mouseClicked(MouseEvent e) {
+                       if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2)
+                               Moosique.getReceiver().send(note.getMessage(), -1);
+               }
+
+               /**
+                * Selects the note if mouse entered with the left mouse button pressed.
+                */
+               public void mouseEntered(MouseEvent e) {
+                       mouseIn = true;
+                       if (mtv.isLeftMouseButtonPressed()) {
+                               select();
+                       }
+               }
+
+               /**
+                * Registers mouse exited.
+                */
+               public void mouseExited(MouseEvent e) {
+                       mouseIn = false;
+                       Moosique.getReceiver().send(note.getNoteOffEvent().getMessage(), -1);
+               }
+
+               /**
+                * Checks if the mouse is pressed.
+                * Increases the pitch or velocity if the right mouse button is pressed while holding CTRL.
+                * Decreases the pitch or velocity if the left mouse button is pressed while holding CTRL.
+                * @param e the event recieved.
+                */
                public void mousePressed(MouseEvent e) {
                        if (e.isControlDown()) {
                                if (pitchRect.contains(e.getPoint())) {
                                        if (SwingUtilities.isRightMouseButton(e)) {
-                                               note.setPitch(note.getPitch() + 1);
+                                               note.transpose(1);
                                        } else if (SwingUtilities.isLeftMouseButton(e)) {
-                                               note.setPitch(note.getPitch() - 1);
+                                               note.transpose(-1);
                                        }
+                                       Moosique.setEdited();
                                        calculateString();
+                                       repaint();
                                } else if (veloRect.contains(e.getPoint())) {
                                        if (SwingUtilities.isRightMouseButton(e)) {
                                                note.setVelocity(note.getVelocity() + 1);
                                        } else if (SwingUtilities.isLeftMouseButton(e)) {
                                                note.setVelocity(note.getVelocity() - 1);
                                        }
+                                       Moosique.setEdited();
                                        calculateString();
+                                       repaint();
                                }
-                               e.getComponent().repaint();
+                       } else {
+                               select();
+                               maybeShowPopup(e);
                        }
-                       else if (e.isPopupTrigger()) {
-                               popup.show(e.getComponent(), e.getX(), e.getY());
+               }
+
+               public void mouseReleased(MouseEvent e) {
+                       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);
+                               
                        }
                }
+
+               /**
+                * Shows the menu if an OS-specific popup-trigger was activated.
+                */
+               private boolean maybeShowPopup(MouseEvent e) {
+                       if (!e.isPopupTrigger()) return false;
+                       if (!e.isControlDown()) {
+                               if (!selected || mtv.isTheOnlySelected((MooNoteElement)e.getComponent())) popup.show(e.getComponent(), e.getX(), e.getY());
+                               else mtv.showSelectionPopup(e.getComponent(), e.getX(), e.getY());
+                       }
+                       return true;
+               }
        }
 
+       /**
+        * Takes the appropriate action when a user selects an item on the popup menu.
+        */
        class PopupListener implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                        Object source = e.getSource();
-                       if  (source == popupProp) {
+                       if (source == popupProp) {
+                               System.out.println("Duration: " + note.getDuration());
                                new MooDialog(note);
-                       } else if  (source == popupRemove) {
+                               System.out.println("Duration: " + note.getDuration());
+                               newLayout();
+                               update();
+                       } else if (source == popupRemove) {
                                remove();
+                       } else if (source == popupTranspUpItems[0]) {
+                               transpose(12);
+                       } else if (source == popupTranspDownItems[0]) {
+                               transpose(-12);
+                       } else {
+                               for (int i = 1; i < 12; i++) {
+                                       if (source == popupTranspUpItems[i]) transpose(i);
+                                       else if (source == popupTranspDownItems[i]) transpose(-i);
+                               }
                        }
                }
        }
-
-       protected void remove(){
-               mtv.remove(this);
-       }
-
 }