| 1 | package net.minecraft.client.gui.spectator;
|
| 2 |
|
| 3 | import com.google.common.base.MoreObjects;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import java.util.List;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 9 | import net.minecraft.client.gui.spectator.categories.SpectatorPage;
|
| 10 | import net.minecraft.client.renderer.RenderPipelines;
|
| 11 | import net.minecraft.network.chat.CommonComponents;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.util.ARGB;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class SpectatorMenu {
|
| 18 | private static final Identifier CLOSE_SPRITE = Identifier.withDefaultNamespace("spectator/close");
|
| 19 | private static final Identifier SCROLL_LEFT_SPRITE = Identifier.withDefaultNamespace("spectator/scroll_left");
|
| 20 | private static final Identifier SCROLL_RIGHT_SPRITE = Identifier.withDefaultNamespace("spectator/scroll_right");
|
| 21 | private static final SpectatorMenuItem CLOSE_ITEM = new SpectatorMenu.CloseSpectatorItem();
|
| 22 | private static final SpectatorMenuItem SCROLL_LEFT = new SpectatorMenu.ScrollMenuItem(-1, true);
|
| 23 | private static final SpectatorMenuItem SCROLL_RIGHT_ENABLED = new SpectatorMenu.ScrollMenuItem(1, true);
|
| 24 | private static final SpectatorMenuItem SCROLL_RIGHT_DISABLED = new SpectatorMenu.ScrollMenuItem(1, false);
|
| 25 | private static final int MAX_PER_PAGE = 8;
|
| 26 | private static final Component CLOSE_MENU_TEXT = Component.translatable("spectatorMenu.close");
|
| 27 | private static final Component PREVIOUS_PAGE_TEXT = Component.translatable("spectatorMenu.previous_page");
|
| 28 | private static final Component NEXT_PAGE_TEXT = Component.translatable("spectatorMenu.next_page");
|
| 29 | public static final SpectatorMenuItem EMPTY_SLOT = new SpectatorMenuItem() {
|
| 30 | @Override
|
| 31 | public void selectItem(final SpectatorMenu menu) {
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public Component getName() {
|
| 36 | return CommonComponents.EMPTY;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public void extractIcon(final GuiGraphicsExtractor graphics, final float brightness, final float alpha) {
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public boolean isEnabled() {
|
| 45 | return false;
|
| 46 | }
|
| 47 | };
|
| 48 | private final SpectatorMenuListener listener;
|
| 49 | private SpectatorMenuCategory category;
|
| 50 | private int selectedSlot = -1;
|
| 51 | private int page;
|
| 52 |
|
| 53 | public SpectatorMenu(final SpectatorMenuListener listener) {
|
| 54 | this.category = new RootSpectatorMenuCategory();
|
| 55 | this.listener = listener;
|
| 56 | }
|
| 57 |
|
| 58 | public SpectatorMenuItem getItem(final int slot) {
|
| 59 | int index = slot + this.page * 6;
|
| 60 | if (this.page > 0 && slot == 0) {
|
| 61 | return SCROLL_LEFT;
|
| 62 | } else if (slot == 7) {
|
| 63 | return index < this.category.getItems().size() ? SCROLL_RIGHT_ENABLED : SCROLL_RIGHT_DISABLED;
|
| 64 | } else if (slot == 8) {
|
| 65 | return CLOSE_ITEM;
|
| 66 | } else {
|
| 67 | return index >= 0 && index < this.category.getItems().size() ? MoreObjects.firstNonNull(this.category.getItems().get(index), EMPTY_SLOT) : EMPTY_SLOT;
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | public List<SpectatorMenuItem> getItems() {
|
| 72 | List<SpectatorMenuItem> items = Lists.newArrayList();
|
| 73 |
|
| 74 | for (int i = 0; i <= 8; i++) {
|
| 75 | items.add(this.getItem(i));
|
| 76 | }
|
| 77 |
|
| 78 | return items;
|
| 79 | }
|
| 80 |
|
| 81 | public SpectatorMenuItem getSelectedItem() {
|
| 82 | return this.getItem(this.selectedSlot);
|
| 83 | }
|
| 84 |
|
| 85 | public SpectatorMenuCategory getSelectedCategory() {
|
| 86 | return this.category;
|
| 87 | }
|
| 88 |
|
| 89 | public void selectSlot(final int slot) {
|
| 90 | SpectatorMenuItem item = this.getItem(slot);
|
| 91 | if (item != EMPTY_SLOT) {
|
| 92 | if (this.selectedSlot == slot && item.isEnabled()) {
|
| 93 | item.selectItem(this);
|
| 94 | } else {
|
| 95 | this.selectedSlot = slot;
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | public void exit() {
|
| 101 | this.listener.onSpectatorMenuClosed(this);
|
| 102 | }
|
| 103 |
|
| 104 | public int getSelectedSlot() {
|
| 105 | return this.selectedSlot;
|
| 106 | }
|
| 107 |
|
| 108 | public void selectCategory(final SpectatorMenuCategory category) {
|
| 109 | this.category = category;
|
| 110 | this.selectedSlot = -1;
|
| 111 | this.page = 0;
|
| 112 | }
|
| 113 |
|
| 114 | public SpectatorPage getCurrentPage() {
|
| 115 | return new SpectatorPage(this.getItems(), this.selectedSlot);
|
| 116 | }
|
| 117 |
|
| 118 | @Environment(EnvType.CLIENT)
|
| 119 | private static class CloseSpectatorItem implements SpectatorMenuItem {
|
| 120 | @Override
|
| 121 | public void selectItem(final SpectatorMenu menu) {
|
| 122 | menu.exit();
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public Component getName() {
|
| 127 | return SpectatorMenu.CLOSE_MENU_TEXT;
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public void extractIcon(final GuiGraphicsExtractor graphics, final float brightness, final float alpha) {
|
| 132 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SpectatorMenu.CLOSE_SPRITE, 0, 0, 16, 16, ARGB.colorFromFloat(alpha, brightness, brightness, brightness));
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public boolean isEnabled() {
|
| 137 | return true;
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | @Environment(EnvType.CLIENT)
|
| 142 | private static class ScrollMenuItem implements SpectatorMenuItem {
|
| 143 | private final int direction;
|
| 144 | private final boolean enabled;
|
| 145 |
|
| 146 | public ScrollMenuItem(final int direction, final boolean enabled) {
|
| 147 | this.direction = direction;
|
| 148 | this.enabled = enabled;
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | public void selectItem(final SpectatorMenu menu) {
|
| 153 | menu.page = menu.page + this.direction;
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public Component getName() {
|
| 158 | return this.direction < 0 ? SpectatorMenu.PREVIOUS_PAGE_TEXT : SpectatorMenu.NEXT_PAGE_TEXT;
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public void extractIcon(final GuiGraphicsExtractor graphics, final float brightness, final float alpha) {
|
| 163 | int color = ARGB.colorFromFloat(alpha, brightness, brightness, brightness);
|
| 164 | if (this.direction < 0) {
|
| 165 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SpectatorMenu.SCROLL_LEFT_SPRITE, 0, 0, 16, 16, color);
|
| 166 | } else {
|
| 167 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SpectatorMenu.SCROLL_RIGHT_SPRITE, 0, 0, 16, 16, color);
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | public boolean isEnabled() {
|
| 173 | return this.enabled;
|
| 174 | }
|
| 175 | }
|
| 176 | }
|
| 177 |
|