From 4b5dbec45c4a25a2c22f9c747817144fc66e6054 Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Sat, 3 Dec 2016 16:47:21 +0100
Subject: [PATCH] Fixed #38, Fixed #36

---
 bin/de/bricked/ui/LevelGUI.fxml          |  8 +--
 src/de/bricked/ui/LevelController.java   | 81 +++++++++++++-----------
 src/de/bricked/ui/LevelGUI.fxml          |  8 +--
 src/de/bricked/utils/CountdownTimer.java | 24 +++++--
 4 files changed, 73 insertions(+), 48 deletions(-)

diff --git a/bin/de/bricked/ui/LevelGUI.fxml b/bin/de/bricked/ui/LevelGUI.fxml
index dabc3a8..8884041 100644
--- a/bin/de/bricked/ui/LevelGUI.fxml
+++ b/bin/de/bricked/ui/LevelGUI.fxml
@@ -8,9 +8,9 @@
 <?import javafx.scene.layout.VBox?>
 <?import javafx.scene.text.Font?>
 
-<AnchorPane fx:id="anchorPane" prefHeight="800.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.bricked.ui.LevelController">
+<AnchorPane fx:id="anchorPane" prefHeight="800.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.bricked.ui.LevelController">
    <children>
-      <AnchorPane fx:id="anchorPaneGame" layoutX="22.0" layoutY="125.0" minHeight="650.0" minWidth="700.0" prefHeight="650.0" prefWidth="700.0" AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="22.0" AnchorPane.rightAnchor="78.0" AnchorPane.topAnchor="125.0">
+      <AnchorPane fx:id="anchorPaneGame" layoutX="14.0" layoutY="125.0" minHeight="650.0" minWidth="670.0" prefHeight="650.0" prefWidth="670.0" AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="116.0" AnchorPane.topAnchor="125.0">
          <children>
             <Label fx:id="labelMultiplicator" alignment="CENTER" contentDisplay="CENTER" layoutX="301.0" layoutY="502.0" prefHeight="45.0" prefWidth="100.0" textAlignment="CENTER">
                <font>
@@ -18,7 +18,7 @@
                </font>
             </Label>
          </children></AnchorPane>
-      <VBox fx:id="vboxPowerUps" layoutX="745.0" layoutY="125.0" minHeight="338.0" minWidth="40.0" prefHeight="402.0" prefWidth="40.0" AnchorPane.bottomAnchor="273.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="125.0" />
+      <VBox fx:id="vboxPowerUps" layoutX="745.0" layoutY="125.0" minHeight="338.0" minWidth="40.0" prefHeight="402.0" prefWidth="90.0" AnchorPane.bottomAnchor="273.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="125.0" />
       <HBox alignment="CENTER" layoutX="34.0" layoutY="23.0" prefHeight="100.0" prefWidth="275.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
          <children>
             <Button fx:id="buttonBack" mnemonicParsing="false" onAction="#back" text="Back" />
@@ -64,7 +64,7 @@
             </Label>
          </children>
       </HBox>
-      <VBox fx:id="vboxLives" layoutX="745.0" layoutY="603.0" minHeight="71.0" minWidth="40.0" prefHeight="227.0" prefWidth="40.0" AnchorPane.bottomAnchor="24.0" AnchorPane.rightAnchor="15.0" />
+      <VBox fx:id="vboxLives" layoutX="745.0" layoutY="603.0" minHeight="71.0" minWidth="40.0" prefHeight="237.0" prefWidth="90.0" AnchorPane.bottomAnchor="24.0" AnchorPane.rightAnchor="14.0" />
       <Label fx:id="labelFPS" layoutX="770.0" layoutY="6.0" text="0" AnchorPane.rightAnchor="8.0" AnchorPane.topAnchor="8.0">
          <font>
             <Font name="System Bold" size="16.0" />
diff --git a/src/de/bricked/ui/LevelController.java b/src/de/bricked/ui/LevelController.java
index df51672..9c95c13 100644
--- a/src/de/bricked/ui/LevelController.java
+++ b/src/de/bricked/ui/LevelController.java
@@ -49,6 +49,8 @@ import javafx.scene.input.MouseEvent;
 import javafx.scene.layout.AnchorPane;
 import javafx.scene.layout.ColumnConstraints;
 import javafx.scene.layout.GridPane;
+import javafx.scene.layout.HBox;
+import javafx.scene.layout.Priority;
 import javafx.scene.layout.RowConstraints;
 import javafx.scene.layout.StackPane;
 import javafx.scene.layout.VBox;
@@ -101,7 +103,7 @@ public class LevelController
 	private double oldMousePosition;
 	private static ArrayList<Label> brickLabels;
 	private ArrayList<Label> movingPowerUps;
