Skip to content

Commit

Permalink
Implement translatable text contents
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Feb 23, 2024
1 parent 14d9939 commit 511cce7
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import fr.thesmyler.smylibgui.widgets.buttons.TextButtonWidget;
import fr.thesmyler.smylibgui.widgets.text.TextAlignment;
import fr.thesmyler.smylibgui.widgets.text.TextWidget;
import net.minecraft.util.text.TextComponentTranslation;

import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.smylib.text.ImmutableText.ofTranslation;

/**
* A {@link PopupScreen} that provides the user with multiple choices, in the form of buttons to click.
Expand All @@ -22,7 +22,7 @@ public class MultiChoicePopupScreen extends PopupScreen {
public MultiChoicePopupScreen(String titleLangKey, Map<String, Runnable> options) {
super(300f, 200f);
WidgetContainer content = this.getContent();
TextWidget titleWidget = new TextWidget(150f, 7f, 1, new TextComponentTranslation(titleLangKey), TextAlignment.CENTER, content.getFont());
TextWidget titleWidget = new TextWidget(150f, 7f, 1, ofTranslation(titleLangKey), TextAlignment.CENTER, content.getFont());
content.addWidget(titleWidget);
FlexibleWidgetContainer container = new FlexibleWidgetContainer(0f, 0f, 1, 275f, 10f);
float y = 5f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import fr.thesmyler.smylibgui.widgets.text.TextWidget;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import net.smyler.smylib.gui.DrawContext;
import net.smyler.smylib.text.Text;
import org.jetbrains.annotations.Nullable;

import static net.smyler.smylib.SmyLib.getGameClient;
Expand Down Expand Up @@ -108,7 +108,7 @@ public void setContentSize(float width, float height) {
*
* @param message the text to display
*/
public static void showMessage(ITextComponent message) {
public static void showMessage(Text message) {
TextWidget text = new TextWidget(0, 0, 0, message, TextAlignment.CENTER, getGameClient().defaultFont());
float padding = 10;
text.setMaxWidth(300);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public TextWidget(float x, float y, int z, float maxWidth, Text text, TextAlignm
this.updateCoords();
}

@Deprecated
public TextWidget(float x, float y, int z, ITextComponent text, TextAlignment alignment, Font font) {
this(x, y, z, ofPlainText(text.getUnformattedText()), alignment, font);
}

public TextWidget(float x, float y, int z, Text text, TextAlignment alignment, Font font) {
this(x, y, z, Float.MAX_VALUE, text, alignment, Color.WHITE, true, font);
}
Expand All @@ -71,20 +66,10 @@ public TextWidget(float x, float y, int z, Font font) {
this(x, y, z, ImmutableText.EMPTY, TextAlignment.RIGHT, font);
}

@Deprecated
public TextWidget(int z, ITextComponent text, TextAlignment alignment, Font font) {
this(z, ofPlainText(text.getUnformattedText()), alignment, font);
}

public TextWidget(int z, Text text, TextAlignment alignment, Font font) {
this(0, 0, z, Float.MAX_VALUE, text, alignment, Color.WHITE, true, font);
}

@Deprecated
public TextWidget(int z, ITextComponent text, Font font) {
this(z, ofPlainText(text.getUnformattedText()), font);
}

public TextWidget(int z, Text text, Font font) {
this(0, 0, z, text, font);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import fr.thesmyler.terramap.gui.widgets.map.MapLayer;
import fr.thesmyler.terramap.gui.widgets.map.MapWidget;
import fr.thesmyler.terramap.gui.widgets.map.layer.RasterMapLayer;
import net.minecraft.util.text.TextComponentTranslation;

import net.smyler.smylib.gui.DrawContext;
import org.jetbrains.annotations.Nullable;
Expand All @@ -26,6 +25,7 @@
import static java.util.Comparator.comparing;
import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.smylib.text.ImmutableText.ofPlainText;
import static net.smyler.smylib.text.ImmutableText.ofTranslation;

class LayerListContainer extends FlexibleWidgetContainer {

Expand Down Expand Up @@ -137,7 +137,7 @@ private class BackgroundLayerEntry extends LayerEntry {
public BackgroundLayerEntry(float y, RasterMapLayer layer) {
super(layer, y, 20);
TextWidget name = new TextWidget(5, 7, 0, ofPlainText(layer.name()), TextAlignment.RIGHT, this.getFont());
TextWidget type = new TextWidget(5, 23, 0, new TextComponentTranslation("terramap.terramapscreen.layerscreen.raster_background.type"), TextAlignment.RIGHT, this.getFont());
TextWidget type = new TextWidget(5, 23, 0, ofTranslation("terramap.terramapscreen.layerscreen.raster_background.type"), TextAlignment.RIGHT, this.getFont());
type.setBaseColor(Color.MEDIUM_GRAY);
this.addWidget(name);
this.addWidget(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static fr.thesmyler.terramap.gui.widgets.map.MapLayerRegistry.OFFSET_PREVIEW;
import static fr.thesmyler.terramap.util.geo.GeoServices.formatZoomLevelForDisplay;
import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.smylib.text.ImmutableText.ofTranslation;

public class LayerRenderingOffsetPopup extends PopupScreen {

Expand Down Expand Up @@ -67,19 +68,19 @@ public LayerRenderingOffsetPopup(MapLayer background, MapLayer layer) {
WidgetContainer content = this.getContent();
TextWidget title = new TextWidget(
content.getWidth() / 2, margin, 0,
new TextComponentTranslation("terramap.popup.renderoffset.title"),
ofTranslation("terramap.popup.renderoffset.title"),
TextAlignment.CENTER,
game.defaultFont());
content.addWidget(title);
TextWidget xText = new TextWidget(
margin, title.getY() + title.getHeight() + interline, 0,
new TextComponentTranslation("terramap.popup.renderoffset.x"),
ofTranslation("terramap.popup.renderoffset.x"),
TextAlignment.RIGHT,
game.defaultFont());
content.addWidget(xText);
TextWidget yText = new TextWidget(
margin, xText.getY() + xText.getHeight() + interline, 0,
new TextComponentTranslation("terramap.popup.renderoffset.y"),
ofTranslation("terramap.popup.renderoffset.y"),
TextAlignment.RIGHT,
game.defaultFont());
content.addWidget(yText);
Expand Down Expand Up @@ -207,7 +208,7 @@ private void updateMap() {
this.yInput.setText(OFFSET_FORMATTER.format(this.updateMapDelta.y()));
}
this.background.setPixelRenderingOffset(this.updateMapDelta.scale(-1d));
this.zoomText.setText(new TextComponentTranslation("terramap.popup.renderoffset.zoom", formatZoomLevelForDisplay(zoom)));
this.zoomText.setText(ofTranslation("terramap.popup.renderoffset.zoom", formatZoomLevelForDisplay(zoom)));
}

private void setOffsetToZero() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import java.util.*;
import java.util.function.Consumer;

import net.minecraft.util.text.TextFormatting;
import net.smyler.smylib.gui.DrawContext;
import net.smyler.smylib.gui.Scissor;
import net.smyler.smylib.text.ImmutableText;
import net.smyler.smylib.text.Text;
import net.smyler.smylib.text.TextStyle;
import org.jetbrains.annotations.Nullable;

Expand All @@ -16,7 +16,6 @@
import fr.thesmyler.terramap.maps.raster.imp.UrlTiledMap;
import fr.thesmyler.terramap.util.geo.*;
import net.buildtheearth.terraplusplus.generator.EarthGeneratorSettings;
import net.minecraft.util.text.*;
import net.smyler.smylib.Color;
import net.smyler.smylib.game.GameClient;
import net.smyler.smylib.game.Translator;
Expand Down Expand Up @@ -73,6 +72,7 @@
import static net.smyler.smylib.math.Math.clamp;
import static java.util.stream.Collectors.toMap;
import static net.smyler.smylib.text.ImmutableText.ofPlainText;
import static net.smyler.smylib.text.ImmutableText.ofTranslation;


public class TerramapScreen extends Screen implements ITabCompleter {
Expand Down Expand Up @@ -263,7 +263,7 @@ public void initGui() {
this.layerPanel.setOpenX(0).setOpenY(this.layerPanel.getClosedY());
this.layerPanel.addWidget(new TextWidget(
this.layerPanel.getWidth() / 2, 7, 1,
new TextComponentTranslation("terramap.terramapscreen.layerscreen.title"), TextAlignment.CENTER,
ofTranslation("terramap.terramapscreen.layerscreen.title"), TextAlignment.CENTER,
content.getFont()));
this.layerPanel.addWidget(new TexturedButtonWidget(
this.layerPanel.getWidth() - 20, 5, 1,
Expand Down Expand Up @@ -362,7 +362,7 @@ public void onUpdate() {
String formatScale = "-";
String formatOrientation = "-";
if(!WebMercatorUtil.PROJECTION_BOUNDS.contains(mouseLocation)) {
this.distortionText.setText(new TextComponentTranslation("terramap.terramapscreen.information.distortion", "-", "-"));
this.distortionText.setText(ofTranslation("terramap.terramapscreen.information.distortion", "-", "-"));
} else {
if(projection != null) {
try {
Expand All @@ -371,9 +371,9 @@ public void onUpdate() {
formatOrientation = GeoServices.formatGeoCoordForDisplay(Math.toDegrees(dist[1]));
} catch (OutOfProjectionBoundsException ignored) {
}
this.distortionText.setText(new TextComponentTranslation("terramap.terramapscreen.information.distortion", formatScale, formatOrientation));
this.distortionText.setText(ofTranslation("terramap.terramapscreen.information.distortion", formatScale, formatOrientation));
} else {
this.distortionText.setText(new TextComponentTranslation("terramap.terramapscreen.information.distortion", "-", "-"));
this.distortionText.setText(ofTranslation("terramap.terramapscreen.information.distortion", "-", "-"));
}
}

Expand All @@ -382,24 +382,24 @@ public void onUpdate() {
GeoPoint<?> markerLocation = marker.getLocation();
String markerName = marker.getDisplayName().getFormattedText();
if(markerLocation == null) {
this.playerGeoLocationText.setText(new TextComponentTranslation("terramap.terramapscreen.information.trackedoutsidemap", markerName));
this.playerGeoLocationText.setText(ofTranslation("terramap.terramapscreen.information.trackedoutsidemap", markerName));
} else {
String trackFormatLon = GeoServices.formatGeoCoordForDisplay(markerLocation.longitude());
String trackFormatLat = GeoServices.formatGeoCoordForDisplay(markerLocation.latitude());
this.playerGeoLocationText.setText(new TextComponentTranslation("terramap.terramapscreen.information.tracked", markerName, trackFormatLat, trackFormatLon));
this.playerGeoLocationText.setText(ofTranslation("terramap.terramapscreen.information.tracked", markerName, trackFormatLat, trackFormatLon));
}
} else if(this.map.getMainPlayerMarker() != null){
Marker marker = this.map.getMainPlayerMarker();
GeoPoint<?> markerLocation = marker.getLocation();
if(markerLocation == null) {
this.playerGeoLocationText.setText(new TextComponentTranslation("terramap.terramapscreen.information.playerout"));
this.playerGeoLocationText.setText(ofTranslation("terramap.terramapscreen.information.playerout"));
} else {
String formatedLon = GeoServices.formatGeoCoordForDisplay(markerLocation.longitude());
String formatedLat = GeoServices.formatGeoCoordForDisplay(markerLocation.latitude());
this.playerGeoLocationText.setText(new TextComponentTranslation("terramap.terramapscreen.information.playergeo", formatedLat, formatedLon));
this.playerGeoLocationText.setText(ofTranslation("terramap.terramapscreen.information.playergeo", formatedLat, formatedLon));
}
} else {
this.playerGeoLocationText.setText(new TextComponentTranslation("terramap.terramapscreen.information.noplayer"));
this.playerGeoLocationText.setText(ofTranslation("terramap.terramapscreen.information.noplayer"));
}

this.offsetWarning.setVisibility(this.map.getRasterBackgroundLayer().map(MapLayer::hasRenderingOffset).orElse(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import net.smyler.smylib.gui.DrawContext;

import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.smylib.text.ImmutableText.ofTranslation;

public class HudConfigScreen extends Screen {

Expand Down Expand Up @@ -181,13 +182,13 @@ public void initGui() {

// Boolean lines
LinkedList<TextWidget> buttonsTexts = new LinkedList<>();
buttonsTexts.add(new TextWidget(10, new TextComponentTranslation("terramap.hudconfig.minimap"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, new TextComponentTranslation("terramap.hudconfig.compass"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, new TextComponentTranslation("terramap.hudconfig.players"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, new TextComponentTranslation("terramap.hudconfig.entities"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, new TextComponentTranslation("terramap.hudconfig.directions"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, new TextComponentTranslation("terramap.hudconfig.rotation"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, new TextComponentTranslation("terramap.hudconfig.chunks"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, ofTranslation("terramap.hudconfig.minimap"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, ofTranslation("terramap.hudconfig.compass"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, ofTranslation("terramap.hudconfig.players"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, ofTranslation("terramap.hudconfig.entities"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, ofTranslation("terramap.hudconfig.directions"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, ofTranslation("terramap.hudconfig.rotation"), game.defaultFont()));
buttonsTexts.add(new TextWidget(10, ofTranslation("terramap.hudconfig.chunks"), game.defaultFont()));
this.minimapButton.setTooltip(translator.format("terramap.hudconfig.minimap.tooltip"));
this.compassButton.setTooltip(translator.format("terramap.hudconfig.compass.tooltip"));
this.otherPlayersButton.setTooltip(translator.format("terramap.hudconfig.players.tooltip"));
Expand Down Expand Up @@ -259,7 +260,7 @@ public void initGui() {
this.buttonPanel.setOpenX(this.buttonPanel.getClosedX()).setOpenY(this.height - this.settingsPanel.getHeight() - this.buttonPanel.getHeight());
this.settingsPanel.setOpenY(this.height - this.settingsPanel.getHeight());

TextWidget explain = new TextWidget(this.width / 2f, this.height / 2f - 100f, 10, new TextComponentTranslation("terramap.hudconfig.explain"), TextAlignment.CENTER, game.defaultFont());
TextWidget explain = new TextWidget(this.width / 2f, this.height / 2f - 100f, 10, ofTranslation("terramap.hudconfig.explain"), TextAlignment.CENTER, game.defaultFont());
content.addWidget(explain.setMaxWidth(this.width * .8f).setAnchorY(this.height / 2f - explain.getHeight() - 10f));

content.addWidget(this.buttonPanel);
Expand Down
Loading

0 comments on commit 511cce7

Please sign in to comment.