Coverage Summary for Class: ChooseGod (it.polimi.ingsw.view.GUI)

Class Class, % Method, % Line, %
ChooseGod 0% (0/ 1) 0% (0/ 28) 0% (0/ 294)


1 package it.polimi.ingsw.view.GUI; 2  3 import it.polimi.ingsw.utils.model.Command; 4 import it.polimi.ingsw.view.model.Player; 5 import javafx.animation.FadeTransition; 6 import javafx.animation.TranslateTransition; 7 import javafx.application.Platform; 8 import javafx.beans.property.DoubleProperty; 9 import javafx.beans.property.SimpleDoubleProperty; 10 import javafx.fxml.FXML; 11 import javafx.scene.control.Label; 12 import javafx.scene.effect.Glow; 13 import javafx.scene.effect.Lighting; 14 import javafx.scene.image.Image; 15 import javafx.scene.image.ImageView; 16 import javafx.scene.input.MouseEvent; 17 import javafx.scene.layout.GridPane; 18 import javafx.scene.layout.Pane; 19 import javafx.util.Duration; 20  21 import java.util.Arrays; 22 import java.util.List; 23  24 public class ChooseGod implements Controller { 25  private static MainController controller = new MainController(); 26  private String[] players = new String[3]; 27  private DoubleProperty height = new SimpleDoubleProperty(720); 28  private DoubleProperty width = new SimpleDoubleProperty(1280); 29  private ImageView[] gods = new ImageView[14]; 30  private Glow glow = new Glow(); 31  private Lighting lighting = new Lighting(); 32  private String currentPlayer; 33  34  @FXML 35  private GridPane gridPane; 36  @FXML 37  private ImageView port, card, god0, god1, god2, god3, god4, god5, god6, god7, god8, god9, god10, god11, god12, 38  god13, podium0, podium1, podium2, cloud, background, action, turn; 39  @FXML 40  private Pane camp0, camp1, camp2, pane; 41  @FXML 42  private Label player0, player1, player2; 43  44  private void setUpDimension() { 45  pane.prefHeightProperty().bind(height); 46  pane.prefWidthProperty().bind(width); 47  background.fitWidthProperty().bind(width); 48  background.fitHeightProperty().bind(height); 49  50  turn.fitWidthProperty().bind(width.multiply(400).divide(1280)); 51  turn.fitHeightProperty().bind(height.multiply(200).divide(720)); 52  turn.layoutXProperty().bind(width.multiply(450).divide(1280)); 53  turn.layoutYProperty().bind(height.multiply(250).divide(720)); 54  55  56  action.fitHeightProperty().bind(height.multiply(80).divide(720)); 57  action.fitWidthProperty().bind(width.multiply(200).divide(1280)); 58  action.layoutXProperty().bind(width.multiply(510).divide(1280)); 59  action.layoutYProperty().bind(height.multiply(-15).divide(720)); 60  cloud.fitWidthProperty().bind(width); 61  cloud.fitHeightProperty().bind(height); 62  camp0.prefWidthProperty().bind(width.multiply(200).divide(1280)); 63  camp1.prefWidthProperty().bind(width.multiply(200).divide(1280)); 64  camp2.prefWidthProperty().bind(width.multiply(200).divide(1280)); 65  camp0.prefHeightProperty().bind(height.multiply(220).divide(720)); 66  camp1.prefHeightProperty().bind(height.multiply(220).divide(720)); 67  camp2.prefHeightProperty().bind(height.multiply(220).divide(720)); 68  camp1.layoutYProperty().bind(height.multiply(220).divide(720)); 69  camp2.layoutYProperty().bind(height.multiply(440).divide(720)); 70  podium0.fitHeightProperty().bind(height.multiply(84).divide(720)); 71  podium1.fitHeightProperty().bind(height.multiply(84).divide(720)); 72  podium2.fitHeightProperty().bind(height.multiply(84).divide(720)); 73  podium0.fitWidthProperty().bind(width.multiply(150).divide(1280)); 74  podium1.fitWidthProperty().bind(width.multiply(150).divide(1280)); 75  podium2.fitWidthProperty().bind(width.multiply(150).divide(1280)); 76  podium0.layoutYProperty().bind(height.multiply(120).divide(720)); 77  podium1.layoutYProperty().bind(height.multiply(120).divide(720)); 78  podium2.layoutYProperty().bind(height.multiply(120).divide(720)); 79  ((ImageView) camp0.getChildren().get(1)).fitWidthProperty().bind(width.multiply(100).divide(1280)); 80  ((ImageView) camp0.getChildren().get(1)).fitHeightProperty().bind(height.multiply(130).divide(720)); 81  ((ImageView) camp0.getChildren().get(1)).layoutYProperty().bind(height.multiply(20).divide(720)); 82  ((ImageView) camp0.getChildren().get(1)).layoutXProperty().bind(width.multiply(25).divide(1280)); 83  84  ((ImageView) camp1.getChildren().get(1)).fitWidthProperty().bind(width.multiply(100).divide(1280)); 85  ((ImageView) camp1.getChildren().get(1)).fitHeightProperty().bind(height.multiply(130).divide(720)); 86  ((ImageView) camp1.getChildren().get(1)).layoutYProperty().bind(height.multiply(20).divide(720)); 87  ((ImageView) camp1.getChildren().get(1)).layoutXProperty().bind(width.multiply(25).divide(1280)); 88  89  ((ImageView) camp2.getChildren().get(1)).fitWidthProperty().bind(width.multiply(100).divide(1280)); 90  ((ImageView) camp2.getChildren().get(1)).fitHeightProperty().bind(height.multiply(130).divide(720)); 91  ((ImageView) camp2.getChildren().get(1)).layoutYProperty().bind(height.multiply(20).divide(720)); 92  ((ImageView) camp2.getChildren().get(1)).layoutXProperty().bind(width.multiply(25).divide(1280)); 93  94  port.fitHeightProperty().bind(height.multiply(72).divide(720)); 95  port.fitWidthProperty().bind(width.multiply(80).divide(1280)); 96  port.layoutYProperty().bind(height.multiply(650).divide(720)); 97  98  card.fitHeightProperty().bind(height.multiply(450).divide(720)); 99  card.fitWidthProperty().bind(width.multiply(262).divide(1280)); 100  card.layoutYProperty().bind(height.multiply(40).divide(720)); 101  card.layoutXProperty().bind(width.multiply(1000).divide(1280)); 102  103  gridPane.prefWidthProperty().bind(width.multiply(730).divide(1280)); 104  gridPane.prefHeightProperty().bind(height.multiply(700).divide(720)); 105  gridPane.layoutXProperty().bind(width.multiply(240).divide(1280)); 106  107  for (int i = 0; i < 14; i++) { 108  ((ImageView) ((Pane) gridPane.getChildren().get(i)).getChildren().get(0)).fitWidthProperty() 109  .bind(width.multiply(145).divide(1280)); 110  ((ImageView) ((Pane) gridPane.getChildren().get(i)).getChildren().get(0)).fitHeightProperty() 111  .bind(height.multiply(82).divide(720)); 112  ((ImageView) ((Pane) gridPane.getChildren().get(i)).getChildren().get(1)).fitWidthProperty() 113  .bind(width.multiply(116).divide(1280)); 114  ((ImageView) ((Pane) gridPane.getChildren().get(i)).getChildren().get(1)).fitHeightProperty() 115  .bind(height.multiply(150).divide(720)); 116  ((ImageView) ((Pane) gridPane.getChildren().get(i)).getChildren().get(1)).layoutXProperty() 117  .bind(width.multiply(10).divide(1280)); 118  ((ImageView) ((Pane) gridPane.getChildren().get(i)).getChildren().get(1)).layoutYProperty() 119  .bind(height.multiply(30).divide(720)); 120  ((ImageView) ((Pane) gridPane.getChildren().get(i)).getChildren().get(0)).layoutYProperty() 121  .bind(height.multiply(150).divide(720)); 122  123  } 124  } 125  126  /** 127  * Set controller for the fxml file 128  * 129  * @param controller controller 130  */ 131  public static void setController(MainController controller) { 132  ChooseGod.controller = controller; 133  } 134  135  /** 136  * Choose elements (gods/players) from the scene 137  * 138  * @param event mouse event 139  */ 140  @FXML 141  private void choose(MouseEvent event) { 142  if (!controller.getPlayer().equals(players[0])) { 143  return; 144  } 145  ImageView node = (ImageView) event.getSource(); 146  String string = (String) node.getUserData(); 147  controller.send(string); 148  } 149  150  /** 151  * Choose player from the scene 152  * 153  * @param event mouse event 154  */ 155  @FXML 156  private void choosePlayer(MouseEvent event) { 157  if (!controller.getPlayer().equals(players[0])) { 158  return; 159  } 160  Pane node = (Pane) event.getSource(); 161  String string = (String) node.getUserData(); 162  int i = Integer.parseInt(string); 163  controller.send(players[i]); 164  } 165  166  /** 167  * Pop up a new window that ask if the player wishes to quit the game 168  */ 169  @FXML 170  private void quit() { 171  controller.quit(); 172  } 173  174  /** 175  * To create animation during the game 176  * 177  * @param imageView Background image of animation 178  * @param state visibility 179  * @param fromValue initial opacity value 180  * @param toValue finale opacity value 181  */ 182  private void animation(ImageView imageView, boolean state, double fromValue, double toValue) { 183  FadeTransition fade = new FadeTransition(); 184  if (state) { 185  imageView.setVisible(true); 186  fade.setFromValue(fromValue); 187  fade.setToValue(toValue); 188  } else { 189  fade.setFromValue(fromValue); 190  fade.setToValue(toValue); 191  fade.setOnFinished(e -> { 192  imageView.setVisible(false); 193  }); 194  } 195  fade.setCycleCount(1); 196  fade.setAutoReverse(false); 197  fade.setNode(imageView); 198  fade.play(); 199  } 200  201  private void changeTurn(ImageView imageView) { 202  FadeTransition fade = new FadeTransition(); 203  fade.setDuration(Duration.millis(500)); 204  imageView.setVisible(true); 205  fade.setFromValue(0); 206  fade.setToValue(10); 207  fade.setCycleCount(1); 208  fade.setAutoReverse(false); 209  fade.setNode(imageView); 210  fade.play(); 211  fade.setOnFinished(e -> { 212  FadeTransition fade1 = new FadeTransition(); 213  fade1.setFromValue(10); 214  fade1.setToValue(0); 215  fade1.setCycleCount(1); 216  fade1.setAutoReverse(false); 217  fade1.setNode(imageView); 218  fade1.play(); 219  }); 220  } 221  222  /** 223  * For the challenger player, shows the 'choose start player' scene 224  * 225  * @param pane Challenger's pane 226  * @param x position x 227  * @param y position y 228  */ 229  private void translation(Pane pane, double x, double y) { 230  TranslateTransition translate = new TranslateTransition(); 231  translate.setDuration(Duration.millis(1000)); 232  translate.setToX(x); 233  translate.setToY(y); 234  translate.setAutoReverse(false); 235  translate.setNode(pane); 236  translate.play(); 237  } 238  239  /** 240  * Show god power's image description 241  * 242  * @param event mouse event 243  */ 244  @FXML 245  private void show(MouseEvent event) { 246  ImageView node = (ImageView) event.getSource(); 247  String string = (String) node.getUserData(); 248  String url = ImageEnum.getUrl(string + "_DEF"); 249  card.setImage(new Image(url)); 250  animation(card, true, 0, 10); 251  } 252  253  /** 254  * Close show() 255  * 256  * @param event mouse event 257  */ 258  @FXML 259  private void close(MouseEvent event) { 260  animation(card, true, 10, 0); 261  } 262  263  /** 264  * Reload the board for all players 265  */ 266  @Override 267  public void reSet() { 268  resetPlayerInfo(); 269  resetAction(); 270  } 271  272  @Override 273  public void setDimension(double width, double height) { 274  if (width * 720 / 1280 < height) { 275  pane.setLayoutY((height - (width * 720 / 1280)) / 2); 276  pane.setLayoutX(0); 277  this.height.set(width * 720 / 1280); 278  this.width.set(width); 279  } else { 280  pane.setLayoutX((width - (height * 1280 / 720)) / 2); 281  pane.setLayoutY(0); 282  this.width.set(height * 1280 / 720); 283  this.height.set(height); 284  } 285  286  } 287  288  /** 289  * Receive player's information from server (Current player, player's turn, ecc) and shows to players 290  */ 291  private void resetPlayerInfo() { 292  podium0.setEffect(lighting); 293  podium1.setEffect(lighting); 294  podium2.setEffect(lighting); 295  String currPlayer = controller.getCurrentPlayer(); 296  if (currentPlayer == null || !currPlayer.equals(currentPlayer)) { 297  currentPlayer = currPlayer; 298  if (currPlayer.equals(players[0])) { 299  changeTurn(turn); 300  podium0.setImage(new Image(ImageEnum.getUrl("PODIUM_GOLD"))); 301  podium0.setEffect(glow); 302  podium1.setImage(new Image(ImageEnum.getUrl("PODIUM"))); 303  podium2.setImage(new Image(ImageEnum.getUrl("PODIUM"))); 304  changeTurn(turn); 305  } else if (currPlayer.equals(players[1])) { 306  podium1.setImage(new Image(ImageEnum.getUrl("PODIUM_GOLD"))); 307  podium1.setEffect(glow); 308  podium0.setImage(new Image(ImageEnum.getUrl("PODIUM"))); 309  podium2.setImage(new Image(ImageEnum.getUrl("PODIUM"))); 310  } else { 311  podium2.setImage(new Image(ImageEnum.getUrl("PODIUM_GOLD"))); 312  podium2.setEffect(glow); 313  podium1.setImage(new Image(ImageEnum.getUrl("PODIUM"))); 314  podium0.setImage(new Image(ImageEnum.getUrl("PODIUM"))); 315  } 316  } 317  } 318  319  private void chooseStartPlayer() { 320  FadeTransition fadeTransition = new FadeTransition(); 321  fadeTransition.setFromValue(10); 322  fadeTransition.setToValue(0); 323  fadeTransition.setCycleCount(1); 324  fadeTransition.setAutoReverse(false); 325  fadeTransition.setNode(gridPane); 326  fadeTransition.setOnFinished(e -> gridPane.setVisible(false)); 327  fadeTransition.play(); 328  329  if (controller.getCurrentPlayer().equals(controller.getPlayer())) { 330  Platform.runLater(() -> { 331  List<Player> playersList = controller.getUserInfo(); 332  playersList.stream().forEach(e -> { 333  if (e.getUsername().equals(players[0])) { 334  translation(camp0, 250 * width.doubleValue() / 1280, 220 * height.doubleValue() / 720); 335  camp0.setDisable(false); 336  } else if (e.getUsername().equals(players[1])) { 337  camp1.layoutYProperty().unbind(); 338  translation(camp1, 550 * width.doubleValue() / 1280, height.doubleValue() / 720); 339  camp1.setDisable(false); 340  } else { 341  camp2.layoutYProperty().unbind(); 342  translation(camp2, 850 * width.doubleValue() / 1280, -220 * height.doubleValue() / 720); 343  camp2.setDisable(false); 344  } 345  }); 346  }); 347  } 348  Platform.runLater(() -> { 349  animation(action, true, 10, 0); 350  action.setImage(new Image(ImageEnum.getUrl("START_PLAYER"))); 351  action.fitWidthProperty().unbind(); 352  action.fitHeightProperty().unbind(); 353  action.fitWidthProperty().bind(width.multiply(400).divide(1280)); 354  action.fitHeightProperty().bind(height.multiply(113).divide(720)); 355  action.layoutXProperty().unbind(); 356  action.layoutXProperty().bind(width.multiply(450).divide(1280)); 357  animation(action, true, 0, 10); 358  List<Player> playersList = controller.getUserInfo(); 359  playersList.stream().forEach(e -> { 360  if (e.getUsername().equals(players[0])) { 361  ((ImageView) camp0.getChildren().get(1)) 362  .setImage(new Image(ImageEnum.getUrl(e.getGod() + "_PLAYER"))); 363  ((ImageView) camp0.getChildren().get(1)).setVisible(true); 364  365  animation(((ImageView) camp0.getChildren().get(1)), true, 0, 10); 366  } else if (e.getUsername().equals(players[1])) { 367  ((ImageView) camp1.getChildren().get(1)) 368  .setImage(new Image(ImageEnum.getUrl(e.getGod() + "_PLAYER"))); 369  370  ((ImageView) camp1.getChildren().get(1)).setVisible(true); 371  animation(((ImageView) camp1.getChildren().get(1)), true, 0, 10); 372  } else { 373  ((ImageView) camp2.getChildren().get(1)) 374  .setImage(new Image(ImageEnum.getUrl(e.getGod() + "_PLAYER"))); 375  ((ImageView) camp2.getChildren().get(1)).setVisible(true); 376  animation(((ImageView) camp2.getChildren().get(1)), true, 0, 10); 377  } 378  }); 379  }); 380  background.setEffect(lighting); 381  } 382  383  /** 384  * Receive all available gods from server and shows to all players 385  */ 386  private void resetAction() { 387  gridPane.setVisible(true); 388  if (controller.getGamePhase().equals("SET_GOD_LIST") || controller.getGamePhase().equals("CHOOSE_GOD")) { 389  List<Command> listCommand = controller.getCommand(); 390  Arrays.stream(gods).forEach(e -> { 391  if (listCommand.stream().anyMatch(e1 -> e1.getFuncData().equals((String) e.getUserData()))) { 392  if (!e.isVisible()) { 393  animation(e, true, 0, 10); 394  } 395  } else { 396  if (e.isVisible()) { 397  animation(e, false, 10, 0); 398  } 399  } 400  }); 401  } else if (controller.getGamePhase().equals("START_PLAYER")) { 402  chooseStartPlayer(); 403  } 404  } 405  406  /** 407  * Change view 408  * 409  * @param state open o close 410  */ 411  @Override 412  public void changePage(Boolean state) { 413  cloud.setVisible(true); 414  FadeTransition fade = new FadeTransition(); 415  fade.setDuration(Duration.millis(1000)); 416  if (!state) { 417  fade.setFromValue(0); 418  fade.setToValue(10); 419  fade.setOnFinished(e -> { 420  controller.changeScene(); 421  }); 422  } else { 423  fade.setFromValue(10); 424  fade.setToValue(0); 425  } 426  fade.setCycleCount(1); 427  fade.setAutoReverse(false); 428  fade.setNode(cloud); 429  fade.play(); 430  } 431  432  433  @FXML 434  private void initialize() { 435  turn.setDisable(true); 436  turn.setVisible(false); 437  for (int i = 0; i < 14; i++) { 438  gods[i] = ((ImageView) ((Pane) gridPane.getChildren().get(i)).getChildren().get(1)); 439  gods[i].setVisible(false); 440  } 441  action.setVisible(true); 442  action.setImage(new Image(ImageEnum.getUrl("SELECT_GOD"))); 443  camp0.setVisible(false); 444  camp0.setDisable(true); 445  camp1.setVisible(false); 446  camp1.setDisable(true); 447  camp2.setVisible(false); 448  camp2.setDisable(true); 449  450  List<Player> listPlayer = controller.getUserInfo(); 451  listPlayer.stream().forEach(e -> { 452  if (e.getUsername().equals(controller.getPlayer())) { 453  players[0] = e.getUsername(); 454  camp0.setVisible(true); 455  player0.setText(e.getUsername()); 456  } else if (!camp1.isVisible()) { 457  players[1] = e.getUsername(); 458  camp1.setVisible(true); 459  player1.setText(e.getUsername()); 460  } else { 461  players[2] = e.getUsername(); 462  camp2.setVisible(true); 463  player2.setText(e.getUsername()); 464  } 465  }); 466  467  cloud.setDisable(true); 468  setUpDimension(); 469  reSet(); 470  } 471 }