| 1 | package net.minecraft.client.gui.components.tabs;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.blaze3d.platform.InputConstants;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.Comparator;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.OptionalInt;
|
| 10 | import java.util.concurrent.atomic.AtomicBoolean;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.client.gui.ComponentPath;
|
| 14 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 15 | import net.minecraft.client.gui.components.AbstractContainerWidget;
|
| 16 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 17 | import net.minecraft.client.gui.components.TabButton;
|
| 18 | import net.minecraft.client.gui.components.Tooltip;
|
| 19 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 20 | import net.minecraft.client.gui.layouts.FrameLayout;
|
| 21 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 22 | import net.minecraft.client.gui.narration.NarratableEntry;
|
| 23 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 24 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 25 | import net.minecraft.client.gui.navigation.FocusNavigationEvent;
|
| 26 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 27 | import net.minecraft.client.input.KeyEvent;
|
| 28 | import net.minecraft.network.chat.CommonComponents;
|
| 29 | import net.minecraft.network.chat.Component;
|
| 30 | import net.minecraft.util.Mth;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | @Environment(EnvType.CLIENT)
|
| 34 | public class TabNavigationBar extends AbstractContainerWidget {
|
| 35 | private static final int NO_TAB = -1;
|
| 36 | private static final Component USAGE_NARRATION = Component.translatable("narration.tab_navigation.usage");
|
| 37 | protected final FrameLayout layout;
|
| 38 | private final TabManager tabManager;
|
| 39 | protected final ImmutableList<Tab> tabs;
|
| 40 | protected final ImmutableList<TabButton> tabButtons;
|
| 41 |
|
| 42 | protected TabNavigationBar(
|
| 43 | final int x,
|
| 44 | final int y,
|
| 45 | final int width,
|
| 46 | final int height,
|
| 47 | final TabManager tabManager,
|
| 48 | final ImmutableList<TabButton> tabButtons,
|
| 49 | final ImmutableList<Tab> tabs
|
| 50 | ) {
|
| 51 | super(x, y, width, height, CommonComponents.EMPTY);
|
| 52 | this.tabManager = tabManager;
|
| 53 | this.tabButtons = tabButtons;
|
| 54 | this.tabs = ImmutableList.copyOf(tabs);
|
| 55 | this.layout = new FrameLayout();
|
| 56 | this.layout.setPosition(x, y);
|
| 57 | LinearLayout linearLayout = this.layout.addChild(LinearLayout.horizontal());
|
| 58 |
|
| 59 | for (TabButton tabButton : tabButtons) {
|
| 60 | linearLayout.addChild(tabButton);
|
| 61 | }
|
| 62 |
|
| 63 | this.layout.arrangeElements();
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected int contentHeight() {
|
| 68 | return this.height;
|
| 69 | }
|
| 70 |
|
| 71 | public static TabNavigationBar.Builder builder(final TabManager tabManager, final int x, final int y, final int width, final int height) {
|
| 72 | return new TabNavigationBar.Builder(tabManager, x, y, width, height);
|
| 73 | }
|
| 74 |
|
| 75 | public void arrangeElements(final int width) {
|
| 76 | this.layout.setPosition(this.getX(), this.getY());
|
| 77 | this.layout.arrangeElements();
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public boolean isMouseOver(final double mouseX, final double mouseY) {
|
| 82 | AtomicBoolean mouseOver = new AtomicBoolean();
|
| 83 | this.layout.visitChildren(child -> {
|
| 84 | if (child.getRectangle().containsPoint((int)mouseX, (int)mouseY)) {
|
| 85 | mouseOver.set(true);
|
| 86 | }
|
| 87 | });
|
| 88 | return mouseOver.get();
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public void setFocused(final boolean focused) {
|
| 93 | super.setFocused(focused);
|
| 94 | if (this.getFocused() != null) {
|
| 95 | this.setFocused(null);
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public void setFocused(final @Nullable GuiEventListener focused) {
|
| 101 | super.setFocused(focused);
|
| 102 | if (focused instanceof TabButton button && button.isActive()) {
|
| 103 | this.tabManager.setCurrentTab(button.tab(), true);
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public @Nullable ComponentPath nextFocusPath(final FocusNavigationEvent navigationEvent) {
|
| 109 | if (!this.isFocused()) {
|
| 110 | TabButton button = this.currentTabButton();
|
| 111 | if (button != null) {
|
| 112 | return ComponentPath.path(this, ComponentPath.leaf(button));
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | return navigationEvent instanceof FocusNavigationEvent.TabNavigation ? null : super.nextFocusPath(navigationEvent);
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | public List<? extends GuiEventListener> children() {
|
| 121 | return this.tabButtons;
|
| 122 | }
|
| 123 |
|
| 124 | public List<Tab> getTabs() {
|
| 125 | return this.tabs;
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public NarratableEntry.NarrationPriority narrationPriority() {
|
| 130 | return this.tabButtons.stream().map(AbstractWidget::narrationPriority).max(Comparator.naturalOrder()).orElse(NarratableEntry.NarrationPriority.NONE);
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | protected void updateWidgetNarration(final NarrationElementOutput output) {
|
| 135 | Optional<TabButton> selected = this.tabButtons.stream().filter(AbstractWidget::isHovered).findFirst().or(() -> Optional.ofNullable(this.currentTabButton()));
|
| 136 | selected.ifPresent(button -> {
|
| 137 | this.narrateListElementPosition(output.nest(), button);
|
| 138 | button.updateNarration(output);
|
| 139 | });
|
| 140 | if (this.isFocused()) {
|
| 141 | output.add(NarratedElementType.USAGE, USAGE_NARRATION);
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | protected void narrateListElementPosition(final NarrationElementOutput output, final TabButton widget) {
|
| 146 | if (this.tabs.size() > 1) {
|
| 147 | int index = this.tabButtons.indexOf(widget);
|
| 148 | if (index != -1) {
|
| 149 | output.add(NarratedElementType.POSITION, Component.translatable("narrator.position.tab", index + 1, this.tabs.size()));
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | protected void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 156 | for (TabButton value : this.tabButtons) {
|
| 157 | value.extractRenderState(graphics, mouseX, mouseY, a);
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public ScreenRectangle getRectangle() {
|
| 163 | return this.layout.getRectangle();
|
| 164 | }
|
| 165 |
|
| 166 | public void selectTab(final int index, final boolean playSound) {
|
| 167 | if (this.isFocused()) {
|
| 168 | this.setFocused(this.tabButtons.get(index));
|
| 169 | } else if (this.tabButtons.get(index).isActive()) {
|
| 170 | this.tabManager.setCurrentTab(this.tabs.get(index), playSound);
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | public void setTabActiveState(final int index, final boolean active) {
|
| 175 | if (index >= 0 && index < this.tabButtons.size()) {
|
| 176 | this.tabButtons.get(index).active = active;
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | public void setTabTooltip(final int index, final @Nullable Tooltip hint) {
|
| 181 | if (index >= 0 && index < this.tabButtons.size()) {
|
| 182 | this.tabButtons.get(index).setTooltip(hint);
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | @Override
|
| 187 | public boolean keyPressed(final KeyEvent event) {
|
| 188 | if (event.hasControlDownWithQuirk()) {
|
| 189 | int tabIndex = this.getNextTabIndex(event);
|
| 190 | if (tabIndex != -1) {
|
| 191 | this.selectTab(Mth.clamp(tabIndex, 0, this.tabs.size() - 1), true);
|
| 192 | return true;
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | return false;
|
| 197 | }
|
| 198 |
|
| 199 | private int getNextTabIndex(final KeyEvent event) {
|
| 200 | return this.getNextTabIndex(this.currentTabIndex(), event);
|
| 201 | }
|
| 202 |
|
| 203 | private int getNextTabIndex(final int currentTab, final KeyEvent event) {
|
| 204 | OptionalInt numericKeyValue = InputConstants.getKey(event).getNumericKeyValue();
|
| 205 | if (numericKeyValue.isPresent()) {
|
| 206 | return Math.floorMod(numericKeyValue.getAsInt() - 1, 10);
|
| 207 | } else if (event.isCycleFocus() && currentTab != -1) {
|
| 208 | int nextTabIndex = event.hasShiftDown() ? currentTab - 1 : currentTab + 1;
|
| 209 | int index = Math.floorMod(nextTabIndex, this.tabs.size());
|
| 210 | return this.tabButtons.get(index).active ? index : this.getNextTabIndex(index, event);
|
| 211 | } else {
|
| 212 | return -1;
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | private int currentTabIndex() {
|
| 217 | Tab currentTab = this.tabManager.getCurrentTab();
|
| 218 | int index = this.tabs.indexOf(currentTab);
|
| 219 | return index != -1 ? index : -1;
|
| 220 | }
|
| 221 |
|
| 222 | private @Nullable TabButton currentTabButton() {
|
| 223 | int index = this.currentTabIndex();
|
| 224 | return index != -1 ? this.tabButtons.get(index) : null;
|
| 225 | }
|
| 226 |
|
| 227 | @Environment(EnvType.CLIENT)
|
| 228 | public static class Builder {
|
| 229 | protected final int x;
|
| 230 | protected final int y;
|
| 231 | protected final int width;
|
| 232 | protected final int height;
|
| 233 | protected final TabManager tabManager;
|
| 234 | protected final List<TabButton> tabButtons = new ArrayList<>();
|
| 235 | protected final List<Tab> tabs = new ArrayList<>();
|
| 236 |
|
| 237 | protected Builder(final TabManager tabManager, final int x, final int y, final int width, final int height) {
|
| 238 | this.tabManager = tabManager;
|
| 239 | this.x = x;
|
| 240 | this.y = y;
|
| 241 | this.width = width;
|
| 242 | this.height = height;
|
| 243 | }
|
| 244 |
|
| 245 | public TabNavigationBar.Builder addTab(final TabButton button, final Tab tab) {
|
| 246 | this.tabButtons.add(button);
|
| 247 | this.tabs.add(tab);
|
| 248 | return this;
|
| 249 | }
|
| 250 |
|
| 251 | public TabNavigationBar build() {
|
| 252 | return new TabNavigationBar(this.x, this.y, this.width, this.height, this.tabManager, ImmutableList.copyOf(this.tabButtons), ImmutableList.copyOf(this.tabs));
|
| 253 | }
|
| 254 | }
|
| 255 | }
|
| 256 |
|