aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorxander <xander@isxander.dev>2022-09-30 17:28:31 +0100
committerxander <xander@isxander.dev>2022-09-30 17:28:31 +0100
commit2eb6fa164da23c547ceccf8d2773a1e12eedb4f3 (patch)
tree2be18d83076c408088773eee086e3d461b3dffb8 /src
parent575faeaf25256e02cbca39501c3ec97655959cea (diff)
downloadYetAnotherConfigLib-2eb6fa164da23c547ceccf8d2773a1e12eedb4f3.tar.gz
YetAnotherConfigLib-2eb6fa164da23c547ceccf8d2773a1e12eedb4f3.tar.bz2
YetAnotherConfigLib-2eb6fa164da23c547ceccf8d2773a1e12eedb4f3.zip
labels now support style events
Diffstat (limited to 'src')
-rw-r--r--src/main/java/dev/isxander/yacl/gui/AbstractWidget.java6
-rw-r--r--src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java6
-rw-r--r--src/main/java/dev/isxander/yacl/gui/controllers/LabelController.java84
-rw-r--r--src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java10
4 files changed, 91 insertions, 15 deletions
diff --git a/src/main/java/dev/isxander/yacl/gui/AbstractWidget.java b/src/main/java/dev/isxander/yacl/gui/AbstractWidget.java
index f0f43d3..d4cae93 100644
--- a/src/main/java/dev/isxander/yacl/gui/AbstractWidget.java
+++ b/src/main/java/dev/isxander/yacl/gui/AbstractWidget.java
@@ -32,6 +32,12 @@ public abstract class AbstractWidget implements Element, Drawable, Selectable {
}
+ @Override
+ public boolean isMouseOver(double mouseX, double mouseY) {
+ if (dim == null) return false;
+ return this.dim.isPointInside((int) mouseX, (int) mouseY);
+ }
+
public void setDimension(Dimension<Integer> dim) {
this.dim = dim;
}
diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java b/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java
index 34efe4f..520efa7 100644
--- a/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java
+++ b/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java
@@ -80,12 +80,6 @@ public abstract class ControllerWidget<T extends Controller<?>> extends Abstract
matrices.pop();
}
- @Override
- public boolean isMouseOver(double mouseX, double mouseY) {
- if (dim == null) return false;
- return this.dim.isPointInside((int) mouseX, (int) mouseY);
- }
-
private void updateTooltip() {
this.wrappedTooltip = MultilineText.create(textRenderer, control.option().tooltip(), screen.width / 3 * 2 - 10);
}
diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/LabelController.java b/src/main/java/dev/isxander/yacl/gui/controllers/LabelController.java
index 4e72d97..b0cafcf 100644
--- a/src/main/java/dev/isxander/yacl/gui/controllers/LabelController.java
+++ b/src/main/java/dev/isxander/yacl/gui/controllers/LabelController.java
@@ -6,10 +6,13 @@ import dev.isxander.yacl.api.utils.Dimension;
import dev.isxander.yacl.gui.AbstractWidget;
import dev.isxander.yacl.gui.YACLScreen;
import net.minecraft.client.font.MultilineText;
+import net.minecraft.client.gui.screen.ConfirmLinkScreen;
import net.minecraft.client.util.math.MatrixStack;
-import net.minecraft.text.Text;
-import org.jetbrains.annotations.ApiStatus;
+import net.minecraft.item.ItemStack;
+import net.minecraft.text.*;
+import net.minecraft.util.Util;
+import java.io.File;
import java.util.List;
/**
@@ -41,14 +44,20 @@ public class LabelController implements Controller<Text> {
@Override
public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) {
- return new LabelControllerElement(widgetDimension);
+ return new LabelControllerElement(screen, widgetDimension);
}
public class LabelControllerElement extends AbstractWidget {
- private MultilineText wrappedText;
+ private List<OrderedText> wrappedText;
+ protected MultilineText wrappedTooltip;
- public LabelControllerElement(Dimension<Integer> dim) {
+ protected final YACLScreen screen;
+
+ public LabelControllerElement(YACLScreen screen, Dimension<Integer> dim) {
super(dim);
+ this.screen = screen;
+ option().addListener((opt, pending) -> updateTooltip());
+ updateTooltip();
updateText();
}
@@ -56,7 +65,62 @@ public class LabelController implements Controller<Text> {
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
updateText();
- wrappedText.drawWithShadow(matrices, dim.x(), dim.y() + getYPadding(), textRenderer.fontHeight, option().available() ? -1 : 0xFFA0A0A0);
+ float y = dim.y();
+ for (OrderedText text : wrappedText) {
+ textRenderer.drawWithShadow(matrices, text, dim.x(), y + getYPadding(), option().available() ? -1 : 0xFFA0A0A0);
+ y += textRenderer.fontHeight;
+ }
+ }
+
+ @Override
+ public void postRender(MatrixStack matrices, int mouseX, int mouseY, float delta) {
+ if (isMouseOver(mouseX, mouseY)) {
+ YACLScreen.renderMultilineTooltip(matrices, textRenderer, wrappedTooltip, dim.centerX(), dim.y() - 5, dim.yLimit() + 5, screen.width, screen.height);
+
+ Style style = getStyle(mouseX, mouseY);
+ if (style != null && style.getHoverEvent() != null) {
+ HoverEvent hoverEvent = style.getHoverEvent();
+ HoverEvent.ItemStackContent itemStackContent = hoverEvent.getValue(HoverEvent.Action.SHOW_ITEM);
+ if (itemStackContent != null) {
+ ItemStack stack = itemStackContent.asStack();
+ screen.renderTooltip(matrices, screen.getTooltipFromItem(stack), stack.getTooltipData(), mouseX, mouseY);
+ } else {
+ HoverEvent.EntityContent entityContent = hoverEvent.getValue(HoverEvent.Action.SHOW_ENTITY);
+ if (entityContent != null) {
+ if (this.client.options.advancedItemTooltips) {
+ screen.renderTooltip(matrices, entityContent.asTooltip(), mouseX, mouseY);
+ }
+ } else {
+ Text text = hoverEvent.getValue(HoverEvent.Action.SHOW_TEXT);
+ if (text != null) {
+ MultilineText multilineText = MultilineText.create(textRenderer, text, dim.width());
+ YACLScreen.renderMultilineTooltip(matrices, textRenderer, multilineText, dim.centerX(), dim.y(), dim.yLimit(), screen.width, screen.height);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public boolean mouseClicked(double mouseX, double mouseY, int button) {
+ if (!isMouseOver(mouseX, mouseY))
+ return false;
+
+ Style style = getStyle((int) mouseX, (int) mouseY);
+ return screen.handleTextClick(style);
+ }
+
+ protected Style getStyle(int mouseX, int mouseY) {
+ int x = mouseX - dim.x();
+ int y = mouseY - dim.y() - getYPadding();
+ int line = y / textRenderer.fontHeight;
+
+ if (x < 0 || x > dim.xLimit()) return null;
+ if (y < 0 || y > dim.yLimit()) return null;
+ if (line < 0 || line >= wrappedText.size()) return null;
+
+ return textRenderer.getTextHandler().getStyleAt(wrappedText.get(line), x);
}
private int getYPadding() {
@@ -64,8 +128,12 @@ public class LabelController implements Controller<Text> {
}
private void updateText() {
- wrappedText = MultilineText.create(textRenderer, formatValue(), dim.width());
- dim.setHeight(wrappedText.count() * textRenderer.fontHeight + getYPadding() * 2);
+ wrappedText = textRenderer.wrapLines(formatValue(), dim.width());
+ dim.setHeight(wrappedText.size() * textRenderer.fontHeight + getYPadding() * 2);
+ }
+
+ private void updateTooltip() {
+ this.wrappedTooltip = MultilineText.create(textRenderer, option().tooltip(), screen.width / 3 * 2 - 10);
}
@Override
diff --git a/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java b/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java
index aca966b..80db142 100644
--- a/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java
+++ b/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java
@@ -15,6 +15,8 @@ import net.minecraft.client.gui.screen.MessageScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.option.GraphicsMode;
import net.minecraft.client.toast.SystemToast;
+import net.minecraft.text.ClickEvent;
+import net.minecraft.text.HoverEvent;
import net.minecraft.text.Text;
import java.awt.*;
@@ -176,7 +178,13 @@ public class ModMenuIntegration implements ModMenuApi {
.controller(ActionController::new)
.build())
.option(Option.createBuilder(Text.class)
- .binding(Binding.immutable(Text.of("Labels that are very large get wrapped around onto a new line! I hope this is a good demonstration!")))
+ .binding(Binding.immutable(Text.empty()
+ .append(Text.literal("a").styled(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.of("a")))))
+ .append(Text.literal("b").styled(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.of("b")))))
+ .append(Text.literal("c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c").styled(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.of("c")))))
+ .append(Text.literal("e").styled(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.of("e")))))
+ .styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://isxander.dev")))
+ ))
.controller(LabelController::new)
.build())
.build())