| 1 | package net.minecraft.client.gui;
|
| 2 |
|
| 3 | import java.util.Objects;
|
| 4 | import java.util.function.Consumer;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.gui.font.ActiveArea;
|
| 8 | import net.minecraft.client.gui.font.EmptyArea;
|
| 9 | import net.minecraft.client.gui.font.TextRenderable;
|
| 10 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 11 | import net.minecraft.client.renderer.state.gui.GuiTextRenderState;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.network.chat.Style;
|
| 14 | import net.minecraft.util.ARGB;
|
| 15 | import net.minecraft.util.FormattedCharSequence;
|
| 16 | import net.minecraft.util.Mth;
|
| 17 | import net.minecraft.util.Util;
|
| 18 | import org.joml.Matrix3x2f;
|
| 19 | import org.joml.Matrix3x2fc;
|
| 20 | import org.joml.Vector2f;
|
| 21 | import org.joml.Vector2fc;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public interface ActiveTextCollector {
|
| 26 | double PERIOD_PER_SCROLLED_PIXEL = 0.5;
|
| 27 | double MIN_SCROLL_PERIOD = 3.0;
|
| 28 |
|
| 29 | ActiveTextCollector.Parameters defaultParameters();
|
| 30 |
|
| 31 | void defaultParameters(ActiveTextCollector.Parameters newParameters);
|
| 32 |
|
| 33 | default void accept(final int x, final int y, final FormattedCharSequence text) {
|
| 34 | this.accept(TextAlignment.LEFT, x, y, this.defaultParameters(), text);
|
| 35 | }
|
| 36 |
|
| 37 | default void accept(final int x, final int y, final Component text) {
|
| 38 | this.accept(TextAlignment.LEFT, x, y, this.defaultParameters(), text.getVisualOrderText());
|
| 39 | }
|
| 40 |
|
| 41 | default void accept(final TextAlignment alignment, final int anchorX, final int y, final ActiveTextCollector.Parameters parameters, final Component text) {
|
| 42 | this.accept(alignment, anchorX, y, parameters, text.getVisualOrderText());
|
| 43 | }
|
| 44 |
|
| 45 | void accept(TextAlignment alignment, int anchorX, int y, ActiveTextCollector.Parameters parameters, FormattedCharSequence text);
|
| 46 |
|
| 47 | default void accept(final TextAlignment alignment, final int anchorX, final int y, final Component text) {
|
| 48 | this.accept(alignment, anchorX, y, text.getVisualOrderText());
|
| 49 | }
|
| 50 |
|
| 51 | default void accept(final TextAlignment alignment, final int anchorX, final int y, final FormattedCharSequence text) {
|
| 52 | this.accept(alignment, anchorX, y, this.defaultParameters(), text);
|
| 53 | }
|
| 54 |
|
| 55 | void acceptScrolling(Component message, int centerX, int left, int right, int top, int bottom, ActiveTextCollector.Parameters parameters);
|
| 56 |
|
| 57 | default void acceptScrolling(final Component message, final int centerX, final int left, final int right, final int top, final int bottom) {
|
| 58 | this.acceptScrolling(message, centerX, left, right, top, bottom, this.defaultParameters());
|
| 59 | }
|
| 60 |
|
| 61 | default void acceptScrollingWithDefaultCenter(final Component message, final int left, final int right, final int top, final int bottom) {
|
| 62 | this.acceptScrolling(message, (left + right) / 2, left, right, top, bottom);
|
| 63 | }
|
| 64 |
|
| 65 | default void defaultScrollingHelper(
|
| 66 | final Component message,
|
| 67 | final int centerX,
|
| 68 | final int left,
|
| 69 | final int right,
|
| 70 | final int top,
|
| 71 | final int bottom,
|
| 72 | final int lineWidth,
|
| 73 | final int lineHeight,
|
| 74 | final ActiveTextCollector.Parameters parameters
|
| 75 | ) {
|
| 76 | int textTop = (top + bottom - lineHeight) / 2 + 1;
|
| 77 | int availableMessageWidth = right - left;
|
| 78 | if (lineWidth > availableMessageWidth) {
|
| 79 | int maxPosition = lineWidth - availableMessageWidth;
|
| 80 | double time = Util.getMillis() / 1000.0;
|
| 81 | double period = Math.max(maxPosition * 0.5, 3.0);
|
| 82 | double alpha = Math.sin((Math.PI / 2) * Math.cos((Math.PI * 2) * time / period)) / 2.0 + 0.5;
|
| 83 | double pos = Mth.lerp(alpha, 0.0, maxPosition);
|
| 84 | ActiveTextCollector.Parameters localParameters = parameters.withScissor(left, right, top, bottom);
|
| 85 | this.accept(TextAlignment.LEFT, left - (int)pos, textTop, localParameters, message.getVisualOrderText());
|
| 86 | } else {
|
| 87 | int textX = Mth.clamp(centerX, left + lineWidth / 2, right - lineWidth / 2);
|
| 88 | this.accept(TextAlignment.CENTER, textX, textTop, message);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | static void findElementUnderCursor(final GuiTextRenderState text, final float testX, final float testY, final Consumer<Style> output) {
|
| 93 | ScreenRectangle bounds = text.bounds();
|
| 94 | if (bounds != null && bounds.containsPoint((int)testX, (int)testY)) {
|
| 95 | Vector2fc localMousePos = text.pose.invert(new Matrix3x2f()).transformPosition(new Vector2f(testX, testY));
|
| 96 | final float localMouseX = localMousePos.x();
|
| 97 | final float localMouseY = localMousePos.y();
|
| 98 | text.ensurePrepared().visit(new Font.GlyphVisitor() {
|
| 99 | @Override
|
| 100 | public void acceptGlyph(final TextRenderable.Styled glyph) {
|
| 101 | this.acceptActiveArea(glyph);
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public void acceptEmptyArea(final EmptyArea empty) {
|
| 106 | this.acceptActiveArea(empty);
|
| 107 | }
|
| 108 |
|
| 109 | private void acceptActiveArea(final ActiveArea glyph) {
|
| 110 | if (ActiveTextCollector.isPointInRectangle(localMouseX, localMouseY, glyph.activeLeft(), glyph.activeTop(), glyph.activeRight(), glyph.activeBottom())) {
|
| 111 | output.accept(glyph.style());
|
| 112 | }
|
| 113 | }
|
| 114 | });
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | static boolean isPointInRectangle(final float x, final float y, final float left, final float top, final float right, final float bottom) {
|
| 119 | return x >= left && x < right && y >= top && y < bottom;
|
| 120 | }
|
| 121 |
|
| 122 | @Environment(EnvType.CLIENT)
|
| 123 | class ClickableStyleFinder implements ActiveTextCollector {
|
| 124 | private static final ActiveTextCollector.Parameters INITIAL = new ActiveTextCollector.Parameters(new Matrix3x2f());
|
| 125 | private final Font font;
|
| 126 | private final int testX;
|
| 127 | private final int testY;
|
| 128 | private ActiveTextCollector.Parameters defaultParameters = INITIAL;
|
| 129 | private boolean includeInsertions;
|
| 130 | private @Nullable Style result;
|
| 131 | private final Consumer<Style> styleScanner = style -> {
|
| 132 | if (style.getClickEvent() != null || this.includeInsertions && style.getInsertion() != null) {
|
| 133 | this.result = style;
|
| 134 | }
|
| 135 | };
|
| 136 |
|
| 137 | public ClickableStyleFinder(final Font font, final int testX, final int testY) {
|
| 138 | this.font = font;
|
| 139 | this.testX = testX;
|
| 140 | this.testY = testY;
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public ActiveTextCollector.Parameters defaultParameters() {
|
| 145 | return this.defaultParameters;
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public void defaultParameters(final ActiveTextCollector.Parameters newParameters) {
|
| 150 | this.defaultParameters = newParameters;
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public void accept(
|
| 155 | final TextAlignment alignment, final int anchorX, final int y, final ActiveTextCollector.Parameters parameters, final FormattedCharSequence text
|
| 156 | ) {
|
| 157 | int leftX = alignment.calculateLeft(anchorX, this.font, text);
|
| 158 | GuiTextRenderState renderState = new GuiTextRenderState(
|
| 159 | this.font, text, parameters.pose(), leftX, y, ARGB.white(parameters.opacity()), 0, true, true, parameters.scissor()
|
| 160 | );
|
| 161 | ActiveTextCollector.findElementUnderCursor(renderState, this.testX, this.testY, this.styleScanner);
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public void acceptScrolling(
|
| 166 | final Component message,
|
| 167 | final int centerX,
|
| 168 | final int left,
|
| 169 | final int right,
|
| 170 | final int top,
|
| 171 | final int bottom,
|
| 172 | final ActiveTextCollector.Parameters parameters
|
| 173 | ) {
|
| 174 | int lineWidth = this.font.width(message);
|
| 175 | int lineHeight = 9;
|
| 176 | this.defaultScrollingHelper(message, centerX, left, right, top, bottom, lineWidth, lineHeight, parameters);
|
| 177 | }
|
| 178 |
|
| 179 | public ActiveTextCollector.ClickableStyleFinder includeInsertions(final boolean flag) {
|
| 180 | this.includeInsertions = flag;
|
| 181 | return this;
|
| 182 | }
|
| 183 |
|
| 184 | public @Nullable Style result() {
|
| 185 | return this.result;
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | @Environment(EnvType.CLIENT)
|
| 190 | record Parameters(Matrix3x2fc pose, float opacity, @Nullable ScreenRectangle scissor) {
|
| 191 | public Parameters(final Matrix3x2fc pose) {
|
| 192 | this(pose, 1.0F, null);
|
| 193 | }
|
| 194 |
|
| 195 | public ActiveTextCollector.Parameters withPose(final Matrix3x2fc pose) {
|
| 196 | return new ActiveTextCollector.Parameters(pose, this.opacity, this.scissor);
|
| 197 | }
|
| 198 |
|
| 199 | public ActiveTextCollector.Parameters withScale(final float scale) {
|
| 200 | return this.withPose(this.pose.scale(scale, scale, new Matrix3x2f()));
|
| 201 | }
|
| 202 |
|
| 203 | public ActiveTextCollector.Parameters withOpacity(final float opacity) {
|
| 204 | return this.opacity == opacity ? this : new ActiveTextCollector.Parameters(this.pose, opacity, this.scissor);
|
| 205 | }
|
| 206 |
|
| 207 | public ActiveTextCollector.Parameters withScissor(final ScreenRectangle scissor) {
|
| 208 | return scissor.equals(this.scissor) ? this : new ActiveTextCollector.Parameters(this.pose, this.opacity, scissor);
|
| 209 | }
|
| 210 |
|
| 211 | public ActiveTextCollector.Parameters withScissor(final int left, final int right, final int top, final int bottom) {
|
| 212 | ScreenRectangle newScissor = new ScreenRectangle(left, top, right - left, bottom - top).transformAxisAligned(this.pose);
|
| 213 | if (this.scissor != null) {
|
| 214 | newScissor = Objects.requireNonNullElse(this.scissor.intersection(newScissor), ScreenRectangle.empty());
|
| 215 | }
|
| 216 |
|
| 217 | return this.withScissor(newScissor);
|
| 218 | }
|
| 219 | }
|
| 220 | }
|
| 221 |
|