From b890c2f0eec3627e552f1c6cfc846c8a55663243 Mon Sep 17 00:00:00 2001 From: isXander Date: Sun, 11 Dec 2022 18:27:11 +0000 Subject: close #21 --- .../java/dev/isxander/yacl/gui/utils/GuiUtils.java | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/client/java/dev/isxander/yacl/gui/utils/GuiUtils.java (limited to 'src/client/java/dev/isxander/yacl/gui/utils/GuiUtils.java') 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; + } +} -- cgit