| 1 | package net.minecraft.client.gui.components.spectator;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.Minecraft;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.gui.spectator.SpectatorMenu;
|
| 8 | import net.minecraft.client.gui.spectator.SpectatorMenuItem;
|
| 9 | import net.minecraft.client.gui.spectator.SpectatorMenuListener;
|
| 10 | import net.minecraft.client.gui.spectator.categories.SpectatorPage;
|
| 11 | import net.minecraft.client.renderer.RenderPipelines;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.util.ARGB;
|
| 15 | import net.minecraft.util.Mth;
|
| 16 | import net.minecraft.util.Util;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class SpectatorGui implements SpectatorMenuListener {
|
| 21 | private static final Identifier HOTBAR_SPRITE = Identifier.withDefaultNamespace("hud/hotbar");
|
| 22 | private static final Identifier HOTBAR_SELECTION_SPRITE = Identifier.withDefaultNamespace("hud/hotbar_selection");
|
| 23 | private static final long FADE_OUT_DELAY = 5000L;
|
| 24 | private static final long FADE_OUT_TIME = 2000L;
|
| 25 | private final Minecraft minecraft;
|
| 26 | private long lastSelectionTime;
|
| 27 | private @Nullable SpectatorMenu menu;
|
| 28 |
|
| 29 | public SpectatorGui(final Minecraft minecraft) {
|
| 30 | this.minecraft = minecraft;
|
| 31 | }
|
| 32 |
|
| 33 | public void onHotbarSelected(final int slot) {
|
| 34 | this.lastSelectionTime = Util.getMillis();
|
| 35 | if (this.menu != null) {
|
| 36 | this.menu.selectSlot(slot);
|
| 37 | } else {
|
| 38 | this.menu = new SpectatorMenu(this);
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | private float getHotbarAlpha() {
|
| 43 | long delta = this.lastSelectionTime - Util.getMillis() + 5000L;
|
| 44 | return Mth.clamp((float)delta / 2000.0F, 0.0F, 1.0F);
|
| 45 | }
|
| 46 |
|
| 47 | public void extractHotbar(final GuiGraphicsExtractor graphics) {
|
| 48 | if (this.menu != null) {
|
| 49 | float alpha = this.getHotbarAlpha();
|
| 50 | if (alpha <= 0.0F) {
|
| 51 | this.menu.exit();
|
| 52 | } else {
|
| 53 | int screenCenter = graphics.guiWidth() / 2;
|
| 54 | int y = Mth.floor(graphics.guiHeight() - 22.0F * alpha);
|
| 55 | SpectatorPage page = this.menu.getCurrentPage();
|
| 56 | this.extractPage(graphics, alpha, screenCenter, y, page);
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | protected void extractPage(final GuiGraphicsExtractor graphics, final float alpha, final int screenCenter, final int y, final SpectatorPage page) {
|
| 62 | int color = ARGB.white(alpha);
|
| 63 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, HOTBAR_SPRITE, screenCenter - 91, y, 182, 22, color);
|
| 64 | if (page.getSelectedSlot() >= 0) {
|
| 65 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, HOTBAR_SELECTION_SPRITE, screenCenter - 91 - 1 + page.getSelectedSlot() * 20, y - 1, 24, 23, color);
|
| 66 | }
|
| 67 |
|
| 68 | for (int slot = 0; slot < 9; slot++) {
|
| 69 | this.extractSlot(graphics, slot, graphics.guiWidth() / 2 - 90 + slot * 20 + 2, y + 3, alpha, page.getItem(slot));
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | private void extractSlot(final GuiGraphicsExtractor graphics, final int slot, final int x, final float y, final float alpha, final SpectatorMenuItem item) {
|
| 74 | if (item != SpectatorMenu.EMPTY_SLOT) {
|
| 75 | graphics.pose().pushMatrix();
|
| 76 | graphics.pose().translate(x, y);
|
| 77 | float brightness = item.isEnabled() ? 1.0F : 0.25F;
|
| 78 | item.extractIcon(graphics, brightness, alpha);
|
| 79 | graphics.pose().popMatrix();
|
| 80 | if (alpha > 0.0F && item.isEnabled()) {
|
| 81 | Component key = this.minecraft.options.keyHotbarSlots[slot].getTranslatedKeyMessage();
|
| 82 | graphics.text(this.minecraft.font, key, x + 19 - 2 - this.minecraft.font.width(key), (int)y + 6 + 3, ARGB.white(alpha));
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | public void extractAction(final GuiGraphicsExtractor graphics) {
|
| 88 | float alpha = this.getHotbarAlpha();
|
| 89 | if (alpha > 0.0F && this.menu != null) {
|
| 90 | SpectatorMenuItem item = this.menu.getSelectedItem();
|
| 91 | Component action = item == SpectatorMenu.EMPTY_SLOT ? this.menu.getSelectedCategory().getPrompt() : item.getName();
|
| 92 | int strWidth = this.minecraft.font.width(action);
|
| 93 | int x = (graphics.guiWidth() - strWidth) / 2;
|
| 94 | int y = graphics.guiHeight() - 35;
|
| 95 | graphics.textWithBackdrop(this.minecraft.font, action, x, y, strWidth, ARGB.white(alpha));
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public void onSpectatorMenuClosed(final SpectatorMenu menu) {
|
| 101 | this.menu = null;
|
| 102 | this.lastSelectionTime = 0L;
|
| 103 | }
|
| 104 |
|
| 105 | public boolean isMenuActive() {
|
| 106 | return this.menu != null;
|
| 107 | }
|
| 108 |
|
| 109 | public void onMouseScrolled(final int wheel) {
|
| 110 | int newSlot = this.menu.getSelectedSlot() + wheel;
|
| 111 |
|
| 112 | while (newSlot >= 0 && newSlot <= 8 && (this.menu.getItem(newSlot) == SpectatorMenu.EMPTY_SLOT || !this.menu.getItem(newSlot).isEnabled())) {
|
| 113 | newSlot += wheel;
|
| 114 | }
|
| 115 |
|
| 116 | if (newSlot >= 0 && newSlot <= 8) {
|
| 117 | this.menu.selectSlot(newSlot);
|
| 118 | this.lastSelectionTime = Util.getMillis();
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | public void onHotbarActionKeyPressed() {
|
| 123 | this.lastSelectionTime = Util.getMillis();
|
| 124 | if (this.isMenuActive()) {
|
| 125 | int selectedSlot = this.menu.getSelectedSlot();
|
| 126 | if (selectedSlot != -1) {
|
| 127 | this.menu.selectSlot(selectedSlot);
|
| 128 | }
|
| 129 | } else {
|
| 130 | this.menu = new SpectatorMenu(this);
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 |
|