TextComponents: reuse same ObservableList (JavaFX)

This commit is contained in:
Bixilon 2020-11-03 18:05:27 +01:00
parent cad9766d51
commit a666b0c924
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 12 additions and 13 deletions

View File

@ -16,7 +16,6 @@ package de.bixilon.minosoft.data.text;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import de.bixilon.minosoft.modding.event.events.annotations.Unsafe; import de.bixilon.minosoft.modding.event.events.annotations.Unsafe;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.scene.Node; import javafx.scene.Node;
@ -197,10 +196,9 @@ public class BaseComponent implements ChatComponent {
} }
@Override @Override
public ObservableList<Node> getJavaFXText() { public ObservableList<Node> getJavaFXText(ObservableList<Node> nodes) {
ObservableList<Node> list = FXCollections.observableArrayList(); parts.forEach((chatPart) -> chatPart.getJavaFXText(nodes));
parts.forEach((chatPart) -> list.addAll(chatPart.getJavaFXText())); return nodes;
return list;
} }
@Unsafe @Unsafe

View File

@ -48,5 +48,5 @@ public interface ChatComponent {
/** /**
* @return Returns the a list of Nodes, drawable in JavaFX (TextFlow) * @return Returns the a list of Nodes, drawable in JavaFX (TextFlow)
*/ */
ObservableList<Node> getJavaFXText(); ObservableList<Node> getJavaFXText(ObservableList<Node> nodes);
} }

View File

@ -16,7 +16,6 @@ package de.bixilon.minosoft.data.text;
import javafx.animation.Animation; import javafx.animation.Animation;
import javafx.animation.KeyFrame; import javafx.animation.KeyFrame;
import javafx.animation.Timeline; import javafx.animation.Timeline;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
@ -179,7 +178,7 @@ public class TextComponent implements ChatComponent {
} }
@Override @Override
public ObservableList<Node> getJavaFXText() { public ObservableList<Node> getJavaFXText(ObservableList<Node> nodes) {
Text text = new Text(this.text); Text text = new Text(this.text);
if (color != null) { if (color != null) {
text.setFill(Color.web(color.toString())); text.setFill(Color.web(color.toString()));
@ -197,7 +196,8 @@ public class TextComponent implements ChatComponent {
case ITALIC -> text.setStyle("-fx-font-weight: italic;"); case ITALIC -> text.setStyle("-fx-font-weight: italic;");
} }
})); }));
return FXCollections.observableArrayList(text); nodes.add(text);
return nodes;
} }
@Override @Override

View File

@ -66,10 +66,10 @@ public class TranslatableComponent implements ChatComponent {
} }
@Override @Override
public ObservableList<Node> getJavaFXText() { public ObservableList<Node> getJavaFXText(ObservableList<Node> nodes) {
// ToDo fix nested base component (formatting), not just a string // ToDo fix nested base component (formatting), not just a string
// This is just a dirty workaround to enable formatting and coloring. Still need to do hover, click, ... stuff // This is just a dirty workaround to enable formatting and coloring. Still need to do hover, click, ... stuff
return new BaseComponent(getLegacyText()).getJavaFXText(); return new BaseComponent(getLegacyText()).getJavaFXText(nodes);
} }
} }

View File

@ -25,6 +25,7 @@ import de.bixilon.minosoft.protocol.ping.ForgeModInfo;
import de.bixilon.minosoft.protocol.ping.ServerListPing; import de.bixilon.minosoft.protocol.ping.ServerListPing;
import de.bixilon.minosoft.util.DNSUtil; import de.bixilon.minosoft.util.DNSUtil;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.geometry.Insets; import javafx.geometry.Insets;
@ -173,7 +174,7 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
} }
serverBrand.setText(ping.getServerModInfo().getBrand()); serverBrand.setText(ping.getServerModInfo().getBrand());
serverBrand.setTooltip(new Tooltip(ping.getServerModInfo().getInfo())); serverBrand.setTooltip(new Tooltip(ping.getServerModInfo().getInfo()));
motd.getChildren().addAll(ping.getMotd().getJavaFXText()); motd.getChildren().addAll(ping.getMotd().getJavaFXText(FXCollections.observableArrayList()));
if (ping.getFavicon() != null) { if (ping.getFavicon() != null) {
icon.setImage(GUITools.getImage(ping.getFavicon())); icon.setImage(GUITools.getImage(ping.getFavicon()));
if (!Arrays.equals(ping.getFavicon(), server.getFavicon())) { if (!Arrays.equals(ping.getFavicon(), server.getFavicon())) {
@ -405,7 +406,7 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
Label serverBrandLabel = new Label(lastPing.getServerBrand()); Label serverBrandLabel = new Label(lastPing.getServerBrand());
Label playersOnlineMaxLabel = new Label(LocaleManager.translate(Strings.SERVER_INFO_SLOTS_PLAYERS_ONLINE, lastPing.getPlayerOnline(), lastPing.getMaxPlayers())); Label playersOnlineMaxLabel = new Label(LocaleManager.translate(Strings.SERVER_INFO_SLOTS_PLAYERS_ONLINE, lastPing.getPlayerOnline(), lastPing.getMaxPlayers()));
TextFlow motdLabel = new TextFlow(); TextFlow motdLabel = new TextFlow();
motdLabel.getChildren().addAll(lastPing.getMotd().getJavaFXText()); motdLabel.getChildren().addAll(lastPing.getMotd().getJavaFXText(FXCollections.observableArrayList()));
Label moddedBrandLabel = new Label(lastPing.getServerModInfo().getBrand()); Label moddedBrandLabel = new Label(lastPing.getServerModInfo().getBrand());
grid.add(new Label(LocaleManager.translate(Strings.SERVER_INFO_REAL_SERVER_ADDRESS) + ":"), 0, ++column); grid.add(new Label(LocaleManager.translate(Strings.SERVER_INFO_REAL_SERVER_ADDRESS) + ":"), 0, ++column);