diff --git a/src/main/java/seng302/Controllers/StartController.java b/src/main/java/seng302/Controllers/StartController.java index 6fd543f2..91452a85 100644 --- a/src/main/java/seng302/Controllers/StartController.java +++ b/src/main/java/seng302/Controllers/StartController.java @@ -1,5 +1,7 @@ package seng302.Controllers; +import javafx.animation.AnimationTimer; +import javafx.application.Platform; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; @@ -11,7 +13,6 @@ import javafx.scene.layout.AnchorPane; import javafx.scene.layout.GridPane; import org.xml.sax.SAXException; import seng302.Model.BoatInRace; -import seng302.Model.Race; import seng302.Model.RaceClock; import seng302.RaceXMLReader; @@ -32,6 +33,8 @@ public class StartController extends Controller { @FXML private TableColumn boatNameColumn; @FXML private TableColumn boatCodeColumn; @FXML private Label timeZoneTime; + @FXML private Label timer; + @FXML private int PRERACE_TIME = 15000; private RaceClock raceClock; @@ -56,10 +59,8 @@ public class StartController extends Controller { startRace(1); } - private void startRace(Integer raceScale){ - start.setVisible(false); - - parent.beginRace(raceScale); + private void startRace(int raceScale){ + countdownTimer(raceScale); } @Override @@ -95,7 +96,54 @@ public class StartController extends Controller { raceClock = new RaceClock(raceXMLReader.getMark()); timeZoneTime.textProperty().bind(raceClock.timeProperty()); + } + + /** + * Updates the calculated time to the timer label + * + * @param time The calculated time from calcTimer() method + */ + protected void updateTime(String time) { + Platform.runLater(() -> { + timer.setText(time); + }); + } + + /** + * Countdown timer until race starts. Use PRERACE_TIME to set countdown duration. + */ + protected void countdownTimer(int scaleFactor) { + new AnimationTimer() { + long currentTime = System.currentTimeMillis(); + long startTime = currentTime + (PRERACE_TIME/scaleFactor); + long minutes; + long currentTimeInSeconds; + long remainingSeconds; + long hours; + long timeLeft; + + @Override + public void handle(long arg0) { + timeLeft = startTime - currentTime; + if (timeLeft <= 0) { + updateTime("Race is starting..."); + stop(); + parent.beginRace(scaleFactor); + startWrapper.setVisible(false); + + } else { + currentTimeInSeconds = (timeLeft*scaleFactor) / 1000; + minutes = currentTimeInSeconds / 60; + remainingSeconds = currentTimeInSeconds % 60; + hours = minutes / 60; + minutes = minutes % 60; + updateTime(String.format("Race Clock: -%02d:%02d:%02d", hours, minutes, remainingSeconds)); + + } + currentTime = System.currentTimeMillis(); + } + }.start(); } } diff --git a/src/main/resources/scenes/start.fxml b/src/main/resources/scenes/start.fxml index 677fc861..c0ae7e9e 100644 --- a/src/main/resources/scenes/start.fxml +++ b/src/main/resources/scenes/start.fxml @@ -1,23 +1,18 @@ - - - - - + - - + @@ -56,6 +51,7 @@