| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import java.util.function.Consumer;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 7 | import net.minecraft.client.gui.Font;
|
| 8 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 9 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 10 | import net.minecraft.client.input.MouseButtonEvent;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.network.chat.Style;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public abstract class AbstractStringWidget extends AbstractWidget {
|
| 17 | private @Nullable Consumer<Style> componentClickHandler = null;
|
| 18 | private final Font font;
|
| 19 |
|
| 20 | public AbstractStringWidget(final int x, final int y, final int width, final int height, final Component message, final Font font) {
|
| 21 | super(x, y, width, height, message);
|
| 22 | this.font = font;
|
| 23 | }
|
| 24 |
|
| 25 | public abstract void visitLines(ActiveTextCollector output);
|
| 26 |
|
| 27 | @Override
|
| 28 | public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 29 | GuiGraphicsExtractor.HoveredTextEffects effects;
|
| 30 | if (this.isHovered()) {
|
| 31 | if (this.componentClickHandler != null) {
|
| 32 | effects = GuiGraphicsExtractor.HoveredTextEffects.TOOLTIP_AND_CURSOR;
|
| 33 | } else {
|
| 34 | effects = GuiGraphicsExtractor.HoveredTextEffects.TOOLTIP_ONLY;
|
| 35 | }
|
| 36 | } else {
|
| 37 | effects = GuiGraphicsExtractor.HoveredTextEffects.NONE;
|
| 38 | }
|
| 39 |
|
| 40 | this.visitLines(graphics.textRendererForWidget(this, effects));
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public void onClick(final MouseButtonEvent event, final boolean doubleClick) {
|
| 45 | if (this.componentClickHandler != null) {
|
| 46 | ActiveTextCollector.ClickableStyleFinder finder = new ActiveTextCollector.ClickableStyleFinder(this.getFont(), (int)event.x(), (int)event.y());
|
| 47 | this.visitLines(finder);
|
| 48 | Style clickedStyle = finder.result();
|
| 49 | if (clickedStyle != null) {
|
| 50 | this.componentClickHandler.accept(clickedStyle);
|
| 51 | return;
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | super.onClick(event, doubleClick);
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected void updateWidgetNarration(final NarrationElementOutput output) {
|
| 60 | }
|
| 61 |
|
| 62 | protected final Font getFont() {
|
| 63 | return this.font;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public void setMessage(final Component message) {
|
| 68 | super.setMessage(message);
|
| 69 | this.setWidth(this.getFont().width(message.getVisualOrderText()));
|
| 70 | }
|
| 71 |
|
| 72 | public AbstractStringWidget setComponentClickHandler(final @Nullable Consumer<Style> clickEventConsumer) {
|
| 73 | this.componentClickHandler = clickEventConsumer;
|
| 74 | return this;
|
| 75 | }
|
| 76 |
|
| 77 | protected boolean handleStyleClick(final Style style) {
|
| 78 | if (this.componentClickHandler != null && style.getClickEvent() != null) {
|
| 79 | this.componentClickHandler.accept(style);
|
| 80 | return true;
|
| 81 | } else {
|
| 82 | return false;
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 |
|