| 1 | package net.minecraft.client.gui.screens.telemetry;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.Comparator;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.DoubleConsumer;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.ChatFormatting;
|
| 10 | import net.minecraft.client.Minecraft;
|
| 11 | import net.minecraft.client.gui.Font;
|
| 12 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 13 | import net.minecraft.client.gui.components.AbstractScrollArea;
|
| 14 | import net.minecraft.client.gui.components.AbstractTextAreaWidget;
|
| 15 | import net.minecraft.client.gui.components.MultiLineTextWidget;
|
| 16 | import net.minecraft.client.gui.layouts.Layout;
|
| 17 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 18 | import net.minecraft.client.gui.layouts.SpacerElement;
|
| 19 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 20 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 21 | import net.minecraft.client.telemetry.TelemetryEventType;
|
| 22 | import net.minecraft.client.telemetry.TelemetryProperty;
|
| 23 | import net.minecraft.network.chat.Component;
|
| 24 | import net.minecraft.network.chat.MutableComponent;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | @Environment(EnvType.CLIENT)
|
| 28 | public class TelemetryEventWidget extends AbstractTextAreaWidget {
|
| 29 | private static final int HEADER_HORIZONTAL_PADDING = 32;
|
| 30 | private static final String TELEMETRY_REQUIRED_TRANSLATION_KEY = "telemetry.event.required";
|
| 31 | private static final String TELEMETRY_OPTIONAL_TRANSLATION_KEY = "telemetry.event.optional";
|
| 32 | private static final String TELEMETRY_OPTIONAL_DISABLED_TRANSLATION_KEY = "telemetry.event.optional.disabled";
|
| 33 | private static final Component PROPERTY_TITLE = Component.translatable("telemetry_info.property_title").withStyle(ChatFormatting.UNDERLINE);
|
| 34 | private final Font font;
|
| 35 | private TelemetryEventWidget.Content content;
|
| 36 | private @Nullable DoubleConsumer onScrolledListener;
|
| 37 |
|
| 38 | public TelemetryEventWidget(final int x, final int y, final int width, final int height, final Font font) {
|
| 39 | super(x, y, width, height, Component.empty(), AbstractScrollArea.defaultSettings(9));
|
| 40 | this.font = font;
|
| 41 | this.content = this.buildContent(Minecraft.getInstance().telemetryOptInExtra());
|
| 42 | }
|
| 43 |
|
| 44 | public void onOptInChanged(final boolean optIn) {
|
| 45 | this.content = this.buildContent(optIn);
|
| 46 | this.refreshScrollAmount();
|
| 47 | }
|
| 48 |
|
| 49 | public void updateLayout() {
|
| 50 | this.content = this.buildContent(Minecraft.getInstance().telemetryOptInExtra());
|
| 51 | this.refreshScrollAmount();
|
| 52 | }
|
| 53 |
|
| 54 | private TelemetryEventWidget.Content buildContent(final boolean hasOptedIn) {
|
| 55 | TelemetryEventWidget.ContentBuilder content = new TelemetryEventWidget.ContentBuilder(this.containerWidth());
|
| 56 | List<TelemetryEventType> eventTypes = new ArrayList<>(TelemetryEventType.values());
|
| 57 | eventTypes.sort(Comparator.comparing(TelemetryEventType::isOptIn));
|
| 58 |
|
| 59 | for (int i = 0; i < eventTypes.size(); i++) {
|
| 60 | TelemetryEventType eventType = eventTypes.get(i);
|
| 61 | boolean isDisabled = eventType.isOptIn() && !hasOptedIn;
|
| 62 | this.addEventType(content, eventType, isDisabled);
|
| 63 | if (i < eventTypes.size() - 1) {
|
| 64 | content.addSpacer(9);
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | return content.build();
|
| 69 | }
|
| 70 |
|
| 71 | public void setOnScrolledListener(final @Nullable DoubleConsumer listener) {
|
| 72 | this.onScrolledListener = listener;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public void setScrollAmount(final double scrollAmount) {
|
| 77 | super.setScrollAmount(scrollAmount);
|
| 78 | if (this.onScrolledListener != null) {
|
| 79 | this.onScrolledListener.accept(this.scrollAmount());
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | protected int getInnerHeight() {
|
| 85 | return this.content.container().getHeight();
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 90 | int top = this.getInnerTop();
|
| 91 | int left = this.getInnerLeft();
|
| 92 | graphics.pose().pushMatrix();
|
| 93 | graphics.pose().translate(left, top);
|
| 94 | this.content.container().visitWidgets(widget -> widget.extractRenderState(graphics, mouseX, mouseY, a));
|
| 95 | graphics.pose().popMatrix();
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | protected void updateWidgetNarration(final NarrationElementOutput output) {
|
| 100 | output.add(NarratedElementType.TITLE, this.content.narration());
|
| 101 | }
|
| 102 |
|
| 103 | private Component grayOutIfDisabled(final Component component, final boolean isDisabled) {
|
| 104 | return isDisabled ? component.copy().withStyle(ChatFormatting.GRAY) : component;
|
| 105 | }
|
| 106 |
|
| 107 | private void addEventType(final TelemetryEventWidget.ContentBuilder builder, final TelemetryEventType eventType, final boolean isDisabled) {
|
| 108 | String titleTranslationPattern = eventType.isOptIn()
|
| 109 | ? (isDisabled ? "telemetry.event.optional.disabled" : "telemetry.event.optional")
|
| 110 | : "telemetry.event.required";
|
| 111 | builder.addHeader(this.font, this.grayOutIfDisabled(Component.translatable(titleTranslationPattern, eventType.title()), isDisabled));
|
| 112 | builder.addHeader(this.font, eventType.description().withStyle(ChatFormatting.GRAY));
|
| 113 | builder.addSpacer(9 / 2);
|
| 114 | builder.addLine(this.font, this.grayOutIfDisabled(PROPERTY_TITLE, isDisabled), 2);
|
| 115 | this.addEventTypeProperties(eventType, builder, isDisabled);
|
| 116 | }
|
| 117 |
|
| 118 | private void addEventTypeProperties(final TelemetryEventType eventType, final TelemetryEventWidget.ContentBuilder content, final boolean isDisabled) {
|
| 119 | for (TelemetryProperty<?> property : eventType.properties()) {
|
| 120 | content.addLine(this.font, this.grayOutIfDisabled(property.title(), isDisabled));
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | private int containerWidth() {
|
| 125 | return this.width - this.totalInnerPadding();
|
| 126 | }
|
| 127 |
|
| 128 | @Environment(EnvType.CLIENT)
|
| 129 | private record Content(Layout container, Component narration) {
|
| 130 | }
|
| 131 |
|
| 132 | @Environment(EnvType.CLIENT)
|
| 133 | private static class ContentBuilder {
|
| 134 | private final int width;
|
| 135 | private final LinearLayout layout;
|
| 136 | private final MutableComponent narration = Component.empty();
|
| 137 |
|
| 138 | public ContentBuilder(final int width) {
|
| 139 | this.width = width;
|
| 140 | this.layout = LinearLayout.vertical();
|
| 141 | this.layout.defaultCellSetting().alignHorizontallyLeft();
|
| 142 | this.layout.addChild(SpacerElement.width(width));
|
| 143 | }
|
| 144 |
|
| 145 | public void addLine(final Font font, final Component line) {
|
| 146 | this.addLine(font, line, 0);
|
| 147 | }
|
| 148 |
|
| 149 | public void addLine(final Font font, final Component line, final int paddingBottom) {
|
| 150 | this.layout.addChild(new MultiLineTextWidget(line, font).setMaxWidth(this.width), s -> s.paddingBottom(paddingBottom));
|
| 151 | this.narration.append(line).append("\n");
|
| 152 | }
|
| 153 |
|
| 154 | public void addHeader(final Font font, final Component line) {
|
| 155 | this.layout
|
| 156 | .addChild(new MultiLineTextWidget(line, font).setMaxWidth(this.width - 64).setCentered(true), s -> s.alignHorizontallyCenter().paddingHorizontal(32));
|
| 157 | this.narration.append(line).append("\n");
|
| 158 | }
|
| 159 |
|
| 160 | public void addSpacer(final int height) {
|
| 161 | this.layout.addChild(SpacerElement.height(height));
|
| 162 | }
|
| 163 |
|
| 164 | public TelemetryEventWidget.Content build() {
|
| 165 | this.layout.arrangeElements();
|
| 166 | return new TelemetryEventWidget.Content(this.layout, this.narration);
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 |
|