X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=MooToolbar.java;h=43027c5a7ddbb75cd4f2191c0f6d8484b261332a;hb=d564984e345995ca352c4f5eee3296c6e02010de;hp=54b90db40f83a871e82fc4b3a666436fa2900ee9;hpb=5380690b9fc56b683d15765382669d79c50d3414;p=moosique.git diff --git a/MooToolbar.java b/MooToolbar.java index 54b90db..43027c5 100644 --- a/MooToolbar.java +++ b/MooToolbar.java @@ -11,9 +11,12 @@ import javax.sound.midi.*; public class MooToolbar extends JToolBar { private JButton rewind, playpause, stop, fastforward; - private JLabel measure, beats, ticks, measureValue, beatsValue, ticksValue; + private JLabel measure, beats, ticks, measuresValue, beatsValue, ticksValue; + private JPanel progIndPanel; private ImageIcon playIcon, pauseIcon; - private MooMouseAdapter mouseAdapter; + private MAdapter mouseAdapter; + private int ticksPerBeat; + public static final int beatsPerMeasure = 4; public static final Color bgColor = new Color(192, 224, 255); /** @@ -23,7 +26,7 @@ public class MooToolbar extends JToolBar { public MooToolbar() { // setAlignmentX(LEFT_ALIGNMENT); setFloatable(false); - mouseAdapter = new MooMouseAdapter(); + mouseAdapter = new MAdapter(); // Creates playback buttons rewind = createButton("images/rewind.gif", "Rewind"); @@ -43,7 +46,7 @@ public class MooToolbar extends JToolBar { measure = createLabel("Msr", 10); beats = createLabel("Beat", 10); ticks = createLabel("Tick", 10); - measureValue = formatProgInd(createLabel("1", 16)); + measuresValue = formatProgInd(createLabel("1", 16)); beatsValue = formatProgInd(createLabel("1", 16)); ticksValue = formatProgInd(createLabel("1", 16)); JPanel spacenorth = new JPanel(); @@ -52,7 +55,7 @@ public class MooToolbar extends JToolBar { spacesouth.setBackground(bgColor); // Creates progress indicator panel and adds components - JPanel progIndPanel = new JPanel(); + progIndPanel = new JPanel(); progIndPanel.setMaximumSize(new Dimension(120,27)); progIndPanel.setLayout(new GridLayout(2,4)); progIndPanel.add(spacenorth); @@ -60,11 +63,10 @@ public class MooToolbar extends JToolBar { progIndPanel.add(beats); progIndPanel.add(ticks); progIndPanel.add(spacesouth); - progIndPanel.add(measureValue); + progIndPanel.add(measuresValue); progIndPanel.add(beatsValue); progIndPanel.add(ticksValue); add(progIndPanel); - } /** @@ -72,17 +74,33 @@ public class MooToolbar extends JToolBar { * @param tickPosition the tick position to visualize */ public void updateProgInd(long tickPosition) { - int ticksPerBeat = Moosique.getSequence().getResolution(); - int beatsPerMeasure = 4; - long measures = tickPosition / (beatsPerMeasure * ticksPerBeat); - long beats = (tickPosition - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat; - long ticks = tickPosition - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat; - measureValue.setText(Long.toString(measures)); - beatsValue.setText(Long.toString(beats)); - ticksValue.setText(Long.toString(ticks)); + ticksPerBeat = Moosique.getSequence().getResolution(); + if (tickPosition == 0) { + resetProgInd(); + } else { + // Otherwise, calculates the current song position in measures, beats and ticks. + long measures = tickPosition / (beatsPerMeasure * ticksPerBeat); + long beats = (tickPosition - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat; + long ticks = tickPosition - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat; + measuresValue.setText(Long.toString(1 + measures)); + beatsValue.setText(Long.toString(1 + beats)); + ticksValue.setText(Long.toString(1 + ticks)); + } } - /* + /** + * Resets the progress indicator. + */ + public void resetProgInd() { + measuresValue.setText("1"); + beatsValue.setText("1"); + ticksValue.setText("1"); + playpause.setIcon(playIcon); + playpause.setToolTipText("Play"); + ticksPerBeat = Moosique.getSequence().getResolution(); + } + + /** * Creates a button with the specified image and tooltip. */ private JButton createButton(String imagelocation, String tooltip) { @@ -92,7 +110,7 @@ public class MooToolbar extends JToolBar { return button; } - /* + /** * Creates labels with the specified text and font size. */ private JLabel createLabel(String title, int fontSize){ @@ -101,44 +119,71 @@ public class MooToolbar extends JToolBar { return label; } - /* + /** * Formats the given label for the progress indicator. */ private JLabel formatProgInd(JLabel label){ + label.addMouseListener(mouseAdapter); label.setBorder(BorderFactory.createLineBorder(Color.black)); label.setBackground(Color.white); return label; } - class MooMouseAdapter extends MouseAdapter { + class MAdapter extends MouseAdapter { public void mouseClicked(MouseEvent e) { - if (((JButton)e.getSource()).getToolTipText() == "Play") { + if (e.getSource() instanceof JButton) { + String toolTip = ((JButton)e.getSource()).getToolTipText(); + if (toolTip == "Play") { playpause.setIcon(pauseIcon); playpause.setToolTipText("Pause"); Moosique.play(); - } else if (((JButton)e.getSource()).getToolTipText() == "Pause") { + } else if (toolTip == "Pause") { playpause.setIcon(playIcon); playpause.setToolTipText("Resume"); Moosique.pause(); - } else if (((JButton)e.getSource()).getToolTipText() == "Resume") { + } else if (toolTip == "Resume") { playpause.setIcon(pauseIcon); playpause.setToolTipText("Pause"); Moosique.resume(); - } else if (((JButton)e.getSource()).getToolTipText() == "Stop") { - playpause.setIcon(playIcon); - playpause.setToolTipText("Play"); - Moosique.stop(); + } else if (toolTip == "Stop") { + Moosique.stop(); + } + } else if (e.getSource() instanceof JLabel) { + long position = Moosique.getEditPosition(); + if (e.getSource().equals(measuresValue)) { + if (SwingUtilities.isRightMouseButton(e)) { + position += beatsPerMeasure * ticksPerBeat; + } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) { + position -= beatsPerMeasure * ticksPerBeat; + } + } else if (e.getSource().equals(beatsValue)) { + if (SwingUtilities.isRightMouseButton(e)) { + position += ticksPerBeat; + } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) { + position -= ticksPerBeat; + } + } else if (e.getSource().equals(ticksValue)) { + if (SwingUtilities.isRightMouseButton(e)) { + position += 1; + } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) { + position -= 1; + } + } + Moosique.setEditPosition(position); + Moosique.getGUI().update(position); } } public void mousePressed(MouseEvent e) { - if (((JButton)e.getSource()).getToolTipText() == "Rewind") { - Moosique.rewind(96); - } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") { - Moosique.forward(96); + if (e.getSource() instanceof JButton) { + if (((JButton)e.getSource()).getToolTipText() == "Rewind") { + Moosique.rewind(beatsPerMeasure * ticksPerBeat); + } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") { + Moosique.forward(beatsPerMeasure * ticksPerBeat); + } } } public void mouseReleased(MouseEvent e) {} } -} \ No newline at end of file +}