diff options
Diffstat (limited to 'src/client/java/dev/isxander/yacl/gui/utils/GuiUtils.java')
-rw-r--r-- | src/client/java/dev/isxander/yacl/gui/utils/GuiUtils.java | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/client/java/dev/isxander/yacl/gui/utils/GuiUtils.java b/src/client/java/dev/isxander/yacl/gui/utils/GuiUtils.java new file mode 100644 index 0000000..670bd12 --- /dev/null +++ b/src/client/java/dev/isxander/yacl/gui/utils/GuiUtils.java @@ -0,0 +1,38 @@ +package dev.isxander.yacl.gui.utils; + +import com.mojang.blaze3d.systems.RenderSystem; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.util.Window; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; +import net.minecraft.util.Language; + +public class GuiUtils { + public static MutableText translatableFallback(String key, Text fallback) { + if (Language.getInstance().hasTranslation(key)) + return Text.translatable(key); + return fallback.copy(); + } + + public static void enableScissor(int x, int y, int width, int height) { + Window window = MinecraftClient.getInstance().getWindow(); + double d = window.getScaleFactor(); + RenderSystem.enableScissor((int)(x * d), (int)((window.getScaledHeight() - y - height) * d), (int)(width * d), (int)(height * d)); + } + + public static String shortenString(String string, TextRenderer textRenderer, int maxWidth, String suffix) { + boolean firstIter = true; + while (textRenderer.getWidth(string) > maxWidth) { + string = string.substring(0, Math.max(string.length() - 1 - (firstIter ? 1 : suffix.length() + 1), 0)).trim(); + string += suffix; + + if (string.equals(suffix)) + break; + + firstIter = false; + } + + return string; + } +} |