From 089628395c88c34b93a6046a8b128604ae669311 Mon Sep 17 00:00:00 2001 From: Sara Freimer Date: Sun, 6 Oct 2024 10:31:40 -0500 Subject: [PATCH] Don't draw the shadow behind text for digital buttons, and fix button text y value being slightly off --- .../element/button/ReactorLogicButton.java | 4 +- .../client/gui/element/GuiElement.java | 13 +++-- .../gui/element/button/BasicColorButton.java | 4 +- .../gui/element/button/ColorButton.java | 4 +- .../gui/element/button/DigitalButton.java | 5 ++ .../gui/element/button/FilterButton.java | 3 +- .../element/button/FilterSelectButton.java | 4 +- .../element/button/MekanismImageButton.java | 4 +- .../gui/element/button/RadioButton.java | 3 +- .../client/gui/element/text/GuiTextField.java | 4 +- .../gui/element/window/GuiUpgradeWindow.java | 5 -- .../client/gui/robit/GuiRobitMain.java | 3 +- .../client/render/IFancyFontRenderer.java | 51 ++++++++++--------- 13 files changed, 60 insertions(+), 47 deletions(-) diff --git a/src/generators/java/mekanism/generators/client/gui/element/button/ReactorLogicButton.java b/src/generators/java/mekanism/generators/client/gui/element/button/ReactorLogicButton.java index 75aa3e8d698..d88f799db47 100644 --- a/src/generators/java/mekanism/generators/client/gui/element/button/ReactorLogicButton.java +++ b/src/generators/java/mekanism/generators/client/gui/element/button/ReactorLogicButton.java @@ -17,7 +17,7 @@ import mekanism.generators.common.base.IReactorLogicMode; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.Tooltip; -import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.CommonComponents; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -42,7 +42,7 @@ public ReactorLogicButton(IGuiWrapper gui, int x, int y, int index, @NotNull IRe } private ReactorLogicButton(IGuiWrapper gui, int x, int y, @NotNull IReactorLogic tile, Class clazz, Consumer onPress, Supplier<@Nullable TYPE> modeSupplier) { - super(gui, x, y, 128, 22, Component.empty(), (element, mouseX, mouseY) -> ((ReactorLogicButton) element).click()); + super(gui, x, y, 128, 22, CommonComponents.EMPTY, (element, mouseX, mouseY) -> ((ReactorLogicButton) element).click()); this.onPress = onPress; this.modeSupplier = modeSupplier; this.tile = tile; diff --git a/src/main/java/mekanism/client/gui/element/GuiElement.java b/src/main/java/mekanism/client/gui/element/GuiElement.java index cba0909ff9d..0b794b52a88 100644 --- a/src/main/java/mekanism/client/gui/element/GuiElement.java +++ b/src/main/java/mekanism/client/gui/element/GuiElement.java @@ -36,6 +36,7 @@ import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.resources.sounds.SimpleSoundInstance; import net.minecraft.client.sounds.SoundManager; +import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvent; @@ -77,7 +78,7 @@ public abstract class GuiElement extends AbstractWidget implements IFancyFontRen private boolean isDragging; public GuiElement(IGuiWrapper gui, int x, int y, int width, int height) { - this(gui, x, y, width, height, Component.empty()); + this(gui, x, y, width, height, CommonComponents.EMPTY); } public GuiElement(IGuiWrapper gui, int x, int y, int width, int height, Component text) { @@ -619,15 +620,19 @@ protected int getButtonTextColor(int mouseX, int mouseY) { return getFGColor(); } + protected boolean displayButtonTextShadow() { + return true; + } + protected void drawButtonText(GuiGraphics guiGraphics, int mouseX, int mouseY) { Component text = getMessage(); //Only attempt to draw the message if we have a message to draw if (!text.getString().isEmpty()) { int color = getButtonTextColor(mouseX, mouseY) | Mth.ceil(alpha * 255.0F) << 24; - //TODO - 1.21: Do we want to be passing false to this so that it has no shadow? I believe previously we didn't render the shadow - // but vanilla does render the shadow for buttons, so I think we may want to be rendering it? + //Note: We add one to the button height as it is considered bounds, and we want to include the bottom pixel of the button in our calculations of where the text should land //Note: We call super as currently getButtonX and getButtonY already factor in the relative positioning - IFancyFontRenderer.super.drawScrollingString(guiGraphics, text, getButtonX(), getButtonY(), TextAlignment.CENTER, color, getButtonWidth(), getButtonHeight(), 2, true); + IFancyFontRenderer.super.drawScrollingString(guiGraphics, text, getButtonX(), getButtonY(), TextAlignment.CENTER, color, getButtonWidth(), + getButtonHeight() + 1, 2, displayButtonTextShadow()); } } diff --git a/src/main/java/mekanism/client/gui/element/button/BasicColorButton.java b/src/main/java/mekanism/client/gui/element/button/BasicColorButton.java index d1217737c82..9fb13751122 100644 --- a/src/main/java/mekanism/client/gui/element/button/BasicColorButton.java +++ b/src/main/java/mekanism/client/gui/element/button/BasicColorButton.java @@ -7,7 +7,7 @@ import mekanism.client.render.MekanismRenderer; import mekanism.common.lib.Color; import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.CommonComponents; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -20,7 +20,7 @@ public static BasicColorButton toggle(IGuiWrapper gui, int x, int y, int size, E private final Supplier colorSupplier; public BasicColorButton(IGuiWrapper gui, int x, int y, int size, Supplier color, @NotNull IClickable onLeftClick, @Nullable IClickable onRightClick) { - super(gui, x, y, size, size, Component.empty(), onLeftClick, onRightClick); + super(gui, x, y, size, size, CommonComponents.EMPTY, onLeftClick, onRightClick); this.colorSupplier = color; } diff --git a/src/main/java/mekanism/client/gui/element/button/ColorButton.java b/src/main/java/mekanism/client/gui/element/button/ColorButton.java index 030dade289a..595929389f3 100644 --- a/src/main/java/mekanism/client/gui/element/button/ColorButton.java +++ b/src/main/java/mekanism/client/gui/element/button/ColorButton.java @@ -9,7 +9,7 @@ import mekanism.common.MekanismLang; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.Tooltip; -import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.CommonComponents; import org.jetbrains.annotations.NotNull; public class ColorButton extends MekanismButton { @@ -20,7 +20,7 @@ public class ColorButton extends MekanismButton { private final Supplier colorSupplier; public ColorButton(IGuiWrapper gui, int x, int y, int width, int height, Supplier colorSupplier, @NotNull IClickable onPress, @NotNull IClickable onRightClick) { - super(gui, x, y, width, height, Component.empty(), onPress, onRightClick); + super(gui, x, y, width, height, CommonComponents.EMPTY, onPress, onRightClick); this.colorSupplier = colorSupplier; } diff --git a/src/main/java/mekanism/client/gui/element/button/DigitalButton.java b/src/main/java/mekanism/client/gui/element/button/DigitalButton.java index 0ea1fc5e474..eb6e00b7207 100644 --- a/src/main/java/mekanism/client/gui/element/button/DigitalButton.java +++ b/src/main/java/mekanism/client/gui/element/button/DigitalButton.java @@ -22,4 +22,9 @@ protected int getButtonTextColor(int mouseX, int mouseY) { } return Color.argb(screenTextColor()).darken(0.4).argb(); } + + @Override + protected boolean displayButtonTextShadow() { + return false; + } } \ No newline at end of file diff --git a/src/main/java/mekanism/client/gui/element/button/FilterButton.java b/src/main/java/mekanism/client/gui/element/button/FilterButton.java index a0ae1d7206d..88450ee6b16 100644 --- a/src/main/java/mekanism/client/gui/element/button/FilterButton.java +++ b/src/main/java/mekanism/client/gui/element/button/FilterButton.java @@ -28,6 +28,7 @@ import mekanism.common.util.MekanismUtils.ResourceType; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; @@ -59,7 +60,7 @@ private static IFilter getFilter(FilterManager filterManager, int index) { public FilterButton(IGuiWrapper gui, int x, int y, int width, int height, int index, IntSupplier filterIndex, FilterManager filterManager, ObjIntConsumer> onPress, IntConsumer toggleButtonPress, Function, List> renderStackSupplier) { - super(gui, x, y, width, height, Component.empty(), (element, mouseX, mouseY) -> { + super(gui, x, y, width, height, CommonComponents.EMPTY, (element, mouseX, mouseY) -> { FilterButton button = (FilterButton) element; int actualIndex = button.filterIndex.getAsInt() + button.index; button.onPress.accept(getFilter(button.filterManager, actualIndex), actualIndex); diff --git a/src/main/java/mekanism/client/gui/element/button/FilterSelectButton.java b/src/main/java/mekanism/client/gui/element/button/FilterSelectButton.java index 5426b1d1730..b9f4ea71f60 100644 --- a/src/main/java/mekanism/client/gui/element/button/FilterSelectButton.java +++ b/src/main/java/mekanism/client/gui/element/button/FilterSelectButton.java @@ -6,7 +6,7 @@ import mekanism.common.util.MekanismUtils; import mekanism.common.util.MekanismUtils.ResourceType; import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.CommonComponents; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.NotNull; @@ -19,7 +19,7 @@ public class FilterSelectButton extends MekanismButton { private final boolean down; public FilterSelectButton(IGuiWrapper gui, int x, int y, boolean down, @NotNull IClickable onPress) { - super(gui, x, y, 11, 7, Component.empty(), onPress); + super(gui, x, y, 11, 7, CommonComponents.EMPTY, onPress); this.down = down; } diff --git a/src/main/java/mekanism/client/gui/element/button/MekanismImageButton.java b/src/main/java/mekanism/client/gui/element/button/MekanismImageButton.java index 1da39c4cfe1..e6c986c7ba4 100644 --- a/src/main/java/mekanism/client/gui/element/button/MekanismImageButton.java +++ b/src/main/java/mekanism/client/gui/element/button/MekanismImageButton.java @@ -2,7 +2,7 @@ import mekanism.client.gui.IGuiWrapper; import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.CommonComponents; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.NotNull; @@ -28,7 +28,7 @@ public MekanismImageButton(IGuiWrapper gui, int x, int y, int width, int height, public MekanismImageButton(IGuiWrapper gui, int x, int y, int width, int height, int textureWidth, int textureHeight, ResourceLocation resource, @NotNull IClickable onLeftClick, @NotNull IClickable onRightClick) { - super(gui, x, y, width, height, Component.empty(), onLeftClick, onRightClick); + super(gui, x, y, width, height, CommonComponents.EMPTY, onLeftClick, onRightClick); this.resourceLocation = resource; this.textureWidth = textureWidth; this.textureHeight = textureHeight; diff --git a/src/main/java/mekanism/client/gui/element/button/RadioButton.java b/src/main/java/mekanism/client/gui/element/button/RadioButton.java index f45f999db2d..5f24989b6de 100644 --- a/src/main/java/mekanism/client/gui/element/button/RadioButton.java +++ b/src/main/java/mekanism/client/gui/element/button/RadioButton.java @@ -8,6 +8,7 @@ import mekanism.common.util.MekanismUtils.ResourceType; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.Tooltip; +import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.NotNull; @@ -22,7 +23,7 @@ public class RadioButton extends MekanismButton { private final BooleanSupplier toggled; public RadioButton(IGuiWrapper gui, int x, int y, BooleanSupplier toggled, @NotNull IClickable onPress, Component toggledComponent, Component altComponent) { - super(gui, x, y, RADIO_SIZE, RADIO_SIZE, Component.empty(), onPress); + super(gui, x, y, RADIO_SIZE, RADIO_SIZE, CommonComponents.EMPTY, onPress); this.toggled = toggled; this.toggledComponent = TooltipUtils.create(toggledComponent); this.altComponent = TooltipUtils.create(altComponent); diff --git a/src/main/java/mekanism/client/gui/element/text/GuiTextField.java b/src/main/java/mekanism/client/gui/element/text/GuiTextField.java index e9d0ce33053..3730a31b57f 100644 --- a/src/main/java/mekanism/client/gui/element/text/GuiTextField.java +++ b/src/main/java/mekanism/client/gui/element/text/GuiTextField.java @@ -17,6 +17,7 @@ import net.minecraft.client.gui.components.EditBox; import net.minecraft.client.gui.components.events.ContainerEventHandler; import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.util.Mth; import net.minecraft.util.StringUtil; @@ -60,7 +61,7 @@ public GuiTextField(IGuiWrapper gui, ContainerEventHandler parent, int x, int y, super(gui, x, y, width, height); this.parent = parent; - textField = new ClearingEditBox(font(), getX(), getY(), width, height, Component.empty()); + textField = new ClearingEditBox(font(), getX(), getY(), width, height, CommonComponents.EMPTY); textField.setBordered(false); textField.setResponder(s -> { if (responder != null) { @@ -216,6 +217,7 @@ public void drawBackground(@NotNull GuiGraphics guiGraphics, int mouseX, int mou // hacky. we should write our own renderer at some point. float reverse = (1 - textScale) / textScale; pose.scale(textScale, textScale, textScale); + //Note: We use 4 instead of half line height (4.5) as text fields use 8 for calculating text positioning pose.translate(textField.getX() * reverse, (textField.getY() + 4) * reverse, 0); textField.render(guiGraphics, mouseX, mouseY, partialTicks); } diff --git a/src/main/java/mekanism/client/gui/element/window/GuiUpgradeWindow.java b/src/main/java/mekanism/client/gui/element/window/GuiUpgradeWindow.java index 597f8c1a362..5b40963e7eb 100644 --- a/src/main/java/mekanism/client/gui/element/window/GuiUpgradeWindow.java +++ b/src/main/java/mekanism/client/gui/element/window/GuiUpgradeWindow.java @@ -102,9 +102,4 @@ public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) { noSelection.renderWithScale(guiGraphics, rightScreen.getRelativeX() + 2, rightScreen.getRelativeY() + 2, screenTextColor(), 56, 0.8F); } } - - /*@Override - protected int getTitlePadEnd() { - return super.getTitlePadEnd() + 22; - }*/ } \ No newline at end of file diff --git a/src/main/java/mekanism/client/gui/robit/GuiRobitMain.java b/src/main/java/mekanism/client/gui/robit/GuiRobitMain.java index 05dcd430d7e..6d31e75fab6 100644 --- a/src/main/java/mekanism/client/gui/robit/GuiRobitMain.java +++ b/src/main/java/mekanism/client/gui/robit/GuiRobitMain.java @@ -27,6 +27,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.Tooltip; +import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Inventory; import org.jetbrains.annotations.NotNull; @@ -62,7 +63,7 @@ protected void addGuiElements() { addRenderableWidget(GuiSideHolder.create(this, imageWidth, 6, 106, false, false, SpecialColors.TAB_ROBIT_MENU)); addRenderableWidget(new GuiInnerScreen(this, 27, 16, 122, 56, () -> List.of( MekanismLang.ROBIT_GREETING.translate(robit.getName()), - Component.empty(), + CommonComponents.EMPTY, MekanismLang.ENERGY.translate(EnergyDisplay.of(robit.getEnergyContainer().getEnergy())), MekanismLang.ROBIT_FOLLOWING.translate(robit.getFollowing()), MekanismLang.ROBIT_DROP_PICKUP.translate(robit.getDropPickup()), diff --git a/src/main/java/mekanism/client/render/IFancyFontRenderer.java b/src/main/java/mekanism/client/render/IFancyFontRenderer.java index 78959011a5c..79c73b792c9 100644 --- a/src/main/java/mekanism/client/render/IFancyFontRenderer.java +++ b/src/main/java/mekanism/client/render/IFancyFontRenderer.java @@ -45,7 +45,7 @@ default int inactiveButtonTextColor() { return SpecialColors.TEXT_INACTIVE_BUTTON.argb(); } - default int drawString(GuiGraphics guiGraphics, Component component, float x, float y, int color, boolean shadow) { + private int drawString(GuiGraphics guiGraphics, Component component, float x, float y, int color, boolean shadow) { return guiGraphics.drawString(font(), component.getVisualOrderText(), x, y, color, shadow); } @@ -87,16 +87,20 @@ default void drawScrollingString(GuiGraphics guiGraphics, Component text, int x, default void drawScrollingString(GuiGraphics guiGraphics, Component text, int minX, int minY, int maxX, int maxY, TextAlignment alignment, int color, boolean shadow) { int textWidth = getStringWidth(text); + int areaWidth = maxX - minX; + boolean isScrolling = textWidth > areaWidth; //Note: Instead of doing what vanilla does, we divide to float, and don't add one // That way if min and max are not just lineHeight away they will be more accurate, and otherwise it won't render one line below where it should be float targetY = (minY + maxY - getLineHeight()) / 2F; - int areaWidth = maxX - minX; - if (textWidth > areaWidth) { - float targetX = prepScrollingString(guiGraphics, textWidth, areaWidth, minX, minY, maxX, maxY); - drawString(guiGraphics, text, targetX, targetY, color, shadow); - guiGraphics.disableScissor(); + float targetX; + if (isScrolling) { + targetX = prepScrollingString(guiGraphics, textWidth, areaWidth, minX, minY, maxX, maxY); } else { - drawString(guiGraphics, text, alignment.getTarget(minX, maxX, areaWidth, textWidth), targetY, color, shadow); + targetX = alignment.getTarget(minX, maxX, areaWidth, textWidth); + } + drawString(guiGraphics, text, targetX, targetY, color, shadow); + if (isScrolling) { + guiGraphics.disableScissor(); } } @@ -121,16 +125,22 @@ default void drawScaledScrollingString(GuiGraphics guiGraphics, Component text, return; } float textWidth = getStringWidth(text) * scale; + int areaWidth = maxX - minX; + boolean isScrolling = textWidth > areaWidth; //Note: Instead of doing what vanilla does, we divide to float, and don't add one // That way if min and max are not just lineHeight away they will be more accurate, and otherwise it won't render one line below where it should be float targetY = (minY + maxY - getLineHeight()) / 2F; - int areaWidth = maxX - minX; - if (textWidth > areaWidth) { - float targetX = prepScrollingString(guiGraphics, textWidth, areaWidth, minX, minY, maxX, maxY); - drawTextWithScale(guiGraphics, text, targetX, targetY, color, shadow, scale); - guiGraphics.disableScissor(); + float targetX; + if (isScrolling) { + targetX = prepScrollingString(guiGraphics, textWidth, areaWidth, minX, minY, maxX, maxY); } else { - drawTextWithScale(guiGraphics, text, alignment.getTarget(minX, maxX, areaWidth, textWidth), targetY, color, shadow, scale); + targetX = alignment.getTarget(minX, maxX, areaWidth, textWidth); + } + PoseStack pose = prepTextScale(guiGraphics, targetX, targetY, scale); + drawString(guiGraphics, text, 0, 0, color, shadow); + pose.popPose(); + if (isScrolling) { + guiGraphics.disableScissor(); } } @@ -142,10 +152,13 @@ default void drawScaledScrollingString(GuiGraphics guiGraphics, Component text, */ private static float prepScrollingString(GuiGraphics guiGraphics, double textWidth, int areaWidth, int minX, int minY, int maxX, int maxY) { double overflowWidth = textWidth - areaWidth; + //TODO: Some variable that keeps track of when the gui was initialized (init method called?) so that we can ensure it always starts + // the text all the way at the left. (For right to left languages we should probably start it all the way at the right) double seconds = Util.getMillis() / 1_000D; double scrollPeriod = Math.max(overflowWidth * AbstractWidget.PERIOD_PER_SCROLLED_PIXEL, AbstractWidget.MIN_SCROLL_PERIOD); + //TODO: Improve this, as it moves very slowly at the edges, and much quicker in the middle double scrolledSoFar = Math.sin((Math.PI / 2) * Math.cos((2 * Math.PI) * seconds / scrollPeriod)) / 2.0 + AbstractWidget.PERIOD_PER_SCROLLED_PIXEL; - //Vanilla uses: Mth.lerp(d2, 0.0, overflowWidth); to calculate overflowedBy. But that is equivalent to just multiplying performing the multiplication + //Vanilla uses: Mth.lerp(d2, 0.0, overflowWidth); to calculate overflowedBy. But that is equivalent to just performing the following multiplication double overflowedBy = scrolledSoFar * overflowWidth; //Note: We are drawing in relative coordinates, but GuiGraphics#enableScissor, is expecting absolute coordinates, // so we need to get the translations from our pose stack @@ -159,20 +172,10 @@ private static float prepScrollingString(GuiGraphics guiGraphics, double textWid return minX - (int) overflowedBy; } - private void drawTextWithScale(GuiGraphics guiGraphics, Component text, float x, float y, int color, boolean shadow, float scale) { - PoseStack pose = prepTextScale(guiGraphics, x, y, scale); - drawString(guiGraphics, text, 0, 0, color, shadow); - pose.popPose(); - } - //Note: As translate will implicitly cast x and y to being floats, we might as well pass these in as floats to reduce duplicate code private PoseStack prepTextScale(GuiGraphics guiGraphics, float x, float y, float scale) { PoseStack pose = guiGraphics.pose(); pose.pushPose(); - return prepTextScale(pose, x, y, scale); - } - - private PoseStack prepTextScale(PoseStack pose, float x, float y, float scale) { float halfLineHeight = getLineHeight() / 2F; float yAdd = halfLineHeight - halfLineHeight * scale; pose.translate(x, y + yAdd, 0);