aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuuxel <kasperi.kauppi@gmail.com>2019-09-26 08:31:33 +0300
committerJuuxel <kasperi.kauppi@gmail.com>2019-09-26 08:31:33 +0300
commitd06633402c20ed45c8c6c9b213d8b3ce19a10a8b (patch)
tree6363ae56176c613c9fdbf3741108aa113756455e
parente5e01ab46867cb839e17fc7a10563c81e9952320 (diff)
downloadLibGui-d06633402c20ed45c8c6c9b213d8b3ce19a10a8b.tar.gz
LibGui-d06633402c20ed45c8c6c9b213d8b3ce19a10a8b.tar.bz2
LibGui-d06633402c20ed45c8c6c9b213d8b3ce19a10a8b.zip
Add text click and hover event support for WLabel
-rw-r--r--build.gradle3
-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
5 files changed, 71 insertions, 5 deletions
diff --git a/build.gradle b/build.gradle
index 1532dec..488f62d 100644
--- a/build.gradle
+++ b/build.gradle
@@ -60,7 +60,8 @@ dependencies {
compileOnly ("com.google.code.findbugs:jsr305:3.0.2") { transitive = false }
- modCompileOnly "io.github.prospector:modmenu:1.7.10-unstable.19w35a+build.2"
+ modCompileOnly "io.github.prospector:modmenu:1.7.13-unstable.19w38b+build.5"
+ modRuntime "io.github.prospector:modmenu:1.7.13-unstable.19w38b+build.5" // for testing
}
processResources {
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