diff options
Diffstat (limited to 'src/main/java')
7 files changed, 40 insertions, 33 deletions
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 5fdd521..fa27766 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 @@ -14,7 +14,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 b6ed959..0510428 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 @@ -22,7 +22,7 @@ import org.lwjgl.opengl.GL11; * * @param <T> the description type */ -public class CottonInventoryScreen<T extends SyncedGuiDescription> extends HandledScreen<T> implements TextHoverRendererScreen, CottonScreenImpl { +public class CottonInventoryScreen<T extends SyncedGuiDescription> extends HandledScreen<T> implements CottonScreenImpl { protected SyncedGuiDescription description; @Nullable protected WWidget lastResponder = null; @@ -296,7 +296,7 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> 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 0d6aca0..d9d5da8 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 @@ -8,19 +8,24 @@ import net.minecraft.client.render.Tessellator; 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.client.CottonScreenImpl; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import io.github.cottonmc.cotton.gui.widget.data.Texture; - -import net.minecraft.util.math.Matrix4f; - +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.Nullable; import org.lwjgl.opengl.GL11; /** * {@code ScreenDrawing} contains utility methods for drawing contents on a screen. */ public class ScreenDrawing { + private static final Logger LOGGER = LogManager.getLogger(); + private ScreenDrawing() {} /** @@ -508,6 +513,28 @@ public class ScreenDrawing { MinecraftClient.getInstance().textRenderer.draw(matrices, text, x, y, color); } + /** + * Draws the text hover effects for a text style. + * + * <p>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.error("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/client/CottonScreenImpl.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/CottonScreenImpl.java index b99911b..2ef9632 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/CottonScreenImpl.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/CottonScreenImpl.java @@ -2,6 +2,8 @@ package io.github.cottonmc.cotton.gui.impl.client; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +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; @@ -12,4 +14,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 2bf9600..1003e47 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.impl.client.LibGuiConfig; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import io.github.cottonmc.cotton.gui.widget.data.InputResult; @@ -103,12 +102,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 cf3d9ec..644279d 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) |