From 2eb6fa164da23c547ceccf8d2773a1e12eedb4f3 Mon Sep 17 00:00:00 2001 From: xander Date: Fri, 30 Sep 2022 17:28:31 +0100 Subject: labels now support style events --- .../java/dev/isxander/yacl/gui/AbstractWidget.java | 6 ++ .../yacl/gui/controllers/ControllerWidget.java | 6 -- .../yacl/gui/controllers/LabelController.java | 84 +++++++++++++++++++--- 3 files changed, 82 insertions(+), 14 deletions(-) (limited to 'src/main/java/dev/isxander') 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 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> 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 { @Override public AbstractWidget provideWidget(YACLScreen screen, Dimension widgetDimension) { - return new LabelControllerElement(widgetDimension); + return new LabelControllerElement(screen, widgetDimension); } public class LabelControllerElement extends AbstractWidget { - private MultilineText wrappedText; + private List wrappedText; + protected MultilineText wrappedTooltip; - public LabelControllerElement(Dimension dim) { + protected final YACLScreen screen; + + public LabelControllerElement(YACLScreen screen, Dimension dim) { super(dim); + this.screen = screen; + option().addListener((opt, pending) -> updateTooltip()); + updateTooltip(); updateText(); } @@ -56,7 +65,62 @@ public class LabelController implements Controller { 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 { } 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 -- cgit