From bde2f61daaab917cca6cbc54e16ee54959d75e38 Mon Sep 17 00:00:00 2001
From: Francesco Bariatti <francesco.bariatti@insa-rennes.fr>
Date: Wed, 11 May 2016 14:59:15 +0200
Subject: [PATCH] GUI can load a state

---
 gui/src/controller/Controller.java   | 114 +++++++++++++++------------
 gui/src/controller/UpdateThread.java |  12 ++-
 gui/src/view/TileView.java           |   6 +-
 gui/src/view/view.fxml               |   4 +-
 4 files changed, 80 insertions(+), 56 deletions(-)

diff --git a/gui/src/controller/Controller.java b/gui/src/controller/Controller.java
index 94eb160..5b49df3 100644
--- a/gui/src/controller/Controller.java
+++ b/gui/src/controller/Controller.java
@@ -1,25 +1,27 @@
 package controller;
 
+import javafx.application.Platform;
 import javafx.event.EventHandler;
 import javafx.fxml.FXML;
 import javafx.fxml.Initializable;
-import javafx.scene.control.Alert;
-import javafx.scene.control.ButtonType;
-import javafx.scene.control.ChoiceDialog;
-import javafx.scene.control.Label;
+import javafx.scene.control.*;
 import javafx.scene.input.MouseEvent;
 import javafx.scene.layout.BorderPane;
-import javafx.scene.paint.Color;
 import javafx.scene.shape.Polygon;
 import model.GameState;
 import model.Move;
 import model.Player;
 import model.Tile;
+import org.json.JSONException;
+import org.json.JSONObject;
 import sun.audio.AudioPlayer;
 import sun.audio.AudioStream;
 import view.TileView;
 
-import java.io.*;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
 import java.net.URL;
 import java.util.List;
 import java.util.ResourceBundle;
@@ -27,6 +29,7 @@ import java.util.ResourceBundle;
 public class Controller implements Initializable
 {
 	private static final String introMusicFile = "./resource/pingu_theme.wav";
+	private static final String clickSoundFile = "./resource/clic.wav";
 
 	private GameState gameState;
 	private Process gameProcess;
@@ -45,7 +48,7 @@ public class Controller implements Initializable
 	private BorderPane mainPane;
 
 	@FXML
-	private Label scoreRed, scoreBlue, turnLabel;
+	private Label scoreRedLabel, scoreBlueLabel, turnLabel;
 
 	@Override
 	public void initialize(URL location, ResourceBundle resources)
@@ -57,12 +60,9 @@ public class Controller implements Initializable
 		for (int i = 0; i < fxTiles.length; i++)
 		{
 			board[i] = new Tile(i, Tile.PenguinPresence.NO_PENGUIN);
-			TileClickHandler clickHandler = new TileClickHandler(i);
-			fxTiles[i].setOnMouseClicked(clickHandler);
-			labels[i].setOnMouseClicked(clickHandler);
-			boardView[i] = new TileView(fxTiles[i], board[i], labels[i]);
+			boardView[i] = new TileView(fxTiles[i], labels[i], board[i]);
 		}
-		this.selectedTile = -1;
+//		this.selectedTile = -1;
 		this.gameState = new GameState();
 		try
 		{
@@ -75,60 +75,82 @@ public class Controller implements Initializable
 					gameProcess.destroy();
 				}
 			});
-		}
-		catch (IOException e)
+			UpdateThread upT = new UpdateThread(gameProcess, this.gameState, this.board, this.boardView, scoreRedLabel, scoreBlueLabel, turnLabel);
+			upT.setDaemon(true);
+			upT.start();
+		} catch (IOException e)
 		{
 			Alert alert = new Alert(Alert.AlertType.ERROR, "Can't run penguin program", ButtonType.OK);
 			alert.showAndWait();
 			e.printStackTrace();
 			System.exit(1);
 		}
-		// SENDING STATE TO THE GAME
-		// TODO: 5/10/16 Creating a state and sending it to the game
-		String state = "{}";
-		gameInput.println(state);
-		//Choice: who will start?
+
+		//PLAY START SOUND
+		new Thread(() -> {
+			try
+			{
+				AudioPlayer.player.start(new AudioStream(new FileInputStream(introMusicFile)));
+			} catch (IOException e)
+			{
+				e.printStackTrace();
+			}
+		}).start();
+
+		//DO WE WANT TO LOAD A STATE?
+		TextInputDialog dialog = new TextInputDialog();
+		dialog.setHeaderText("Do you want to load a state?");
+		dialog.setContentText("Enter JSON (or leave blank for random)");
+		dialog.showAndWait();
+		String result = dialog.getResult();
+		if (result == null) //If the user clicked exit
+			System.exit(0);
+		try
+		{
+			new JSONObject(result); //Verify if it is valid json (if not, an exception is thrown)
+			gameInput.println(result);
+			Platform.runLater(() -> startGame());
+
+		} catch (JSONException e)
+		{
+			System.out.println("TODO: RANDOM STATE");
+			System.exit(0);
+		}
+	}
+
+	private void startGame()
+	{
 		ChoiceDialog<Player> playerChoice = new ChoiceDialog<>(Player.Red, Player.Red, Player.Blue);
 		playerChoice.setTitle("Penguin game!");
 		playerChoice.setHeaderText("Choose your color (red starts)");
 		playerChoice.showAndWait();
 		humanPlayer = playerChoice.getResult();
 		if (humanPlayer == null)
-		{
 			System.exit(0);
-		}
+		if (humanPlayer.equals(Player.Red))
+			gameInput.println("h");
 		else
+			gameInput.println("c");
+		this.selectedTile = -1;
+		for (TileView t : boardView)
 		{
-			if (humanPlayer.equals(Player.Red))
-				gameInput.println("h");
-			else
-				gameInput.println("c");
-		}
+			GameClickHandler gch = new GameClickHandler(t.getModelTile().getNumber());
+			t.getFxTile().setOnMouseClicked(gch);
+			t.getFishLabel().setOnMouseClicked(gch);
 
-		UpdateThread upT = new UpdateThread(gameProcess, this.gameState, this.board, this.boardView, humanPlayer, scoreRed, scoreBlue, turnLabel);
-		upT.setDaemon(true);
-		upT.start();
-		try
-		{
-			AudioPlayer.player.start(new AudioStream(new FileInputStream(introMusicFile)));
-		} catch (IOException e)
-		{
-			e.printStackTrace();
 		}
+
 	}
 
 	/**
-	 * Event handler that will listen for a click on a tile.
+	 * Event handler that will listen for a click on a tile during the game and move/select the penguin.
 	 * It is associated with a TileView
 	 */
