Skip to content

Commit

Permalink
Don't draw the shadow behind text for digital buttons, and fix button…
Browse files Browse the repository at this point in the history
… text y value being slightly off
  • Loading branch information
pupnewfster committed Oct 6, 2024
1 parent 50a38e8 commit 0896283
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TYPE> tile, Class<TYPE> clazz, Consumer<TYPE> 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;
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/mekanism/client/gui/element/GuiElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,7 +20,7 @@ public static BasicColorButton toggle(IGuiWrapper gui, int x, int y, int size, E
private final Supplier<EnumColor> colorSupplier;

public BasicColorButton(IGuiWrapper gui, int x, int y, int size, Supplier<EnumColor> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -20,7 +20,7 @@ public class ColorButton extends MekanismButton {
private final Supplier<EnumColor> colorSupplier;

public ColorButton(IGuiWrapper gui, int x, int y, int width, int height, Supplier<EnumColor> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IFilter<?>> onPress, IntConsumer toggleButtonPress, Function<IFilter<?>, List<ItemStack>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}*/
}
3 changes: 2 additions & 1 deletion src/main/java/mekanism/client/gui/robit/GuiRobitMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()),
Expand Down
51 changes: 27 additions & 24 deletions src/main/java/mekanism/client/render/IFancyFontRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 0896283

Please sign in to comment.