aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorJuuxel <6596629+Juuxel@users.noreply.github.com>2020-12-17 11:41:14 +0200
committerJuuxel <6596629+Juuxel@users.noreply.github.com>2020-12-17 11:41:14 +0200
commit68cb08a1042d9f5e0c3315fc5755a6e8db3941aa (patch)
treeea3b1192731833ee9bb47ba3de1db252ca8aad64 /src/main
parent023898b178ca5b74c505f574241d81a39463c709 (diff)
downloadLibGui-68cb08a1042d9f5e0c3315fc5755a6e8db3941aa.tar.gz
LibGui-68cb08a1042d9f5e0c3315fc5755a6e8db3941aa.tar.bz2
LibGui-68cb08a1042d9f5e0c3315fc5755a6e8db3941aa.zip
Move text hover effect drawing to ScreenDrawing
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java4
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java4
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java29
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/TextHoverRendererScreen.java11
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/impl/CottonScreenImpl.java4
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java8
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java9
7 files changed, 39 insertions, 30 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 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 <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;
protected WWidget lastResponder = null;
@@ -293,7 +293,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 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.
+ *
+ * <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.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)