colored message (with evil regex)

This commit is contained in:
Clément
2025-03-12 22:17:17 +01:00
parent 2b17891379
commit 39a65e1ca6

View File

@@ -16,6 +16,8 @@ import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ClientGuiController implements ClientListener { public class ClientGuiController implements ClientListener {
@@ -92,15 +94,14 @@ public void initialize() throws SocketException {
String timeString = formatter.format(time); String timeString = formatter.format(time);
Text chatterText = new Text(chatter + ": "); Text chatterText = new Text(chatter + ": ");
chatterText.setStyle("-fx-fill: black; -fx-font-weight: bold;"); chatterText.setStyle("-fx-fill: black; -fx-font-weight: bold;");
Text messageText = new Text(content); TextFlow messageText = formatMessage(chatter, content);
messageText.setStyle("-fx-fill: black;");
TextFlow wholeMessage = new TextFlow(chatterText, messageText); TextFlow wholeMessage = new TextFlow(chatterText, messageText);
Text timeText = new Text(" " + timeString); Text timeText = new Text(" " + timeString);
timeText.setStyle("-fx-fill: gray; -fx-font-size: 10px;"); timeText.setStyle("-fx-fill: gray; -fx-font-size: 10px;");
HBox messageContainer = new HBox(5, wholeMessage, timeText); HBox messageContainer = new HBox(5, wholeMessage, timeText);
messageContainer.setStyle("-fx-background-color: lightblue; -fx-padding: 8; -fx-background-radius: 5;"); messageContainer.setStyle("-fx-background-color: lightblue; -fx-padding: 8; -fx-background-radius: 5;");
messageContainer.setMaxWidth(Double.MAX_VALUE); messageContainer.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(messageText, javafx.scene.layout.Priority.ALWAYS); HBox.setHgrow(messageText, Priority.ALWAYS);
chatList.getChildren().add(messageContainer); chatList.getChildren().add(messageContainer);
}); });
} }
@@ -121,8 +122,6 @@ public void initialize() throws SocketException {
} }
private void createChatEnv(String roomName) { private void createChatEnv(String roomName) {
// <TextField fx:id="messageInput" promptText="Type a message..." styleClass="message-input"/>
// <Button fx:id="sendButton" text="Send" styleClass="send-button"/>
Platform.runLater(() -> { Platform.runLater(() -> {
chatList.getChildren().clear(); chatList.getChildren().clear();
chatInput.getChildren().clear(); chatInput.getChildren().clear();
@@ -154,6 +153,43 @@ public void initialize() throws SocketException {
}); });
} }
private TextFlow formatMessage(String chatter, String content) {
TextFlow textFlow = new TextFlow();
Pattern pattern = Pattern.compile("&([rbgyn])([^&]*)");
Matcher matcher = pattern.matcher(content);
int lastIndex = 0;
while (matcher.find()) {
if (matcher.start() > lastIndex) {
textFlow.getChildren().add(new Text(content.substring(lastIndex, matcher.start())));
}
String colorCode = matcher.group(1);
String textPart = matcher.group(2);
String color = switch (colorCode) {
case "r" -> "red";
case "b" -> "blue";
case "g" -> "green";
case "y" -> "gray";
default -> "black";
};
Text coloredText = new Text(textPart);
coloredText.setStyle("-fx-fill: " + color + ";");
textFlow.getChildren().add(coloredText);
lastIndex = matcher.end();
}
// Add the remaining text (works if there's no color code at all)
if (lastIndex < content.length()) {
textFlow.getChildren().add(new Text(content.substring(lastIndex)));
}
return textFlow;
}
@Override @Override
public void handleServerResponse(ServerResponsePacket.Response response) { public void handleServerResponse(ServerResponsePacket.Response response) {