| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.text2speech.Narrator;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.Minecraft;
|
| 7 | import net.minecraft.client.NarratorStatus;
|
| 8 | import net.minecraft.client.Options;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.components.Button;
|
| 11 | import net.minecraft.client.gui.components.CommonButtons;
|
| 12 | import net.minecraft.client.gui.components.CycleButton;
|
| 13 | import net.minecraft.client.gui.components.FocusableTextWidget;
|
| 14 | import net.minecraft.client.gui.components.LogoRenderer;
|
| 15 | import net.minecraft.client.gui.components.SpriteIconButton;
|
| 16 | import net.minecraft.client.gui.layouts.GridLayout;
|
| 17 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 18 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 19 | import net.minecraft.client.gui.screens.options.AccessibilityOptionsScreen;
|
| 20 | import net.minecraft.client.gui.screens.options.LanguageSelectScreen;
|
| 21 | import net.minecraft.client.gui.screens.options.SoundOptionsScreen;
|
| 22 | import net.minecraft.network.chat.CommonComponents;
|
| 23 | import net.minecraft.network.chat.Component;
|
| 24 | import net.minecraft.resources.Identifier;
|
| 25 | import net.minecraft.sounds.SoundSource;
|
| 26 | import net.minecraft.util.Mth;
|
| 27 | import net.minecraft.util.Util;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class AccessibilityOnboardingScreen extends Screen {
|
| 32 | private static final Component TITLE = Component.translatable("accessibility.onboarding.screen.title");
|
| 33 | private static final Component ONBOARDING_NARRATOR_MESSAGE = Component.translatable("accessibility.onboarding.screen.narrator");
|
| 34 | private static final int PADDING = 4;
|
| 35 | private static final int TITLE_PADDING = 16;
|
| 36 | private static final float FADE_OUT_TIME = 1000.0F;
|
| 37 | private static final int TEXT_WIDGET_WIDTH = 374;
|
| 38 | private final LogoRenderer logoRenderer;
|
| 39 | private final Options options;
|
| 40 | private final boolean narratorAvailable;
|
| 41 | private boolean hasNarrated;
|
| 42 | private float timer;
|
| 43 | private final Runnable onClose;
|
| 44 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this, this.initTitleYPos(), 33);
|
| 45 | private @Nullable FocusableTextWidget focusableTextWidget;
|
| 46 | private float fadeInStart;
|
| 47 | private boolean fadingIn = true;
|
| 48 | private float fadeOutStart;
|
| 49 |
|
| 50 | public AccessibilityOnboardingScreen(final Options options, final Runnable onClose) {
|
| 51 | super(TITLE);
|
| 52 | this.options = options;
|
| 53 | this.onClose = onClose;
|
| 54 | this.logoRenderer = new LogoRenderer(true);
|
| 55 | this.narratorAvailable = Minecraft.getInstance().getNarrator().isActive();
|
| 56 | this.minecraft.gameRenderer.panorama().holdSpin();
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public void init() {
|
| 61 | LinearLayout content = this.layout.addToContents(LinearLayout.vertical());
|
| 62 | content.defaultCellSetting().alignHorizontallyCenter().padding(4);
|
| 63 | this.focusableTextWidget = content.addChild(FocusableTextWidget.builder(this.title, this.font).maxWidth(374).build(), w -> w.padding(8));
|
| 64 | GridLayout grid = content.addChild(new GridLayout());
|
| 65 | grid.defaultCellSetting().padding(4);
|
| 66 | GridLayout.RowHelper rowHelper = grid.createRowHelper(2);
|
| 67 | if (this.options.narrator().createButton(this.options) instanceof CycleButton<?> cycleButton) {
|
| 68 | this.narratorButton = (CycleButton<NarratorStatus>)cycleButton;
|
| 69 | this.narratorButton.active = this.narratorAvailable;
|
| 70 | rowHelper.addChild(this.narratorButton);
|
| 71 | }
|
| 72 |
|
| 73 | rowHelper.addChild(
|
| 74 | SpriteIconButton.builder(Component.translatable("options.sounds"), button -> this.closeAndSetScreen(new SoundOptionsScreen(this, this.options)), false)
|
| 75 | .width(150)
|
| 76 | .sprite(Identifier.withDefaultNamespace("icon/music_notes"), 16, 16)
|
| 77 | .build()
|
| 78 | );
|
| 79 | rowHelper.addChild(CommonButtons.accessibility(150, button -> this.closeAndSetScreen(new AccessibilityOptionsScreen(this, this.minecraft.options)), false));
|
| 80 | rowHelper.addChild(
|
| 81 | CommonButtons.language(
|
| 82 | 150, button -> this.closeAndSetScreen(new LanguageSelectScreen(this, this.minecraft.options, this.minecraft.getLanguageManager())), false
|
| 83 | )
|
| 84 | );
|
| 85 | this.layout.addToFooter(Button.builder(CommonComponents.GUI_CONTINUE, button -> this.onClose()).build());
|
| 86 | this.layout.visitWidgets(this::addRenderableWidget);
|
| 87 | this.repositionElements();
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | protected void repositionElements() {
|
| 92 | if (this.focusableTextWidget != null) {
|
| 93 | this.focusableTextWidget.updateHeight();
|
| 94 | }
|
| 95 |
|
| 96 | this.layout.arrangeElements();
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | protected void setInitialFocus() {
|
| 101 | if (this.narratorAvailable && this.narratorButton != null) {
|
| 102 | this.setInitialFocus(this.narratorButton);
|
| 103 | } else {
|
| 104 | super.setInitialFocus();
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private int initTitleYPos() {
|
| 109 | return 90;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public void onClose() {
|
| 114 | if (this.fadeOutStart == 0.0F) {
|
| 115 | this.fadeOutStart = (float)Util.getMillis();
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | private void closeAndSetScreen(final Screen screen) {
|
| 120 | this.close(false, () -> this.minecraft.gui.setScreen(screen));
|
| 121 | }
|
| 122 |
|
| 123 | private void close(final boolean onboardingFinished, final Runnable runnable) {
|
| 124 | if (onboardingFinished) {
|
| 125 | this.options.onboardingAccessibilityFinished();
|
| 126 | }
|
| 127 |
|
| 128 | Narrator.getNarrator().clear();
|
| 129 | runnable.run();
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 134 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 135 | this.handleInitialNarrationDelay();
|
| 136 | if (this.fadeInStart == 0.0F && this.fadingIn) {
|
| 137 | this.fadeInStart = (float)Util.getMillis();
|
| 138 | }
|
| 139 |
|
| 140 | if (this.fadeInStart > 0.0F) {
|
| 141 | float fade = ((float)Util.getMillis() - this.fadeInStart) / 2000.0F;
|
| 142 | float widgetAlpha = 1.0F;
|
| 143 | if (fade >= 1.0F) {
|
| 144 | this.fadingIn = false;
|
| 145 | this.fadeInStart = 0.0F;
|
| 146 | } else {
|
| 147 | fade = Mth.clamp(fade, 0.0F, 1.0F);
|
| 148 | widgetAlpha = Mth.clampedMap(fade, 0.5F, 1.0F, 0.0F, 1.0F);
|
| 149 | }
|
| 150 |
|
| 151 | this.fadeWidgets(widgetAlpha);
|
| 152 | }
|
| 153 |
|
| 154 | if (this.fadeOutStart > 0.0F) {
|
| 155 | float fade = 1.0F - ((float)Util.getMillis() - this.fadeOutStart) / 1000.0F;
|
| 156 | float widgetAlpha = 0.0F;
|
| 157 | if (fade <= 0.0F) {
|
| 158 | this.fadeOutStart = 0.0F;
|
| 159 | this.close(true, this.onClose);
|
| 160 | } else {
|
| 161 | fade = Mth.clamp(fade, 0.0F, 1.0F);
|
| 162 | widgetAlpha = Mth.clampedMap(fade, 0.5F, 1.0F, 0.0F, 1.0F);
|
| 163 | }
|
| 164 |
|
| 165 | this.fadeWidgets(widgetAlpha);
|
| 166 | }
|
| 167 |
|
| 168 | this.logoRenderer.extractRenderState(graphics, this.width, 1.0F);
|
| 169 | }
|
| 170 |
|
| 171 | private void handleInitialNarrationDelay() {
|
| 172 | if (!this.hasNarrated && this.narratorAvailable) {
|
| 173 | if (this.timer < 40.0F) {
|
| 174 | this.timer++;
|
| 175 | } else if (this.minecraft.isWindowActive()) {
|
| 176 | Narrator.getNarrator().say(ONBOARDING_NARRATOR_MESSAGE.getString(), true, this.minecraft.options.getFinalSoundSourceVolume(SoundSource.VOICE));
|
| 177 | this.hasNarrated = true;
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 | }
|
| 182 |
|