330 lines
13 KiB
HTML
330 lines
13 KiB
HTML
|
|
|
|
|
|
<!DOCTYPE html>
|
|
<html id="htmlId">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
|
|
<title>Coverage Report > LoginFXMLController</title>
|
|
<style type="text/css">
|
|
@import "../../css/coverage.css";
|
|
@import "../../css/idea.min.css";
|
|
</style>
|
|
<script type="text/javascript" src="../../js/highlight.min.js"></script>
|
|
<script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="content">
|
|
<div class="breadCrumbs">
|
|
Current scope: <a href="../../index.html">all classes</a>
|
|
<span class="separator">|</span>
|
|
<a href="../index.html">it.polimi.ingsw.gc14.View.GUI</a>
|
|
</div>
|
|
|
|
<h1>Coverage Summary for Class: LoginFXMLController (it.polimi.ingsw.gc14.View.GUI)</h1>
|
|
|
|
<table class="coverageStats">
|
|
<tr>
|
|
<th class="name">Class</th>
|
|
<th class="coverageStat
|
|
">
|
|
Class, %
|
|
</th>
|
|
<th class="coverageStat
|
|
">
|
|
Method, %
|
|
</th>
|
|
<th class="coverageStat
|
|
">
|
|
Branch, %
|
|
</th>
|
|
<th class="coverageStat
|
|
">
|
|
Line, %
|
|
</th>
|
|
</tr>
|
|
<tr>
|
|
<td class="name">LoginFXMLController</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/1)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/14)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/14)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/63)
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<br/>
|
|
<br/>
|
|
|
|
|
|
<pre>
|
|
<code class="sourceCode" id="sourceCode"> package it.polimi.ingsw.gc14.View.GUI;
|
|
|
|
import it.polimi.ingsw.gc14.Controller.ClientController;
|
|
import it.polimi.ingsw.gc14.ErrorType;
|
|
import it.polimi.ingsw.gc14.Network.IClient;
|
|
import it.polimi.ingsw.gc14.Network.NetworkConfig;
|
|
import it.polimi.ingsw.gc14.Network.InterfaceResolver;
|
|
import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
|
|
import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
|
|
import javafx.animation.*;
|
|
import javafx.application.Platform;
|
|
import javafx.css.PseudoClass;
|
|
import javafx.fxml.FXML;
|
|
import javafx.geometry.Rectangle2D;
|
|
import javafx.scene.control.*;
|
|
import javafx.scene.image.Image;
|
|
import javafx.scene.image.ImageView;
|
|
import javafx.scene.layout.*;
|
|
import javafx.scene.paint.Color;
|
|
import javafx.stage.Screen;
|
|
import javafx.util.Duration;
|
|
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* FXML controller for the login scene.
|
|
*
|
|
* <p>Handles username/IP/player-count input, protocol selection (TCP/RMI),
|
|
* and initiates the connection to the server.
|
|
*/
|
|
<b class="nc"> public class LoginFXMLController {</b>
|
|
/** Background image displayed behind the login form. */
|
|
@FXML private ImageView backgroundImage;
|
|
/** Text field for the player's username. */
|
|
@FXML private TextField campoNome;
|
|
/** Text field for the desired number of players. */
|
|
@FXML private TextField campoNumPlayers;
|
|
/** Text field for the server IP address. */
|
|
@FXML private TextField campoIP;
|
|
/** Button that submits the login form. */
|
|
@FXML private Button btnAccedi;
|
|
/** Label used to show error or success messages. */
|
|
@FXML private Label labelErrore;
|
|
/** VBox containing the login form controls. */
|
|
@FXML private VBox formPanel;
|
|
/** Toggle container for switching between RMI and TCP. */
|
|
@FXML private StackPane protocolToggle;
|
|
/** Sliding thumb inside the protocol toggle. */
|
|
@FXML private Region toggleThumb;
|
|
/** Label for the RMI side of the protocol toggle. */
|
|
@FXML private Label labelRMI;
|
|
/** Label for the TCP side of the protocol toggle. */
|
|
@FXML private Label labelTCP;
|
|
/** {@code true} when RMI is selected; {@code false} for TCP. */
|
|
<b class="nc"> private boolean isRMI = true;</b>
|
|
/** Client controller used to initiate the connection. */
|
|
private ClientController controller;
|
|
/** CSS pseudo-class applied to the active protocol label. */
|
|
<b class="nc"> private final PseudoClass activeProtocolPseudo = PseudoClass.getPseudoClass("active-protocol");</b>
|
|
|
|
/**
|
|
* Injects the client controller into this FXML controller.
|
|
*
|
|
* @param controller the client controller to use.
|
|
*/
|
|
public void setController(ClientController controller) {
|
|
<b class="nc"> this.controller = controller;</b>
|
|
}
|
|
|
|
/** Initializes the scene: loads background, sets up animations, and binds input listeners. */
|
|
@FXML
|
|
public void initialize() {
|
|
// Background setup
|
|
<b class="nc"> Image img = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/GUIImages/BackgroundLogin.png")));</b>
|
|
<b class="nc"> backgroundImage.setImage(img);</b>
|
|
<b class="nc"> Rectangle2D screenBounds = Screen.getPrimary().getBounds();</b>
|
|
<b class="nc"> backgroundImage.setFitWidth(screenBounds.getWidth());</b>
|
|
<b class="nc"> backgroundImage.setFitHeight(screenBounds.getHeight());</b>
|
|
|
|
// Protocol toggle configuration
|
|
<b class="nc"> protocolToggle.setOnMouseClicked(e -> switchProtocol());</b>
|
|
<b class="nc"> protocolToggle.setStyle(protocolToggle.getStyle() + " -fx-cursor: hand;");</b>
|
|
|
|
<b class="nc"> labelRMI.pseudoClassStateChanged(activeProtocolPseudo, true);</b>
|
|
<b class="nc"> labelTCP.pseudoClassStateChanged(activeProtocolPseudo, false);</b>
|
|
|
|
// Login Action
|
|
<b class="nc"> btnAccedi.setOnAction(e -> onAccediClick());</b>
|
|
}
|
|
|
|
/** Toggles the selected protocol between RMI and TCP, animating the toggle thumb and updating label styles. */
|
|
private void switchProtocol() {
|
|
<b class="nc"> isRMI = !isRMI;</b>
|
|
|
|
<b class="nc"> TranslateTransition tt = new TranslateTransition(Duration.millis(200), toggleThumb);</b>
|
|
<b class="nc"> tt.setToX(isRMI ? 0 : 110);</b>
|
|
<b class="nc"> tt.play();</b>
|
|
|
|
<b class="nc"> labelRMI.pseudoClassStateChanged(activeProtocolPseudo, isRMI);</b>
|
|
<b class="nc"> labelTCP.pseudoClassStateChanged(activeProtocolPseudo, !isRMI);</b>
|
|
}
|
|
|
|
/** Validates form input and starts a background thread to connect to the server. */
|
|
@FXML
|
|
private void onAccediClick() {
|
|
<b class="nc"> String name = campoNome.getText().trim();</b>
|
|
<b class="nc"> String ip = campoIP.getText().trim();</b>
|
|
|
|
<b class="nc"> if (name.isEmpty()) {</b>
|
|
<b class="nc"> showError("Please select a name.");</b>
|
|
return;
|
|
}
|
|
|
|
int numPlayers;
|
|
try {
|
|
<b class="nc"> numPlayers = Integer.parseInt(campoNumPlayers.getText().trim());</b>
|
|
} catch (NumberFormatException e) {
|
|
<b class="nc"> showError("Invalid number of players.");</b>
|
|
return;
|
|
}
|
|
|
|
<b class="nc"> updateLoginButton(false);</b>
|
|
<b class="nc"> controller.setMyUsername(name);</b>
|
|
|
|
<b class="nc"> new Thread(() -> {</b>
|
|
try {
|
|
<b class="nc"> String localInterface = InterfaceResolver.resolveLocalInterface(ip);</b>
|
|
<b class="nc"> System.setProperty("java.rmi.server.hostname", localInterface);</b>
|
|
<b class="nc"> connect(name, ip, numPlayers, localInterface);</b>
|
|
} catch (Exception ex) {
|
|
<b class="nc"> Platform.runLater(() -> {</b>
|
|
<b class="nc"> showError("Network error: " + ex.getMessage());</b>
|
|
<b class="nc"> updateLoginButton(true);</b>
|
|
});
|
|
}
|
|
<b class="nc"> }).start();</b>
|
|
}
|
|
|
|
/**
|
|
* Enables or disables the login button and updates its visual style.
|
|
*
|
|
* @param enabled {@code true} to enable the button, {@code false} to disable it.
|
|
*/
|
|
public void updateLoginButton(boolean enabled) {
|
|
<b class="nc"> btnAccedi.setDisable(!enabled);</b>
|
|
<b class="nc"> btnAccedi.setStyle(</b>
|
|
"-fx-font-family: 'Cinzel'; -fx-font-size: 12; -fx-font-weight: bold;" +
|
|
"-fx-letter-spacing: 4; -fx-text-fill: #0c0601;" +
|
|
"-fx-background-color: linear-gradient(to right, #f4c05a, #c8791a, #f4c05a);" +
|
|
"-fx-padding: 13 48 13 48; -fx-background-radius: 2;" +
|
|
<b class="nc"> "-fx-opacity: " + (enabled ? "1.0" : "0.3") + ";" +</b>
|
|
<b class="nc"> "-fx-cursor: " + (enabled ? "hand" : "default") + ";"</b>
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Connects to the server using the selected protocol and transitions to the waiting state on success. */
|
|
private void connect(String nome, String ip, int numPlayers, String localInterface) {
|
|
IClient client;
|
|
<b class="nc"> if (isRMI) {</b>
|
|
<b class="nc"> client = new RMIClient(controller, ip, NetworkConfig.RMI_PORT, localInterface);</b>
|
|
<b class="nc"> ErrorType serverResponse = client.connect(nome, numPlayers);</b>
|
|
<b class="nc"> if (serverResponse == null) {</b>
|
|
<b class="nc"> controller.setClient(client);</b>
|
|
<b class="nc"> Platform.runLater(() -> showSuccess("Connected! Waiting for other players…"));</b>
|
|
} else {
|
|
<b class="nc"> Platform.runLater(() -> {</b>
|
|
<b class="nc"> showError(serverResponse);</b>
|
|
<b class="nc"> updateLoginButton(true);</b>
|
|
});
|
|
}
|
|
} else {
|
|
<b class="nc"> client = new TCPClient(controller, ip, NetworkConfig.TCP_PORT, NetworkConfig.HEARTBEAT_PORT);</b>
|
|
<b class="nc"> ErrorType serverResponse = client.connect(nome, numPlayers);</b>
|
|
<b class="nc"> if (serverResponse == null) {</b>
|
|
<b class="nc"> controller.setClient(client);</b>
|
|
<b class="nc"> Platform.runLater(() -> showSuccess("Connected! Waiting for other players…"));</b>
|
|
} else {
|
|
<b class="nc"> Platform.runLater(() -> {</b>
|
|
<b class="nc"> showError(serverResponse);</b>
|
|
<b class="nc"> updateLoginButton(true);</b>
|
|
});
|
|
}
|
|
}
|
|
}
|
|
/**
|
|
* Displays an error from an {@link ErrorType} constant in the login form label.
|
|
*
|
|
* @param errorType the error to display.
|
|
*/
|
|
public void showError(ErrorType errorType) {
|
|
<b class="nc"> labelErrore.setTextFill(Color.web("#e05050"));</b>
|
|
<b class="nc"> labelErrore.setText(errorType.toString());</b>
|
|
}
|
|
|
|
/**
|
|
* Displays an arbitrary error message in the login form label.
|
|
*
|
|
* @param msg the error message to display.
|
|
*/
|
|
public void showError(String msg) {
|
|
<b class="nc"> labelErrore.setTextFill(Color.web("#e05050"));</b>
|
|
<b class="nc"> labelErrore.setText(msg);</b>
|
|
}
|
|
|
|
/** Displays a success message in green in the login form label. */
|
|
private void showSuccess(String msg) {
|
|
<b class="nc"> labelErrore.setTextFill(Color.web("#6fcf8a"));</b>
|
|
<b class="nc"> labelErrore.setText(msg);</b>
|
|
}
|
|
}
|
|
</code>
|
|
</pre>
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
|
(function() {
|
|
var msie = false, msie9 = false;
|
|
/*@cc_on
|
|
msie = true;
|
|
@if (@_jscript_version >= 9)
|
|
msie9 = true;
|
|
@end
|
|
@*/
|
|
|
|
if (!msie || msie && msie9) {
|
|
hljs.highlightAll()
|
|
hljs.initLineNumbersOnLoad();
|
|
}
|
|
})();
|
|
</script>
|
|
|
|
<div class="footer">
|
|
|
|
<div style="float:right;">generated on 2026-06-19 22:53</div>
|
|
</div>
|
|
</body>
|
|
</html>
|