From 68cb08a1042d9f5e0c3315fc5755a6e8db3941aa Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Thu, 17 Dec 2020 11:41:14 +0200 Subject: Move text hover effect drawing to ScreenDrawing --- .../cotton/gui/client/CottonClientScreen.java | 4 +-- .../cotton/gui/client/CottonInventoryScreen.java | 4 +-- .../cottonmc/cotton/gui/client/ScreenDrawing.java | 29 ++++++++++++++++++++++ .../cotton/gui/client/TextHoverRendererScreen.java | 11 -------- .../cottonmc/cotton/gui/impl/CottonScreenImpl.java | 4 +++ .../github/cottonmc/cotton/gui/widget/WLabel.java | 8 +----- .../github/cottonmc/cotton/gui/widget/WText.java | 9 +------ 7 files changed, 39 insertions(+), 30 deletions(-) delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/client/TextHoverRendererScreen.java 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 16615f5..6ebe8fc 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 @@ -15,7 +15,7 @@ import io.github.cottonmc.cotton.gui.widget.WWidget; import org.jetbrains.annotations.Nullable; import org.lwjgl.opengl.GL11; -public class CottonClientScreen extends Screen implements TextHoverRendererScreen, CottonScreenImpl { +public class CottonClientScreen extends Screen implements CottonScreenImpl { protected GuiDescription description; protected int left = 0; protected int top = 0; @@ -252,7 +252,7 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree //} @Override - public void renderTextHover(MatrixStack matrices, Style textStyle, int x, int y) { + public void renderTextHover(MatrixStack matrices, @Nullable Style textStyle, int x, int y) { renderTextHoverEffect(matrices, textStyle, x, y); } 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 f40a05b..c9ff831 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 @@ -23,7 +23,7 @@ import org.lwjgl.opengl.GL11; * * @param the description type */ -public class CottonInventoryScreen extends HandledScreen implements TextHoverRendererScreen, CottonScreenImpl { +public class CottonInventoryScreen extends HandledScreen implements CottonScreenImpl { protected SyncedGuiDescription description; protected WWidget lastResponder = null; @@ -293,7 +293,7 @@ public class CottonInventoryScreen extends Handl } @Override - public void renderTextHover(MatrixStack matrices, Style textStyle, int x, int y) { + public void renderTextHover(MatrixStack matrices, @Nullable Style textStyle, int x, int y) { renderTextHoverEffect(matrices, textStyle, 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 c91a33d..f121c47 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 @@ -9,16 +9,23 @@ import net.minecraft.client.render.VertexFormat; import net.minecraft.client.render.VertexFormats; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.OrderedText; +import net.minecraft.text.Style; import net.minecraft.util.Identifier; import net.minecraft.util.math.Matrix4f; +import io.github.cottonmc.cotton.gui.impl.CottonScreenImpl; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import io.github.cottonmc.cotton.gui.widget.data.Texture; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.Nullable; /** * {@code ScreenDrawing} contains utility methods for drawing contents on a screen. */ public class ScreenDrawing { + private static final Logger LOGGER = LogManager.getLogger(); + private ScreenDrawing() {} /** @@ -505,6 +512,28 @@ public class ScreenDrawing { MinecraftClient.getInstance().textRenderer.draw(matrices, text, x, y, color); } + /** + * Draws the text hover effects for a text style. + * + *

This method should only be called from a widget in a screen. + * For example, there will be nothing drawn in HUDs. + * + * @param matrices the rendering matrix stack + * @param textStyle the text style + * @param x the X position + * @param y the Y position + * @since 4.0.0 + */ + public static void drawTextHover(MatrixStack matrices, @Nullable Style textStyle, int x, int y) { + CottonScreenImpl screen = (CottonScreenImpl) MinecraftClient.getInstance().currentScreen; + + if (screen != null) { + screen.renderTextHover(matrices, textStyle, x, y); + } else { + LOGGER.warn("Rendering text hover effects outside of a screen!"); + } + } + public static int colorAtOpacity(int opaque, float opacity) { if (opacity<0.0f) opacity=0.0f; if (opacity>1.0f) opacity=1.0f; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/TextHoverRendererScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/TextHoverRendererScreen.java deleted file mode 100644 index d6b4ca1..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/TextHoverRendererScreen.java +++ /dev/null @@ -1,11 +0,0 @@ -package io.github.cottonmc.cotton.gui.client; - -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.Style; - -/** - * Implemented by LibGui screens to access {@code Screen.renderTextHoverEffect()}. - */ -public interface TextHoverRendererScreen { - void renderTextHover(MatrixStack matrices, Style textStyle, int x, int y); -} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/CottonScreenImpl.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/CottonScreenImpl.java index fa9f56f..69a6f77 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/impl/CottonScreenImpl.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/CottonScreenImpl.java @@ -3,6 +3,8 @@ package io.github.cottonmc.cotton.gui.impl; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Style; import io.github.cottonmc.cotton.gui.widget.WWidget; import org.jetbrains.annotations.Nullable; @@ -17,4 +19,6 @@ public interface CottonScreenImpl { WWidget getLastResponder(); void setLastResponder(@Nullable WWidget lastResponder); + + void renderTextHover(MatrixStack matrices, @Nullable Style textStyle, int x, int y); } 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 7e2a122..2943f1b 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 @@ -12,7 +12,6 @@ import net.minecraft.text.Text; import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; -import io.github.cottonmc.cotton.gui.client.TextHoverRendererScreen; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import io.github.cottonmc.cotton.gui.widget.data.InputResult; import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment; @@ -102,12 +101,7 @@ public class WLabel extends WWidget { ScreenDrawing.drawString(matrices, text.asOrderedText(), horizontalAlignment, x, y + yOffset, this.getWidth(), LibGui.isDarkMode() ? darkmodeColor : color); Style hoveredTextStyle = getTextStyleAt(mouseX, mouseY); - if (hoveredTextStyle != null) { - Screen screen = MinecraftClient.getInstance().currentScreen; - if (screen instanceof TextHoverRendererScreen) { - ((TextHoverRendererScreen) screen).renderTextHover(matrices, hoveredTextStyle, x + mouseX, y + mouseY); - } - } + ScreenDrawing.drawTextHover(matrices, hoveredTextStyle, x + mouseX, y + mouseY); } @Environment(EnvType.CLIENT) 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 c6bddb5..6f8afca 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 @@ -4,7 +4,6 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.OrderedText; import net.minecraft.text.Style; @@ -12,7 +11,6 @@ import net.minecraft.text.Text; import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; -import io.github.cottonmc.cotton.gui.client.TextHoverRendererScreen; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import io.github.cottonmc.cotton.gui.widget.data.InputResult; import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment; @@ -116,12 +114,7 @@ public class WText extends WWidget { } Style hoveredTextStyle = getTextStyleAt(mouseX, mouseY); - if (hoveredTextStyle != null) { - Screen screen = MinecraftClient.getInstance().currentScreen; - if (screen instanceof TextHoverRendererScreen) { - ((TextHoverRendererScreen) screen).renderTextHover(matrices, hoveredTextStyle, x + mouseX, y + mouseY); - } - } + ScreenDrawing.drawTextHover(matrices, hoveredTextStyle, x + mouseX, y + mouseY); } @Environment(EnvType.CLIENT) -- cgit