-	private ArrayList<Label> timedPowerUps;
+	private ArrayList<CountdownTimer> timedPowerUps;
 	private long previousTime = 0;
 	private float secondsElapsedSinceLastFpsUpdate = 0f;
 
@@ -902,22 +904,43 @@ public class LevelController
 
 	private void addTimedPowerUp(PowerUp powerUp)
 	{
-		Label labelPowerUp = new Label(String.valueOf(powerUp.getDurationInSeconds()));
-		labelPowerUp.setStyle("-fx-background-image: url(\"de/bricked/resources/textures/powerups/" + powerUp.getID() + ".png\");" + "-fx-background-position: center center;" + "-fx-background-repeat: no-repeat;" + "-fx-background-size: contain;" + "-fx-font-size: 16;" + "-fx-font-weight: bold;"
-				+ "-fx-text-fill: #000000;");
-		labelPowerUp.setAlignment(Pos.CENTER);
-		labelPowerUp.setUserData(powerUp);
-
-		labelPowerUp.setPrefWidth(35);
-		labelPowerUp.setPrefHeight(20);
-
-		vboxPowerUps.getChildren().add(labelPowerUp);
-
-		timedPowerUps.add(labelPowerUp);
-
-		new CountdownTimer(powerUp.getDurationInSeconds(), labelPowerUp, this);
+		boolean alreadyActivated = false;
+		
+		for(CountdownTimer currentTimer : timedPowerUps)
+		{
+			PowerUp currentPowerUp = (PowerUp)currentTimer.getHBox().getUserData();
+			if(currentPowerUp.getID() == powerUp.getID())
+			{				
+				alreadyActivated = true;
+				currentTimer.addSecondsToTimer(powerUp.getDurationInSeconds());
+				break;
+			}
+		}	
+		
+		if(!alreadyActivated)
+		{
+			HBox hbox = new HBox();			
+			Label labelIcon = new Label();
+			labelIcon.setStyle("-fx-background-image: url(\"de/bricked/resources/textures/powerups/" + powerUp.getID() + ".png\");" + "-fx-background-position: center center;" + "-fx-background-repeat: no-repeat;" + "-fx-background-size: contain;");				
+			labelIcon.setPrefWidth(35);
+			labelIcon.setPrefHeight(20);
+			
+			Label labelSeconds = new Label(String.valueOf(powerUp.getDurationInSeconds()));
+			labelSeconds.setStyle("-fx-font-size: 16;" + "-fx-font-weight: bold;");
+			
+			hbox.getChildren().add(labelIcon);
+			hbox.getChildren().add(labelSeconds);
+			HBox.setHgrow(labelSeconds, Priority.ALWAYS);
+			HBox.setMargin(labelSeconds, new Insets(0, 0, 0, 7));
+			hbox.setAlignment(Pos.CENTER_LEFT);
+						
+			hbox.setUserData(powerUp);	
+			vboxPowerUps.getChildren().add(hbox);
+	
+			timedPowerUps.add(new CountdownTimer(powerUp.getDurationInSeconds(), hbox, this));
+		}	
 	}
-
+	
 	private void resetPowerUps()
 	{
 		clearMovingPowerUps();
@@ -926,26 +949,12 @@ public class LevelController
 		vboxPowerUps.getChildren().clear();
 	}
 
