X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=MooNoteElement.java;h=1c6dadf50f1547fd7235441d7bb4d3f6956a413d;hb=d2e307b3375152f1b47c59bc05210c96dc99f679;hp=f6bda05e3b447642af0acffa007f5083e6eaf915;hpb=078f6f3d4afcd990dadf7cf5f05b2af39ea7b3a9;p=moosique.git diff --git a/MooNoteElement.java b/MooNoteElement.java index f6bda05..1c6dadf 100644 --- a/MooNoteElement.java +++ b/MooNoteElement.java @@ -14,10 +14,10 @@ public class MooNoteElement extends JPanel { private MooTrackView mtv; private MooNote note; private JPopupMenu popup; - private JMenuItem popupRemove, popupProp, popupTransposeOctUp, popupTransposeOctDown; + private JMenuItem popupRemove, popupProp, popupTranspOctUp, popupTranspOctDown; private Rectangle pitchRect, veloRect; private String notePitch, noteVelocity; - private boolean selected = false; + 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); @@ -25,7 +25,7 @@ public class MooNoteElement extends JPanel { /** * Creates a new note element. * @param parent The MooTrackView that this element will be painted on. - * @param mn the note that will be graphically represented + * @param mn the note that will be graphically represented */ public MooNoteElement (MooTrackView parent, MooNote mn) { mtv = parent; @@ -49,12 +49,12 @@ public class MooNoteElement extends JPanel { 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); + popupTranspOctUp = new JMenuItem("Transpose one octave up"); + popupTranspOctUp.addActionListener(pList); + popup.add(popupTranspOctUp); + popupTranspOctDown = new JMenuItem("Transpose one octave down"); + popupTranspOctDown.addActionListener(pList); + popup.add(popupTranspOctDown); } /** @@ -70,7 +70,7 @@ public class MooNoteElement extends JPanel { */ public void select() { selected = true; - mtv.addSelected(this); + mtv.selectNote(this); setBackground(invBgColor); textColor = Color.white; repaint(); @@ -81,12 +81,21 @@ public class MooNoteElement extends JPanel { */ public void deselect() { selected = false; - // mtv.removeSelected(this); + // 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. @@ -134,7 +143,7 @@ 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(); } @@ -147,26 +156,42 @@ public class MooNoteElement extends JPanel { } /** - * layout this changed elemnt + * 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); } + /** * Listener that checks the mouse actions on this element. */ class MAdapter extends MouseAdapter { /** - * If left mouse button is clicked, selects the note and plays it. + * Selects the note if mouse entered with the left mouse button pressed. */ - public void mouseClicked(MouseEvent e) { - if (SwingUtilities.isLeftMouseButton(e) && !e.isControlDown()) { + public void mouseEntered(MouseEvent e) { + mouseIn = true; + if (mtv.isLeftMouseButtonPressed()) { select(); - // Play the note } } + /** + * Registers mouse exited. + */ + public void mouseExited(MouseEvent e) { + mouseIn = false; + } + /** * Checks if the mouse is pressed. * Increases the pitch or velocity if the right mouse button is pressed while holding CTRL. @@ -177,9 +202,9 @@ public class MooNoteElement extends JPanel { 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(); @@ -194,21 +219,27 @@ public class MooNoteElement extends JPanel { calculateString(); repaint(); } - } else maybeShowPopup(e); + } else { + select(); + // Play the note + maybeShowPopup(e); + } } public void mouseReleased(MouseEvent e) { - maybeShowPopup(e); + if (!maybeShowPopup(e) && !mouseIn) mtv.maybeMoveSelectedNotes(getY(), getY() + e.getY()); } /** * Shows the menu if an OS-specific popup-trigger was activated. */ - private void maybeShowPopup(MouseEvent e) { - if (e.isPopupTrigger() && !e.isControlDown()) { + 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; } } @@ -221,21 +252,14 @@ public class MooNoteElement extends JPanel { if (source == popupProp) { new MooDialog(note); newLayout(); + repaint(); } 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(); + } else if (source == popupTranspOctUp) { + transpose(12); + } else if (source == popupTranspOctDown) { + transpose(-12); } } - - private void update() { - calculateString(); - repaint(); - } - } }