From a1388bf8fa33d888e71b07f26768208dca853a79 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Sat, 25 Apr 2020 23:15:06 +0300 Subject: Update to 20w17a I hate this snapshot. --- build.gradle | 2 +- gradle.properties | 8 +- .../cotton/gui/client/CottonClientScreen.java | 12 ++- .../cottonmc/cotton/gui/client/CottonHud.java | 4 +- .../cotton/gui/client/CottonInventoryScreen.java | 18 ++-- .../cottonmc/cotton/gui/client/ScreenDrawing.java | 109 +++++++++++++++++++-- .../io/github/cottonmc/cotton/gui/widget/WBar.java | 13 +-- .../github/cottonmc/cotton/gui/widget/WButton.java | 2 +- .../github/cottonmc/cotton/gui/widget/WLabel.java | 12 +-- .../cottonmc/cotton/gui/widget/WLabeledSlider.java | 2 +- .../github/cottonmc/cotton/gui/widget/WText.java | 11 +-- .../cottonmc/cotton/gui/widget/WTextField.java | 10 +- .../cottonmc/cotton/gui/widget/WToggleButton.java | 3 +- .../github/cottonmc/cotton/gui/widget/WWidget.java | 30 ++++-- 14 files changed, 170 insertions(+), 66 deletions(-) diff --git a/build.gradle b/build.gradle index dc89864..3d0e1db 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,7 @@ buildscript { System.out.println(rootProject.name); if (rootProject.name.equalsIgnoreCase("LibGUI")) { System.out.println("Added libgui to classpath"); - classpath 'fabric-loom:fabric-loom.gradle.plugin:0.2.6-SNAPSHOT' + classpath 'fabric-loom:fabric-loom.gradle.plugin:0.2.7-SNAPSHOT' } } } diff --git a/gradle.properties b/gradle.properties index eac0999..b1db372 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://fabricmc.net/use - minecraft_version=20w15a - yarn_mappings=20w15a+build.3 + minecraft_version=20w17a + yarn_mappings=20w17a+build.4 loader_version=0.8.2+build.194 # Mod Properties - mod_version = 1.8.1 + mod_version = 1.9.0 maven_group = io.github.cottonmc archives_base_name = LibGui # Dependencies - fabric_version=0.5.9+build.319-1.16 + fabric_version=0.6.2+build.327-1.16 jankson_version=2.0.1+j1.2.0 diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java index 1a8c326..05f3b68 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java @@ -5,6 +5,7 @@ import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; @@ -55,7 +56,7 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree } public void paint(int mouseX, int mouseY) { - super.renderBackground(); + super.renderBackground(ScreenDrawing.matrices); if (description!=null) { WPanel root = description.getRootPanel(); @@ -65,16 +66,17 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree } if (getTitle() != null) { - textRenderer.draw(getTitle().asFormattedString(), left, top, description.getTitleColor()); + textRenderer.method_27528(ScreenDrawing.matrices, getTitle(), left, top, description.getTitleColor()); } } @SuppressWarnings("deprecation") @Override - public void render(int mouseX, int mouseY, float partialTicks) { + public void render(MatrixStack matrices, int mouseX, int mouseY, float partialTicks) { + ScreenDrawing.matrices = matrices; paint(mouseX, mouseY); - super.render(mouseX, mouseY, partialTicks); + super.render(matrices, mouseX, mouseY, partialTicks); if (description!=null) { WPanel root = description.getRootPanel(); @@ -222,6 +224,6 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree @Override public void renderTextHover(Text text, int x, int y) { - renderTextHoverEffect(text, x, y); + renderTextHoverEffect(ScreenDrawing.matrices, text, x, y); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java index 6294ac6..0e6fa4f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java @@ -6,6 +6,7 @@ import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.minecraft.client.MinecraftClient; import net.minecraft.client.util.Window; +import net.minecraft.client.util.math.MatrixStack; import java.util.*; @@ -104,7 +105,8 @@ public enum CottonHud implements HudRenderCallback { } @Override - public void onHudRender(float tickDelta) { + public void onHudRender(MatrixStack matrices, float tickDelta) { + ScreenDrawing.matrices = matrices; Window window = MinecraftClient.getInstance().getWindow(); int hudWidth = window.getScaledWidth(); int hudHeight = window.getScaledHeight(); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java index 83c1837..5dca5f1 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java @@ -2,6 +2,7 @@ package io.github.cottonmc.cotton.gui.client; import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.client.render.DiffuseLighting; +import net.minecraft.client.util.math.MatrixStack; import org.lwjgl.glfw.GLFW; import io.github.cottonmc.cotton.gui.CottonCraftingController; @@ -182,12 +183,12 @@ public class CottonInventoryScreen extends H WWidget child = root.hit(containerX, containerY); child.onMouseMove(containerX - child.getAbsoluteX(), containerY - child.getAbsoluteY()); } - + @Override - protected void drawBackground(float partialTicks, int mouseX, int mouseY) {} //This is just an AbstractContainerScreen thing; most Screens don't work this way. + protected void drawBackground(MatrixStack matrices, float partialTicks, int mouseX, int mouseY) {} //This is just an AbstractContainerScreen thing; most Screens don't work this way. public void paint(int mouseX, int mouseY) { - super.renderBackground(); + super.renderBackground(ScreenDrawing.matrices); if (description!=null) { WPanel root = description.getRootPanel(); @@ -197,16 +198,17 @@ public class CottonInventoryScreen extends H } if (getTitle() != null) { - textRenderer.draw(getTitle().asFormattedString(), x, y, description.getTitleColor()); + textRenderer.method_27528(ScreenDrawing.matrices, getTitle(), x, y, description.getTitleColor()); } } @SuppressWarnings("deprecation") @Override - public void render(int mouseX, int mouseY, float partialTicks) { + public void render(MatrixStack matrices, int mouseX, int mouseY, float partialTicks) { + ScreenDrawing.matrices = matrices; paint(mouseX, mouseY); - super.render(mouseX, mouseY, partialTicks); + super.render(matrices, mouseX, mouseY, partialTicks); DiffuseLighting.disable(); //Needed because super.render leaves dirty state if (description!=null) { @@ -219,7 +221,7 @@ public class CottonInventoryScreen extends H } } - drawMouseoverTooltip(mouseX, mouseY); //Draws the itemstack tooltips + drawMouseoverTooltip(matrices, mouseX, mouseY); //Draws the itemstack tooltips } @Override @@ -235,6 +237,6 @@ public class CottonInventoryScreen extends H @Override public void renderTextHover(Text text, int x, int y) { - renderTextHoverEffect(text, x, y); + renderTextHoverEffect(ScreenDrawing.matrices, text, x, y); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java index 0cfac29..199909f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.client; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; import org.lwjgl.opengl.GL11; import com.mojang.blaze3d.platform.GlStateManager; @@ -17,8 +19,22 @@ import net.minecraft.util.Identifier; * {@code ScreenDrawing} contains utility methods for drawing contents on a screen. */ public class ScreenDrawing { + // Internal MatrixStack for rendering strings. + // TODO (2.0): Remove + static MatrixStack matrices; + private ScreenDrawing() {} + /** + * Gets the currently bound matrix stack. + * + * @return the matrix stack + * @since 1.9.0 + */ + public static MatrixStack getMatrices() { + return matrices; + } + /** * Draws a textured rectangle. * @@ -295,19 +311,51 @@ public class ScreenDrawing { public static void drawString(String s, Alignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { - MinecraftClient.getInstance().textRenderer.draw(s, x, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, s, x, y, color); } break; case CENTER: { int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); int l = (width/2) - (wid/2); - MinecraftClient.getInstance().textRenderer.draw(s, x+l, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, s, x+l, y, color); } break; case RIGHT: { int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); int l = width - wid; - MinecraftClient.getInstance().textRenderer.draw(s, x+l, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, s, x+l, y, color); + } + break; + } + } + + /** + * Draws a text component with a custom alignment. + * + * @param text the text + * @param align the alignment of the string + * @param x the X position + * @param y the Y position + * @param width the width of the string, used for aligning + * @param color the text color + * @since 1.9.0 + */ + public static void drawString(Text text, Alignment align, int x, int y, int width, int color) { + switch(align) { + case LEFT: { + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x, y, color); + } + break; + case CENTER: { + int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int l = (width/2) - (wid/2); + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); + } + break; + case RIGHT: { + int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int l = width - wid; + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); } break; } @@ -326,19 +374,50 @@ public class ScreenDrawing { public static void drawStringWithShadow(String s, Alignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { - MinecraftClient.getInstance().textRenderer.drawWithShadow(s, x, y, color); + MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, s, x, y, color); } break; case CENTER: { int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); int l = (width/2) - (wid/2); - MinecraftClient.getInstance().textRenderer.drawWithShadow(s, x+l, y, color); + MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, s, x+l, y, color); } break; case RIGHT: { int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); int l = width - wid; - MinecraftClient.getInstance().textRenderer.drawWithShadow(s, x+l, y, color); + MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, s, x+l, y, color); + } + break; + } + } + + /** + * Draws a shadowed text component. + * + * @param text the text component + * @param align the alignment of the string + * @param x the X position + * @param y the Y position + * @param width the width of the string, used for aligning + * @param color the text color + */ + public static void drawStringWithShadow(Text text, Alignment align, int x, int y, int width, int color) { + switch(align) { + case LEFT: { + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x, y, color); + } + break; + case CENTER: { + int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int l = (width/2) - (wid/2); + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); + } + break; + case RIGHT: { + int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int l = width - wid; + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); } break; } @@ -353,7 +432,19 @@ public class ScreenDrawing { * @param color the text color */ public static void drawString(String s, int x, int y, int color) { - MinecraftClient.getInstance().textRenderer.draw(s, x, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, s, x, y, color); + } + + /** + * Draws a left-aligned text component. + * + * @param text the text component + * @param x the X position + * @param y the Y position + * @param color the text color + */ + public static void drawString(Text text, int x, int y, int color) { + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x, y, color); } /** @@ -361,8 +452,8 @@ public class ScreenDrawing { */ @Deprecated public static void drawCenteredWithShadow(String s, int x, int y, int color) { - TextRenderer render = MinecraftClient.getInstance().getFontManager().getTextRenderer(MinecraftClient.DEFAULT_TEXT_RENDERER_ID); - render.drawWithShadow(s, (float)(x - render.getStringWidth(s) / 2), (float)y, color); + TextRenderer render = MinecraftClient.getInstance().textRenderer; + render.drawWithShadow(matrices, s, (float)(x - render.getStringWidth(s) / 2), (float)y, color); } public static int colorAtOpacity(int opaque, float opacity) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java index 519d1a3..168e540 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java @@ -7,6 +7,7 @@ import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.screen.PropertyDelegate; +import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.Identifier; @@ -160,23 +161,23 @@ public class WBar extends WWidget { } @Override - public void addInformation(List information) { + public void addTooltip(List information) { if (tooltipLabel!=null) { int value = (field>=0) ? properties.get(field) : 0; int valMax = (max>=0) ? properties.get(max) : maxValue; - String formatted = tooltipLabel; + Text formatted; try { - formatted = new TranslatableText(tooltipLabel, Integer.valueOf(value), Integer.valueOf(valMax)).asFormattedString(); + formatted = new TranslatableText(tooltipLabel, Integer.valueOf(value), Integer.valueOf(valMax)); } catch (Throwable t) { - formatted = t.getLocalizedMessage(); + formatted = new LiteralText(t.getLocalizedMessage()); } //Fallback to raw tooltipLabel information.add(formatted); } if (tooltipTextComponent!=null) { try { - information.add(tooltipTextComponent.asFormattedString()); + information.add(tooltipTextComponent); } catch (Throwable t) { - information.add(t.getLocalizedMessage()); + information.add(new LiteralText(t.getLocalizedMessage())); } } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java index fbf664d..aa025f0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java @@ -58,7 +58,7 @@ public class WButton extends WWidget { color = 0xFFFFA0; }*/ - ScreenDrawing.drawStringWithShadow(label.asFormattedString(), alignment, x, y + ((20 - 8) / 2), width, color); //LibGuiClient.config.darkMode ? darkmodeColor : color); + ScreenDrawing.drawStringWithShadow(label, alignment, x, y + ((20 - 8) / 2), width, color); //LibGuiClient.config.darkMode ? darkmodeColor : color); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index d925c17..6db09b9 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -69,8 +69,7 @@ public class WLabel extends WWidget { @Override public void paintBackground(int x, int y, int mouseX, int mouseY) { - String translated = text.asFormattedString(); - ScreenDrawing.drawString(translated, alignment, x, y, this.getWidth(), LibGuiClient.config.darkMode ? darkmodeColor : color); + ScreenDrawing.drawString(text, alignment, x, y, this.getWidth(), LibGuiClient.config.darkMode ? darkmodeColor : color); Text hoveredText = getTextAt(mouseX, mouseY); if (hoveredText != null) { @@ -96,14 +95,7 @@ public class WLabel extends WWidget { @Nullable private Text getTextAt(int x, int y) { if (isWithinBounds(x, y)) { - int i = 0; - for (Text component : text) { - TextRenderer renderer = MinecraftClient.getInstance().textRenderer; - i += renderer.getStringWidth(component.asFormattedString()); - if (i > x) { - return component; - } - } + return MinecraftClient.getInstance().textRenderer.method_27527().method_27489(text, x); } return null; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java index 4541427..5b7e812 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java @@ -129,7 +129,7 @@ public class WLabeledSlider extends WAbstractSlider { if (label != null) { int color = isMouseInsideBounds(mouseX, mouseY) ? 0xFFFFA0 : 0xE0E0E0; - ScreenDrawing.drawStringWithShadow(label.asFormattedString(), labelAlignment, 2, aHeight / 2 - 4, aWidth - 4, color); + ScreenDrawing.drawStringWithShadow(label, labelAlignment, 2, aHeight / 2 - 4, aWidth - 4, color); } RenderSystem.popMatrix(); } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java index 7982b2d..2522644 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java @@ -48,7 +48,7 @@ public class WText extends WWidget { @Environment(EnvType.CLIENT) private void wrapLines() { TextRenderer font = MinecraftClient.getInstance().textRenderer; - wrappedLines = Texts.wrapLines(text, width, font, true, true); + wrappedLines = Texts.wrapLines(text, width, font); } @Environment(EnvType.CLIENT) @@ -59,11 +59,7 @@ public class WText extends WWidget { if (lineIndex >= 0 && lineIndex < wrappedLines.size()) { Text line = wrappedLines.get(lineIndex); - int xi = 0; - for (Text part : line) { - xi += font.getStringWidth(part.asFormattedString()); - if (xi > x) return part; - } + return font.method_27527().method_27489(line, x); } return null; @@ -81,9 +77,8 @@ public class WText extends WWidget { for (int i = 0; i < wrappedLines.size(); i++) { Text line = wrappedLines.get(i); int c = LibGuiClient.config.darkMode ? darkmodeColor : color; - String str = line.asFormattedString(); - ScreenDrawing.drawString(str, alignment, x, y + i * font.fontHeight, width, c); + ScreenDrawing.drawString(line, alignment, x, y + i * font.fontHeight, width, c); } Text hoveredText = getTextAt(mouseX, mouseY); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java index 97023aa..7896f6e 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java @@ -321,7 +321,7 @@ public class WTextField extends WWidget { int textColor = this.editable ? this.enabledColor : this.uneditableColor; //TODO: Scroll offset - String trimText = font.trimToWidth(this.text, this.width-OFFSET_X_TEXT); + String trimText = font.method_27523(this.text, this.width-OFFSET_X_TEXT); boolean selection = (select!=-1); boolean focused = this.isFocused(); //this.isFocused() && this.focusedTicks / 6 % 2 == 0 && boolean_1; //Blinks the cursor @@ -347,16 +347,16 @@ public class WTextField extends WWidget { int preCursorAdvance = textX; if (!trimText.isEmpty()) { String string_2 = trimText.substring(0,adjustedCursor); - preCursorAdvance = font.drawWithShadow(string_2, textX, textY, textColor); + preCursorAdvance = font.drawWithShadow(ScreenDrawing.getMatrices(), string_2, textX, textY, textColor); } if (adjustedCursor info = new ArrayList<>(); - addInformation(info); + List info = new ArrayList<>(); + addTooltip(info); + + List stringInfo = new ArrayList<>(); + addInformation(stringInfo); + for (String line : stringInfo) { + info.add(new LiteralText(line)); + } if (info.size() == 0) return; - + Screen screen = MinecraftClient.getInstance().currentScreen; - screen.renderTooltip(info, tX+x, tY+y); + screen.renderTooltip(ScreenDrawing.getMatrices(), info, tX+x, tY+y); } /** @@ -248,9 +257,18 @@ public class WWidget { /** * Adds information to this widget's tooltip. If information remains empty after this call, no tooltip will be drawn. * @param information List containing all previous tooltip data. + * @deprecated Replaced with {@link #addTooltip(List)} */ + @Deprecated public void addInformation(List information) { } + + /** + * Adds lines to this widget's tooltip. If the lines remain empty after this call, no tooltip will be drawn. + * @param tooltip List containing all previous tooltip data. + */ + public void addTooltip(List tooltip) { + } /** * Find the most specific child node at this location. For non-panel widgets, returns this widget. -- cgit