-	public void deactivatePowerUp(Label label)
-	{
-		int counter = 0;
-		PowerUp powerUp = (PowerUp)label.getUserData();
-		for(Label currentLabel : timedPowerUps)
-		{
-			PowerUp currentPowerUp = (PowerUp)currentLabel.getUserData();
-			if(currentPowerUp.getID() == powerUp.getID())
-			{
-				counter++;
-			}
-		}
-
-		// if no other power ups of same kind are active
-		if(counter <= 1)
-		{
-			vboxPowerUps.getChildren().remove(label);
-			timedPowerUps.remove(label);
-			powerUp.deactivate(this, game);
-		}
+	public void deactivatePowerUp(CountdownTimer timer, HBox hbox)
+	{		
+		PowerUp powerUp = (PowerUp)hbox.getUserData();		
+		vboxPowerUps.getChildren().remove(hbox);
+		timedPowerUps.remove(timer);
+		powerUp.deactivate(this, game);		
 	}
 
 	public void showLabelFPS(boolean value)
diff --git a/src/de/bricked/ui/LevelGUI.fxml b/src/de/bricked/ui/LevelGUI.fxml
index dabc3a8..8884041 100644
--- a/src/de/bricked/ui/LevelGUI.fxml
+++ b/src/de/bricked/ui/LevelGUI.fxml
@@ -8,9 +8,9 @@
 <?import javafx.scene.layout.VBox?>
 <?import javafx.scene.text.Font?>
 
-<AnchorPane fx:id="anchorPane" prefHeight="800.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.bricked.ui.LevelController">
+<AnchorPane fx:id="anchorPane" prefHeight="800.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.bricked.ui.LevelController">
    <children>
-      <AnchorPane fx:id="anchorPaneGame" layoutX="22.0" layoutY="125.0" minHeight="650.0" minWidth="700.0" prefHeight="650.0" prefWidth="700.0" AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="22.0" AnchorPane.rightAnchor="78.0" AnchorPane.topAnchor="125.0">
+      <AnchorPane fx:id="anchorPaneGame" layoutX="14.0" layoutY="125.0" minHeight="650.0" minWidth="670.0" prefHeight="650.0" prefWidth="670.0" AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="116.0" AnchorPane.topAnchor="125.0">
          <children>
             <Label fx:id="labelMultiplicator" alignment="CENTER" contentDisplay="CENTER" layoutX="301.0" layoutY="502.0" prefHeight="45.0" prefWidth="100.0" textAlignment="CENTER">
                <font>
@@ -18,7 +18,7 @@
                </font>
             </Label>
          </children></AnchorPane>
-      <VBox fx:id="vboxPowerUps" layoutX="745.0" layoutY="125.0" minHeight="338.0" minWidth="40.0" prefHeight="402.0" prefWidth="40.0" AnchorPane.bottomAnchor="273.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="125.0" />
+      <VBox fx:id="vboxPowerUps" layoutX="745.0" layoutY="125.0" minHeight="338.0" minWidth="40.0" prefHeight="402.0" prefWidth="90.0" AnchorPane.bottomAnchor="273.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="125.0" />
       <HBox alignment="CENTER" layoutX="34.0" layoutY="23.0" prefHeight="100.0" prefWidth="275.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
          <children>
             <Button fx:id="buttonBack" mnemonicParsing="false" onAction="#back" text="Back" />
@@ -64,7 +64,7 @@
             </Label>
          </children>
       </HBox>
-      <VBox fx:id="vboxLives" layoutX="745.0" layoutY="603.0" minHeight="71.0" minWidth="40.0" prefHeight="227.0" prefWidth="40.0" AnchorPane.bottomAnchor="24.0" AnchorPane.rightAnchor="15.0" />
+      <VBox fx:id="vboxLives" layoutX="745.0" layoutY="603.0" minHeight="71.0" minWidth="40.0" prefHeight="237.0" prefWidth="90.0" AnchorPane.bottomAnchor="24.0" AnchorPane.rightAnchor="14.0" />
       <Label fx:id="labelFPS" layoutX="770.0" layoutY="6.0" text="0" AnchorPane.rightAnchor="8.0" AnchorPane.topAnchor="8.0">
          <font>
             <Font name="System Bold" size="16.0" />
diff --git a/src/de/bricked/utils/CountdownTimer.java b/src/de/bricked/utils/CountdownTimer.java
index f5a8078..83d2763 100644
--- a/src/de/bricked/utils/CountdownTimer.java
+++ b/src/de/bricked/utils/CountdownTimer.java
@@ -6,14 +6,19 @@ import java.util.TimerTask;
 import de.bricked.ui.LevelController;
 import javafx.application.Platform;
 import javafx.scene.control.Label;
+import javafx.scene.layout.HBox;
 
 public class CountdownTimer
 {
-	private int count;
+	private int count;	
+	private HBox hbox;	
 
-	public CountdownTimer(int seconds, Label label, LevelController levelController)
+	public CountdownTimer(int seconds, HBox hbox, LevelController levelController)
 	{
 		this.count = seconds;
+		this.hbox = hbox;
+		
+		CountdownTimer self = this;
 		
 		Timer timer = new Timer();
 		TimerTask task = new TimerTask()
@@ -24,7 +29,8 @@ public class CountdownTimer
 				Platform.runLater(()->{
 					try
 					{
-						label.setText(String.valueOf(count));
+						Label labelSeconds = (Label)hbox.getChildren().get(1);
+						labelSeconds.setText(String.valueOf(count));
 					}
 					catch(Exception e)
 					{
@@ -39,7 +45,7 @@ public class CountdownTimer
 				if(count == 0)
 				{
 					Platform.runLater(()->{
-						levelController.deactivatePowerUp(label);
+						levelController.deactivatePowerUp(self, hbox);
 					});
 					timer.cancel();
 				}				
@@ -47,4 +53,14 @@ public class CountdownTimer
 		};
 		timer.schedule(task, 0, 1000);
 	}
+	
+	public void addSecondsToTimer(int seconds)
+	{
+		this.count += seconds;
+	}	
+	
+	public HBox getHBox()
+	{
+		return hbox;
+	}	
 }
\ No newline at end of file
-- 
GitLab