| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.mojang.blaze3d.Blaze3D;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import java.net.URI;
|
| 8 | import java.nio.file.Path;
|
| 9 | import java.util.Comparator;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Objects;
|
| 12 | import java.util.concurrent.Executor;
|
| 13 | import java.util.concurrent.TimeUnit;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import net.minecraft.SharedConstants;
|
| 17 | import net.minecraft.client.Minecraft;
|
| 18 | import net.minecraft.client.NarratorStatus;
|
| 19 | import net.minecraft.client.gui.ComponentPath;
|
| 20 | import net.minecraft.client.gui.Font;
|
| 21 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 22 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 23 | import net.minecraft.client.gui.components.CycleButton;
|
| 24 | import net.minecraft.client.gui.components.Renderable;
|
| 25 | import net.minecraft.client.gui.components.TabOrderedElement;
|
| 26 | import net.minecraft.client.gui.components.events.AbstractContainerEventHandler;
|
| 27 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 28 | import net.minecraft.client.gui.narration.NarratableEntry;
|
| 29 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 30 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 31 | import net.minecraft.client.gui.narration.NarrationTrigger;
|
| 32 | import net.minecraft.client.gui.narration.ScreenNarrationCollector;
|
| 33 | import net.minecraft.client.gui.navigation.FocusNavigationEvent;
|
| 34 | import net.minecraft.client.gui.navigation.ScreenDirection;
|
| 35 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 36 | import net.minecraft.client.input.KeyEvent;
|
| 37 | import net.minecraft.client.player.LocalPlayer;
|
| 38 | import net.minecraft.client.renderer.RenderPipelines;
|
| 39 | import net.minecraft.commands.Commands;
|
| 40 | import net.minecraft.network.chat.ClickEvent;
|
| 41 | import net.minecraft.network.chat.Component;
|
| 42 | import net.minecraft.network.chat.ClickEvent.CopyToClipboard;
|
| 43 | import net.minecraft.network.chat.ClickEvent.Custom;
|
| 44 | import net.minecraft.network.chat.ClickEvent.OpenFile;
|
| 45 | import net.minecraft.network.chat.ClickEvent.OpenUrl;
|
| 46 | import net.minecraft.network.chat.ClickEvent.RunCommand;
|
| 47 | import net.minecraft.network.chat.ClickEvent.ShowDialog;
|
| 48 | import net.minecraft.network.chat.ClickEvent.SuggestCommand;
|
| 49 | import net.minecraft.network.protocol.common.ServerboundCustomClickActionPacket;
|
| 50 | import net.minecraft.resources.Identifier;
|
| 51 | import net.minecraft.sounds.Music;
|
| 52 | import net.minecraft.util.Util;
|
| 53 | import net.minecraft.world.item.ItemStack;
|
| 54 | import net.minecraft.world.item.Item.TooltipContext;
|
| 55 | import net.minecraft.world.item.TooltipFlag.Default;
|
| 56 | import org.jspecify.annotations.Nullable;
|
| 57 | import org.slf4j.Logger;
|
| 58 |
|
| 59 | @Environment(EnvType.CLIENT)
|
| 60 | public abstract class Screen extends AbstractContainerEventHandler implements Renderable {
|
| 61 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 62 | private static final Component SCREEN_USAGE_NARRATION = Component.translatable("narrator.screen.usage");
|
| 63 | public static final Identifier MENU_BACKGROUND = Identifier.withDefaultNamespace("textures/gui/menu_background.png");
|
| 64 | public static final Identifier HEADER_SEPARATOR = Identifier.withDefaultNamespace("textures/gui/header_separator.png");
|
| 65 | public static final Identifier FOOTER_SEPARATOR = Identifier.withDefaultNamespace("textures/gui/footer_separator.png");
|
| 66 | private static final Identifier INWORLD_MENU_BACKGROUND = Identifier.withDefaultNamespace("textures/gui/inworld_menu_background.png");
|
| 67 | public static final Identifier INWORLD_HEADER_SEPARATOR = Identifier.withDefaultNamespace("textures/gui/inworld_header_separator.png");
|
| 68 | public static final Identifier INWORLD_FOOTER_SEPARATOR = Identifier.withDefaultNamespace("textures/gui/inworld_footer_separator.png");
|
| 69 | protected static final float FADE_IN_TIME = 2000.0F;
|
| 70 | protected final Component title;
|
| 71 | private final List<GuiEventListener> children = Lists.newArrayList();
|
| 72 | private final List<NarratableEntry> narratables = Lists.newArrayList();
|
| 73 | protected final Minecraft minecraft;
|
| 74 | private boolean initialized;
|
| 75 | public int width;
|
| 76 | public int height;
|
| 77 | private final List<Renderable> renderables = Lists.newArrayList();
|
| 78 | protected final Font font;
|
| 79 | private static final long NARRATE_SUPPRESS_AFTER_INIT_TIME = TimeUnit.SECONDS.toMillis(2L);
|
| 80 | private static final long NARRATE_DELAY_NARRATOR_ENABLED = NARRATE_SUPPRESS_AFTER_INIT_TIME;
|
| 81 | private static final long NARRATE_DELAY_MOUSE_MOVE = 750L;
|
| 82 | private static final long NARRATE_DELAY_MOUSE_ACTION = 200L;
|
| 83 | private static final long NARRATE_DELAY_KEYBOARD_ACTION = 200L;
|
| 84 | private final ScreenNarrationCollector narrationState = new ScreenNarrationCollector();
|
| 85 | private long narrationSuppressTime = Long.MIN_VALUE;
|
| 86 | private long nextNarrationTime = Long.MAX_VALUE;
|
| 87 | protected @Nullable CycleButton<NarratorStatus> narratorButton;
|
| 88 | private @Nullable NarratableEntry lastNarratable;
|
| 89 | private NarrationTrigger nextNarrationTriggeredBy = NarrationTrigger.SYSTEM;
|
| 90 | protected final Executor screenExecutor;
|
| 91 |
|
| 92 | protected Screen(final Component title) {
|
| 93 | this(Minecraft.getInstance(), Minecraft.getInstance().font, title);
|
| 94 | }
|
| 95 |
|
| 96 | protected Screen(final Minecraft minecraft, final Font font, final Component title) {
|
| 97 | this.minecraft = minecraft;
|
| 98 | this.font = font;
|
| 99 | this.title = title;
|
| 100 | this.screenExecutor = runnable -> minecraft.execute(() -> {
|
| 101 | if (minecraft.gui.screen() == this) {
|
| 102 | runnable.run();
|
| 103 | }
|
| 104 | });
|
| 105 | }
|
| 106 |
|
| 107 | public Component getTitle() {
|
| 108 | return this.title;
|
| 109 | }
|
| 110 |
|
| 111 | public Component getNarrationMessage() {
|
| 112 | return this.getTitle();
|
| 113 | }
|
| 114 |
|
| 115 | public final void extractRenderStateWithTooltipAndSubtitles(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 116 | graphics.nextStratum();
|
| 117 | this.extractBackground(graphics, mouseX, mouseY, a);
|
| 118 | graphics.nextStratum();
|
| 119 | this.extractRenderState(graphics, mouseX, mouseY, a);
|
| 120 | graphics.extractDeferredElements(mouseX, mouseY, a);
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 125 | for (Renderable renderable : this.renderables) {
|
| 126 | renderable.extractRenderState(graphics, mouseX, mouseY, a);
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public boolean keyPressed(final KeyEvent event) {
|
| 132 | if (event.isEscape() && this.shouldCloseOnEsc()) {
|
| 133 | this.onClose();
|
| 134 | return true;
|
| 135 | }
|
| 136 |
|
| 137 | if (super.keyPressed(event)) {
|
| 138 | return true;
|
| 139 | }
|
| 140 |
|
| 141 | FocusNavigationEvent navigationEvent = switch (event.shortcutKey()) {
|
| 142 | case 9 -> this.createTabEvent(!event.hasShiftDown());
|
| 143 | case 1073741903 -> this.createArrowEvent(ScreenDirection.RIGHT);
|
| 144 | case 1073741904 -> this.createArrowEvent(ScreenDirection.LEFT);
|
| 145 | case 1073741905 -> this.createArrowEvent(ScreenDirection.DOWN);
|
| 146 | case 1073741906 -> this.createArrowEvent(ScreenDirection.UP);
|
| 147 | default -> null;
|
| 148 | };
|
| 149 | if (navigationEvent != null) {
|
| 150 | ComponentPath focusPath = super.nextFocusPath(navigationEvent);
|
| 151 | if (focusPath == null && navigationEvent instanceof FocusNavigationEvent.TabNavigation) {
|
| 152 | this.clearFocus();
|
| 153 | focusPath = super.nextFocusPath(navigationEvent);
|
| 154 | }
|
| 155 |
|
| 156 | if (focusPath != null) {
|
| 157 | this.changeFocus(focusPath);
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | return false;
|
| 162 | }
|
| 163 |
|
| 164 | private FocusNavigationEvent.TabNavigation createTabEvent(final boolean forward) {
|
| 165 | return new FocusNavigationEvent.TabNavigation(forward);
|
| 166 | }
|
| 167 |
|
| 168 | private FocusNavigationEvent.ArrowNavigation createArrowEvent(final ScreenDirection direction) {
|
| 169 | return new FocusNavigationEvent.ArrowNavigation(direction);
|
| 170 | }
|
| 171 |
|
| 172 | protected void setInitialFocus() {
|
| 173 | if (this.minecraft.getLastInputType().isKeyboard()) {
|
| 174 | FocusNavigationEvent.TabNavigation forwardTabEvent = new FocusNavigationEvent.TabNavigation(true);
|
| 175 | ComponentPath focusPath = super.nextFocusPath(forwardTabEvent);
|
| 176 | if (focusPath != null) {
|
| 177 | this.changeFocus(focusPath);
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | protected void setInitialFocus(final GuiEventListener target) {
|
| 183 | ComponentPath path = ComponentPath.path(this, target.nextFocusPath(new FocusNavigationEvent.InitialFocus()));
|
| 184 | if (path != null) {
|
| 185 | this.changeFocus(path);
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | public void clearFocus() {
|
| 190 | ComponentPath componentPath = this.getCurrentFocusPath();
|
| 191 | if (componentPath != null) {
|
| 192 | componentPath.applyFocus(false);
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | public boolean isInputCaptured() {
|
| 197 | ComponentPath focusPath = this.getCurrentFocusPath();
|
| 198 | return focusPath != null && focusPath.leafComponent().capturesInput();
|
| 199 | }
|
| 200 |
|
| 201 | @VisibleForTesting
|
| 202 | protected void changeFocus(final ComponentPath componentPath) {
|
| 203 | this.clearFocus();
|
| 204 | componentPath.applyFocus(true);
|
| 205 | }
|
| 206 |
|
| 207 | public boolean shouldCloseOnEsc() {
|
| 208 | return true;
|
| 209 | }
|
| 210 |
|
| 211 | public void onClose() {
|
| 212 | this.minecraft.gui.setScreen(null);
|
| 213 | }
|
| 214 |
|
| 215 | protected <T extends GuiEventListener & Renderable & NarratableEntry> T addRenderableWidget(final T widget) {
|
| 216 | this.renderables.add(widget);
|
| 217 | return this.addWidget(widget);
|
| 218 | }
|
| 219 |
|
| 220 | protected <T extends Renderable> T addRenderableOnly(final T renderable) {
|
| 221 | this.renderables.add(renderable);
|
| 222 | return renderable;
|
| 223 | }
|
| 224 |
|
| 225 | protected <T extends GuiEventListener & NarratableEntry> T addWidget(final T widget) {
|
| 226 | this.children.add(widget);
|
| 227 | this.narratables.add(widget);
|
| 228 | return widget;
|
| 229 | }
|
| 230 |
|
| 231 | protected void removeWidget(final GuiEventListener widget) {
|
| 232 | if (widget instanceof Renderable renderable) {
|
| 233 | this.renderables.remove(renderable);
|
| 234 | }
|
| 235 |
|
| 236 | if (widget instanceof NarratableEntry narratableEntry) {
|
| 237 | this.narratables.remove(narratableEntry);
|
| 238 | }
|
| 239 |
|
| 240 | if (this.getFocused() == widget) {
|
| 241 | this.clearFocus();
|
| 242 | }
|
| 243 |
|
| 244 | this.children.remove(widget);
|
| 245 | }
|
| 246 |
|
| 247 | protected void clearWidgets() {
|
| 248 | this.renderables.clear();
|
| 249 | this.children.clear();
|
| 250 | this.narratables.clear();
|
| 251 | }
|
| 252 |
|
| 253 | public static List<Component> getTooltipFromItem(final Minecraft minecraft, final ItemStack itemStack) {
|
| 254 | return itemStack.getTooltipLines(
|
| 255 | TooltipContext.of(minecraft.level), minecraft.player, minecraft.options.advancedItemTooltips ? Default.ADVANCED : Default.NORMAL
|
| 256 | );
|
| 257 | }
|
| 258 |
|
| 259 | protected void insertText(final String text, final boolean replace) {
|
| 260 | }
|
| 261 |
|
| 262 | protected static void defaultHandleGameClickEvent(final ClickEvent event, final Minecraft minecraft, final @Nullable Screen activeScreen) {
|
| 263 | LocalPlayer player = Objects.requireNonNull(minecraft.player, "Player not available");
|
| 264 | switch (event) {
|
| 265 | case RunCommand(String command):
|
| 266 | clickCommandAction(player, command, activeScreen);
|
| 267 | break;
|
| 268 | case ShowDialog dialog:
|
| 269 | player.connection.showDialog(dialog.dialog(), activeScreen);
|
| 270 | break;
|
| 271 | case Custom custom:
|
| 272 | player.connection.send(new ServerboundCustomClickActionPacket(custom.id(), custom.payload()));
|
| 273 | if (minecraft.gui.screen() != activeScreen) {
|
| 274 | minecraft.gui.setScreen(activeScreen);
|
| 275 | }
|
| 276 | break;
|
| 277 | default:
|
| 278 | defaultHandleClickEvent(event, minecraft, activeScreen);
|
| 279 | }
|
| 280 | }
|
| 281 |
|
| 282 | protected static void defaultHandleClickEvent(final ClickEvent event, final Minecraft minecraft, final @Nullable Screen activeScreen) {
|
| 283 | boolean shouldActivateScreen = switch (event) {
|
| 284 | case OpenUrl(URI uri) -> {
|
| 285 | clickUrlAction(minecraft, activeScreen, uri);
|
| 286 | yield false;
|
| 287 | }
|
| 288 | case OpenFile openFile -> {
|
| 289 | Blaze3D.openPath(openFile.file().toPath());
|
| 290 | yield true;
|
| 291 | }
|
| 292 | case SuggestCommand(String command) -> {
|
| 293 | if (activeScreen != null) {
|
| 294 | activeScreen.insertText(command, true);
|
| 295 | }
|
| 296 |
|
| 297 | yield true;
|
| 298 | }
|
| 299 | case CopyToClipboard(String value) -> {
|
| 300 | minecraft.keyboardHandler.setClipboard(value);
|
| 301 | yield true;
|
| 302 | }
|
| 303 | default -> {
|
| 304 | LOGGER.error("Don't know how to handle {}", event);
|
| 305 | yield true;
|
| 306 | }
|
| 307 | };
|
| 308 | if (shouldActivateScreen && minecraft.gui.screen() != activeScreen) {
|
| 309 | minecraft.gui.setScreen(activeScreen);
|
| 310 | }
|
| 311 | }
|
| 312 |
|
| 313 | protected static boolean clickUrlAction(final Minecraft minecraft, final @Nullable Screen screen, final URI uri) {
|
| 314 | if (!minecraft.options.chatLinks().get()) {
|
| 315 | return false;
|
| 316 | }
|
| 317 |
|
| 318 | if (minecraft.options.chatLinksPrompt().get()) {
|
| 319 | minecraft.gui.setScreen(new ConfirmLinkScreen(result -> {
|
| 320 | if (result) {
|
| 321 | Blaze3D.openUri(uri);
|
| 322 | }
|
| 323 |
|
| 324 | minecraft.gui.setScreen(screen);
|
| 325 | }, uri, false));
|
| 326 | } else {
|
| 327 | Blaze3D.openUri(uri);
|
| 328 | }
|
| 329 |
|
| 330 | return true;
|
| 331 | }
|
| 332 |
|
| 333 | protected static void clickCommandAction(final LocalPlayer player, final String command, final @Nullable Screen screenAfterCommand) {
|
| 334 | player.connection.sendUnattendedCommand(Commands.trimOptionalPrefix(command), screenAfterCommand);
|
| 335 | }
|
| 336 |
|
| 337 | public final void init(final int width, final int height) {
|
| 338 | this.width = width;
|
| 339 | this.height = height;
|
| 340 | if (!this.initialized) {
|
| 341 | this.init();
|
| 342 | this.setInitialFocus();
|
| 343 | } else {
|
| 344 | this.repositionElements();
|
| 345 | }
|
| 346 |
|
| 347 | this.initialized = true;
|
| 348 | this.triggerImmediateNarration(false);
|
| 349 | if (this.minecraft.getLastInputType().isKeyboard()) {
|
| 350 | this.setNarrationSuppressTime(Long.MAX_VALUE);
|
| 351 | } else {
|
| 352 | this.suppressNarration(NARRATE_SUPPRESS_AFTER_INIT_TIME);
|
| 353 | }
|
| 354 | }
|
| 355 |
|
| 356 | protected void rebuildWidgets() {
|
| 357 | this.clearWidgets();
|
| 358 | this.clearFocus();
|
| 359 | this.init();
|
| 360 | this.setInitialFocus();
|
| 361 | }
|
| 362 |
|
| 363 | protected void fadeWidgets(final float widgetFade) {
|
| 364 | for (GuiEventListener button : this.children()) {
|
| 365 | if (button instanceof AbstractWidget widget) {
|
| 366 | widget.setAlpha(widgetFade);
|
| 367 | }
|
| 368 | }
|
| 369 | }
|
| 370 |
|
| 371 | @Override
|
| 372 | public List<? extends GuiEventListener> children() {
|
| 373 | return this.children;
|
| 374 | }
|
| 375 |
|
| 376 | protected void init() {
|
| 377 | }
|
| 378 |
|
| 379 | public void tick() {
|
| 380 | }
|
| 381 |
|
| 382 | public void removed() {
|
| 383 | }
|
| 384 |
|
| 385 | public void added() {
|
| 386 | }
|
| 387 |
|
| 388 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 389 | if (this.isInGameUi()) {
|
| 390 | this.extractTransparentBackground(graphics);
|
| 391 | } else {
|
| 392 | if (this.minecraft.level == null) {
|
| 393 | this.extractPanorama(graphics, a);
|
| 394 | }
|
| 395 |
|
| 396 | this.extractBlurredBackground(graphics);
|
| 397 | this.extractMenuBackground(graphics);
|
| 398 | }
|
| 399 |
|
| 400 | this.minecraft.gui.hud.extractDeferredSubtitles();
|
| 401 | }
|
| 402 |
|
| 403 | protected void extractBlurredBackground(final GuiGraphicsExtractor graphics) {
|
| 404 | float blurRadius = this.minecraft.options.getMenuBackgroundBlurriness();
|
| 405 | if (blurRadius >= 1.0F) {
|
| 406 | graphics.blurBeforeThisStratum();
|
| 407 | }
|
| 408 | }
|
| 409 |
|
| 410 | protected void extractPanorama(final GuiGraphicsExtractor graphics, final float a) {
|
| 411 | this.minecraft.gameRenderer.panorama().extractRenderState(graphics, this.width, this.height);
|
| 412 | }
|
| 413 |
|
| 414 | protected void extractMenuBackground(final GuiGraphicsExtractor graphics) {
|
| 415 | this.extractMenuBackground(graphics, 0, 0, this.width, this.height);
|
| 416 | }
|
| 417 |
|
| 418 | protected void extractMenuBackground(final GuiGraphicsExtractor graphics, final int x, final int y, final int width, final int height) {
|
| 419 | extractMenuBackgroundTexture(graphics, this.minecraft.level == null ? MENU_BACKGROUND : INWORLD_MENU_BACKGROUND, x, y, 0.0F, 0.0F, width, height);
|
| 420 | }
|
| 421 |
|
| 422 | public static void extractMenuBackgroundTexture(
|
| 423 | final GuiGraphicsExtractor graphics,
|
| 424 | final Identifier menuBackground,
|
| 425 | final int x,
|
| 426 | final int y,
|
| 427 | final float u,
|
| 428 | final float v,
|
| 429 | final int width,
|
| 430 | final int height
|
| 431 | ) {
|
| 432 | int size = 32;
|
| 433 | graphics.blit(RenderPipelines.GUI_TEXTURED, menuBackground, x, y, u, v, width, height, 32, 32);
|
| 434 | }
|
| 435 |
|
| 436 | public void extractTransparentBackground(final GuiGraphicsExtractor graphics) {
|
| 437 | graphics.fillGradient(0, 0, this.width, this.height, -1072689136, -804253680);
|
| 438 | }
|
| 439 |
|
| 440 | public boolean isPauseScreen() {
|
| 441 | return true;
|
| 442 | }
|
| 443 |
|
| 444 | public boolean isInGameUi() {
|
| 445 | return false;
|
| 446 | }
|
| 447 |
|
| 448 | public boolean isAllowedInPortal() {
|
| 449 | return this.isPauseScreen();
|
| 450 | }
|
| 451 |
|
| 452 | protected void repositionElements() {
|
| 453 | this.rebuildWidgets();
|
| 454 | }
|
| 455 |
|
| 456 | public void resize(final int width, final int height) {
|
| 457 | this.width = width;
|
| 458 | this.height = height;
|
| 459 | this.repositionElements();
|
| 460 | }
|
| 461 |
|
| 462 | protected boolean isValidCharacterForName(final String currentName, final int newChar, final int cursorPos) {
|
| 463 | int colonPos = currentName.indexOf(58);
|
| 464 | int slashPos = currentName.indexOf(47);
|
| 465 | if (newChar == 58) {
|
| 466 | return (slashPos == -1 || cursorPos <= slashPos) && colonPos == -1;
|
| 467 | } else {
|
| 468 | return newChar == 47
|
| 469 | ? cursorPos > colonPos
|
| 470 | : newChar == 95 || newChar == 45 || newChar >= 97 && newChar <= 122 || newChar >= 48 && newChar <= 57 || newChar == 46;
|
| 471 | }
|
| 472 | }
|
| 473 |
|
| 474 | @Override
|
| 475 | public boolean isMouseOver(final double mouseX, final double mouseY) {
|
| 476 | return true;
|
| 477 | }
|
| 478 |
|
| 479 | public void onFilesDrop(final List<Path> files) {
|
| 480 | }
|
| 481 |
|
| 482 | private void scheduleNarration(final long delay, final boolean ignoreSuppression, final NarrationTrigger narrationTrigger) {
|
| 483 | this.nextNarrationTime = Util.getMillis() + delay;
|
| 484 | this.nextNarrationTriggeredBy = narrationTrigger;
|
| 485 | if (ignoreSuppression) {
|
| 486 | this.narrationSuppressTime = Long.MIN_VALUE;
|
| 487 | }
|
| 488 | }
|
| 489 |
|
| 490 | private void suppressNarration(final long duration) {
|
| 491 | this.setNarrationSuppressTime(Util.getMillis() + duration);
|
| 492 | }
|
| 493 |
|
| 494 | private void setNarrationSuppressTime(final long narrationSuppressTime) {
|
| 495 | this.narrationSuppressTime = narrationSuppressTime;
|
| 496 | }
|
| 497 |
|
| 498 | public void scheduleNarration() {
|
| 499 | this.scheduleNarration(0L, true, NarrationTrigger.SYSTEM);
|
| 500 | }
|
| 501 |
|
| 502 | public void afterMouseMove() {
|
| 503 | this.scheduleNarration(750L, false, NarrationTrigger.MOUSE);
|
| 504 | }
|
| 505 |
|
| 506 | public void afterMouseAction() {
|
| 507 | this.scheduleNarration(200L, true, NarrationTrigger.MOUSE);
|
| 508 | }
|
| 509 |
|
| 510 | public void afterKeyboardAction() {
|
| 511 | this.scheduleNarration(200L, true, NarrationTrigger.KEYBOARD);
|
| 512 | }
|
| 513 |
|
| 514 | private boolean shouldRunNarration() {
|
| 515 | return SharedConstants.DEBUG_UI_NARRATION || this.minecraft.getNarrator().isActive();
|
| 516 | }
|
| 517 |
|
| 518 | public void handleDelayedNarration() {
|
| 519 | if (this.shouldRunNarration()) {
|
| 520 | long currentTime = Util.getMillis();
|
| 521 | if (currentTime > this.nextNarrationTime && currentTime > this.narrationSuppressTime) {
|
| 522 | this.runNarration(true, this.nextNarrationTriggeredBy);
|
| 523 | this.nextNarrationTime = Long.MAX_VALUE;
|
| 524 | }
|
| 525 | }
|
| 526 | }
|
| 527 |
|
| 528 | public void triggerImmediateNarration(final boolean onlyChanged) {
|
| 529 | if (this.shouldRunNarration()) {
|
| 530 | this.runNarration(onlyChanged, NarrationTrigger.SYSTEM);
|
| 531 | }
|
| 532 | }
|
| 533 |
|
| 534 | private void runNarration(final boolean onlyChanged, final NarrationTrigger narrationTrigger) {
|
| 535 | this.narrationState.update(this::updateNarrationState, narrationTrigger);
|
| 536 | String narration = this.narrationState.collectNarrationText(!onlyChanged);
|
| 537 | if (!narration.isEmpty()) {
|
| 538 | this.minecraft.getNarrator().saySystemNow(narration);
|
| 539 | }
|
| 540 | }
|
| 541 |
|
| 542 | protected boolean shouldNarrateNavigation() {
|
| 543 | return true;
|
| 544 | }
|
| 545 |
|
| 546 | protected void updateNarrationState(final NarrationElementOutput output) {
|
| 547 | output.add(NarratedElementType.TITLE, this.getNarrationMessage());
|
| 548 | this.updateNarratedWidget(output);
|
| 549 | }
|
| 550 |
|
| 551 | protected void updateNarratedWidget(final NarrationElementOutput output) {
|
| 552 | List<? extends NarratableEntry> activeNarratables = this.narratables
|
| 553 | .stream()
|
| 554 | .flatMap(narratableEntry -> narratableEntry.getNarratables().stream())
|
| 555 | .filter(NarratableEntry::isActive)
|
| 556 | .sorted(Comparator.comparingInt(TabOrderedElement::getTabOrderGroup))
|
| 557 | .toList();
|
| 558 | Screen.NarratableSearchResult result = findNarratableWidget(activeNarratables, this.lastNarratable);
|
| 559 | if (result != null) {
|
| 560 | if (result.priority.isTerminal()) {
|
| 561 | this.lastNarratable = result.entry;
|
| 562 | }
|
| 563 |
|
| 564 | if (activeNarratables.size() > 1) {
|
| 565 | output.add(NarratedElementType.POSITION, Component.translatable("narrator.position.screen", result.index + 1, activeNarratables.size()));
|
| 566 | if (result.priority == NarratableEntry.NarrationPriority.FOCUSED) {
|
| 567 | output.add(NarratedElementType.USAGE, this.getUsageNarration());
|
| 568 | }
|
| 569 | }
|
| 570 |
|
| 571 | result.entry.updateNarration(output.nest());
|
| 572 | } else if (this.shouldNarrateNavigation()) {
|
| 573 | output.add(NarratedElementType.USAGE, SCREEN_USAGE_NARRATION);
|
| 574 | }
|
| 575 | }
|
| 576 |
|
| 577 | protected Component getUsageNarration() {
|
| 578 | return Component.translatable("narration.component_list.usage");
|
| 579 | }
|
| 580 |
|
| 581 | public static Screen.@Nullable NarratableSearchResult findNarratableWidget(
|
| 582 | final List<? extends NarratableEntry> narratableEntries, final @Nullable NarratableEntry lastNarratable
|
| 583 | ) {
|
| 584 | Screen.NarratableSearchResult result = null;
|
| 585 | Screen.NarratableSearchResult lowPrioNarratable = null;
|
| 586 | int i = 0;
|
| 587 |
|
| 588 | for (int narratablesSize = narratableEntries.size(); i < narratablesSize; i++) {
|
| 589 | NarratableEntry narratable = narratableEntries.get(i);
|
| 590 | NarratableEntry.NarrationPriority priority = narratable.narrationPriority();
|
| 591 | if (priority.isTerminal()) {
|
| 592 | if (narratable != lastNarratable) {
|
| 593 | return new Screen.NarratableSearchResult(narratable, i, priority);
|
| 594 | }
|
| 595 |
|
| 596 | lowPrioNarratable = new Screen.NarratableSearchResult(narratable, i, priority);
|
| 597 | } else if (priority.compareTo(result != null ? result.priority : NarratableEntry.NarrationPriority.NONE) > 0) {
|
| 598 | result = new Screen.NarratableSearchResult(narratable, i, priority);
|
| 599 | }
|
| 600 | }
|
| 601 |
|
| 602 | return result != null ? result : lowPrioNarratable;
|
| 603 | }
|
| 604 |
|
| 605 | public void updateNarratorStatus(final boolean wasDisabled, final NarrationTrigger narrationTrigger) {
|
| 606 | if (wasDisabled) {
|
| 607 | this.scheduleNarration(NARRATE_DELAY_NARRATOR_ENABLED, false, narrationTrigger);
|
| 608 | }
|
| 609 |
|
| 610 | if (this.narratorButton != null) {
|
| 611 | this.narratorButton.setValue(this.minecraft.options.narrator().get());
|
| 612 | }
|
| 613 | }
|
| 614 |
|
| 615 | public Font getFont() {
|
| 616 | return this.font;
|
| 617 | }
|
| 618 |
|
| 619 | public boolean showsActiveEffects() {
|
| 620 | return false;
|
| 621 | }
|
| 622 |
|
| 623 | public boolean canInterruptWithAnotherScreen() {
|
| 624 | return this.shouldCloseOnEsc();
|
| 625 | }
|
| 626 |
|
| 627 | @Override
|
| 628 | public ScreenRectangle getRectangle() {
|
| 629 | return new ScreenRectangle(0, 0, this.width, this.height);
|
| 630 | }
|
| 631 |
|
| 632 | public @Nullable Music getBackgroundMusic() {
|
| 633 | return null;
|
| 634 | }
|
| 635 |
|
| 636 | @Environment(EnvType.CLIENT)
|
| 637 | public record NarratableSearchResult(NarratableEntry entry, int index, NarratableEntry.NarrationPriority priority) {
|
| 638 | }
|
| 639 | }
|
| 640 |
|