colored message (with evil regex)
This commit is contained in:
@@ -16,6 +16,8 @@ import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ClientGuiController implements ClientListener {
|
||||
|
||||
@@ -92,15 +94,14 @@ public void initialize() throws SocketException {
|
||||
String timeString = formatter.format(time);
|
||||
Text chatterText = new Text(chatter + ": ");
|
||||
chatterText.setStyle("-fx-fill: black; -fx-font-weight: bold;");
|
||||
Text messageText = new Text(content);
|
||||
messageText.setStyle("-fx-fill: black;");
|
||||
TextFlow messageText = formatMessage(chatter, content);
|
||||
TextFlow wholeMessage = new TextFlow(chatterText, messageText);
|
||||
Text timeText = new Text(" " + timeString);
|
||||
timeText.setStyle("-fx-fill: gray; -fx-font-size: 10px;");
|
||||
HBox messageContainer = new HBox(5, wholeMessage, timeText);
|
||||
messageContainer.setStyle("-fx-background-color: lightblue; -fx-padding: 8; -fx-background-radius: 5;");
|
||||
messageContainer.setMaxWidth(Double.MAX_VALUE);
|
||||
HBox.setHgrow(messageText, javafx.scene.layout.Priority.ALWAYS);
|
||||
HBox.setHgrow(messageText, Priority.ALWAYS);
|
||||
chatList.getChildren().add(messageContainer);
|
||||
});
|
||||
}
|
||||
@@ -121,8 +122,6 @@ public void initialize() throws SocketException {
|
||||
}
|
||||
|
||||
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(() -> {
|
||||
chatList.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
|
||||
public void handleServerResponse(ServerResponsePacket.Response response) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user