-	private class TileClickHandler implements EventHandler<MouseEvent>
+	private class GameClickHandler implements EventHandler<MouseEvent>
 	{
-
-
 		private int tileNumber;
-		private final String clickSoundFile = "./resource/clic.wav";
 
-		public TileClickHandler(int tileNumber)
+		public GameClickHandler(int tileNumber)
 		{
 			this.tileNumber = tileNumber;
 		}
@@ -242,15 +264,5 @@ public class Controller implements Initializable
 		}
 	}
 
-	private class MyHexEnteredHandler implements EventHandler<MouseEvent>
-	{
-		@Override
-		public void handle(MouseEvent event)
-		{
-			Polygon poly = (Polygon) event.getSource();
-			poly.setFill(Color.GOLD);
-		}
-	}
-
 
 }
diff --git a/gui/src/controller/UpdateThread.java b/gui/src/controller/UpdateThread.java
index 8d22719..ad6c6f1 100644
--- a/gui/src/controller/UpdateThread.java
+++ b/gui/src/controller/UpdateThread.java
@@ -21,19 +21,27 @@ public class UpdateThread extends Thread
 	Player humanPlayer;
 	Label scoreRed, scoreBlue, turnLabel;
 
-	public UpdateThread(Process program, GameState gameState, Tile[] board, TileView[] boardView, Player humanPlayer, Label scoreRed, Label scoreBlue, Label turnLabel)
+	public UpdateThread(Process program, GameState gameState, Tile[] board, TileView[] boardView, Label scoreRed, Label scoreBlue, Label turnLabel)
 	{
 		this.reader = new BufferedReader(new InputStreamReader(program.getInputStream()));
 		this.writer = new PrintWriter(new OutputStreamWriter(program.getOutputStream()), true);
 		this.gameState = gameState;
 		this.board = board;
 		this.boardView = boardView;
-		this.humanPlayer = humanPlayer;
 		this.scoreRed = scoreRed;
 		this.scoreBlue = scoreBlue;
 		this.turnLabel = turnLabel;
 	}
 
+	public Player getHumanPlayer()
+	{
+		return humanPlayer;
+	}
+
+	public void setHumanPlayer(Player humanPlayer)
+	{
+		this.humanPlayer = humanPlayer;
+	}
 
 	public void run()
 	{
diff --git a/gui/src/view/TileView.java b/gui/src/view/TileView.java
index 36f5928..be46bf5 100644
--- a/gui/src/view/TileView.java
+++ b/gui/src/view/TileView.java
@@ -17,7 +17,7 @@ public class TileView
 	private boolean selected, highlighted;
 	private Move highlightMove = null; // How do we get here from the selected tile (used when highlighting)
 
-	public TileView(Polygon fxTile, Tile modelTile, Label fishLabel)
+	public TileView(Polygon fxTile, Label fishLabel, Tile modelTile)
 	{
 
 		this.fxTile = fxTile;
@@ -63,6 +63,10 @@ public class TileView
 
 	public boolean isSelected() { return selected; }
 
+	public Polygon getFxTile() { return fxTile; }
+
+	public Label getFishLabel() { return fishLabel; }
+
 	public void setSelected(boolean selected) { this.selected = selected; }
 
 	public boolean isHighlighted() { return highlighted; }
diff --git a/gui/src/view/view.fxml b/gui/src/view/view.fxml
index c63064b..8ec6c08 100644
--- a/gui/src/view/view.fxml
+++ b/gui/src/view/view.fxml
@@ -997,12 +997,12 @@
                   <Font size="22.0" />
                </font>
             </Label>
-            <Label fx:id="scoreRed" layoutX="556.0" layoutY="152.0" text="0">
+            <Label fx:id="scoreRedLabel" layoutX="556.0" layoutY="152.0" text="0">
                <font>
                   <Font size="22.0" />
                </font>
             </Label>
-            <Label fx:id="scoreBlue" layoutX="556.0" layoutY="183.0" text="0">
+            <Label fx:id="scoreBlueLabel" layoutX="556.0" layoutY="183.0" text="0">
                <font>
                   <Font size="22.0" />
                </font>
-- 
GitLab