Search in sources :

Example 1 with StringVisitable

use of net.minecraft.text.StringVisitable in project MiniMap by pl3xgaming.

the class Tab method render.

@Override
public void render(MatrixStack matrixStack, float mouseX, float mouseY, float delta) {
    super.render(matrixStack, mouseX, mouseY, delta);
    icon().tint(matrixStack, iconX(), iconY(), iconSize(), color());
    if (width() > Sidebar.DEFAULT_WIDTH) {
        if (width() < Sidebar.HOVER_WIDTH) {
            // only trim text if sidebar is between open and
            // closed states since this is sort of expensive
            StringVisitable str = Font.RALEWAY.trimToWidth(text(), (int) (width() - textX()));
            for (OrderedText orderedText : Font.RALEWAY.wrapLines(str, Integer.MAX_VALUE)) {
                Font.RALEWAY.draw(matrixStack, orderedText, textX(), textY(), color());
            }
        } else {
            Font.RALEWAY.draw(matrixStack, text(), textX(), textY(), color());
        }
    }
    if (hovered()) {
        Mouse.INSTANCE.cursor(Cursor.HAND_POINTER);
    }
}
Also used : StringVisitable(net.minecraft.text.StringVisitable) OrderedText(net.minecraft.text.OrderedText)

Example 2 with StringVisitable

use of net.minecraft.text.StringVisitable in project BleachHack by BleachDrinker420.

the class UpdateScreen method init.

