From 359bad2e3d5cec5edfe8d505fc63a33ab8481458 Mon Sep 17 00:00:00 2001 From: DC37 Date: Thu, 2 Jan 2014 22:10:57 -0800 Subject: [PATCH] End-of-file behaviour has been fixed. --- src/smp/components/Values.java | 2 +- src/smp/components/controls/ArrowButton.java | 9 ++- src/smp/components/staff/Staff.java | 72 +++++++++----------- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/smp/components/Values.java b/src/smp/components/Values.java index dc13a826..5b672eb3 100644 --- a/src/smp/components/Values.java +++ b/src/smp/components/Values.java @@ -70,7 +70,7 @@ public class Values { /** * The number of lines in the staff, by default. This number - * is typically 384. + * is typically 400. */ public static int DEFAULT_LINES_PER_SONG = 400; diff --git a/src/smp/components/controls/ArrowButton.java b/src/smp/components/controls/ArrowButton.java index a308a8d9..905e8390 100644 --- a/src/smp/components/controls/ArrowButton.java +++ b/src/smp/components/controls/ArrowButton.java @@ -25,6 +25,9 @@ public class ArrowButton extends ImagePushButton { */ private double skipAmount; + /** Tells us whether we're at the end of the file. */ + private boolean endOfFile = false; + /** This is the slider that the scrollbar will affect. */ private Slider scrollbar; @@ -62,7 +65,7 @@ public double getSkipAmount() { protected void reactPressed(MouseEvent event) { super.reactPressed(event); scrollbar.adjustValue(scrollbar.getValue() + skipAmount); - if (scrollbar.getMax() <= scrollbar.getValue()) { + if (scrollbar.getMax() <= scrollbar.getValue() && endOfFile) { scrollbar.setMax(scrollbar.getMax() + Values.NOTELINES_IN_THE_WINDOW * 2); StaffSequence s = theStaff.getSequence(); @@ -71,6 +74,10 @@ protected void reactPressed(MouseEvent event) { + Values.NOTELINES_IN_THE_WINDOW * 2; i++) s.addLine(new StaffNoteLine(i)); } + if (scrollbar.getMax() <= scrollbar.getValue()) + endOfFile = true; + else + endOfFile = false; } @Override diff --git a/src/smp/components/staff/Staff.java b/src/smp/components/staff/Staff.java index 1f99bc49..19e312b1 100644 --- a/src/smp/components/staff/Staff.java +++ b/src/smp/components/staff/Staff.java @@ -131,11 +131,21 @@ public synchronized void redraw() { setLocation(StateMachine.getMeasureLineNum()); } + /** Turns off all highlights in the play bars in the staff. */ + private void highlightsOff() { + ArrayList playBars = staffImages.getPlayBars(); + for(ImageView i : playBars) { + i.setImage(ImageLoader.getSpriteFX( + ImageIndex.NONE)); + } + } + /** * Begins animation of the Staff. */ public void startSong() { + highlightsOff(); lastLine = findLastLine(); if (lastLine == 0 && theSequence.getLine(0).isEmpty()) { theControls.getStopButton().reactPressed(null); @@ -151,11 +161,10 @@ public void startSong() { */ private int findLastLine() { ArrayList lines = theSequence.getTheLines(); - for (int i = lines.size() - 1; i >= 0; i--) { + for (int i = lines.size() - 1; i >= 0; i--) if (!lines.get(i).isEmpty()) { return i; } - } return 0; } @@ -164,11 +173,18 @@ private int findLastLine() { */ public void stopSong() { songPlaying = false; - for (ImageView i : staffImages.getPlayBars()) { - i.setImage(ImageLoader.getSpriteFX(ImageIndex.NONE)); - } animationService.cancel(); animationService.reset(); + try { + Thread.sleep(1); + while (true) { + if (!animationService.isRunning()) + break; + } + } catch (InterruptedException e) { + + } + highlightsOff(); } /** @@ -272,7 +288,7 @@ public static ImageIndex switchAcc(int acc) { * */ public void setTempo(double tempo) { - double mill = (1 / (tempo / 60.0)) * 1000; + double mill = (60.0 / tempo) * 1000; delayMillis = (int) mill; double nano = (mill - delayMillis) * Math.pow(10, 6); delayNanos = (int) nano; @@ -292,6 +308,9 @@ public void setCurrVal(DoubleProperty d) { */ class AnimationService extends Service { + /** Whether we have zeroed the staff or not. */ + private boolean zero = false; + @Override protected Task createTask() { return new AnimationTask(); @@ -305,15 +324,13 @@ protected Task createTask() { */ private void bumpHighlights(ArrayList playBars, int index, boolean advance) { - for (ImageView i : playBars) { - i.setImage( - ImageLoader.getSpriteFX(ImageIndex.NONE)); - } + highlightsOff(); playBars.get(index).setImage( ImageLoader.getSpriteFX(ImageIndex.PLAY_BAR1)); - if (advance) { + if (advance && !zero) shiftStaff(); - } + if (zero) + zero = false; } @@ -370,11 +387,6 @@ class AnimationTask extends Task { */ private int index = 0; - /** - * This is the current measure line. - */ - private int currMeasureLine = 0; - /** * Whether we need to advance the satff ahead by a few lines or not. */ @@ -386,19 +398,16 @@ class AnimationTask extends Task { @Override protected Staff call() throws Exception { playBars = staffImages.getPlayBars(); - currMeasureLine = StateMachine.getMeasureLineNum(); - int counter = currMeasureLine; + int counter = StateMachine.getMeasureLineNum(); do { playNextLine(); counter++; - if (counter % Values.NOTELINES_IN_THE_WINDOW == 0) - currMeasureLine += Values.NOTELINES_IN_THE_WINDOW; - if (counter % 4 == 0 && counter > lastLine) { + if (counter > lastLine && counter % 4 == 0) { if (StateMachine.isLoopPressed()) { - currMeasureLine = 0; counter = 0; index = 0; zeroStaff(); + zero = true; } else { songPlaying = false; } @@ -408,20 +417,12 @@ protected Staff call() throws Exception { } catch (InterruptedException e) { // Do nothing } - } while (songPlaying && currMeasureLine - <= Values.DEFAULT_LINES_PER_SONG); - highlightsOff(); + } while (songPlaying); hitStop(); + highlightsOff(); return theMatrix.getStaff(); } - /** Turns off all highlights in the play bars in the staff. */ - private void highlightsOff() { - for(ImageView i : playBars) { - i.setImage(ImageLoader.getSpriteFX( - ImageIndex.NONE)); - } - } /** Hits the stop button for the song. */ private void hitStop() { @@ -435,11 +436,6 @@ private void hitStop() { * they are. */ private void playNextLine() { - if (StateMachine.getMeasureLineNum() >= - Values.DEFAULT_LINES_PER_SONG - - Values.NOTELINES_IN_THE_WINDOW) { - songPlaying = false; - } bumpHighlights(playBars, index, advance); playSoundLine(index); advance = false;