| 1 | package net.minecraft.client.gui.components.tabs;
|
| 2 |
|
| 3 | import java.util.Objects;
|
| 4 | import java.util.function.Consumer;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 9 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 10 | import net.minecraft.client.resources.sounds.SimpleSoundInstance;
|
| 11 | import net.minecraft.sounds.SoundEvents;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public class TabManager {
|
| 16 | private final Consumer<AbstractWidget> addWidget;
|
| 17 | private final Consumer<AbstractWidget> removeWidget;
|
| 18 | private final Consumer<Tab> onSelected;
|
| 19 | private final Consumer<Tab> onDeselected;
|
| 20 | private @Nullable Tab currentTab;
|
| 21 | private @Nullable ScreenRectangle tabArea;
|
| 22 |
|
| 23 | public TabManager(final Consumer<AbstractWidget> addWidget, final Consumer<AbstractWidget> removeWidget) {
|
| 24 | this(addWidget, removeWidget, t -> {}, t -> {});
|
| 25 | }
|
| 26 |
|
| 27 | public TabManager(
|
| 28 | final Consumer<AbstractWidget> addWidget, final Consumer<AbstractWidget> removeWidget, final Consumer<Tab> onSelected, final Consumer<Tab> onDeselected
|
| 29 | ) {
|
| 30 | this.addWidget = addWidget;
|
| 31 | this.removeWidget = removeWidget;
|
| 32 | this.onSelected = onSelected;
|
| 33 | this.onDeselected = onDeselected;
|
| 34 | }
|
| 35 |
|
| 36 | public void setTabArea(final ScreenRectangle tabArea) {
|
| 37 | this.tabArea = tabArea;
|
| 38 | Tab tab = this.getCurrentTab();
|
| 39 | if (tab != null) {
|
| 40 | tab.doLayout(tabArea);
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | public void setCurrentTab(final Tab tab, final boolean playSound) {
|
| 45 | this.setCurrentTab(tab, playSound, true);
|
| 46 | }
|
| 47 |
|
| 48 | public void setCurrentTab(final Tab tab, final boolean playSound, final boolean addWidget) {
|
| 49 | if (!Objects.equals(this.currentTab, tab)) {
|
| 50 | if (this.currentTab != null) {
|
| 51 | this.currentTab.visitChildren(this.removeWidget);
|
| 52 | }
|
| 53 |
|
| 54 | Tab oldTab = this.currentTab;
|
| 55 | this.currentTab = tab;
|
| 56 | if (addWidget) {
|
| 57 | tab.visitChildren(this.addWidget);
|
| 58 | }
|
| 59 |
|
| 60 | if (this.tabArea != null) {
|
| 61 | tab.doLayout(this.tabArea);
|
| 62 | }
|
| 63 |
|
| 64 | if (playSound) {
|
| 65 | Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F));
|
| 66 | }
|
| 67 |
|
| 68 | this.onDeselected.accept(oldTab);
|
| 69 | this.onSelected.accept(this.currentTab);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | public @Nullable Tab getCurrentTab() {
|
| 74 | return this.currentTab;
|
| 75 | }
|
| 76 | }
|
| 77 |
|