Login remake with font

This commit is contained in:
2026-05-20 16:53:43 +02:00
parent c48180e1b8
commit 2157925746
3 changed files with 83 additions and 43 deletions
@@ -5,6 +5,7 @@ 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.*;
@@ -35,29 +36,31 @@ public class LoginFXMLController {
private boolean isRMI = true;
private ClientController controller;
// Definiamo lo pseudo-stato custom per il CSS corrispondente a ":active-protocol"
private final PseudoClass activeProtocolPseudo = PseudoClass.getPseudoClass("active-protocol");
public void setController(ClientController controller) {
this.controller = controller;
}
@FXML
public void initialize() {
// Background
// Background setup
Image img = new Image(getClass().getResourceAsStream("/GUIImages/Background.png"));
backgroundImage.setImage(img);
Rectangle2D screenBounds = Screen.getPrimary().getBounds();
backgroundImage.setFitWidth(screenBounds.getWidth());
backgroundImage.setFitHeight(screenBounds.getHeight());
// Protocol toggle
// Protocol toggle configuration
protocolToggle.setOnMouseClicked(e -> switchProtocol());
protocolToggle.setStyle(protocolToggle.getStyle() + " -fx-cursor: hand;");
// Button hover
final String BASE_BTN = btnAccedi.getStyle();
btnAccedi.setOnMouseEntered(e -> btnAccedi.setStyle(BASE_BTN +
"-fx-effect: dropshadow(gaussian, rgba(244,192,90,0.45), 18, 0, 0, 0);"));
btnAccedi.setOnMouseExited(e -> btnAccedi.setStyle(BASE_BTN));
// Inizializza lo stato grafico iniziale (RMI attivo)
labelRMI.pseudoClassStateChanged(activeProtocolPseudo, true);
labelTCP.pseudoClassStateChanged(activeProtocolPseudo, false);
// Login Action
btnAccedi.setOnAction(e -> onAccediClick());
}
@@ -68,10 +71,9 @@ public class LoginFXMLController {
tt.setToX(isRMI ? 0 : 110);
tt.play();
labelRMI.setStyle("-fx-font-family: 'Cinzel'; -fx-font-size: 11; -fx-font-weight: bold;" +
"-fx-text-fill: " + (isRMI ? "#0c0601" : "rgba(244,192,90,0.55)") + ";");
labelTCP.setStyle("-fx-font-family: 'Cinzel'; -fx-font-size: 11; -fx-font-weight: bold;" +
"-fx-text-fill: " + (isRMI ? "rgba(244,192,90,0.55)" : "#0c0601") + ";");
// Modifica in modo sicuro lo stato senza distruggere i parametri di layout dei nodi
labelRMI.pseudoClassStateChanged(activeProtocolPseudo, isRMI);
labelTCP.pseudoClassStateChanged(activeProtocolPseudo, !isRMI);
}
@FXML
@@ -94,7 +96,6 @@ public class LoginFXMLController {
controller.setMyUsername(nome);
// Resolve the local interface to use, then connect
new Thread(() -> {
try {
String localInterface = resolveLocalInterface(ip);
@@ -106,21 +107,12 @@ public class LoginFXMLController {
}).start();
}
/**
* Given the server IP, finds the local network interface whose subnet
* contains that IP. Falls back to the default route for non-LAN addresses.
* Handles "localhost" and "127.x.x.x" explicitly.
*/
private String resolveLocalInterface(String serverIp) throws Exception {
// localhost shortcut
if (serverIp.equalsIgnoreCase("localhost") ||
serverIp.startsWith("127.") || serverIp.isEmpty()) {
if (serverIp.equalsIgnoreCase("localhost") || serverIp.startsWith("127.") || serverIp.isEmpty()) {
return "127.0.0.1";
}
InetAddress serverAddr = InetAddress.getByName(serverIp);
// Walk every local interface and check if serverAddr is in its subnet
Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface ni = ifaces.nextElement();
@@ -137,17 +129,12 @@ public class LoginFXMLController {
}
}
// No subnet match — use the address the OS would naturally pick
// by opening a UDP socket toward the server (no data is sent)
try (DatagramSocket socket = new DatagramSocket()) {
socket.connect(serverAddr, 9); // port 9 = discard, never actually used
socket.connect(serverAddr, 9);
return socket.getLocalAddress().getHostAddress();
}
}
/**
* Returns true if {@code a} and {@code b} are in the same /prefix subnet.
*/
private boolean sameSubnet(InetAddress a, InetAddress b, int prefix) {
if (prefix < 0 || prefix > 32) return false;
int maskBits = prefix == 0 ? 0 : (0xFFFFFFFF << (32 - prefix));