public void init() {
    super.init();
    int wd = Math.min(width / 2 - 30, 175);
    addWindow(new Window(width / 2 - wd, height / 16, width / 2 + wd, height - height / 16, String.format("BleachHack Update [%s -> %s]", BleachHack.VERSION, updateJson.get("name").getAsString()), new ItemStack(Items.MAGENTA_GLAZED_TERRACOTTA)));
    int w = getWindow(0).x2 - getWindow(0).x1;
    int h = getWindow(0).y2 - getWindow(0).y1;
    getWindow(0).addWidget(new WindowTextWidget("A new BleachHack update is available.", true, WindowTextWidget.TextAlign.MIDDLE, 1.5f, w / 2, 18, 0xe0e0e0));
    getWindow(0).addWidget(new WindowBoxWidget(3, 50, w - 3, h - 23));
    ImmutablePairList<String, Boolean> changelog = new ImmutablePairList<>();
    if (updateJson.has("changelog") && updateJson.get("changelog").isJsonArray()) {
        for (JsonElement je : updateJson.get("changelog").getAsJsonArray()) {
            if (je.isJsonPrimitive()) {
                String string = je.getAsString();
                if (string.charAt(0) == '-')
                    string = "\u00a77-\u00a7r" + string.substring(1);
                List<StringVisitable> wrapped = client.textRenderer.getTextHandler().wrapLines(string, w - 32, Style.EMPTY);
                for (int i = 0; i < wrapped.size(); i++) changelog.add(wrapped.get(i).getString(), i == 0);
            }
        }
    } else {
        changelog.add("Could not find changelog.", false);
    }
    changelogWidgets.clear();
    changelogWidgets.add(getWindow(0).addWidget(new WindowTextWidget(updateJson.get("name").getAsString(), true, WindowTextWidget.TextAlign.MIDDLE, 2.5f, w / 2, 58, 0xe0e0e0)));
    for (int i = 0; i < changelog.size(); i++) {
        if (changelog.get(i).getValue()) {
            changelogWidgets.add(getWindow(0).addWidget(new WindowTextWidget("*", true, 10, 87 + i * 10, 0xa0a0a0)));
        }
        changelogWidgets.add(getWindow(0).addWidget(new WindowTextWidget(changelog.get(i).getKey(), true, 19, 85 + i * 10, 0xe0e0e0)));
    }
    scrollbar = getWindow(0).addWidget(new WindowScrollbarWidget(w - 14, 51, 37 + changelog.size() * 10, h - 75, 0));
    getWindow(0).addWidget(new WindowButtonWidget(3, h - 21, w / 2 - 2, h - 3, "Website", () -> Util.getOperatingSystem().open(URI.create("https://bleachhack.org/"))));
    getWindow(0).addWidget(new WindowButtonWidget(w / 2 + 2, h - 21, w - 3, h - 3, "Update", () -> {
        try {
            JsonObject installerJson = updateJson.get("installer").getAsJsonObject();
            if (installerJson.has("os") && installerJson.get("os").isJsonPrimitive() && !System.getProperty("os.name").startsWith(installerJson.get("os").getAsString())) {
                updateResult = "Updater doesn't support your OS!";
                selectWindow(1);
                return;
            }
            File modpath = new File(FabricLoader.getInstance().getModContainer("bleachhack").get().getOrigin().getPaths().get(0).toUri());
            if (!modpath.isFile()) {
                updateResult = "Invalid mod path!";
                selectWindow(1);
                return;
            }
            String link = installerJson.get("link").getAsString();
            String name = link.replaceFirst("^.*\\/", "");
            File installerFile = new File(System.getProperty("java.io.tmpdir"), name);
            BleachLogger.logger.info("\n> Installer path: " + installerFile + "\n> Installer URL: " + link + "\n> Installer file name: " + name + "\n> Regular File: " + Files.isRegularFile(installerFile.toPath()) + "\n> File Length: " + installerFile.length());
            if (!Files.isRegularFile(installerFile.toPath()) || installerFile.length() <= 1024L) {
                FileUtils.copyURLToFile(new URL(link), installerFile);
            }
            String execCommand = link.endsWith(".jar") ? "java -jar " : "cmd /c start ";
            Runtime.getRuntime().exec(execCommand + installerFile.getAbsolutePath() + " " + modpath + " " + installerJson.get("url").getAsString());
            client.scheduleStop();
        } catch (Exception e) {
            updateResult = "Unknown error!";
            selectWindow(1);
            e.printStackTrace();
        }
    }));
    wd = Math.min(width / 2 - 20, 90);
    addWindow(new Window(width / 2 - wd, height / 2 - 15, width / 2 + wd, height / 2 + 15, "Error updating!", new ItemStack(Items.RED_BANNER), true));
    getWindow(1).addWidget(new WindowTextWidget("", true, WindowTextWidget.TextAlign.MIDDLE, wd, 16, 0xc05050).withRenderEvent((wg, ms, wx, wy) -> ((WindowTextWidget) wg).setText(new LiteralText(updateResult))));
}
Also used : Window(org.bleachhack.gui.window.Window) FabricLoader(net.fabricmc.loader.api.FabricLoader) JsonObject(com.google.gson.JsonObject) LiteralText(net.minecraft.text.LiteralText) Util(net.minecraft.util.Util) URL(java.net.URL) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet) ItemStack(net.minecraft.item.ItemStack) Style(net.minecraft.text.Style) StringVisitable(net.minecraft.text.StringVisitable) Window(org.bleachhack.gui.window.Window) BleachLogger(org.bleachhack.util.BleachLogger) ImmutablePairList(org.bleachhack.util.collections.ImmutablePairList) URI(java.net.URI) org.bleachhack.gui.window.widget(org.bleachhack.gui.window.widget) WindowScreen(org.bleachhack.gui.window.WindowScreen) Files(java.nio.file.Files) BleachHack(org.bleachhack.BleachHack) MatrixStack(net.minecraft.client.util.math.MatrixStack) Set(java.util.Set) FileUtils(org.apache.commons.io.FileUtils) Items(net.minecraft.item.Items) File(java.io.File) List(java.util.List) Screen(net.minecraft.client.gui.screen.Screen) ImmutablePairList(org.bleachhack.util.collections.ImmutablePairList) JsonObject(com.google.gson.JsonObject) URL(java.net.URL) JsonElement(com.google.gson.JsonElement) StringVisitable(net.minecraft.text.StringVisitable) ItemStack(net.minecraft.item.ItemStack) File(java.io.File) LiteralText(net.minecraft.text.LiteralText)

Example 3 with StringVisitable

use of net.minecraft.text.StringVisitable in project Polymorph by TheIllusiveC4.

the class SelectionWidget method drawHoveringText.

