X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=blobdiff_plain;f=MooToolbar.java;h=de416699065e7ba60b327b2c7dddc292f483bfc6;hp=92bf3457e083b11f79269e02e91590b02fc21dd3;hb=HEAD;hpb=fe67e0acf0d44c09dcfbbfd1a02a91f43d2cf60e diff --git a/MooToolbar.java b/MooToolbar.java index 92bf345..de41669 100644 --- a/MooToolbar.java +++ b/MooToolbar.java @@ -1,7 +1,7 @@ +import javax.sound.midi.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; -import javax.sound.midi.*; /** * The application's toolbar, with the most frequently used commands. @@ -10,11 +10,13 @@ import javax.sound.midi.*; */ public class MooToolbar extends JToolBar { - private JButton rewind, playpause, stop, fastforward; - private JLabel measure, beats, ticks, measureValue, beatsValue, ticksValue; + private JButton rewind, playpause, stop, fastforward, record; + 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); /** @@ -22,12 +24,14 @@ public class MooToolbar extends JToolBar { */ public MooToolbar() { + setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); // setAlignmentX(LEFT_ALIGNMENT); setFloatable(false); - mouseAdapter = new MooMouseAdapter(); + mouseAdapter = new MAdapter(); // Creates playback buttons rewind = createButton("images/rewind.gif", "Rewind"); + record = createButton("images/record.gif", "Record"); playpause = createButton("images/play.gif", "Play"); stop = createButton("images/stop.gif", "Stop"); fastforward = createButton("images/forward.gif", "Fast forward"); @@ -36,6 +40,7 @@ public class MooToolbar extends JToolBar { // Adds playback buttons add(rewind); + add(record); add(playpause); add(stop); add(fastforward); @@ -44,7 +49,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(); @@ -61,11 +66,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); - } /** @@ -73,27 +77,42 @@ 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(1 + measures)); - beatsValue.setText(Long.toString(1 + beats)); - ticksValue.setText(Long.toString(1 + ticks)); + System.out.print("Updating to " + tickPosition + " = "); + if (tickPosition == 0) { + resetProgInd(); + System.out.println("1:1:1"); + } else { + int[] position = Moosique.getPositionForTicks(tickPosition); + System.out.println("" + (position[0] + 1) + ":" + (position[1] + 1) + ":" + (position[2] + 1)); + measuresValue.setText(Integer.toString(position[0] + 1)); + beatsValue.setText(Long.toString(position[1] + 1)); + ticksValue.setText(Long.toString(position[2] + 1)); + } } - /* - * Creates a button with the specified image and tooltip. + /** + * Resets the progress indicator. + */ + public void resetProgInd() { + measuresValue.setText("1"); + beatsValue.setText("1"); + ticksValue.setText("1"); + updatePlayButton("Play", playIcon); + ticksPerBeat = Moosique.getSequence().getResolution(); + } + + /** + * Creates a button with the specified image and action command / tooltip. */ - private JButton createButton(String imagelocation, String tooltip) { + private JButton createButton(String imagelocation, String command) { JButton button = new JButton (new ImageIcon(imagelocation)); - button.setToolTipText(tooltip); + button.setToolTipText(command); + button.setActionCommand(command); button.addMouseListener(mouseAdapter); return button; } - /* + /** * Creates labels with the specified text and font size. */ private JLabel createLabel(String title, int fontSize){ @@ -102,44 +121,86 @@ 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 { + /** + * Updates the play button with the given command and icon. + */ + private void updatePlayButton(String command, Icon icon) { + playpause.setIcon(icon); + playpause.setActionCommand(command); + playpause.setToolTipText(command); + } + + class MAdapter extends MouseAdapter { public void mouseClicked(MouseEvent e) { - if (((JButton)e.getSource()).getToolTipText() == "Play") { - playpause.setIcon(pauseIcon); - playpause.setToolTipText("Pause"); + if (e.getSource() instanceof JButton) { + String command = ((JButton)e.getSource()).getActionCommand(); + if (command == "Play") { + updatePlayButton("Pause", pauseIcon); Moosique.play(); - } else if (((JButton)e.getSource()).getToolTipText() == "Pause") { - playpause.setIcon(playIcon); - playpause.setToolTipText("Resume"); + } else if (command == "Pause") { + updatePlayButton("Resume", playIcon); Moosique.pause(); - } else if (((JButton)e.getSource()).getToolTipText() == "Resume") { - playpause.setIcon(pauseIcon); - playpause.setToolTipText("Pause"); + } else if (command == "Resume") { + updatePlayButton("Pause", pauseIcon); Moosique.resume(); - } else if (((JButton)e.getSource()).getToolTipText() == "Stop") { - playpause.setIcon(playIcon); - playpause.setToolTipText("Play"); - Moosique.stop(); + } else if (command == "Stop") { + updatePlayButton("Play", playIcon); + Moosique.stop(); + } else if (command == "Record") { + MooDialog recordDialog = new MooDialog(MooDialog.RECORD); + } else if (command == "Rewind") { + updatePlayButton("Play", playIcon); + Moosique.setEditPosition(0); + Moosique.stop(); + } else if (command == "Fast forward") { + + } + } 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); +/* public void mousePressed(MouseEvent e) { + 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 +*/ } +}