[1.19.x] Fix ForgeSlider not respecting custom height (#9237)

This commit is contained in:
matyrobbrt authored and GitHub committed 2023-02-05 18:58:08 +01:00
1 parent 3c19a0309e
commit 94af5cc43a
5 files changed
+95 -9

No files matched your search

@@ -0,0 +1,20 @@
--- a/net/minecraft/client/gui/Font.java
+++ b/net/minecraft/client/gui/Font.java
@@ -34,7 +_,7 @@
import org.joml.Vector3f;
@OnlyIn(Dist.CLIENT)
-public class Font {
+public class Font implements net.minecraftforge.client.extensions.IForgeFont {
private static final float f_168643_ = 0.01F;
private static final Vector3f f_92712_ = new Vector3f(0.0F, 0.0F, 0.03F);
public static final int f_193827_ = 8;
@@ -261,6 +_,8 @@
public StringSplitter m_92865_() {
return this.f_92714_;
}
+
+ @Override public Font self() { return this; }
@OnlyIn(Dist.CLIENT)
public static enum DisplayMode {
@@ -0,0 +1,42 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.client.extensions;
import net.minecraft.client.gui.Font;
import net.minecraft.network.chat.FormattedText;
/**
* Extension interface for {@link Font}.
*/
public interface IForgeFont
{
FormattedText ELLIPSIS = FormattedText.of("...");
Font self();
/**
* If the width of the text exceeds {@code maxWidth}, an ellipse is added and the text is substringed.
*
* @param text the text to ellipsize if needed
* @param maxWidth the maximum width of the text
* @return the ellipsized text
*/
default FormattedText ellipsize(FormattedText text, int maxWidth)
{
final Font self = self();
final int strWidth = self.width(text);
final int ellipsisWidth = self.width(ELLIPSIS);
if (strWidth > maxWidth)
{
if (ellipsisWidth >= maxWidth) return self.substrByWidth(text, maxWidth);
return FormattedText.composite(
self.substrByWidth(text, maxWidth - ellipsisWidth),
ELLIPSIS
);
}
return text;
}
}
@@ -8,7 +8,9 @@ package net.minecraftforge.client.gui.widget;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.locale.Language;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FormattedText;
import net.minecraftforge.client.gui.ScreenUtils;
/**
@@ -49,14 +51,7 @@ public class ExtendedButton extends Button
ScreenUtils.blitWithBorder(poseStack, WIDGETS_LOCATION, this.getX(), this.getY(), 0, 46 + k * 20, this.width, this.height, 200, 20, 2, 3, 2, 2, this.getBlitOffset());
this.renderBg(poseStack, mc, mouseX, mouseY);
Component buttonText = this.getMessage();
int strWidth = mc.font.width(buttonText);
int ellipsisWidth = mc.font.width("...");
if (strWidth > width - 6 && strWidth > ellipsisWidth)
//TODO, srg names make it hard to figure out how to append to an ITextProperties from this trim operation, wraping this in StringTextComponent is kinda dirty.
buttonText = Component.literal(mc.font.substrByWidth(buttonText, width - 6 - ellipsisWidth).getString() + "...");
drawCenteredString(poseStack, mc.font, buttonText, this.getX() + this.width / 2, this.getY() + (this.height - 8) / 2, getFGColor());
final FormattedText buttonText = mc.font.ellipsize(this.getMessage(), this.width - 6); // Remove 6 pixels so that the text is always contained within the button's borders
drawCenteredString(poseStack, mc.font, Language.getInstance().getVisualOrder(buttonText), this.getX() + this.width / 2, this.getY() + (this.height - 8) / 2, getFGColor());
}
}
@@ -5,9 +5,17 @@
package net.minecraftforge.client.gui.widget;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.AbstractSliderButton;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.locale.Language;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.util.Mth;
import net.minecraftforge.client.gui.ScreenUtils;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;
import java.text.DecimalFormat;
@@ -216,4 +224,21 @@ public class ForgeSlider extends AbstractSliderButton
@Override
protected void applyValue() {}
@Override
public void renderButton(@NotNull PoseStack poseStack, int mouseX, int mouseY, float partialTick)
{
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, WIDGETS_LOCATION);
final Minecraft mc = Minecraft.getInstance();
final int bgYImage = this.getYImage(this.isHoveredOrFocused());
ScreenUtils.blitWithBorder(poseStack, this.getX(), this.getY(), 0, 46 + bgYImage * 20, this.width, this.height, 200, 20, 2, 3, 2, 2, this.getBlitOffset());
final int sliderYImage = (this.isHoveredOrFocused() ? 2 : 1) * 20;
ScreenUtils.blitWithBorder(poseStack, this.getX() + (int)(this.value * (double)(this.width - 8)), this.getY(), 0, 46 + sliderYImage, 8, this.height, 200, 20 , 2, 3, 2, 2, this.getBlitOffset());
final FormattedText message = mc.font.ellipsize(getMessage(), this.width - 6);
drawCenteredString(poseStack, mc.font, Language.getInstance().getVisualOrder(message), this.getX() + this.width / 2, this.getY() + (this.height - 8) / 2, getFGColor());
}
}
@@ -6,6 +6,7 @@
package net.minecraftforge.debug.client;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
@@ -14,6 +15,7 @@ import net.minecraft.network.chat.Component;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.ScreenEvent;
import net.minecraftforge.client.gui.widget.ExtendedButton;
import net.minecraftforge.client.gui.widget.ForgeSlider;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@@ -79,6 +81,8 @@ public class GuiLayeringTest
this.addRenderableWidget(Button.builder(Component.literal("Push New Layer"), this::pushLayerButton).pos(xoff, yoff + buttonSpacing * (cnt++)).size(buttonWidth, buttonHeight).build(ExtendedButton::new));
this.addRenderableWidget(Button.builder(Component.literal("Pop Current Layer"), this::popLayerButton).pos(xoff, yoff + buttonSpacing * (cnt++)).size(buttonWidth, buttonHeight).build(ExtendedButton::new));
this.addRenderableWidget(Button.builder(Component.literal("Close entire stack"), this::closeStack).pos(xoff, yoff + buttonSpacing * (cnt++)).size(buttonWidth, buttonHeight).build(ExtendedButton::new));
this.addRenderableWidget(new ForgeSlider(xoff, yoff + buttonSpacing * cnt, 50, 25, Component.literal("Val: ").withStyle(ChatFormatting.GOLD), Component.literal("some text which will be cut off"), 5, 55, 6, true));
}
private void closeStack(Button button)