public static void drawHoveringText(MatrixStack mStack, List<? extends StringVisitable> textLines, int mouseX, int mouseY, int screenWidth, int screenHeight, int maxTextWidth, int backgroundColor, int borderColorStart, int borderColorEnd, TextRenderer font) {
    if (!textLines.isEmpty()) {
        RenderSystem.disableDepthTest();
        int tooltipTextWidth = 0;
        for (StringVisitable textLine : textLines) {
            int textLineWidth = font.getWidth(textLine);
            if (textLineWidth > tooltipTextWidth) {
                tooltipTextWidth = textLineWidth;
            }
        }
        boolean needsWrap = false;
        int titleLinesCount = 1;
        int tooltipX = mouseX + 12;
        if (tooltipX + tooltipTextWidth + 4 > screenWidth) {
            tooltipX = mouseX - 16 - tooltipTextWidth;
            if (tooltipX < 4) {
                if (mouseX > screenWidth / 2) {
                    tooltipTextWidth = mouseX - 12 - 8;
                } else {
                    tooltipTextWidth = screenWidth - 16 - mouseX;
                }
                needsWrap = true;
            }
        }
        if (maxTextWidth > 0 && tooltipTextWidth > maxTextWidth) {
            tooltipTextWidth = maxTextWidth;
            needsWrap = true;
        }
        if (needsWrap) {
            int wrappedTooltipWidth = 0;
            List<StringVisitable> wrappedTextLines = new ArrayList<>();
            for (int i = 0; i < textLines.size(); i++) {
                StringVisitable textLine = textLines.get(i);
                List<StringVisitable> wrappedLine = font.getTextHandler().wrapLines(textLine, tooltipTextWidth, Style.EMPTY);
                if (i == 0) {
                    titleLinesCount = wrappedLine.size();
                }
                for (StringVisitable line : wrappedLine) {
                    int lineWidth = font.getWidth(line);
                    if (lineWidth > wrappedTooltipWidth) {
                        wrappedTooltipWidth = lineWidth;
                    }
                    wrappedTextLines.add(line);
                }
            }
            tooltipTextWidth = wrappedTooltipWidth;
            textLines = wrappedTextLines;
            if (mouseX > screenWidth / 2) {
                tooltipX = mouseX - 16 - tooltipTextWidth;
            } else {
                tooltipX = mouseX + 12;
            }
        }
        int tooltipY = mouseY - 12;
        int tooltipHeight = 8;
        if (textLines.size() > 1) {
            tooltipHeight += (textLines.size() - 1) * 10;
            if (textLines.size() > titleLinesCount) {
                tooltipHeight += 2;
            }
        }
        if (tooltipY < 4) {
            tooltipY = 4;
        } else if (tooltipY + tooltipHeight + 4 > screenHeight) {
            tooltipY = screenHeight - tooltipHeight - 4;
        }
        final int zLevel = 900;
        mStack.push();
        Matrix4f mat = mStack.peek().getPositionMatrix();
        drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY - 4, tooltipX + tooltipTextWidth + 3, tooltipY - 3, backgroundColor, backgroundColor);
        drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY + tooltipHeight + 3, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 4, backgroundColor, backgroundColor);
        drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY - 3, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor);
        drawGradientRect(mat, zLevel, tooltipX - 4, tooltipY - 3, tooltipX - 3, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor);
        drawGradientRect(mat, zLevel, tooltipX + tooltipTextWidth + 3, tooltipY - 3, tooltipX + tooltipTextWidth + 4, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor);
        drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY - 3 + 1, tooltipX - 3 + 1, tooltipY + tooltipHeight + 3 - 1, borderColorStart, borderColorEnd);
        drawGradientRect(mat, zLevel, tooltipX + tooltipTextWidth + 2, tooltipY - 3 + 1, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3 - 1, borderColorStart, borderColorEnd);
        drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY - 3, tooltipX + tooltipTextWidth + 3, tooltipY - 3 + 1, borderColorStart, borderColorStart);
        drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY + tooltipHeight + 2, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3, borderColorEnd, borderColorEnd);
        VertexConsumerProvider.Immediate renderType = VertexConsumerProvider.immediate(Tessellator.getInstance().getBuffer());
        mStack.translate(0.0D, 0.0D, zLevel);
        for (int lineNumber = 0; lineNumber < textLines.size(); ++lineNumber) {
            StringVisitable line = textLines.get(lineNumber);
            if (line != null) {
                font.draw(Language.getInstance().reorder(line), (float) tooltipX, (float) tooltipY, -1, true, mat, renderType, false, 0, 15728880);
            }
            if (lineNumber + 1 == titleLinesCount) {
                tooltipY += 2;
            }
            tooltipY += 10;
        }
        renderType.draw();
        mStack.pop();
        RenderSystem.enableDepthTest();
    }
}
Also used : Matrix4f(net.minecraft.util.math.Matrix4f) StringVisitable(net.minecraft.text.StringVisitable) ArrayList(java.util.ArrayList) VertexConsumerProvider(net.minecraft.client.render.VertexConsumerProvider)

Aggregations

StringVisitable (net.minecraft.text.StringVisitable)3 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 File (java.io.File)1 URI (java.net.URI)1 URL (java.net.URL)1 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 FabricLoader (net.fabricmc.loader.api.FabricLoader)1 Screen (net.minecraft.client.gui.screen.Screen)1 VertexConsumerProvider (net.minecraft.client.render.VertexConsumerProvider)1 MatrixStack (net.minecraft.client.util.math.MatrixStack)1 ItemStack (net.minecraft.item.ItemStack)1 Items (net.minecraft.item.Items)1 LiteralText (net.minecraft.text.LiteralText)1 OrderedText (net.minecraft.text.OrderedText)1 Style (net.minecraft.text.Style)1