aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFalkreon <falkreon@gmail.com>2019-09-28 08:42:58 -0500
committerGitHub <noreply@github.com>2019-09-28 08:42:58 -0500
commite511933cb169218373b70084a133b50f28175560 (patch)
tree0d57379fe4932c99e816ce9ccbc6e2b56dc4b731 /src
parente7d9bc84854a14301ce387188a5cec93721306bc (diff)
parentd06633402c20ed45c8c6c9b213d8b3ce19a10a8b (diff)
downloadLibGui-e511933cb169218373b70084a133b50f28175560.tar.gz
LibGui-e511933cb169218373b70084a133b50f28175560.tar.bz2
LibGui-e511933cb169218373b70084a133b50f28175560.zip
Merge pull request #14 from Juuxel/text-events
Add text click and hover event support for WLabel
Diffstat (limited to 'src')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java7
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java10
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/TextHoverRendererScreen.java10
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java46
4 files changed, 69 insertions, 4 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 91cf7b5..d8a986f 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
@@ -8,7 +8,7 @@ import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
-public class CottonClientScreen extends Screen {
+public class CottonClientScreen extends Screen implements TextHoverRendererScreen {
protected GuiDescription description;
protected int left = 0;
protected int top = 0;
@@ -207,4 +207,9 @@ public class CottonClientScreen extends Screen {
//public Element getFocused() {
//return this;
//}
+
+ @Override
+ public void renderTextHover(Text text, int x, int y) {
+ renderComponentHoverEffect(text, 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 c18fc88..5e0cc09 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
@@ -10,8 +10,9 @@ import net.minecraft.client.gui.screen.ingame.AbstractContainerScreen;
import net.minecraft.client.render.GuiLighting;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.LiteralText;
+import net.minecraft.text.Text;
-public class CottonInventoryScreen<T extends CottonCraftingController> extends AbstractContainerScreen<T> {
+public class CottonInventoryScreen<T extends CottonCraftingController> extends AbstractContainerScreen<T> implements TextHoverRendererScreen {
protected CottonCraftingController description;
public static final int PADDING = 8;
protected WWidget lastResponder = null;
@@ -155,7 +156,7 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends A
}
return result;
}
-
+
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
if (description.getRootPanel()==null) return super.mouseScrolled(mouseX, mouseY, amount);
@@ -218,4 +219,9 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends A
}
}
}
+
+ @Override
+ public void renderTextHover(Text text, int x, int y) {
+ renderComponentHoverEffect(text, x, y);
+ }
}
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
new file mode 100644
index 0000000..c99b325
--- /dev/null
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/TextHoverRendererScreen.java
@@ -0,0 +1,10 @@
+package io.github.cottonmc.cotton.gui.client;
+
+import net.minecraft.text.Text;
+
+/**
+ * Implemented by LibGui screens to access {@code Screen.renderComponentHoverEffect()}.
+ */
+public interface TextHoverRendererScreen {
+ void renderTextHover(Text text, 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 94fc8ff..f09c263 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
@@ -2,9 +2,18 @@ package io.github.cottonmc.cotton.gui.widget;
import io.github.cottonmc.cotton.gui.client.LibGuiClient;
import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
+import io.github.cottonmc.cotton.gui.client.TextHoverRendererScreen;
import io.github.cottonmc.cotton.gui.widget.data.Alignment;
+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.text.LiteralText;
import net.minecraft.text.Text;
+
+import javax.annotation.Nullable;
+
public class WLabel extends WWidget {
protected Text text;
protected Alignment alignment = Alignment.LEFT;
@@ -29,9 +38,44 @@ public class WLabel extends WWidget {
}
@Override
- public void paintBackground(int x, int y) {
+ 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);
+
+ Text hoveredText = getTextAt(mouseX, mouseY);
+ if (hoveredText != null) {
+ Screen screen = MinecraftClient.getInstance().currentScreen;
+ if (screen instanceof TextHoverRendererScreen) {
+ ((TextHoverRendererScreen) screen).renderTextHover(hoveredText, x + mouseX, y + mouseY);
+ }
+ }
+ }
+
+ @Override
+ public void onClick(int x, int y, int button) {
+ Text hoveredText = getTextAt(x, y);
+ if (hoveredText != null) {
+ Screen screen = MinecraftClient.getInstance().currentScreen;
+ if (screen != null) {
+ screen.handleComponentClicked(hoveredText);
+ }
+ }
+ }
+
+ @Environment(EnvType.CLIENT)
+ @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 null;
}
@Override